Blame view

HHECS.Web/src/views/map/components/mapContextMenu.vue 5.96 KB
1
2
3
4
5
6
<template>
  <div
    id="context-menu"
    v-if="showContextMenu"
    :style="{ left: menuX + 'px', top: menuY + 'px' }"
  >
胡菁 authored
7
8
9
10
11
12
    <ul style="display: flex; flex-direction: column">
      <li
        class="el-icon-delete"
        v-show="showNodeMenu"
        @click="handleMenuItemClick('delete')"
      >
13
14
        删除节点
      </li>
胡菁 authored
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
      <li
        class="el-icon-document-copy"
        v-show="showNodeMenu"
        @click="handleMenuItemClick('copy')"
      >
        复制节点
      </li>
      <li
        class="el-icon-brush"
        v-show="showNodeMenu"
        @click="handleMenuItemClick('paste')"
      >
        粘贴节点
      </li>
      <li
        class="el-icon-edit"
        v-show="showNodeMenu"
        @click="handleMenuItemClick('edit')"
      >
34
35
        编辑属性
      </li>
胡菁 authored
36
37
38
39
40
      <li
        class="el-icon-film"
        v-show="showNodeMenu"
        @click="handleMenuItemClick('control')"
      >
41
42
        管制区域
      </li>
胡菁 authored
43
44
45
46
47
      <li
        class="el-icon-refresh"
        v-show="showCanvasMenu"
        @click="handleMenuItemClick('reset')"
      >
48
49
50
51
        地图复位
      </li>
      <!-- <li v-show="showCanvasMenu" @click="handleMenuItemClick('import')">导入</li>
      <li v-show="showCanvasMenu" @click="handleMenuItemClick('export')">导出</li> -->
胡菁 authored
52
53
54
55
56
      <li
        class="el-icon-data-board"
        v-show="showCanvasMenu"
        @click="handleMenuItemClick('clear')"
      >
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
        清空画布
      </li>
    </ul>
  </div>
</template>

<script>
import { formatTime, openConfirm, showMsg } from "@/utils/index.js";

export default {
  name: "mapContextMenu",
  props: {},
  data() {
    return {
      //画布
      graph: null,
      menuX: 0,
      menuY: 0,
      showContextMenu: false,
      showNodeMenu: false,
      showCanvasMenu: false,
      selectedNode: null,

      isRight: false,
    };
  },
  mounted() {},
  methods: {
    //显示菜单
    showMenu(point, menuType, node) {
      if (node != null) {
        if (this.isRight) {
          if (this.selectedNode) {
            this.graph.unselect(this.selectedNode);
          }
        }
        this.graph.select(node);
        this.isRight = true;
      } else {
        let isselect = this.graph.getSelectedCells();
        isselect.forEach((i) => {
          this.graph.unselect(i);
        });
      }
      this.selectedNode = node;
      this.menuX = point.x;
      this.menuY = point.y;
      this.showContextMenu = true;
      this.showCanvasMenu = false;
      this.showNodeMenu = false;
      if (menuType == 0) {
        this.showCanvasMenu = true;
      } else if (menuType == 1) {
        this.showNodeMenu = true;
      }
      // console.log("进来了");
    },
    //隐藏菜单
    hideMenu() {
      this.isRight = false;
      this.showContextMenu = false;
      this.showCanvasMenu = false;
      this.showNodeMenu = false;
    },
    //右键菜单
    handleMenuItemClick(action) {
      // if (this.selectedNode) {
      switch (action) {
        case "delete":
          // if (this.selectedNode) {
          //   this.graph.removeNode(this.selectedNode.id);
          // }
胡菁 authored
129
130
          let selected1 = this.graph.getSelectedCells();
          selected1.forEach((i) => {
131
132
133
            this.graph.removeNode(i.id);
          });
          break;
胡菁 authored
134
135
136
137
138
139
140
        case "copy":
          let selected2 = this.graph.getSelectedCells();
          this.graph.copy(selected2);
          break;
        case "paste":
          this.graph.paste();
          break;
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
        case "edit":
          // console.log("编辑节点:", this.selectedNode);
          // this.attributeEdit(this.selectedNode);
          if (this.selectedNode) {
            this.$emit("attribute", this.selectedNode);
          }
          break;
        case "control":
          this.$emit("control");
          break;
        case "clear":
          // 获取所有节点并移除
          openConfirm(this, "是否进行清屏操作?", () => {
            this.graph.clearCells();
          });
          break;
        case "reset":
          this.reset();
          break;
        case "import":
          this.clickSave();
          break;
        case "export":
          this.save();
          break;
      }
      // }
      this.showContextMenu = false;
    },

    clickSave() {
      document.getElementById("files").click();
    },

    //导入
    savetemplate(e) {
      const file = event.target.files[0];
      const reader = new FileReader();
      reader.readAsText(file, "UTF-8");
      reader.onload = (evt) => {
        const fileContent = JSON.parse(evt.target.result);
        sessionStorage.setItem("mapData", evt.target.result);
        this.graph.fromJSON(fileContent);
      };
    },

    //导出
    save() {
      // // console.log(data.cells,'datadatadatadata')
      // let str = new Blob([data.cells], {type: 'text/plain;charset=utf-8'});
      // // 注意这里要手动写上文件的后缀名
      // saveAs(str, `组件json.txt`);
      sessionStorage.setItem("mapData", JSON.stringify(this.graph.toJSON()));
      const blob = new Blob([JSON.stringify(this.graph.toJSON())], {
        type: "text/plain",
      });
      const url = URL.createObjectURL(blob);
      const a = document.createElement("a");
      a.href = url;
      a.download = "组件json.text";
      document.body.appendChild(a);
      a.style.display = "none";
      a.click();
      document.body.removeChild(a);
      URL.revokeObjectURL(url);
    },

    //复位
    reset() {
      // 恢复初始缩放比例
      this.graph.scale(1, 1);
      // 恢复初始位置
      this.graph.translate(0, 0);
    },
  },
};
</script>

<style lang="scss" scoped>
/* ... 原有样式 ... */
#context-menu {
  position: absolute;
  background: white;
  border: 1px solid #ccc;
  box-shadow: 2px 2px 5px rgba(0, 0, 0, 0.2);
  z-index: 1000;
  font-size: 14px;
  color: #606266;
}

#context-menu ul {
  list-style-type: none;
  padding: 0;
  margin: 0;
}

#context-menu li {
  padding: 8px 16px;
  cursor: pointer;
}

#context-menu li:hover {
  background: #f0f0f0;
}
</style>