mutations.js 12 KB

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