mutations.js 14 KB

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