mutations.js 11 KB

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