chartContextMenu.js 6.4 KB

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