-
Notifications
You must be signed in to change notification settings - Fork 68
/
Copy pathContextMenuItem.vue
387 lines (369 loc) · 11.4 KB
/
ContextMenuItem.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
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
<template>
<div
v-if="!hidden"
class="mx-context-menu-item-wrapper"
ref="menuItemRef"
data-type="ContextMenuItem"
>
<!--Custom render-->
<VNodeRender v-if="globalHasSlot('itemRender')" :vnode="() => globalRenderSlot('itemRender', getItemDataForChildren())" />
<VNodeRender v-else-if="customRender" :vnode="customRender" :data="getItemDataForChildren()" />
<!--Default item-->
<div
v-else
:class="[
'mx-context-menu-item',
(disabled ? 'disabled' : ''),
(keyBoardFocusMenu ? 'keyboard-focus' : ''),
(customClass ? (' ' + customClass) : ''),
(showSubMenu ? 'open' : ''),
]"
@click="onClick"
@mouseenter="onMouseEnter"
>
<slot>
<div class="mx-item-row">
<div :class="[
'mx-icon-placeholder',
preserveIconWidth ? 'preserve-width': '',
]">
<slot name="icon">
<VNodeRender v-if="globalHasSlot('itemIconRender')" :vnode="() => globalRenderSlot('itemIconRender', getItemDataForChildren())" />
<svg v-else-if="typeof svgIcon === 'string' && svgIcon" class="icon svg" v-bind="svgProps">
<use :xlink:href="svgIcon"></use>
</svg>
<VNodeRender v-else-if="(typeof icon !== 'string')" :vnode="icon" :data="icon" />
<i v-else-if="typeof icon === 'string' && icon !== ''" :class="icon + ' icon '+ iconFontClass + ' ' + options.iconFontClass"></i>
</slot>
<slot v-if="checked" name="check">
<VNodeRender v-if="globalHasSlot('itemCheckRender')" :vnode="() => globalRenderSlot('itemCheckRender', getItemDataForChildren())" />
<ContextMenuIconCheck />
</slot>
</div>
<slot name="label">
<VNodeRender v-if="globalHasSlot('itemLabelRender')" :vnode="() => globalRenderSlot('itemLabelRender', getItemDataForChildren())" />
<span class="label" v-else-if="typeof label === 'string'">{{ label }}</span>
<VNodeRender v-else :vnode="label" :data="label" />
</slot>
</div>
<div class="mx-item-row">
<slot v-if="shortcut || $slots.shortcut" name="shortcut">
<VNodeRender v-if="globalHasSlot('itemShortcutRender')" :vnode="() => globalRenderSlot('itemShortcutRender', getItemDataForChildren())" />
<span class="mx-shortcut">{{ shortcut }}</span>
</slot>
<slot v-if="showRightArrow" name="rightArrow">
<VNodeRender v-if="globalHasSlot('itemRightArrowRender')" :vnode="() => globalRenderSlot('itemRightArrowRender', getItemDataForChildren())" />
<ContextMenuIconRight />
</slot>
</div>
</slot>
</div>
<!--Sub menu render-->
<Transition v-if="options.menuTransitionProps" v-bind="options.menuTransitionProps">
<slot v-if="showSubMenu" name="submenu" :context="menuItemInstance"></slot>
</Transition>
<slot v-else-if="showSubMenu" name="submenu" :context="menuItemInstance"></slot>
</div>
</template>
<script setup lang="ts">
import { inject, nextTick, onBeforeUnmount, onMounted, type PropType, ref, type SVGAttributes, toRefs, type TransitionProps, type Ref, computed, provide } from 'vue'
import type { SubMenuParentContext } from './ContextSubMenu.vue'
import type { GlobalHasSlot, GlobalRenderSlot } from './ContextMenu.vue'
import type { MenuItem, MenuItemContext, MenuOptions } from './ContextMenuDefine'
import { VNodeRender } from './ContextMenuUtils'
import ContextMenuIconCheck from './ContextMenuIconCheck.vue'
import ContextMenuIconRight from './ContextMenuIconRight.vue'
/**
* Menu Item
*/
const props = defineProps({
/**
* Is this menu disabled?
*/
disabled: {
type: Boolean,
default: false
},
/**
* Is this menu hidden?
*/
hidden: {
type: Boolean,
default: false
},
customRender: {
type: Function,
default: null
},
/**
* Custom css class for submenu
*/
customClass: {
type: String,
default: ''
},
clickHandler: {
type: Function as PropType<(e: MouseEvent|KeyboardEvent) => void>,
default: null
},
/**
* Menu label
*/
label: {
type: [String, Object, Function],
default: ''
},
/**
* Menu icon (for icon class)
*/
icon: {
type: [String, Object, Function],
default: ''
},
/**
* Custom icon library font class name.
*
* Only for css font icon, If you use the svg icon, you do not need to use this.
*/
iconFontClass: {
type: String,
default: 'iconfont'
},
/**
* Is this menu item checked?
*
* The check mark are displayed on the left side of the icon, so it is not recommended to display the icon at the same time.
*/
checked: {
type: Boolean,
default: false
},
/**
* Shortcut key text display on the right.
*
* The shortcut keys here are only for display. You need to handle the key events by yourself.
*/
shortcut: {
type: String,
default: ''
},
/**
* Display icons use svg symbol (`<use xlink:href="#icon-symbol-name">`) , only valid when icon attribute is empty.
*/
svgIcon: {
type: String,
default: ''
},
/**
* The user-defined attribute of the svg tag, which is valid when using `svgIcon`.
*/
svgProps: {
type: Object as PropType<SVGAttributes>,
default: null
},
/**
* Should a fixed-width icon area be reserved for menu items without icon. (this item)
*
* Default is true .
*
* The width of icon area can be override with css var `--mx-menu-placeholder-width`.
*/
preserveIconWidth: {
type: Boolean,
default: true,
},
/**
* Show right arrow on this menu?
*/
showRightArrow: {
type: Boolean,
default: false
},
hasChildren: {
type: Boolean,
default: false
},
/**
* Should close menu when Click this menu item ?
*/
clickClose: {
type: Boolean,
default: true
},
/**
* When there are subitems in this item, is it allowed to trigger its own click event? Default is false
*/
clickableWhenHasChildren: {
type: Boolean,
default: false
},
rawMenuItem: {
type: Object as PropType<MenuItem>,
default: undefined
},
});
const emit = defineEmits([
'click',
'subMenuOpen',
'subMenuClose',
])
const {
clickHandler, clickClose, clickableWhenHasChildren, disabled, hidden,
label, icon, iconFontClass,
showRightArrow, shortcut,
hasChildren,
} = toRefs(props);
const showSubMenu = ref(false);
const keyBoardFocusMenu = ref(false);
const menuItemRef = ref<HTMLElement>();
const options = inject('globalOptions') as Ref<MenuOptions>;
const globalHasSlot = inject('globalHasSlot') as GlobalHasSlot;
const globalRenderSlot = inject('globalRenderSlot') as GlobalRenderSlot;
const globalCloseMenu = inject('globalCloseMenu') as (fromItem: MenuItem|undefined) => void;
const menuContext = inject('menuContext') as SubMenuParentContext;
//Instance Contet for keyboadr control
const menuItemInstance : MenuItemContext = {
getSubMenuInstance: () => undefined,
showSubMenu: () => {
if (showSubMenu.value) {
//Mark current item
menuContext.markActiveMenuItem(menuItemInstance, true);
return true;
} else if (hasChildren.value) {
onMouseEnter();
return true;
}
return false;
},
hideSubMenu: () => {
//closeSubMenu();
menuContext.closeOtherSubMenu();
},
isDisabledOrHidden: () => disabled.value || hidden.value,
getElement: () => menuItemRef.value,
focus: () => keyBoardFocusMenu.value = true,
blur: () => keyBoardFocusMenu.value = false,
click: onClick,
}
provide("menuItemInstance", menuItemInstance);
onMounted(() => {
if (menuContext.isMenuItemDataCollectedFlag()) {
//当前菜单条目是在整体加载完成后才显示的,此时菜单顺序已经无法知道,
//所以这里需要在父级元素中查找得出当前菜单的位置。
//
//The current menu item is displayed after the overall loading is completed.
//At this time, the menu order cannot be known, so here we need to
//find the position of the current menu in the parent element.
nextTick(() => {
let index = 0;
const parentEl = menuContext.getElement();
if (parentEl) {
let indexCounting = 0;
for (let i = 0; i < parentEl.children.length; i++) {
const el = parentEl.children[i];
if (el.getAttribute('data-type') === 'ContextMenuItem') {
if (el === menuItemRef.value) {
index = indexCounting;
break;
}
indexCounting++;
}
}
}
//Insert to pos
menuContext.addChildMenuItem(menuItemInstance, index);
});
} else
menuContext.addChildMenuItem(menuItemInstance);
});
onBeforeUnmount(() => {
menuContext.removeChildMenuItem(menuItemInstance);
});
//Click handler
function onClick(e: MouseEvent|KeyboardEvent) {
//Ignore clicking when disabled
if (disabled.value)
return;
//Ignore clicking when click on some special elements
if (e) {
const currentTarget = e.target as HTMLElement;
if (currentTarget.classList.contains('mx-context-no-clickable'))
return;
if (options.value.ignoreClickClassName && currentTarget.classList.contains(options.value.ignoreClickClassName))
return;
if (options.value.clickCloseClassName && currentTarget.classList.contains(options.value.clickCloseClassName)) {
e.stopPropagation();
globalCloseMenu(props.rawMenuItem);
return;
}
}
//Has submenu?
if (hasChildren.value) {
if (clickableWhenHasChildren.value) {
if (typeof clickHandler.value === 'function')
clickHandler.value(e);
emit('click', e);
}
else if (!showSubMenu.value)
onMouseEnter();
} else {
//Call hander from options
if (typeof clickHandler.value === 'function')
clickHandler.value(e);
emit('click', e);
if (clickClose.value) {
//emit close
globalCloseMenu(props.rawMenuItem);
}
}
}
//MouseEnter handler: show item submenu
function onMouseEnter(e?: MouseEvent) {
//Clear keyBoard focus style
keyBoardFocusMenu.value = false;
//等待一个延时,以防止用户过快移动鼠标导致菜单隐藏
//Wait for a delay to prevent the menu from being hidden due to the user moving the mouse too fast
if (!menuContext.checkCloseOtherSubMenuTimeOut())
menuContext.closeOtherSubMenu();
if (!disabled.value) {
//Mark current item
menuContext.markActiveMenuItem(menuItemInstance);
if (hasChildren.value) {
if (!e)
menuContext.markThisOpenedByKeyBoard();
//Open sub menu
menuContext.addOpenedSubMenu(closeSubMenu);
showSubMenu.value = true;
nextTick(() => emit('subMenuOpen', menuItemInstance));
}
}
}
function closeSubMenu()
{
keyBoardFocusMenu.value = false;
showSubMenu.value = false;
emit('subMenuClose', menuItemInstance);
}
//Data for custom render
function getItemDataForChildren() {
return {
disabled: disabled.value,
label: label.value,
icon: icon.value,
iconFontClass: iconFontClass.value,
showRightArrow: showRightArrow.value,
clickClose: clickClose.value,
clickableWhenHasChildren: clickableWhenHasChildren.value,
shortcut: shortcut.value,
theme: options.value.theme,
isOpen: showSubMenu,
hasChildren: hasChildren,
onClick,
onMouseEnter,
closeMenu: globalCloseMenu,
}
}
defineExpose(menuItemInstance);
</script>
<style>
</style>