mutations.js 12 KB

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