whyczyk
2021-10-14 09f409e9d5f40fd5d1b3a6a342389acf7adf7f2e
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
251
252
253
254
255
<template>
  <div class="pageWarp" ref="pageWarp">
    <vue-draggable-resizable :w="item.w" :h="item.h" :x="item.x" :y="item.y" @dragging="onDrag" @dragstop="onDragstop"
      @refLineParams="getRefLineParams" @resizing="onResize" @resizestop="onResizstop" :parent="true" :debug="false"
      :snap="true" :snapTolerance="5" :draggable="draggable" :resizable="resizable" :grid="[10,10]"
      style="transition: none; will-change: transform;" @activated="onActived(item)"
      v-for="(item,i) in nowlayOut.children" :key="i">
      <div @contextmenu.prevent.stop="openMenu(item,$event)" style="width:100%;height:100%;">
        <layout-box :title="item.name">
          <div style="width:100%;height:100%" :id="'layout-box'+item.id" :ref="'layout-box'+item.id"></div>
        </layout-box>
      </div>
    </vue-draggable-resizable>
 
 
    <span class="ref-line v-line" v-for="(item,index) in vLine" :key="'vLine'+index" v-show="item.display"
      :style="{ left: item.position, top: item.origin, height: item.lineLength}"></span>
    <span class="ref-line h-line" v-for="(item,index) in hLine" :key="'hLine'+index" v-show="item.display"
      :style="{ top: item.position, left: item.origin, width: item.lineLength}"></span>
    <ul v-show="rightMenuVisible" :style="{left:contextmenuLeft+'px',top:contextmenuTop+'px'}" class="contextmenu">
      <li><i class="el-icon-refresh-right"></i> 重新加载</li>
      <li @click="removeModular"><i class="el-icon-delete"></i> 删除</li>
    </ul>
  </div>
</template>
 
