mutations.js 12 KB

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