chartContextMenu.js 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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. dataView (config) {
  55. this.changeActiveCode(config.code)
  56. this.$emit('openDataViewDialog', config)
  57. },
  58. // 复制组件
  59. copyItem (config) {
  60. const newConfig = cloneDeep(config)
  61. newConfig.code = randomString(8)
  62. newConfig.title = newConfig.title + '_副本'
  63. // 区分是从左侧添加还是复制的组件
  64. newConfig.isCopy = true
  65. newConfig.x = config.x + 20
  66. newConfig.y = config.y + 20
  67. if (config.group) {
  68. newConfig.group = 'copy_' + config.group
  69. }
  70. this.addItem(newConfig)
  71. },
  72. // 删除单个组件
  73. deleteItem (config) {
  74. this.$confirm('确定删除该组件吗?', '提示', {
  75. confirmButtonText: '确定',
  76. cancelButtonText: '取消',
  77. type: 'warning',
  78. customClass: 'bs-el-message-box'
  79. }).then(() => {
  80. this.delItem(config.code)
  81. })
  82. },
  83. // 批量删除组合元素
  84. deleteGroupItem (config) {
  85. this.$confirm('确定批量删除选中的组件吗?', '提示', {
  86. confirmButtonText: '确定',
  87. cancelButtonText: '取消',
  88. type: 'warning',
  89. customClass: 'bs-el-message-box'
  90. }).then(() => {
  91. // 找到和本组件group相同的组件 删除
  92. const codes = this.chartList.filter(_chart => _chart.group === config.group && config.group).map(_chart => _chart.code)
  93. if (!isEmpty(codes)) {
  94. this.delItem(codes)
  95. } else {
  96. this.delItem(config.code)
  97. }
  98. })
  99. },
  100. // 获取组件的坐标字符串,取整 (100, 100)
  101. getPoint ({ x, y }) {
  102. return `(${Math.round(x)}, ${Math.round(y)})`
  103. },
  104. // 组合/取消组合图表
  105. groupChart (chart) {
  106. if (!chart.group || chart.group === 'tempGroup') {
  107. // 添加组合
  108. // eslint-disable-next-line no-unused-expressions
  109. this.activeCodes?.forEach(code => {
  110. const config = this.chartList.find(item => item.code === code)
  111. this.changeChartConfig({
  112. ...config,
  113. group: `group_${chart.code}`
  114. })
  115. })
  116. this.saveTimeLine('组合图表')
  117. } else {
  118. // 取消组合
  119. // 找到和本组件group相同的组件 取消group
  120. this.chartList.forEach(_chart => {
  121. if (_chart.group === chart.group) {
  122. this.changeChartConfig({
  123. ..._chart,
  124. group: ''
  125. })
  126. }
  127. })
  128. this.saveTimeLine('取消组合图表')
  129. }
  130. },
  131. // 右键菜单
  132. onContextmenu (event, chart) {
  133. const isHidden = !chart?.option?.displayOption?.dataAllocation?.enable
  134. event.preventDefault()
  135. if (this.isPreview) {
  136. this.$contextmenu({
  137. items: [
  138. {
  139. label: '查看数据',
  140. icon: 'el-icon-view',
  141. hidden: isHidden,
  142. onClick: () => {
  143. this.dataView(chart)
  144. }
  145. }
  146. ],
  147. event, // 鼠标事件信息
  148. customClass: 'bs-context-menu-class', // 自定义菜单 class
  149. zIndex: 999, // 菜单样式 z-index
  150. minWidth: 150 // 主菜单最小宽度
  151. })
  152. } else {
  153. this.$contextmenu({
  154. items: [
  155. {
  156. label: '配置',
  157. icon: 'el-icon-setting',
  158. onClick: () => {
  159. this.openRightPanel(chart)
  160. }
  161. },
  162. {
  163. label: '删除',
  164. icon: 'el-icon-delete',
  165. onClick: () => {
  166. this.deleteItem(chart)
  167. }
  168. },
  169. {
  170. label: '批量删除',
  171. icon: 'el-icon-delete',
  172. onClick: () => {
  173. this.deleteGroupItem(chart)
  174. }
  175. },
  176. {
  177. label: '复制',
  178. icon: 'el-icon-copy-document',
  179. onClick: () => {
  180. this.copyItem(chart)
  181. }
  182. },
  183. {
  184. label: '组合复制',
  185. icon: 'el-icon-copy-document',
  186. onClick: () => {
  187. this.copyCharts()
  188. this.pasteCharts()
  189. }
  190. },
  191. {
  192. label: '置于顶层',
  193. icon: 'el-icon-arrow-up',
  194. onClick: () => {
  195. let chartList = cloneDeep(this.chartList)
  196. // 将当前图表置底
  197. chartList = chartList.filter(item => item.code !== chart.code)
  198. chartList.unshift(chart)
  199. this.changeLayout(chartList)
  200. this.changeZIndex(chartList)
  201. }
  202. },
  203. {
  204. label: '置于底层',
  205. icon: 'el-icon-arrow-down',
  206. onClick: () => {
  207. let chartList = cloneDeep(this.chartList)
  208. // 将当前图表置顶
  209. chartList = chartList.filter(item => item.code !== chart.code)
  210. chartList.push(chart)
  211. this.changeLayout(chartList)
  212. this.changeZIndex(chartList)
  213. }
  214. },
  215. {
  216. label: chart.locked ? '解锁' : '锁定',
  217. icon: chart.locked ? 'el-icon-unlock' : 'el-icon-lock',
  218. onClick: () => {
  219. this.changeLocked(chart)
  220. }
  221. },
  222. {
  223. label: (chart.group && chart.group !== 'tempGroup') ? '取消组合' : '组合',
  224. icon: (chart.group && chart.group !== 'tempGroup') ? 'iconfont-bigscreen icon-quxiaoguanlian' : 'iconfont-bigscreen icon-zuhe',
  225. onClick: () => {
  226. this.groupChart(chart)
  227. }
  228. },
  229. {
  230. label: '查看数据',
  231. icon: 'el-icon-view',
  232. hidden: isHidden,
  233. onClick: () => {
  234. this.dataView(chart)
  235. }
  236. }
  237. ],
  238. event, // 鼠标事件信息
  239. customClass: 'bs-context-menu-class', // 自定义菜单 class
  240. zIndex: 999, // 菜单样式 z-index
  241. minWidth: 150 // 主菜单最小宽度
  242. })
  243. }
  244. return false
  245. }
  246. }
  247. }