mutations.js 17 KB

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