chartContextMenu.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. import { mapMutations, mapState } from 'vuex'
  2. import _ from 'lodash'
  3. import { randomString } from 'packages/js/utils'
  4. import Contextmenu from 'vue-contextmenujs'
  5. import Vue from 'vue'
  6. Vue.use(Contextmenu)
  7. export default {
  8. computed: {
  9. ...mapState({
  10. activeCode: state => state.bigScreen.activeCode,
  11. activeCodes: state => state.bigScreen.activeCodes,
  12. hoverCode: state => state.bigScreen.hoverCode,
  13. activeItemConfig: state => state.bigScreen.activeItemConfig,
  14. chartList: state => state.bigScreen.pageInfo.chartList,
  15. presetLine: state => state.bigScreen.presetLine
  16. })
  17. },
  18. data () {
  19. return {
  20. }
  21. },
  22. mounted () {},
  23. methods: {
  24. ...mapMutations('bigScreen', [
  25. 'changeHoverCode',
  26. 'changeActiveCode',
  27. 'changeChartConfig',
  28. 'addItem',
  29. 'delItem',
  30. 'resetPresetLine',
  31. 'changeLayout',
  32. 'changeZIndex',
  33. 'changeLocked',
  34. 'saveTimeLine',
  35. 'copyCharts',
  36. 'pasteCharts'
  37. ]),
  38. // 改变hover的组件
  39. changeHover (code) {
  40. this.changeHoverCode(code)
  41. },
  42. // 改变激活的组件
  43. changeActive (code) {
  44. this.changeActiveCode(code)
  45. },
  46. // 打开右侧面板
  47. openRightPanel (config) {
  48. this.changeActiveCode(config.code)
  49. this.$emit('openRightPanel', config)
  50. },
  51. // 复制组件
  52. copyItem (config) {
  53. const newConfig = _.cloneDeep(config)
  54. newConfig.code = randomString(8)
  55. newConfig.title = newConfig.title + '_副本'
  56. // 区分是从左侧添加还是复制的组件
  57. newConfig.isCopy = true
  58. newConfig.x = config.x + 20
  59. newConfig.y = config.y + 20
  60. if (config.group) {
  61. newConfig.group = 'copy_' + config.group
  62. }
  63. this.addItem(newConfig)
  64. },
  65. // 删除单个组件
  66. deleteItem (config) {
  67. this.$confirm('确定删除该组件吗?', '提示', {
  68. confirmButtonText: '确定',
  69. cancelButtonText: '取消',
  70. type: 'warning',
  71. customClass: 'bs-el-message-box'
  72. }).then(() => {
  73. this.delItem(config.code)
  74. })
  75. },
  76. // 批量删除组合元素
  77. deleteGroupItem (config) {
  78. this.$confirm('确定批量删除选中的组件吗?', '提示', {
  79. confirmButtonText: '确定',
  80. cancelButtonText: '取消',
  81. type: 'warning',
  82. customClass: 'bs-el-message-box'
  83. }).then(() => {
  84. // 找到和本组件group相同的组件 删除
  85. const codes = this.chartList.filter(_chart => _chart.group === config.group && config.group).map(_chart => _chart.code)
  86. if (!_.isEmpty(codes)) {
  87. this.delItem(codes)
  88. } else {
  89. this.delItem(config.code)
  90. }
  91. })
  92. },
  93. // 获取组件的坐标字符串,取整 (100, 100)
  94. getPoint ({ x, y }) {
  95. return `(${Math.round(x)}, ${Math.round(y)})`
  96. },
  97. // 组合/取消组合图表
  98. groupChart (chart) {
  99. if (!chart.group || chart.group === 'tempGroup') {
  100. // 添加组合
  101. // eslint-disable-next-line no-unused-expressions
  102. this.activeCodes?.forEach(code => {
  103. const config = this.chartList.find(item => item.code === code)
  104. this.changeChartConfig({
  105. ...config,
  106. group: `group_${chart.code}`
  107. })
  108. })
  109. this.saveTimeLine('组合图表')
  110. } else {
  111. // 取消组合
  112. // 找到和本组件group相同的组件 取消group
  113. this.chartList.forEach(_chart => {
  114. if (_chart.group === chart.group) {
  115. this.changeChartConfig({
  116. ..._chart,
  117. group: ''
  118. })
  119. }
  120. })
  121. this.saveTimeLine('取消组合图表')
  122. }
  123. },
  124. // 右键菜单
  125. onContextmenu (event, chart) {
  126. event.preventDefault()
  127. this.$contextmenu({
  128. items: [
  129. {
  130. label: '配置',
  131. icon: 'el-icon-setting',
  132. onClick: () => {
  133. this.openRightPanel(chart)
  134. }
  135. },
  136. {
  137. label: '删除',
  138. icon: 'el-icon-delete',
  139. onClick: () => {
  140. this.deleteItem(chart)
  141. }
  142. },
  143. {
  144. label: '批量删除',
  145. icon: 'el-icon-delete',
  146. onClick: () => {
  147. this.deleteGroupItem(chart)
  148. }
  149. },
  150. {
  151. label: '复制',
  152. icon: 'el-icon-copy-document',
  153. onClick: () => {
  154. this.copyItem(chart)
  155. }
  156. },
  157. {
  158. label: '组合复制',
  159. icon: 'el-icon-copy-document',
  160. onClick: () => {
  161. this.copyCharts()
  162. this.pasteCharts()
  163. }
  164. },
  165. {
  166. label: '置于顶层',
  167. icon: 'el-icon-arrow-up',
  168. onClick: () => {
  169. let chartList = _.cloneDeep(this.chartList)
  170. // 将当前图表置底
  171. chartList = chartList.filter(item => item.code !== chart.code)
  172. chartList.unshift(chart)
  173. this.changeLayout(chartList)
  174. this.changeZIndex(chartList)
  175. }
  176. },
  177. {
  178. label: '置于底层',
  179. icon: 'el-icon-arrow-down',
  180. onClick: () => {
  181. let chartList = _.cloneDeep(this.chartList)
  182. // 将当前图表置顶
  183. chartList = chartList.filter(item => item.code !== chart.code)
  184. chartList.push(chart)
  185. this.changeLayout(chartList)
  186. this.changeZIndex(chartList)
  187. }
  188. },
  189. {
  190. label: chart.locked ? '解锁' : '锁定',
  191. icon: chart.locked ? 'el-icon-unlock' : 'el-icon-lock',
  192. onClick: () => {
  193. this.changeLocked(chart)
  194. }
  195. },
  196. {
  197. label: (chart.group && chart.group !== 'tempGroup') ? '取消组合' : '组合',
  198. icon: (chart.group && chart.group !== 'tempGroup') ? 'iconfont-bigscreen icon-quxiaoguanlian' : 'iconfont-bigscreen icon-zuhe',
  199. onClick: () => {
  200. this.groupChart(chart)
  201. }
  202. }
  203. ],
  204. event, // 鼠标事件信息
  205. customClass: 'bs-context-menu-class', // 自定义菜单 class
  206. zIndex: 999, // 菜单样式 z-index
  207. minWidth: 150 // 主菜单最小宽度
  208. })
  209. return false
  210. }
  211. }
  212. }