mutations.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 { defaultData } from './state'
  11. import moment from 'moment'
  12. import { randomString } from 'packages/js/utils'
  13. import { EventBus } from 'packages/js/utils/eventBus'
  14. export default {
  15. // 改变页面基本信息,后端请求的页面信息存储到此处
  16. changePageInfo (state, pageInfo) {
  17. state.pageInfo = pageInfo
  18. },
  19. // 改变组件列表
  20. changeLayout (state, layout) {
  21. state.pageInfo.chartList = layout
  22. },
  23. //
  24. changeIframeDialog (state, dialogVisible) {
  25. state.iframeDialog = dialogVisible
  26. },
  27. // 改变当前选择组件id
  28. changeActiveCode (state, code) {
  29. state.activeCode = code
  30. state.hoverCode = code
  31. const activeItem = _.cloneDeep(state.pageInfo.chartList?.find(
  32. item => item.code === code
  33. ))
  34. changeGroup(code, state)
  35. state.activeItemConfig = _.cloneDeep(activeItem)
  36. },
  37. changeActiveCodes (state, codes) {
  38. state.activeCodes = codes
  39. state.pageInfo.chartList = state.pageInfo.chartList?.map(chart => {
  40. return {
  41. ...chart,
  42. group: (codes.includes(chart.code) && !chart.group) ? 'tempGroup' : chart.group
  43. }
  44. })
  45. },
  46. // 改变当前hover组件id
  47. changeHoverCode (state, code) {
  48. state.hoverCode = code
  49. },
  50. changePageLoading (state, booleanValue) {
  51. state.pageLoading = booleanValue
  52. },
  53. // 改变当前组件配置
  54. changeChartConfig (state, itemConfig) {
  55. const index = state.pageInfo.chartList.findIndex(
  56. item => item.code === itemConfig.code
  57. )
  58. Vue.set(state.pageInfo.chartList, index, {
  59. ...state.pageInfo.chartList[index],
  60. ...itemConfig
  61. })
  62. // 对比之前的config和当前的itemConfig的xywh,如果有变化,就改变卡尺对齐线
  63. const oldConfig = state.pageInfo.chartList[index]
  64. if (
  65. oldConfig.x !== itemConfig.x ||
  66. oldConfig.y !== itemConfig.y ||
  67. oldConfig.w !== itemConfig.w ||
  68. oldConfig.h !== itemConfig.h
  69. ) {
  70. // 改变当前组件的卡尺对齐线
  71. changePresetLine(state, itemConfig)
  72. }
  73. },
  74. setPresetLine (state, { x, y, w, h }) {
  75. state.presetLine = [
  76. { type: 'h', site: y || 0 },
  77. { type: 'v', site: x || 0 }
  78. ]
  79. },
  80. changeActiveItemConfig (state, config) {
  81. state.activeItemConfig = _.cloneDeep(config)
  82. },
  83. // 新增一个组件
  84. addItem (state, itemConfig) {
  85. // 放到第一项
  86. state.pageInfo.chartList.unshift(itemConfig)
  87. changeZIndexFuc(state, state.pageInfo.chartList)
  88. saveTimeLineFunc(state, '新增组件' + itemConfig?.title)
  89. },
  90. // 删除组件/批量删除组件
  91. delItem (state, codes) {
  92. if (Array.isArray(codes)) {
  93. state.pageInfo.chartList = state.pageInfo.chartList.filter(chart => !codes.includes(chart.code))
  94. } else {
  95. state.pageInfo.chartList = state.pageInfo.chartList.filter(chart => codes !== chart.code)
  96. }
  97. // 存储删除后的状态
  98. saveTimeLineFunc(state, '删除组件')
  99. // 删除后,清空当前选中组件
  100. state.activeItemConfig = null
  101. state.activeCode = null
  102. // 发送事件,关闭配置面板
  103. EventBus.$emit('closeRightPanel')
  104. },
  105. changePageConfig (state, pageConfig) {
  106. Vue.set(state.pageInfo, 'pageConfig', _.cloneDeep(pageConfig))
  107. state.updateKey = new Date().getTime()
  108. },
  109. changeActiveItem (state, activeItem) {
  110. state.activeItem = _.cloneDeep(activeItem)
  111. state.activeId = activeItem.code
  112. // state.settingJson = _.cloneDeep(activeItem.settingConfig) || {}
  113. },
  114. // 改变当前组件的xywh
  115. changeActiveItemWH (state, chart) {
  116. if (chart.code === state.activeItemConfig.code) {
  117. state.activeItemConfig = {
  118. ...state.activeItemConfig,
  119. ...chart
  120. }
  121. }
  122. },
  123. // 清空卡尺对齐线
  124. resetPresetLine (state) {
  125. state.presetLine = []
  126. },
  127. // 改变组件的层级
  128. changeZIndex (state, list) {
  129. changeZIndexFuc(state, list)
  130. },
  131. // 改变锁定状态
  132. changeLocked (state, config) {
  133. const index = state.pageInfo.chartList.findIndex(
  134. item => item.code === config.code
  135. )
  136. Vue.set(state.pageInfo.chartList[index], 'locked', !config.locked)
  137. saveTimeLineFunc(state, !config.locked ? `解锁${config?.title}` : `锁定${config?.title}`)
  138. },
  139. // 改变网格显示状态
  140. changeGridShow (state, isShow) {
  141. state.hasGrid = isShow
  142. },
  143. // 改变组件的key
  144. changeChartKey (state, code) {
  145. const index = state.pageInfo.chartList.findIndex(
  146. item => item.code === code
  147. )
  148. if (index < 0) {
  149. return
  150. }
  151. const config = state.pageInfo.chartList[index]
  152. Vue.set(config, 'key', config.code + new Date().getTime())
  153. },
  154. // 改变缓存数据集中的字段列表
  155. changeCacheDataFields (state, { dataSetId, data }) {
  156. // 将 state.pageInfo.pageConfig.cacheDataSets 中的 dataSetId 对应fields字段数据替换为 data
  157. const index = state.pageInfo.pageConfig.cacheDataSets.findIndex(cacheData => cacheData.dataSetId === dataSetId)
  158. if (index < 0) {
  159. return
  160. }
  161. Vue.set(state.pageInfo.pageConfig.cacheDataSets[index], 'fields', data?.fields || [])
  162. },
  163. // 改变缓存数据集中的数据参数
  164. changeCacheDataParams (state, { dataSetId, data }) {
  165. // 将 state.pageInfo.pageConfig.cacheDataSets 中的 dataSetId 对应fields字段数据替换为 data
  166. const index = state.pageInfo.pageConfig.cacheDataSets.findIndex(cacheData => cacheData.dataSetId === dataSetId)
  167. if (index < 0) {
  168. return
  169. }
  170. Vue.set(state.pageInfo.pageConfig.cacheDataSets[index], 'params', data?.params || [])
  171. },
  172. // 改变缓存数据集中的数据
  173. changeCacheDataSetData (state, { dataSetId, data }) {
  174. const index = state.pageInfo.pageConfig.cacheDataSets.findIndex(cacheData => cacheData.dataSetId === dataSetId)
  175. if (index < 0) {
  176. return
  177. }
  178. state.pageInfo.pageConfig.cacheDataSets[index].data = data || []
  179. },
  180. // 改变shift是否被按下
  181. changeCtrlOrCommandDown (state, isDown) {
  182. state.shiftKeyDown = isDown
  183. },
  184. // 初始化store中的数据,防止污染
  185. resetStoreData (state) {
  186. for (const stateKey in state) {
  187. state[stateKey] = _.cloneDeep(defaultData[stateKey])
  188. }
  189. },
  190. changeZoom (state, zoom) {
  191. state.zoom = zoom
  192. },
  193. changeFitZoom (state, zoom) {
  194. state.fitZoom = zoom
  195. },
  196. changeActivePos (state, { diffX, diffY }) {
  197. const activeCodes = state.activeCodes
  198. activeCodes?.forEach(code => {
  199. const chart = state.pageInfo.chartList.find(item => item.code === code)
  200. if (chart) {
  201. chart.x += diffX
  202. chart.y += diffY
  203. }
  204. const index = state.pageInfo.chartList.findIndex(
  205. item => item.code === chart.code
  206. )
  207. if (index < 0) {
  208. return
  209. }
  210. Vue.set(state.pageInfo.chartList, index, {
  211. ...state.pageInfo.chartList[index],
  212. ...chart
  213. })
  214. changePresetLine(state, chart)
  215. })
  216. },
  217. // 保存当前状态
  218. saveTimeLine (state, title) {
  219. const date = new Date()
  220. const time = moment(date).format('HH:mm:ss')
  221. // title默认获取当前时间,时分秒
  222. if (!title) {
  223. const date = new Date()
  224. title = `${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`
  225. }
  226. saveTimeLineFunc(state, title, time)
  227. },
  228. // 撤回/反撤回当前事件线 (undo和redo放到一个函数中,用isUndo区分)
  229. undoTimeLine (state, isUndo = true) {
  230. let currentStore = {}
  231. // 撤回
  232. if (isUndo) {
  233. if (state.timelineStore.length > 0 && state.currentTimeLine > 1) {
  234. // 时间线往前推一个
  235. state.currentTimeLine = state.currentTimeLine - 1
  236. currentStore = state.timelineStore[state.currentTimeLine - 1]
  237. if (currentStore?.chartList) {
  238. state.pageInfo.chartList = _.cloneDeep(currentStore?.chartList)
  239. }
  240. }
  241. }
  242. // 反撤回 redo
  243. if (!isUndo) {
  244. if (state.currentTimeLine < state.timelineStore.length) {
  245. // 时间线往后推一个
  246. state.currentTimeLine = state.currentTimeLine + 1
  247. currentStore = state.timelineStore[state.currentTimeLine - 1]
  248. state.pageInfo.chartList = _.cloneDeep(currentStore?.chartList || [])
  249. }
  250. }
  251. },
  252. clearTimeline (state) {
  253. // 最后一个状态
  254. const lastStore = state.timelineStore[state.timelineStore.length - 1]
  255. // 将最后一个状态作为初始状态,否则下次拖拽后无法回到之前
  256. state.timelineStore = [
  257. {
  258. ...lastStore,
  259. timelineTitle: '初始状态',
  260. updateTime: moment(new Date()).format('HH:mm:ss')
  261. }
  262. ]
  263. state.currentTimeLine = 1
  264. },
  265. // 回退到指定时间线
  266. rollbackTimeline (state, index) {
  267. state.pageInfo.chartList = _.cloneDeep(state.timelineStore[index]?.chartList || [])
  268. state.currentTimeLine = index + 1
  269. },
  270. // 复制组件
  271. copyCharts (state) {
  272. state.copyChartCodes = _.cloneDeep(state.activeCodes)
  273. },
  274. // 粘贴组件
  275. pasteCharts (state) {
  276. const copyChartCodes = state.copyChartCodes
  277. const chartList = state.pageInfo.chartList
  278. // 将选中的组件复制一份, code加上 随机后缀, key 也加上随机后缀, x, y 各增加50
  279. const additionCode = randomString(5)
  280. const copyCharts = copyChartCodes.map(code => {
  281. const chart = chartList.find(item => item.code === code)
  282. const copyChart = _.cloneDeep(chart)
  283. copyChart.code = `${copyChart.code}_${additionCode}`
  284. copyChart.key = `${copyChart.key}_${additionCode}`
  285. copyChart.group = (copyChart.group && copyChart.group !== 'tempGroup') ? `${copyChart.group}_${additionCode}` : ''
  286. copyChart.x += 50
  287. copyChart.y += 50
  288. return copyChart
  289. })
  290. // 将复制的组件添加到chartList中
  291. state.pageInfo.chartList = [...copyCharts, ...state.pageInfo.chartList]
  292. }
  293. }
  294. function changeZIndexFuc (state, list) {
  295. const len = list?.length - 1 || 0
  296. list.forEach((item, i) => {
  297. const index = state.pageInfo.chartList.findIndex(
  298. _item => _item.code === item.code
  299. )
  300. Vue.set(state.pageInfo.chartList[len - index], 'z', i)
  301. })
  302. }
  303. // 改变当前组件的卡尺对齐线
  304. function changePresetLine (state, { x, y, w, h }) {
  305. state.presetLine = [
  306. { type: 'h', site: y || 0 },
  307. { type: 'v', site: x || 0 }
  308. ]
  309. }
  310. function changeGroup (code, state) {
  311. if (code) {
  312. // 找到和此组件group相同的组件,并添加到activeCodes中
  313. const group = state.pageInfo.chartList?.find(item => item.code === code)?.group
  314. if (group) {
  315. state.activeCodes = state.pageInfo.chartList?.filter(chart => chart.group === group && chart.group).map(item => item.code)
  316. }
  317. if (state.shiftKeyDown) {
  318. state.activeCodes = _.uniq([...state.activeCodes, code])
  319. // eslint-disable-next-line no-unused-expressions
  320. state.pageInfo.chartList?.forEach(chart => {
  321. if (state.activeCodes.includes(chart.code)) {
  322. chart.group = 'tempGroup'
  323. }
  324. })
  325. } else {
  326. if (!group) {
  327. state.activeCodes = [code]
  328. }
  329. }
  330. } else {
  331. state.activeCodes = []
  332. state.pageInfo.chartList = state.pageInfo.chartList?.map(chart => ({
  333. ...chart,
  334. group: chart.group === 'tempGroup' ? '' : chart.group
  335. }))
  336. }
  337. }
  338. function saveTimeLineFunc (state, title, time) {
  339. // 最多保存10个状态
  340. const MAX_TIME_LINE = 10
  341. const stateCopy = _.cloneDeep(state.pageInfo)
  342. const date = new Date()
  343. time = time || moment(date).format('HH:mm:ss')
  344. stateCopy.timelineTitle = title
  345. stateCopy.updateTime = time
  346. if (!Array.isArray(state.timelineStore)) {
  347. state.timelineStore = []
  348. }
  349. if (!Number.isInteger(state.currentTimeLine)) {
  350. state.currentTimeLine = 0
  351. }
  352. if (state.timelineStore.length >= MAX_TIME_LINE) {
  353. // 去掉最早的一个
  354. state.timelineStore.shift()
  355. }
  356. state.timelineStore?.push(stateCopy)
  357. state.currentTimeLine = state.timelineStore?.length
  358. }