chartContextMenu.js 7.6 KB

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