mutations.js 17 KB

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