mutations.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  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. changePageConfig (state, pageConfig) {
  100. Vue.set(state.pageInfo, 'pageConfig', _.cloneDeep(pageConfig))
  101. state.updateKey = new Date().getTime()
  102. },
  103. changeActiveItem (state, activeItem) {
  104. state.activeItem = _.cloneDeep(activeItem)
  105. state.activeId = activeItem.code
  106. // state.settingJson = _.cloneDeep(activeItem.settingConfig) || {}
  107. },
  108. // 改变当前组件的xywh
  109. changeActiveItemWH(state, chart) {
  110. if (chart.code === state.activeItemConfig.code) {
  111. state.activeItemConfig = {
  112. ...state.activeItemConfig,
  113. ...chart
  114. }
  115. }
  116. },
  117. // 清空卡尺对齐线
  118. resetPresetLine (state) {
  119. state.presetLine = []
  120. },
  121. // 改变组件的层级
  122. changeZIndex (state, list) {
  123. changeZIndexFuc(state, list)
  124. },
  125. // 改变锁定状态
  126. changeLocked (state, config) {
  127. const index = state.pageInfo.chartList.findIndex(
  128. item => item.code === config.code
  129. )
  130. Vue.set(state.pageInfo.chartList[index], 'locked', !config.locked)
  131. saveTimeLineFunc(state, !config.locked ? `解锁${config?.title}` : `锁定${config?.title}`)
  132. },
  133. // 改变网格显示状态
  134. changeGridShow (state, isShow) {
  135. state.hasGrid = isShow
  136. },
  137. // 改变组件的key
  138. changeChartKey (state, code) {
  139. const index = state.pageInfo.chartList.findIndex(
  140. item => item.code === code
  141. )
  142. if (index < 0) {
  143. return
  144. }
  145. const config = state.pageInfo.chartList[index]
  146. Vue.set(config, 'key', config.code + new Date().getTime())
  147. },
  148. // 改变缓存数据集中的字段列表
  149. changeCacheDataFields (state, { dataSetId, data }) {
  150. // 将 state.pageInfo.pageConfig.cacheDataSets 中的 dataSetId 对应fields字段数据替换为 data
  151. const index = state.pageInfo.pageConfig.cacheDataSets.findIndex(cacheData => cacheData.dataSetId === dataSetId)
  152. if (index < 0) {
  153. return
  154. }
  155. Vue.set(state.pageInfo.pageConfig.cacheDataSets[index], 'fields', data?.fields || [])
  156. },
  157. // 改变缓存数据集中的数据参数
  158. changeCacheDataParams (state, { dataSetId, data }) {
  159. // 将 state.pageInfo.pageConfig.cacheDataSets 中的 dataSetId 对应fields字段数据替换为 data
  160. const index = state.pageInfo.pageConfig.cacheDataSets.findIndex(cacheData => cacheData.dataSetId === dataSetId)
  161. if (index < 0) {
  162. return
  163. }
  164. Vue.set(state.pageInfo.pageConfig.cacheDataSets[index], 'params', data?.params || [])
  165. },
  166. // 改变缓存数据集中的数据
  167. changeCacheDataSetData (state, { dataSetId, data }) {
  168. const index = state.pageInfo.pageConfig.cacheDataSets.findIndex(cacheData => cacheData.dataSetId === dataSetId)
  169. if (index < 0) {
  170. return
  171. }
  172. state.pageInfo.pageConfig.cacheDataSets[index].data = data || []
  173. },
  174. // 改变shift是否被按下
  175. changeCtrlOrCommandDown (state, isDown) {
  176. state.shiftKeyDown = isDown
  177. },
  178. // 初始化store中的数据,防止污染
  179. resetStoreData (state) {
  180. for (const stateKey in state) {
  181. state[stateKey] = _.cloneDeep(defaultData[stateKey])
  182. }
  183. },
  184. changeZoom (state, zoom) {
  185. state.zoom = zoom
  186. },
  187. changeFitZoom (state, zoom) {
  188. state.fitZoom = zoom
  189. },
  190. changeActivePos (state, { diffX, diffY }) {
  191. const activeCodes = state.activeCodes
  192. activeCodes?.forEach(code => {
  193. const chart = state.pageInfo.chartList.find(item => item.code === code)
  194. if (chart) {
  195. chart.x += diffX
  196. chart.y += diffY
  197. }
  198. const index = state.pageInfo.chartList.findIndex(
  199. item => item.code === chart.code
  200. )
  201. if (index < 0) {
  202. return
  203. }
  204. Vue.set(state.pageInfo.chartList, index, {
  205. ...state.pageInfo.chartList[index],
  206. ...chart
  207. })
  208. changePresetLine(state, chart)
  209. })
  210. },
  211. // 保存当前状态
  212. saveTimeLine (state, title) {
  213. const date = new Date()
  214. const time = moment(date).format('HH:mm:ss')
  215. // title默认获取当前时间,时分秒
  216. if (!title) {
  217. const date = new Date()
  218. title = `${date.getHours()}:${date.getMinutes()}:${date.getSeconds()}`
  219. }
  220. saveTimeLineFunc(state, title, time)
  221. },
  222. // 撤回/反撤回当前事件线 (undo和redo放到一个函数中,用isUndo区分)
  223. undoTimeLine (state, isUndo = true) {
  224. let currentStore = {}
  225. // 撤回
  226. if (isUndo) {
  227. if (state.timelineStore.length > 0 && state.currentTimeLine > 1) {
  228. // 时间线往前推一个
  229. state.currentTimeLine = state.currentTimeLine - 1
  230. currentStore = state.timelineStore[state.currentTimeLine - 1]
  231. if (currentStore?.chartList) {
  232. state.pageInfo.chartList = _.cloneDeep(currentStore?.chartList)
  233. }
  234. }
  235. }
  236. // 反撤回 redo
  237. if (!isUndo) {
  238. if (state.currentTimeLine < state.timelineStore.length) {
  239. // 时间线往后推一个
  240. state.currentTimeLine = state.currentTimeLine + 1
  241. currentStore = state.timelineStore[state.currentTimeLine - 1]
  242. state.pageInfo.chartList = _.cloneDeep(currentStore?.chartList || [])
  243. }
  244. }
  245. },
  246. clearTimeline (state) {
  247. // 最后一个状态
  248. const lastStore = state.timelineStore[state.timelineStore.length - 1]
  249. // 将最后一个状态作为初始状态,否则下次拖拽后无法回到之前
  250. state.timelineStore = [
  251. {
  252. ...lastStore,
  253. timelineTitle: '初始状态',
  254. updateTime: moment(new Date()).format('HH:mm:ss')
  255. }
  256. ]
  257. state.currentTimeLine = 1
  258. },
  259. // 回退到指定时间线
  260. rollbackTimeline (state, index) {
  261. state.pageInfo.chartList = _.cloneDeep(state.timelineStore[index]?.chartList || [])
  262. state.currentTimeLine = index + 1
  263. },
  264. // 复制组件
  265. copyCharts (state) {
  266. state.copyChartCodes = _.cloneDeep(state.activeCodes)
  267. },
  268. // 粘贴组件
  269. pasteCharts (state) {
  270. const copyChartCodes = state.copyChartCodes
  271. const chartList = state.pageInfo.chartList
  272. // 将选中的组件复制一份, code加上 随机后缀, key 也加上随机后缀, x, y 各增加50
  273. const additionCode = randomString(5)
  274. const copyCharts = copyChartCodes.map(code => {
  275. const chart = chartList.find(item => item.code === code)
  276. const copyChart = _.cloneDeep(chart)
  277. copyChart.code = `${copyChart.code}_${additionCode}`
  278. copyChart.key = `${copyChart.key}_${additionCode}`
  279. copyChart.group = (copyChart.group && copyChart.group !== 'tempGroup') ? `${copyChart.group}_${additionCode}` : ''
  280. copyChart.x += 50
  281. copyChart.y += 50
  282. return copyChart
  283. })
  284. // 将复制的组件添加到chartList中
  285. state.pageInfo.chartList = [...copyCharts, ...state.pageInfo.chartList]
  286. }
  287. }
  288. function changeZIndexFuc (state, list) {
  289. const len = list?.length - 1 || 0
  290. list.forEach((item, i) => {
  291. const index = state.pageInfo.chartList.findIndex(
  292. _item => _item.code === item.code
  293. )
  294. Vue.set(state.pageInfo.chartList[len - index], 'z', i)
  295. })
  296. }
  297. // 改变当前组件的卡尺对齐线
  298. function changePresetLine (state, { x, y, w, h }) {
  299. state.presetLine = [
  300. { type: 'h', site: y || 0 },
  301. { type: 'v', site: x || 0 }
  302. ]
  303. }
  304. function changeGroup (code, state) {
  305. if (code) {
  306. // 找到和此组件group相同的组件,并添加到activeCodes中
  307. const group = state.pageInfo.chartList?.find(item => item.code === code)?.group
  308. if (group) {
  309. state.activeCodes = state.pageInfo.chartList?.filter(chart => chart.group === group && chart.group).map(item => item.code)
  310. }
  311. if (state.shiftKeyDown) {
  312. state.activeCodes = _.uniq([...state.activeCodes, code])
  313. // eslint-disable-next-line no-unused-expressions
  314. state.pageInfo.chartList?.forEach(chart => {
  315. if (state.activeCodes.includes(chart.code)) {
  316. chart.group = 'tempGroup'
  317. }
  318. })
  319. } else {
  320. if (!group) {
  321. state.activeCodes = [code]
  322. }
  323. }
  324. } else {
  325. state.activeCodes = []
  326. state.pageInfo.chartList = state.pageInfo.chartList?.map(chart => ({
  327. ...chart,
  328. group: chart.group === 'tempGroup' ? '' : chart.group
  329. }))
  330. }
  331. }
  332. function saveTimeLineFunc (state, title, time) {
  333. // 最多保存10个状态
  334. const MAX_TIME_LINE = 10
  335. const stateCopy = _.cloneDeep(state.pageInfo)
  336. const date = new Date()
  337. time = time || moment(date).format('HH:mm:ss')
  338. stateCopy.timelineTitle = title
  339. stateCopy.updateTime = time
  340. if (!Array.isArray(state.timelineStore)) {
  341. state.timelineStore = []
  342. }
  343. if (!Number.isInteger(state.currentTimeLine)) {
  344. state.currentTimeLine = 0
  345. }
  346. if (state.timelineStore.length >= MAX_TIME_LINE) {
  347. // 去掉最早的一个
  348. state.timelineStore.shift()
  349. }
  350. state.timelineStore?.push(stateCopy)
  351. state.currentTimeLine = state.timelineStore?.length
  352. }