mutations.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438
  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. // 存储删除后的状态
  167. saveTimeLineFunc(state, '删除组件')
  168. // 删除后,清空当前选中组件
  169. state.activeItemConfig = null
  170. state.activeCode = null
  171. // 发送事件,关闭配置面板
  172. EventBus.$emit('closeRightPanel')
  173. },
  174. changePageConfig (state, pageConfig) {
  175. Vue.set(state.pageInfo, 'pageConfig', cloneDeep(pageConfig))
  176. state.updateKey = new Date().getTime()
  177. },
  178. changeActiveItem (state, activeItem) {
  179. state.activeItem = cloneDeep(activeItem)
  180. state.activeId = activeItem.code
  181. // state.settingJson = cloneDeep(activeItem.settingConfig) || {}
  182. },
  183. // 改变当前组件的xywh
  184. changeActiveItemWH (state, chart) {
  185. if (chart.code === state.activeItemConfig.code) {
  186. state.activeItemConfig = {
  187. ...state.activeItemConfig,
  188. ...chart
  189. }
  190. }
  191. },
  192. // 清空卡尺对齐线
  193. resetPresetLine (state) {
  194. state.presetLine = []
  195. },
  196. // 改变组件的层级
  197. changeZIndex (state, list) {
  198. changeZIndexFuc(state, list)
  199. },
  200. // 改变锁定状态
  201. changeLocked (state, config) {
  202. const index = state.pageInfo.chartList.findIndex(
  203. item => item.code === config.code
  204. )
  205. Vue.set(state.pageInfo.chartList[index], 'locked', !config.locked)
  206. saveTimeLineFunc(state, !config.locked ? `解锁${config?.title}` : `锁定${config?.title}`)
  207. },
  208. // 改变网格显示状态
  209. changeGridShow (state, isShow) {
  210. state.hasGrid = isShow
  211. },
  212. // 改变组件的key
  213. changeChartKey (state, code) {
  214. const index = state.pageInfo.chartList.findIndex(
  215. item => item.code === code
  216. )
  217. if (index < 0) {
  218. return
  219. }
  220. const config = state.pageInfo.chartList[index]
  221. Vue.set(config, 'key', config.code + new Date().getTime())
  222. },
  223. // 改变缓存数据集中的字段列表
  224. changeCacheDataFields (state, { dataSetId, data }) {
  225. // 将 state.pageInfo.pageConfig.cacheDataSets 中的 dataSetId 对应fields字段数据替换为 data
  226. const index = state.pageInfo.pageConfig.cacheDataSets.findIndex(cacheData => cacheData.dataSetId === dataSetId)
  227. if (index < 0) {
  228. return
  229. }
  230. Vue.set(state.pageInfo.pageConfig.cacheDataSets[index], 'fields', data?.fields || [])
  231. },
  232. // 改变缓存数据集中的数据参数
  233. changeCacheDataParams (state, { dataSetId, data }) {
  234. // 将 state.pageInfo.pageConfig.cacheDataSets 中的 dataSetId 对应fields字段数据替换为 data
  235. const index = state.pageInfo.pageConfig.cacheDataSets.findIndex(cacheData => cacheData.dataSetId === dataSetId)
  236. if (index < 0) {
  237. return
  238. }
  239. Vue.set(state.pageInfo.pageConfig.cacheDataSets[index], 'params', data?.params || [])
  240. },
  241. // 改变缓存数据集中的数据
  242. changeCacheDataSetData (state, { dataSetId, data }) {
  243. const index = state.pageInfo.pageConfig.cacheDataSets.findIndex(cacheData => cacheData.dataSetId === dataSetId)
  244. if (index < 0) {
  245. return
  246. }
  247. state.pageInfo.pageConfig.cacheDataSets[index].data = data || []
  248. },
  249. // 改变shift是否被按下
  250. changeCtrlOrCommandDown (state, isDown) {
  251. state.shiftKeyDown = isDown
  252. },
  253. // 初始化store中的数据,防止污染
  254. resetStoreData (state) {
  255. for (const stateKey in state) {
  256. state[stateKey] = cloneDeep(defaultData[stateKey])
  257. }
  258. },
  259. changeZoom (state, zoom) {
  260. state.zoom = zoom
  261. },
  262. changeFitZoom (state, zoom) {
  263. state.fitZoom = zoom
  264. },
  265. changeActivePos (state, { diffX, diffY }) {
  266. const activeCodes = state.activeCodes
  267. activeCodes?.forEach(code => {
  268. const chart = state.pageInfo.chartList.find(item => item.code === code)
  269. if (chart) {
  270. chart.x += diffX
  271. chart.y += diffY
  272. }
  273. const index = state.pageInfo.chartList.findIndex(
  274. item => item.code === chart.code
  275. )
  276. if (index < 0) {
  277. return
  278. }
  279. Vue.set(state.pageInfo.chartList, index, {
  280. ...state.pageInfo.chartList[index],
  281. ...chart
  282. })
  283. changePresetLine(state, chart)
  284. })
  285. },
  286. // 保存当前状态
  287. saveTimeLine (state, title) {
  288. const date = new Date()
  289. const time = moment(date).format('HH:mm:ss')
  290. // title默认获取当前时间,时分秒
  291. if (!title) {
  292. const date = new Date()
  293. title = `${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`
  294. }
  295. saveTimeLineFunc(state, title, time)
  296. },
  297. // 撤回/反撤回当前事件线 (undo和redo放到一个函数中,用isUndo区分)
  298. undoTimeLine (state, isUndo = true) {
  299. let currentStore = {}
  300. // 撤回
  301. if (isUndo) {
  302. if (state.timelineStore.length > 0 && state.currentTimeLine > 1) {
  303. // 时间线往前推一个
  304. state.currentTimeLine = state.currentTimeLine - 1
  305. currentStore = state.timelineStore[state.currentTimeLine - 1]
  306. if (currentStore?.chartList) {
  307. state.pageInfo.chartList = cloneDeep(currentStore?.chartList)
  308. }
  309. }
  310. }
  311. // 反撤回 redo
  312. if (!isUndo) {
  313. if (state.currentTimeLine < state.timelineStore.length) {
  314. // 时间线往后推一个
  315. state.currentTimeLine = state.currentTimeLine + 1
  316. currentStore = state.timelineStore[state.currentTimeLine - 1]
  317. state.pageInfo.chartList = cloneDeep(currentStore?.chartList || [])
  318. }
  319. }
  320. state.pageInfo.chartList = state.pageInfo.chartList.map(chart => {
  321. return {
  322. ...chart,
  323. key: chart.code + new Date().getTime()
  324. }
  325. })
  326. },
  327. clearTimeline (state) {
  328. // 最后一个状态
  329. const lastStore = state.timelineStore[state.timelineStore.length - 1]
  330. // 将最后一个状态作为初始状态,否则下次拖拽后无法回到之前
  331. state.timelineStore = [
  332. {
  333. ...lastStore,
  334. timelineTitle: '初始状态',
  335. updateTime: moment(new Date()).format('HH:mm:ss')
  336. }
  337. ]
  338. state.currentTimeLine = 1
  339. },
  340. // 回退到指定时间线
  341. rollbackTimeline (state, index) {
  342. state.pageInfo.chartList = cloneDeep(state.timelineStore[index]?.chartList || [])
  343. state.currentTimeLine = index + 1
  344. },
  345. // 复制组件
  346. copyCharts (state) {
  347. state.copyChartCodes = cloneDeep(state.activeCodes)
  348. },
  349. // 粘贴组件
  350. pasteCharts (state) {
  351. const copyChartCodes = state.copyChartCodes
  352. const chartList = state.pageInfo.chartList
  353. // 将选中的组件复制一份, code加上 随机后缀, key 也加上随机后缀, x, y 各增加50
  354. const additionCode = randomString(5)
  355. const copyCharts = copyChartCodes.map(code => {
  356. const chart = chartList.find(item => item.code === code)
  357. const copyChart = cloneDeep(chart)
  358. copyChart.code = `${copyChart.code}_${additionCode}`
  359. copyChart.key = `${copyChart.key}_${additionCode}`
  360. copyChart.group = (copyChart.group && copyChart.group !== 'tempGroup') ? `${copyChart.group}_${additionCode}` : ''
  361. copyChart.x += 50
  362. copyChart.y += 50
  363. return copyChart
  364. })
  365. // 将复制的组件添加到chartList中
  366. state.pageInfo.chartList = [...copyCharts, ...state.pageInfo.chartList]
  367. }
  368. }
  369. function changeZIndexFuc (state, list) {
  370. const len = list?.length - 1 || 0
  371. list.forEach((item, i) => {
  372. const index = state.pageInfo.chartList.findIndex(
  373. _item => _item.code === item.code
  374. )
  375. Vue.set(state.pageInfo.chartList[len - index], 'z', i)
  376. })
  377. }
  378. // 改变当前组件的卡尺对齐线
  379. function changePresetLine (state, { x, y, w, h }) {
  380. state.presetLine = [
  381. { type: 'h', site: y || 0 },
  382. { type: 'v', site: x || 0 }
  383. ]
  384. }
  385. function changeGroup (code, state) {
  386. if (code) {
  387. // 找到和此组件group相同的组件,并添加到activeCodes中
  388. const group = state.pageInfo.chartList?.find(item => item.code === code)?.group
  389. if (group) {
  390. state.activeCodes = state.pageInfo.chartList?.filter(chart => chart.group === group && chart.group).map(item => item.code)
  391. }
  392. if (state.shiftKeyDown) {
  393. state.activeCodes = uniq([...state.activeCodes, code])
  394. // eslint-disable-next-line no-unused-expressions
  395. state.pageInfo.chartList?.forEach(chart => {
  396. if (state.activeCodes.includes(chart.code)) {
  397. chart.group = 'tempGroup'
  398. }
  399. })
  400. } else {
  401. if (!group) {
  402. state.activeCodes = [code]
  403. }
  404. }
  405. } else {
  406. state.activeCodes = []
  407. state.pageInfo.chartList = state.pageInfo.chartList?.map(chart => ({
  408. ...chart,
  409. group: chart.group === 'tempGroup' ? '' : chart.group
  410. }))
  411. }
  412. }
  413. function saveTimeLineFunc (state, title, time) {
  414. // 最多保存10个状态
  415. const MAX_TIME_LINE = 10
  416. const stateCopy = cloneDeep(state.pageInfo)
  417. const date = new Date()
  418. time = time || moment(date).format('HH:mm:ss')
  419. stateCopy.timelineTitle = title
  420. stateCopy.updateTime = time
  421. if (!Array.isArray(state.timelineStore)) {
  422. state.timelineStore = []
  423. }
  424. if (!Number.isInteger(state.currentTimeLine)) {
  425. state.currentTimeLine = 0
  426. }
  427. if (state.timelineStore.length >= MAX_TIME_LINE) {
  428. // 去掉最早的一个
  429. state.timelineStore.shift()
  430. }
  431. state.timelineStore?.push(stateCopy)
  432. state.currentTimeLine = state.timelineStore?.length
  433. }