mutations.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391
  1. /*
  2. * @description: vuex mutations 事件
  3. * @Date: 2023-03-13 10:04:59
  4. * @Author: xing.heng
  5. * @LastEditors: xing.heng
  6. * @LastEditTime: 2023-06-08 15:24:01
  7. */
  8. import Vue from 'vue'
  9. // import _ from 'lodash'
  10. import cloneDeep from 'lodash/cloneDeep'
  11. import uniq from 'lodash/uniq'
  12. import { defaultData } from './state'
  13. import moment from 'moment'
  14. import { randomString } from 'data-room-ui/js/utils'
  15. import { EventBus } from 'data-room-ui/js/utils/eventBus'
  16. export default {
  17. // 改变页面基本信息,后端请求的页面信息存储到此处
  18. changePageInfo (state, pageInfo) {
  19. state.pageInfo = pageInfo
  20. },
  21. // 改变组件列表
  22. changeLayout (state, layout) {
  23. state.pageInfo.chartList = layout
  24. },
  25. //
  26. changeIframeDialog (state, dialogVisible) {
  27. state.iframeDialog = dialogVisible
  28. },
  29. // 改变当前选择组件id
  30. changeActiveCode (state, code) {
  31. state.activeCode = code
  32. state.hoverCode = code
  33. const activeItem = cloneDeep(state.pageInfo.chartList?.find(
  34. item => item.code === code
  35. ))
  36. changeGroup(code, state)
  37. state.activeItemConfig = cloneDeep(activeItem)
  38. },
  39. changeActiveCodes (state, codes) {
  40. // 传入codes,将codes中的组件的group改为tempGroup,不传入或者传入空数组,将所有组件的group改为'',即取消组合
  41. if (codes.length !== 0) {
  42. state.activeCodes = codes
  43. state.pageInfo.chartList = state.pageInfo.chartList?.map(chart => {
  44. return {
  45. ...chart,
  46. group: (codes.includes(chart.code) && !chart.group) ? 'tempGroup' : chart.group
  47. }
  48. })
  49. } else {
  50. state.activeCodes = codes
  51. state.pageInfo.chartList = state.pageInfo.chartList?.map(chart => {
  52. return {
  53. ...chart,
  54. group: ''
  55. }
  56. })
  57. }
  58. },
  59. // 改变当前hover组件id
  60. changeHoverCode (state, code) {
  61. state.hoverCode = code
  62. },
  63. changePageLoading (state, booleanValue) {
  64. state.pageLoading = booleanValue
  65. },
  66. // 改变当前组件配置
  67. changeChartConfig (state, itemConfig) {
  68. const index = state.pageInfo.chartList.findIndex(
  69. item => item.code === itemConfig.code
  70. )
  71. Vue.set(state.pageInfo.chartList, index, {
  72. ...state.pageInfo.chartList[index],
  73. ...itemConfig
  74. })
  75. // 对比之前的config和当前的itemConfig的xywh,如果有变化,就改变卡尺对齐线
  76. const oldConfig = state.pageInfo.chartList[index]
  77. if (
  78. oldConfig.x !== itemConfig.x ||
  79. oldConfig.y !== itemConfig.y ||
  80. oldConfig.w !== itemConfig.w ||
  81. oldConfig.h !== itemConfig.h
  82. ) {
  83. // 改变当前组件的卡尺对齐线
  84. changePresetLine(state, itemConfig)
  85. }
  86. },
  87. setPresetLine (state, { x, y, w, h }) {
  88. state.presetLine = [
  89. { type: 'h', site: y || 0 },
  90. { type: 'v', site: x || 0 }
  91. ]
  92. },
  93. changeActiveItemConfig (state, config) {
  94. state.activeItemConfig = cloneDeep(config)
  95. },
  96. // 新增一个组件
  97. addItem (state, itemConfig) {
  98. // 放到第一项
  99. state.pageInfo.chartList.unshift(itemConfig)
  100. changeZIndexFuc(state, state.pageInfo.chartList)
  101. saveTimeLineFunc(state, '新增组件' + itemConfig?.title)
  102. },
  103. // 删除组件/批量删除组件
  104. delItem (state, codes) {
  105. if (Array.isArray(codes)) {
  106. const delCharts = state.pageInfo.chartList.filter(chart => codes.includes(chart.code))
  107. // 如果删除的组件中有跑马灯,需要删除将跑马灯组件的音频实例销毁
  108. delCharts.some(item => { item.type === 'marquee' && EventBus.$emit('deleteComponent', item.code) })
  109. state.pageInfo.chartList = state.pageInfo.chartList.filter(chart => !codes.includes(chart.code))
  110. } else {
  111. // 如果删除的组件是跑马灯,需要删除将跑马灯组件的音频实例销毁
  112. const delChart = state.pageInfo.chartList.find(chart => codes === chart.code)
  113. if (delChart && delChart.type === 'marquee') {
  114. EventBus.$emit('deleteComponent', codes)
  115. }
  116. state.pageInfo.chartList = state.pageInfo.chartList.filter(chart => codes !== chart.code)
  117. }
  118. // 存储删除后的状态
  119. saveTimeLineFunc(state, '删除组件')
  120. // 删除后,清空当前选中组件
  121. state.activeItemConfig = null
  122. state.activeCode = null
  123. // 发送事件,关闭配置面板
  124. EventBus.$emit('closeRightPanel')
  125. },
  126. changePageConfig (state, pageConfig) {
  127. Vue.set(state.pageInfo, 'pageConfig', cloneDeep(pageConfig))
  128. state.updateKey = new Date().getTime()
  129. },
  130. changeActiveItem (state, activeItem) {
  131. state.activeItem = cloneDeep(activeItem)
  132. state.activeId = activeItem.code
  133. // state.settingJson = cloneDeep(activeItem.settingConfig) || {}
  134. },
  135. // 改变当前组件的xywh
  136. changeActiveItemWH (state, chart) {
  137. if (chart.code === state.activeItemConfig.code) {
  138. state.activeItemConfig = {
  139. ...state.activeItemConfig,
  140. ...chart
  141. }
  142. }
  143. },
  144. // 清空卡尺对齐线
  145. resetPresetLine (state) {
  146. state.presetLine = []
  147. },
  148. // 改变组件的层级
  149. changeZIndex (state, list) {
  150. changeZIndexFuc(state, list)
  151. },
  152. // 改变锁定状态
  153. changeLocked (state, config) {
  154. const index = state.pageInfo.chartList.findIndex(
  155. item => item.code === config.code
  156. )
  157. Vue.set(state.pageInfo.chartList[index], 'locked', !config.locked)
  158. saveTimeLineFunc(state, !config.locked ? `解锁${config?.title}` : `锁定${config?.title}`)
  159. },
  160. // 改变网格显示状态
  161. changeGridShow (state, isShow) {
  162. state.hasGrid = isShow
  163. },
  164. // 改变组件的key
  165. changeChartKey (state, code) {
  166. const index = state.pageInfo.chartList.findIndex(
  167. item => item.code === code
  168. )
  169. if (index < 0) {
  170. return
  171. }
  172. const config = state.pageInfo.chartList[index]
  173. Vue.set(config, 'key', config.code + new Date().getTime())
  174. },
  175. // 改变缓存数据集中的字段列表
  176. changeCacheDataFields (state, { dataSetId, data }) {
  177. // 将 state.pageInfo.pageConfig.cacheDataSets 中的 dataSetId 对应fields字段数据替换为 data
  178. const index = state.pageInfo.pageConfig.cacheDataSets.findIndex(cacheData => cacheData.dataSetId === dataSetId)
  179. if (index < 0) {
  180. return
  181. }
  182. Vue.set(state.pageInfo.pageConfig.cacheDataSets[index], 'fields', data?.fields || [])
  183. },
  184. // 改变缓存数据集中的数据参数
  185. changeCacheDataParams (state, { dataSetId, data }) {
  186. // 将 state.pageInfo.pageConfig.cacheDataSets 中的 dataSetId 对应fields字段数据替换为 data
  187. const index = state.pageInfo.pageConfig.cacheDataSets.findIndex(cacheData => cacheData.dataSetId === dataSetId)
  188. if (index < 0) {
  189. return
  190. }
  191. Vue.set(state.pageInfo.pageConfig.cacheDataSets[index], 'params', data?.params || [])
  192. },
  193. // 改变缓存数据集中的数据
  194. changeCacheDataSetData (state, { dataSetId, data }) {
  195. const index = state.pageInfo.pageConfig.cacheDataSets.findIndex(cacheData => cacheData.dataSetId === dataSetId)
  196. if (index < 0) {
  197. return
  198. }
  199. state.pageInfo.pageConfig.cacheDataSets[index].data = data || []
  200. },
  201. // 改变shift是否被按下
  202. changeCtrlOrCommandDown (state, isDown) {
  203. state.shiftKeyDown = isDown
  204. },
  205. // 初始化store中的数据,防止污染
  206. resetStoreData (state) {
  207. for (const stateKey in state) {
  208. state[stateKey] = cloneDeep(defaultData[stateKey])
  209. }
  210. },
  211. changeZoom (state, zoom) {
  212. state.zoom = zoom
  213. },
  214. changeFitZoom (state, zoom) {
  215. state.fitZoom = zoom
  216. },
  217. changeActivePos (state, { diffX, diffY }) {
  218. const activeCodes = state.activeCodes
  219. activeCodes?.forEach(code => {
  220. const chart = state.pageInfo.chartList.find(item => item.code === code)
  221. if (chart) {
  222. chart.x += diffX
  223. chart.y += diffY
  224. }
  225. const index = state.pageInfo.chartList.findIndex(
  226. item => item.code === chart.code
  227. )
  228. if (index < 0) {
  229. return
  230. }
  231. Vue.set(state.pageInfo.chartList, index, {
  232. ...state.pageInfo.chartList[index],
  233. ...chart
  234. })
  235. changePresetLine(state, chart)
  236. })
  237. },
  238. // 保存当前状态
  239. saveTimeLine (state, title) {
  240. const date = new Date()
  241. const time = moment(date).format('HH:mm:ss')
  242. // title默认获取当前时间,时分秒
  243. if (!title) {
  244. const date = new Date()
  245. title = `${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`
  246. }
  247. saveTimeLineFunc(state, title, time)
  248. },
  249. // 撤回/反撤回当前事件线 (undo和redo放到一个函数中,用isUndo区分)
  250. undoTimeLine (state, isUndo = true) {
  251. let currentStore = {}
  252. // 撤回
  253. if (isUndo) {
  254. if (state.timelineStore.length > 0 && state.currentTimeLine > 1) {
  255. // 时间线往前推一个
  256. state.currentTimeLine = state.currentTimeLine - 1
  257. currentStore = state.timelineStore[state.currentTimeLine - 1]
  258. if (currentStore?.chartList) {
  259. state.pageInfo.chartList = cloneDeep(currentStore?.chartList)
  260. }
  261. }
  262. }
  263. // 反撤回 redo
  264. if (!isUndo) {
  265. if (state.currentTimeLine < state.timelineStore.length) {
  266. // 时间线往后推一个
  267. state.currentTimeLine = state.currentTimeLine + 1
  268. currentStore = state.timelineStore[state.currentTimeLine - 1]
  269. state.pageInfo.chartList = cloneDeep(currentStore?.chartList || [])
  270. }
  271. }
  272. state.pageInfo.chartList = state.pageInfo.chartList.map(chart => {
  273. return {
  274. ...chart,
  275. key: chart.code + new Date().getTime()
  276. }
  277. })
  278. },
  279. clearTimeline (state) {
  280. // 最后一个状态
  281. const lastStore = state.timelineStore[state.timelineStore.length - 1]
  282. // 将最后一个状态作为初始状态,否则下次拖拽后无法回到之前
  283. state.timelineStore = [
  284. {
  285. ...lastStore,
  286. timelineTitle: '初始状态',
  287. updateTime: moment(new Date()).format('HH:mm:ss')
  288. }
  289. ]
  290. state.currentTimeLine = 1
  291. },
  292. // 回退到指定时间线
  293. rollbackTimeline (state, index) {
  294. state.pageInfo.chartList = cloneDeep(state.timelineStore[index]?.chartList || [])
  295. state.currentTimeLine = index + 1
  296. },
  297. // 复制组件
  298. copyCharts (state) {
  299. state.copyChartCodes = cloneDeep(state.activeCodes)
  300. },
  301. // 粘贴组件
  302. pasteCharts (state) {
  303. const copyChartCodes = state.copyChartCodes
  304. const chartList = state.pageInfo.chartList
  305. // 将选中的组件复制一份, code加上 随机后缀, key 也加上随机后缀, x, y 各增加50
  306. const additionCode = randomString(5)
  307. const copyCharts = copyChartCodes.map(code => {
  308. const chart = chartList.find(item => item.code === code)
  309. const copyChart = cloneDeep(chart)
  310. copyChart.code = `${copyChart.code}_${additionCode}`
  311. copyChart.key = `${copyChart.key}_${additionCode}`
  312. copyChart.group = (copyChart.group && copyChart.group !== 'tempGroup') ? `${copyChart.group}_${additionCode}` : ''
  313. copyChart.x += 50
  314. copyChart.y += 50
  315. return copyChart
  316. })
  317. // 将复制的组件添加到chartList中
  318. state.pageInfo.chartList = [...copyCharts, ...state.pageInfo.chartList]
  319. }
  320. }
  321. function changeZIndexFuc (state, list) {
  322. const len = list?.length - 1 || 0
  323. list.forEach((item, i) => {
  324. const index = state.pageInfo.chartList.findIndex(
  325. _item => _item.code === item.code
  326. )
  327. Vue.set(state.pageInfo.chartList[len - index], 'z', i)
  328. })
  329. }
  330. // 改变当前组件的卡尺对齐线
  331. function changePresetLine (state, { x, y, w, h }) {
  332. state.presetLine = [
  333. { type: 'h', site: y || 0 },
  334. { type: 'v', site: x || 0 }
  335. ]
  336. }
  337. function changeGroup (code, state) {
  338. if (code) {
  339. // 找到和此组件group相同的组件,并添加到activeCodes中
  340. const group = state.pageInfo.chartList?.find(item => item.code === code)?.group
  341. if (group) {
  342. state.activeCodes = state.pageInfo.chartList?.filter(chart => chart.group === group && chart.group).map(item => item.code)
  343. }
  344. if (state.shiftKeyDown) {
  345. state.activeCodes = uniq([...state.activeCodes, code])
  346. // eslint-disable-next-line no-unused-expressions
  347. state.pageInfo.chartList?.forEach(chart => {
  348. if (state.activeCodes.includes(chart.code)) {
  349. chart.group = 'tempGroup'
  350. }
  351. })
  352. } else {
  353. if (!group) {
  354. state.activeCodes = [code]
  355. }
  356. }
  357. } else {
  358. state.activeCodes = []
  359. state.pageInfo.chartList = state.pageInfo.chartList?.map(chart => ({
  360. ...chart,
  361. group: chart.group === 'tempGroup' ? '' : chart.group
  362. }))
  363. }
  364. }
  365. function saveTimeLineFunc (state, title, time) {
  366. // 最多保存10个状态
  367. const MAX_TIME_LINE = 10
  368. const stateCopy = cloneDeep(state.pageInfo)
  369. const date = new Date()
  370. time = time || moment(date).format('HH:mm:ss')
  371. stateCopy.timelineTitle = title
  372. stateCopy.updateTime = time
  373. if (!Array.isArray(state.timelineStore)) {
  374. state.timelineStore = []
  375. }
  376. if (!Number.isInteger(state.currentTimeLine)) {
  377. state.currentTimeLine = 0
  378. }
  379. if (state.timelineStore.length >= MAX_TIME_LINE) {
  380. // 去掉最早的一个
  381. state.timelineStore.shift()
  382. }
  383. state.timelineStore?.push(stateCopy)
  384. state.currentTimeLine = state.timelineStore?.length
  385. }