mutations.js 15 KB

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