mutations.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  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. let activeItem = {}
  34. // let activeItem = cloneDeep(state.pageInfo.chartList?.find(
  35. // item => item.code === code
  36. // ))
  37. for (const item of state.pageInfo.chartList) {
  38. // 检查当前项的 code 是否与 currentCode 匹配
  39. if (item.code === code) {
  40. activeItem = item
  41. break // 找到匹配的项后,退出循环
  42. }
  43. // 如果当前项的 type 为 'chartTab',则进一步检查其 tabList
  44. if (item.type === 'chartTab') {
  45. for (const tabItem of item.customize.tabList) {
  46. // 检查 tabList 中的每一项的 code 是否与 currentCode 匹配
  47. if (tabItem.chartCode === code) {
  48. activeItem = tabItem.chart
  49. break // 找到匹配的项后,退出循环
  50. }
  51. }
  52. }
  53. }
  54. changeGroup(code, state)
  55. state.activeItemConfig = cloneDeep(activeItem)
  56. },
  57. changeActiveCodes (state, codes) {
  58. // 传入codes,将codes中的组件的group改为tempGroup,不传入或者传入空数组,将所有组件的group改为'',即取消组合
  59. if (codes.length !== 0) {
  60. state.activeCodes = codes
  61. state.pageInfo.chartList = state.pageInfo.chartList?.map(chart => {
  62. return {
  63. ...chart,
  64. group: (codes.includes(chart.code) && !chart.group) ? 'tempGroup' : chart.group
  65. }
  66. })
  67. } else {
  68. state.activeCodes = codes
  69. state.pageInfo.chartList = state.pageInfo.chartList?.map(chart => {
  70. return {
  71. ...chart,
  72. // 确保取消高亮状态时不会使得原本设置过的组合被取消
  73. group: chart.group === 'tempGroup' ? '' : chart.group
  74. }
  75. })
  76. }
  77. },
  78. // 改变当前hover组件id
  79. changeHoverCode (state, code) {
  80. state.hoverCode = code
  81. },
  82. // 改变当前选中组件id
  83. changePageLoading (state, booleanValue) {
  84. // 改变loading状态
  85. state.pageLoading = booleanValue
  86. },
  87. // 改变当前组件的加载状态
  88. changeChartLoading (state, itemConfig) {
  89. // 改变loading状态
  90. state.pageInfo.chartList.forEach((chart, index) => {
  91. if (chart.code === itemConfig.code) {
  92. chart.loading = itemConfig.loading
  93. }
  94. })
  95. },
  96. // 改变当前组件配置
  97. changeChartConfig (state, itemConfig) {
  98. // 如果存在parentCode的组件,则是tab中的组件
  99. if (itemConfig?.parentCode) {
  100. state.pageInfo.chartList.forEach((chart, index) => {
  101. if (chart.code === itemConfig.parentCode) {
  102. chart.customize.tabList.forEach((tabItem, i) => {
  103. if (tabItem.chartCode === itemConfig.code) {
  104. Vue.set(state.pageInfo.chartList[index].customize.tabList[i], 'chart', {
  105. ...state.pageInfo.chartList[index].customize.tabList[i].chart,
  106. ...itemConfig
  107. })
  108. }
  109. })
  110. }
  111. })
  112. } else {
  113. // 如果是一般的组件
  114. let index = null
  115. index = state.pageInfo.chartList.findIndex(
  116. item => item.code === itemConfig.code
  117. )
  118. Vue.set(state.pageInfo.chartList, index, {
  119. ...state.pageInfo.chartList[index],
  120. ...itemConfig
  121. })
  122. // 对比之前的config和当前的itemConfig的xywh,如果有变化,就改变卡尺对齐线
  123. const oldConfig = state.pageInfo.chartList[index]
  124. if (
  125. oldConfig.x !== itemConfig.x ||
  126. oldConfig.y !== itemConfig.y ||
  127. oldConfig.w !== itemConfig.w ||
  128. oldConfig.h !== itemConfig.h
  129. ) {
  130. // 改变当前组件的卡尺对齐线
  131. changePresetLine(state, itemConfig)
  132. }
  133. }
  134. },
  135. setPresetLine (state, { x, y, w, h }) {
  136. state.presetLine = [
  137. { type: 'h', site: y || 0 },
  138. { type: 'v', site: x || 0 }
  139. ]
  140. },
  141. changeActiveItemConfig (state, config) {
  142. state.activeItemConfig = cloneDeep(config)
  143. },
  144. // 新增一个组件
  145. addItem (state, itemConfig) {
  146. // 放到第一项
  147. state.pageInfo.chartList.unshift(itemConfig)
  148. changeZIndexFuc(state, state.pageInfo.chartList)
  149. saveTimeLineFunc(state, '新增组件' + itemConfig?.title)
  150. },
  151. // 删除组件/批量删除组件
  152. delItem (state, codes) {
  153. if (Array.isArray(codes)) {
  154. const delCharts = state.pageInfo.chartList.filter(chart => codes.includes(chart.code))
  155. // 如果删除的组件中有跑马灯,需要删除将跑马灯组件的音频实例销毁
  156. delCharts.some(item => { item.type === 'marquee' && EventBus.$emit('deleteComponent', item.code) })
  157. state.pageInfo.chartList = state.pageInfo.chartList.filter(chart => !codes.includes(chart.code))
  158. } else {
  159. // 如果删除的组件是跑马灯,需要删除将跑马灯组件的音频实例销毁
  160. const delChart = state.pageInfo.chartList.find(chart => codes === chart.code)
  161. if (delChart && delChart.type === 'marquee') {
  162. EventBus.$emit('deleteComponent', codes)
  163. }
  164. state.pageInfo.chartList = state.pageInfo.chartList.filter(chart => codes !== chart.code)
  165. // 删除组件时,将该组件的缓存数据库中的数据也删除
  166. deldataset(state, 'dataset', codes)
  167. deldataset(state, 'computedDatas', codes)
  168. }
  169. // 存储删除后的状态
  170. saveTimeLineFunc(state, '删除组件')
  171. if (state.pageInfo.chartList.findIndex(item => item.code === state.activeCode) == -1) {
  172. state.activeItemConfig = null
  173. state.activeCode = null
  174. EventBus.$emit('closeRightPanel')
  175. }
  176. // 发送事件,关闭配置面板
  177. },
  178. changePageConfig (state, pageConfig) {
  179. Vue.set(state.pageInfo, 'pageConfig', cloneDeep(pageConfig))
  180. state.updateKey = new Date().getTime()
  181. },
  182. changeActiveItem (state, activeItem) {
  183. state.activeItem = cloneDeep(activeItem)
  184. state.activeId = activeItem.code
  185. // state.settingJson = cloneDeep(activeItem.settingConfig) || {}
  186. },
  187. // 改变当前组件的xywh
  188. changeActiveItemWH (state, chart) {
  189. if (chart.code === state.activeItemConfig.code) {
  190. state.activeItemConfig = {
  191. ...state.activeItemConfig,
  192. ...chart
  193. }
  194. }
  195. },
  196. // 清空卡尺对齐线
  197. resetPresetLine (state) {
  198. state.presetLine = []
  199. },
  200. // 改变组件的层级
  201. changeZIndex (state, list) {
  202. changeZIndexFuc(state, list)
  203. },
  204. // 改变锁定状态
  205. changeLocked (state, config) {
  206. const index = state.pageInfo.chartList.findIndex(
  207. item => item.code === config.code
  208. )
  209. Vue.set(state.pageInfo.chartList[index], 'locked', !config.locked)
  210. saveTimeLineFunc(state, !config.locked ? `解锁${config?.title}` : `锁定${config?.title}`)
  211. },
  212. // 改变网格显示状态
  213. changeGridShow (state, isShow) {
  214. state.hasGrid = isShow
  215. },
  216. // 改变组件的key
  217. changeChartKey (state, code) {
  218. const index = state.pageInfo.chartList.findIndex(
  219. item => item.code === code
  220. )
  221. if (index < 0) {
  222. return
  223. }
  224. const config = state.pageInfo.chartList[index]
  225. Vue.set(config, 'key', config.code + new Date().getTime())
  226. },
  227. // 改变缓存数据集中的字段列表
  228. changeCacheDataFields (state, { dataSetId, data }) {
  229. // 将 state.pageInfo.pageConfig.cacheDataSets 中的 dataSetId 对应fields字段数据替换为 data
  230. const index = state.pageInfo.pageConfig.cacheDataSets.findIndex(cacheData => cacheData.dataSetId === dataSetId)
  231. if (index < 0) {
  232. return
  233. }
  234. Vue.set(state.pageInfo.pageConfig.cacheDataSets[index], 'fields', data?.fields || [])
  235. },
  236. // 改变缓存数据集中的数据参数
  237. changeCacheDataParams (state, { dataSetId, data }) {
  238. // 将 state.pageInfo.pageConfig.cacheDataSets 中的 dataSetId 对应fields字段数据替换为 data
  239. const index = state.pageInfo.pageConfig.cacheDataSets.findIndex(cacheData => cacheData.dataSetId === dataSetId)
  240. if (index < 0) {
  241. return
  242. }
  243. Vue.set(state.pageInfo.pageConfig.cacheDataSets[index], 'params', data?.params || [])
  244. },
  245. // 改变缓存数据集中的数据
  246. changeCacheDataSetData (state, { dataSetId, data }) {
  247. const index = state.pageInfo.pageConfig.cacheDataSets.findIndex(cacheData => cacheData.dataSetId === dataSetId)
  248. if (index < 0) {
  249. return
  250. }
  251. state.pageInfo.pageConfig.cacheDataSets[index].data = data || []
  252. },
  253. // 改变shift是否被按下
  254. changeCtrlOrCommandDown (state, isDown) {
  255. state.shiftKeyDown = isDown
  256. },
  257. // 初始化store中的数据,防止污染
  258. resetStoreData (state) {
  259. for (const stateKey in state) {
  260. state[stateKey] = cloneDeep(defaultData[stateKey])
  261. }
  262. },
  263. changeZoom (state, zoom) {
  264. state.zoom = zoom
  265. },
  266. changeFitZoom (state, zoom) {
  267. state.fitZoom = zoom
  268. },
  269. changeActivePos (state, { diffX, diffY }) {
  270. const activeCodes = state.activeCodes
  271. activeCodes?.forEach(code => {
  272. const chart = state.pageInfo.chartList.find(item => item.code === code)
  273. if (chart) {
  274. chart.x += diffX
  275. chart.y += diffY
  276. }
  277. const index = state.pageInfo.chartList.findIndex(
  278. item => item.code === chart.code
  279. )
  280. if (index < 0) {
  281. return
  282. }
  283. Vue.set(state.pageInfo.chartList, index, {
  284. ...state.pageInfo.chartList[index],
  285. ...chart
  286. })
  287. changePresetLine(state, chart)
  288. })
  289. },
  290. // 保存当前状态
  291. saveTimeLine (state, title) {
  292. const date = new Date()
  293. const time = moment(date).format('HH:mm:ss')
  294. // title默认获取当前时间,时分秒
  295. if (!title) {
  296. const date = new Date()
  297. title = `${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`
  298. }
  299. saveTimeLineFunc(state, title, time)
  300. },
  301. // 撤回/反撤回当前事件线 (undo和redo放到一个函数中,用isUndo区分)
  302. undoTimeLine (state, isUndo = true) {
  303. let currentStore = {}
  304. // 撤回
  305. if (isUndo) {
  306. if (state.timelineStore.length > 0 && state.currentTimeLine > 1) {
  307. // 时间线往前推一个
  308. state.currentTimeLine = state.currentTimeLine - 1
  309. currentStore = state.timelineStore[state.currentTimeLine - 1]
  310. if (currentStore?.chartList) {
  311. state.pageInfo.chartList = cloneDeep(currentStore?.chartList)
  312. }
  313. }
  314. }
  315. // 反撤回 redo
  316. if (!isUndo) {
  317. if (state.currentTimeLine < state.timelineStore.length) {
  318. // 时间线往后推一个
  319. state.currentTimeLine = state.currentTimeLine + 1
  320. currentStore = state.timelineStore[state.currentTimeLine - 1]
  321. state.pageInfo.chartList = cloneDeep(currentStore?.chartList || [])
  322. }
  323. }
  324. state.pageInfo.chartList = state.pageInfo.chartList.map(chart => {
  325. return {
  326. ...chart,
  327. key: chart.code + new Date().getTime()
  328. }
  329. })
  330. },
  331. clearTimeline (state) {
  332. // 最后一个状态
  333. const lastStore = state.timelineStore[state.timelineStore.length - 1]
  334. // 将最后一个状态作为初始状态,否则下次拖拽后无法回到之前
  335. state.timelineStore = [
  336. {
  337. ...lastStore,
  338. timelineTitle: '初始状态',
  339. updateTime: moment(new Date()).format('HH:mm:ss')
  340. }
  341. ]
  342. state.currentTimeLine = 1
  343. },
  344. // 回退到指定时间线
  345. rollbackTimeline (state, index) {
  346. state.pageInfo.chartList = cloneDeep(state.timelineStore[index]?.chartList || [])
  347. state.currentTimeLine = index + 1
  348. },
  349. // 复制组件
  350. copyCharts (state) {
  351. state.copyChartCodes = cloneDeep(state.activeCodes)
  352. },
  353. // 粘贴组件
  354. pasteCharts (state) {
  355. const copyChartCodes = state.copyChartCodes
  356. const chartList = state.pageInfo.chartList
  357. // 将选中的组件复制一份, code加上 随机后缀, key 也加上随机后缀, x, y 各增加50
  358. const additionCode = randomString(5)
  359. const copyCharts = copyChartCodes.map(code => {
  360. const chart = chartList.find(item => item.code === code)
  361. const copyChart = cloneDeep(chart)
  362. copyChart.code = `${copyChart.code}_${additionCode}`
  363. copyChart.key = `${copyChart.key}_${additionCode}`
  364. copyChart.group = (copyChart.group && copyChart.group !== 'tempGroup') ? `${copyChart.group}_${additionCode}` : ''
  365. copyChart.x += 50
  366. copyChart.y += 50
  367. return copyChart
  368. })
  369. // 将复制的组件添加到chartList中
  370. state.pageInfo.chartList = [...copyCharts, ...state.pageInfo.chartList]
  371. },
  372. // 更新数据集库中的内容
  373. updateDataset (state, res) {
  374. Vue.set(state.dataset, res.name + res.code, res.data)
  375. },
  376. // 更新数据集库中的内容
  377. updateComputedDatas (state, res) {
  378. Vue.set(state.computedDatas, res.name + res.code, res.data)
  379. },
  380. // 清空数据集库
  381. emptyDataset (state) {
  382. state.dataset = {}
  383. },
  384. // 清空数据集库
  385. emptyComputedDatas (state) {
  386. state.computedDatas = {}
  387. }
  388. }
  389. function deldataset (state, type, codes) {
  390. const datasets = state[type]
  391. for (const code of codes) {
  392. for (const key in datasets) {
  393. if (key.endsWith(code)) {
  394. delete state[type][key]
  395. break // 找到匹配的属性后,退出内层循环
  396. }
  397. }
  398. }
  399. }
  400. function changeZIndexFuc (state, list) {
  401. const len = list?.length - 1 || 0
  402. list.forEach((item, i) => {
  403. const index = state.pageInfo.chartList.findIndex(
  404. _item => _item.code === item.code
  405. )
  406. Vue.set(state.pageInfo.chartList[len - index], 'z', i)
  407. })
  408. }
  409. // 改变当前组件的卡尺对齐线
  410. function changePresetLine (state, { x, y, w, h }) {
  411. state.presetLine = [
  412. { type: 'h', site: y || 0 },
  413. { type: 'v', site: x || 0 }
  414. ]
  415. }
  416. function changeGroup (code, state) {
  417. if (code) {
  418. // 找到和此组件group相同的组件,并添加到activeCodes中
  419. const group = state.pageInfo.chartList?.find(item => item.code === code)?.group
  420. if (group) {
  421. state.activeCodes = state.pageInfo.chartList?.filter(chart => chart.group === group && chart.group).map(item => item.code)
  422. }
  423. if (state.shiftKeyDown) {
  424. state.activeCodes = uniq([...state.activeCodes, code])
  425. // eslint-disable-next-line no-unused-expressions
  426. state.pageInfo.chartList?.forEach(chart => {
  427. if (state.activeCodes.includes(chart.code)) {
  428. chart.group = 'tempGroup'
  429. }
  430. })
  431. } else {
  432. if (!group) {
  433. state.activeCodes = [code]
  434. }
  435. }
  436. } else {
  437. state.activeCodes = []
  438. state.pageInfo.chartList = state.pageInfo.chartList?.map(chart => ({
  439. ...chart,
  440. group: chart.group === 'tempGroup' ? '' : chart.group
  441. }))
  442. }
  443. }
  444. function saveTimeLineFunc (state, title, time) {
  445. // 最多保存10个状态
  446. const MAX_TIME_LINE = 10
  447. const stateCopy = cloneDeep(state.pageInfo)
  448. const date = new Date()
  449. time = time || moment(date).format('HH:mm:ss')
  450. stateCopy.timelineTitle = title
  451. stateCopy.updateTime = time
  452. if (!Array.isArray(state.timelineStore)) {
  453. state.timelineStore = []
  454. }
  455. if (!Number.isInteger(state.currentTimeLine)) {
  456. state.currentTimeLine = 0
  457. }
  458. if (state.timelineStore.length >= MAX_TIME_LINE) {
  459. // 去掉最早的一个
  460. state.timelineStore.shift()
  461. }
  462. state.timelineStore?.push(stateCopy)
  463. state.currentTimeLine = state.timelineStore?.length
  464. }