3328350766
6 天以前 761eb03d6b3bebd0b197179564c84d89d3d12a0d
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
import { mapMutations, mapState } from 'vuex'
import cloneDeep from 'lodash/cloneDeep'
import { toJpeg, toPng } from 'html-to-image'
import isEmpty from 'lodash/isEmpty'
import { randomString } from 'data-room-ui/js/utils'
import Contextmenu from 'vue-contextmenujs'
import Vue from 'vue'
 
Vue.use(Contextmenu)
export default {
  computed: {
    ...mapState({
      activeCode: state => state.bigScreen.activeCode,
      activeCodes: state => state.bigScreen.activeCodes,
      hoverCode: state => state.bigScreen.hoverCode,
      activeItemConfig: state => state.bigScreen.activeItemConfig,
      chartList: state => state.bigScreen.pageInfo.chartList,
      presetLine: state => state.bigScreen.presetLine
    })
  },
  data () {
    return {}
  },
  mounted () {
  },
  methods: {
    ...mapMutations('bigScreen', ['changeHoverCode', 'changeActiveCode', 'changeChartConfig', 'addItem', 'delItem', 'resetPresetLine', 'changeLayout', 'changeZIndex', 'changeLocked', 'saveTimeLine', 'copyCharts', 'pasteCharts', 'clearActiveCodes']), // 改变hover的组件
    changeHover (code) {
      this.changeHoverCode(code)
    }, // 改变激活的组件
    changeActive (code) {
      this.changeActiveCode(code)
    }, // 打开右侧面板
    openRightPanel (config) {
      this.changeActiveCode(config.code)
      this.$emit('openRightPanel', config)
    }, // 查看数据
    dataView (config) {
      this.changeActiveCode(config.code)
      this.$emit('openDataViewDialog', config)
    }, // 复制组件
    copyItem (config) {
      const newConfig = cloneDeep(config)
      newConfig.code = randomString(8)
      newConfig.title = newConfig.title + '_副本'
      // 区分是从左侧添加还是复制的组件
      newConfig.isCopy = true
      newConfig.x = config.x + 20
      newConfig.y = config.y + 20
      if (config.group) {
        newConfig.group = 'copy_' + config.group
      }
      this.addItem(newConfig)
    }, // 删除单个组件
    deleteItem (config) {
      this.$confirm('确定删除该组件吗?', '提示', {
        confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning', customClass: 'bs-el-message-box'
      }).then(() => {
        this.delItem(config.code)
      })
    }, // 批量删除组合元素
    deleteGroupItem (config) {
      this.$confirm('确定批量删除选中的组件吗?', '提示', {
        confirmButtonText: '确定', cancelButtonText: '取消', type: 'warning', customClass: 'bs-el-message-box'
      }).then(() => {
        // 找到和本组件group相同的组件 删除
        const codes = this.chartList.filter(_chart => _chart.group === config.group && config.group).map(_chart => _chart.code)
        if (!isEmpty(codes)) {
          this.delItem(codes)
        } else {
          this.delItem(config.code)
        }
      })
    }, // 获取组件的坐标字符串,取整 (100, 100)
    getPoint ({ x, y }) {
      return `(${Math.round(x)}, ${Math.round(y)})`
    }, // 组合/取消组合图表
    groupChart (chart) {
      if (!chart.group || chart.group === 'tempGroup') {
        // 添加组合
        // eslint-disable-next-line no-unused-expressions
        this.activeCodes?.forEach(code => {
          const config = this.chartList.find(item => item.code === code)
          this.changeChartConfig({
            ...config, group: `group_${chart.code}`
          })
        })
        this.saveTimeLine('组合图表')
      } else {
        // 取消组合
        this.clearActiveCodes()
        // 找到和本组件group相同的组件 取消group
        this.chartList.forEach(_chart => {
          if (_chart.group === chart.group) {
            this.changeChartConfig({
              ..._chart, group: ''
            })
          }
        })
        this.saveTimeLine('取消组合图表')
      }
    }, // 生成图片
    generateImage (chart) {
      let componentDom = document.querySelector(`#${chart.code} .render-item-wrap`)
      if (this.isPreview) {
        componentDom = document.querySelector(`#${chart.code}`)
      }
      toPng(componentDom)
        .then((dataUrl) => {
          const link = document.createElement('a')
          link.download = `${chart.title}.png`
          link.href = dataUrl
          link.click()
          link.addEventListener('click', () => {
            link.remove()
          })
        }).catch((error) => {
          if (error.type === 'error') {
            // 判断的error.currentTarget是img标签,如果是的,就弹出消息说是图片跨域
            if (error.currentTarget.tagName.toLowerCase() === 'img') {
              // 确认框
              this.$confirm('图片资源跨域导致使用toDataURL API生成图片失败,请将图片上传到资源库,然后在组件中使用资源库中的图片资源,确保没有跨域问题。', '提示', {
                confirmButtonText: '确定',
                showCancelButton: false,
                type: 'warning',
                customClass: 'bs-el-message-box'
              }).then(() => { }).catch(() => { })
            }
          } else {
            this.$message.warning('出现未知错误,请重试')
          }
        })
    }, // 右键菜单
    onContextmenu (event, chart) {
      const isHidden = !chart?.option?.displayOption?.dataAllocation?.enable
      event.preventDefault()
      if (this.isPreview) {
        this.$contextmenu({
          items: [{
            label: '查看数据',
            icon: 'el-icon-view',
            hidden: isHidden,
            onClick: () => {
              this.dataView(chart)
            }
          },
          {
            label: '生成图片',
            icon: 'el-icon-download',
            hidden: isHidden,
            onClick: () => {
              this.generateImage(chart)
            }
          }],
          event, // 鼠标事件信息
          customClass: 'bs-context-menu-class', // 自定义菜单 class
          zIndex: 999, // 菜单样式 z-index
          minWidth: 150 // 主菜单最小宽度
        })
      } else {
        this.$contextmenu({
          items: [{
            label: '配置',
            icon: 'el-icon-setting',
            onClick: () => {
              this.openRightPanel(chart)
            }
          }, {
            label: '删除',
            icon: 'el-icon-delete',
            onClick: () => {
              this.deleteItem(chart)
            }
          }, {
            label: '批量删除',
            icon: 'el-icon-delete',
            onClick: () => {
              this.deleteGroupItem(chart)
            }
          }, {
            label: '复制',
            icon: 'el-icon-copy-document',
            onClick: () => {
              this.copyItem(chart)
            }
          }, {
            label: '组合复制',
            icon: 'el-icon-copy-document',
            onClick: () => {
              this.copyCharts()
              this.pasteCharts()
            }
          }, {
            label: '置于顶层',
            icon: 'el-icon-arrow-up',
            onClick: () => {
              let chartList = cloneDeep(this.chartList)
              // 将当前图表置底
              chartList = chartList.filter(item => item.code !== chart.code)
              chartList.unshift(chart)
              this.changeLayout(chartList)
              this.changeZIndex(chartList)
            }
          }, {
            label: '置于底层',
            icon: 'el-icon-arrow-down',
            onClick: () => {
              let chartList = cloneDeep(this.chartList)
              // 将当前图表置顶
              chartList = chartList.filter(item => item.code !== chart.code)
              chartList.push(chart)
              this.changeLayout(chartList)
              this.changeZIndex(chartList)
            }
          }, {
            label: chart.locked ? '解锁' : '锁定',
            icon: chart.locked ? 'el-icon-unlock' : 'el-icon-lock',
            onClick: () => {
              this.changeLocked(chart)
            }
          }, {
            label: (chart.group && chart.group !== 'tempGroup') ? '取消组合' : '组合',
            icon: (chart.group && chart.group !== 'tempGroup') ? 'iconfont-bigscreen icon-quxiaoguanlian' : 'iconfont-bigscreen icon-zuhe',
            onClick: () => {
              this.groupChart(chart)
            }
          }, {
            label: '查看数据',
            icon: 'el-icon-view',
            hidden: isHidden,
            onClick: () => {
              this.dataView(chart)
            }
          }, {
            label: '生成图片',
            icon: 'el-icon-download',
            onClick: () => {
              this.generateImage(chart)
            }
          }],
          event, // 鼠标事件信息
          customClass: 'bs-context-menu-class', // 自定义菜单 class
          zIndex: 999, // 菜单样式 z-index
          minWidth: 150 // 主菜单最小宽度
        })
      }
      return false
    }
  }
}