<script>
  import Vue from "vue";
  import VueDraggableResizable from 'vue-draggable-resizable-gorkys/src/components/vue-draggable-resizable'
  import 'vue-draggable-resizable-gorkys/dist/VueDraggableResizable.css'
  import LayoutBox from "@/components/LayoutBox";
  let clientWidth, clientHeight;
  export default {
    props: ['layOut', 'draggable', 'resizable'],
    components: {
      VueDraggableResizable,
      LayoutBox,
    },
    data() {
      return {
        draggableActived: null,
        vLine: [],
        hLine: [],
        rightMenuVisible: false,
        contextmenuTop: 0,
        contextmenuLeft: 0,
        clickItem: null,
        parentByValue: false,
        nowlayOut: {},
        modularArr: []
      }
    },
    watch: {
      rightMenuVisible(value) {
        if (value) {
          document.body.addEventListener('click', this.closeMenu)
        } else {
          document.body.removeEventListener('click', this.closeMenu)
        }
      },
      'layOut': {
        handler(val) {
          this.nowlayOut = JSON.parse(JSON.stringify(val));
          this.loadLayout();
        },
        deep: true
      }
    },
    mounted() {
      //禁止鼠标右键菜单
      document.oncontextmenu = function () {
        return false;
      };
      this.$nextTick(() => {
        clientWidth = this.$refs.pageWarp.clientWidth;
        clientHeight = this.$refs.pageWarp.clientHeight;
      });
    },
    methods: {
      // 选中要调整的面板时
      onActived(panel) {
        this.draggableActived = panel;
      },
      //面板改变大小时
      onResize(x, y, width, height) {
        this.nowlayOut.children.filter((card) => {
          if (this.draggableActived.id === card.id) {
            card.w = width;
            card.h = height;
            card.x = x;
            card.y = y;
          }
          return true;
        });
      },
      onResizstop(x, y, width, height) {
        this.nowlayOut.children.filter((card) => {
          if (this.draggableActived.id === card.id) {
            card.w = width;
            card.h = height;
            card.x = x;
            card.y = y;
          }
          return true;
        });
        let sendData = JSON.parse(JSON.stringify(this.nowlayOut));
        sendData.children.map((item) => {
          item.x = item.x / clientWidth;
          item.y = item.y / clientHeight;
          item.w = item.w / clientWidth;
          item.h = item.h / clientHeight;
        })
        sendData.isremoved = false;
        this.$emit('onDragstop', sendData);
      },
      //面板改变位置时
      onDrag(x, y) {
        this.nowlayOut.children.filter((card) => {
          if (this.draggableActived.id === card.id) {
            card.x = x;
            card.y = y;
          }
          return true;
        });
      },
      onDragstop(x, y) {
        this.nowlayOut.children.filter((card) => {
          if (this.draggableActived.id === card.id) {
            card.x = x;
            card.y = y;
          }
          return true;
        });
        let sendData = JSON.parse(JSON.stringify(this.nowlayOut));
        sendData.children.map((item) => {
          item.x = item.x / clientWidth;
          item.y = item.y / clientHeight;
          item.w = item.w / clientWidth;
          item.h = item.h / clientHeight;
        })
        sendData.isremoved = false;
        this.$emit('onDragstop', sendData);
      },
      // 辅助线回调事件
      getRefLineParams(params) {
        const {
          vLine,
          hLine
        } = params;
        this.vLine = vLine
        this.hLine = hLine
      },
      //模块右键菜单打开
      openMenu(clickItem, e) {
        this.clickItem = clickItem;
        this.contextmenuLeft = e.clientX
        this.contextmenuTop = e.clientY
        this.rightMenuVisible = true
      },
      //模块右键菜单关闭
      closeMenu() {
        this.rightMenuVisible = false
      },
      //删除大屏数据模块
      removeModular() {
        this.nowlayOut.children.map((item, i) => {
          if (item.name == this.clickItem.name) {
            this.nowlayOut.children.splice(i, 1);
            this.modularArr.splice(i, 1);
          }
        })
        let sendData = JSON.parse(JSON.stringify(this.nowlayOut));
        sendData.children.map((item) => {
          item.x = item.x / clientWidth;
          item.y = item.y / clientHeight;
          item.w = item.w / clientWidth;
          item.h = item.h / clientHeight;
        });
        sendData.isremoved = true;
        this.$emit('removeModular', sendData);
      },
      // 加载布局数据
      loadLayout() {
        this.$nextTick(() => {
          this.nowlayOut.children.map((item) => {
            item.x *= clientWidth;
            item.y *= clientHeight;
            item.w *= clientWidth;
            item.h *= clientHeight;
            let modular = require(`./charts/${item.type}.vue`).default;
            let modularExtend = Vue.extend(modular);
            let chartModular = new modularExtend().$mount();
            chartModular.id = `chart${item.id}`;
            let nowBox = this.$refs[`layout-box${item.id}`];
            if (nowBox[0].children.length == 0) {
              nowBox[0].appendChild(chartModular.$el)
              this.modularArr.push(chartModular);
            } else {
              nowBox[0].replaceChild(chartModular.$el, nowBox[0].children[0])
            }
            // if (this.nowlayOut.isremoved) {
            //   nowBox[0].replaceChild(chartModular.$el, nowBox[0].children[0])
            // }
            chartModular.setData(item.setData);
            chartModular.resize();
          })
        })
      }
    }
  }
</script>
 
<style scoped>
  .pageWarp {
    width: 100%;
    height: calc(100% - 52px);
    position: relative;
  }
 
  .contextmenu {
    margin: 0;
    background: #27343e;
    z-index: 3000;
    position: fixed;
    list-style-type: none;
    padding: 5px 0;
    border-radius: 4px;
    font-size: 12px;
    font-weight: 400;
    color: #bcc9d4;
    box-shadow: 2px 2px 3px 0 rgba(0, 0, 0, 0.3);
  }
 
  .contextmenu li {
    padding: 0 6px;
    line-height: 28px;
    height: 28px;
    border-left: 2px solid transparent;
    cursor: pointer;
    overflow: hidden;
    padding-right: 3em;
    display: flex;
    align-items: center;
  }
 
  .contextmenu li i {
    margin-right: 8px;
  }
 
  .contextmenu li:hover {
    background-color: #1d262e;
    color: #2681ff;
    border-left: 2px solid #2681ff;
  }
</style>