forked from ymm-tech/gods-pen
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContextMenu.vue
executable file
·236 lines (227 loc) · 6.22 KB
/
ContextMenu.vue
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
<template>
<div v-if="visible" class="tool" :style="style">
<div class="item-h" v-show="!isRootNode">
<a class="item-sub" @click="itemClick('copy')">
<i class="iconfont icon-copy"></i>
复制
</a>
<a class="item-sub" @click="itemClick('deleteNode')">
<i class="iconfont icon-delete"></i>
删除
</a>
</div>
<a class="item" @click="itemClick('copyStyle')">
<i class="iconfont icon-copy"></i>
复制样式
</a>
<a class="item" @click="itemClick('pasteStyle')">
<i class="iconfont icon-paste"></i>
粘贴样式
</a>
<a class="item" @click="itemClick('exportJson')">
<i class="iconfont icon-export"></i>
导出节点
</a>
<a class="item" @click="itemClick('importJson')">
<i class="iconfont icon-import"></i>
导入节点
</a>
<a class="item" @click="itemClick('combinedNode')">
<i class="iconfont icon-import"></i>
另存为组合组件
</a>
<a class="item" @click="itemClick('reloadNode')">
<i class="iconfont icon-refresh"></i>
重载节点
</a>
<template v-if="!isRootNode && stacked">
<div class="item-h">
<a class="item-sub" @click="moveLayer(1)">
上移
</a>
<a class="item-sub" @click="moveLayer(-1)" v-show="!isRootNode">
下移
</a>
<a class="item-sub" @click="moveLayer(Number.MAX_VALUE)" v-show="!isRootNode">
置顶
</a>
<a class="item-sub" @click="moveLayer(Number.MIN_VALUE)" v-show="!isRootNode">
置底
</a>
</div>
</template>
<template v-if="!isRootNode && !stacked && !fixed">
<div class="item-h">
<a class="item-sub" @click="moveLayer(1)">
下移
</a>
<a class="item-sub" @click="moveLayer(-1)" v-show="!isRootNode">
上移
</a>
</div>
</template>
</div>
</template>
<style lang="stylus" rel="stylesheet/stylus" scoped type="text/stylus">
.tool {
position: fixed;
z-index: 19900;
width: 160px;
background-color: rgb(84, 92, 100);
color: rgb(255, 255, 255);
line-height: 40px;
font-size: 14px;
.item {
display: block;
height: 40px;
cursor: pointer;
text-align: left;
.iconfont {
margin: 0 0 0 15px;
}
&:hover {
color: rgb(255, 208, 75);
background-color: rgb(84, 92, 100);
}
}
.item-h {
display: flex;
height: 40px;
text-align: center;
.item-sub {
flex: 1;
cursor: pointer;
&:hover {
color: rgb(255, 208, 75);
background-color: rgb(84, 92, 100);
}
}
}
}
</style>
<script type="text/ecmascript-6">
/**
* 对节点右键弹出的功能菜单
*/
import BaseComponent from 'src/extend/BaseComponent'
export default {
mixins: [BaseComponent],
components: {},
name: 'ContextMenu',
props: {
},
data: function () {
return {
visible: false,
node: null,
style: {
left: 0,
top: 0
}
}
},
computed: {
isRootNode () {
var node = this.node
if (!node) return false
return node.nodeInfo && (node.nodeInfo.id === 'root' || node.nodeInfo.type === 'node')
},
stacked () {
return this.node.stacked
},
fixed () {
return this.node.nodeInfo.style && this.node.nodeInfo.style.position === 'fixed'
}
},
mounted: function () {
var maxHeight = document.documentElement.clientHeight
this.ema.bind('show.contextMenu', (node, e) => {
var pointerY = e.pageY
var menuHeight = 320
// var menuWidth = 160
this.style.left = (2 + e.pageX) + 'px' // 横向基本不会碰撞
this.style.top = (2 + pointerY - Math.max(menuHeight - (maxHeight - pointerY), 0)) + 'px' // 处理底部碰撞
this.node = node
this.visible = true
node.actived('contextMenu')
})
this.ema.bind('hide.contextMenu', () => {
this.visible = false
})
},
methods: {
itemClick: function (key) {
this.visible = false
if (typeof this[key] == 'function') {
this[key](this.node)
}
},
deleteNode: function (node) {
this.ema.fire('move.node', this.node.nodeInfo.id, null)
},
moveLayer: function (number) {
this.visible = false
if (this.node && this.node.$parent) {
this.node.$parent.moveLayer(this.node.nodeInfo, number)
}
},
copy: function () {
if (this.node && this.node.$parent) {
this.node.$parent.copyChild(this.node.nodeInfo, {})
}
},
copyStyle () {
this.node.copyStyle()
},
pasteStyle () {
this.node.pasteStyle()
},
reloadNode () {
if (this.node) this.ema.fire('component.reload', this.node && this.node.nodeInfo.id)
},
importJson: function () {
var me = this
var content = JSON.stringify(this.node.nodeInfo, null, 2)
var reg = /(?:"id"\s?\:\s?")(\w+)"/
var idCache = (content.match(new RegExp(reg, 'g')) || []).map(ids => ids.replace(reg, (m, p) => p))
this.openDialog({
name: 'd-nodejson',
data: {
idCache,
content,
type: 'import'
},
methods: {
saveContent: function (node) {
Object.assign(me.node.nodeInfo, node)
}
}
})
},
exportJson: function () {
this.openDialog({
name: 'd-nodejson',
data: {
content: JSON.stringify(this.node.nodeInfo, null, 2),
type: 'export'
},
methods: {
}
})
},
combinedNode () {
if (this.demoMode) return this.$alert('您处在 demo 模式下,不能保存数据哦')
var el = this.node.$el
var elID = `el_${Date.now()}`
el.setAttribute('el', elID)
this.openDialog({
name: 'd-saveCombinedComponent',
data: {
elID,
content: JSON.stringify(this.node.nodeInfo),
}
})
}
}
}
</script>