-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathhelpers.js
More file actions
60 lines (52 loc) · 1.89 KB
/
helpers.js
File metadata and controls
60 lines (52 loc) · 1.89 KB
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
import { fire, oneEvent } from '@vaadin/testing-helpers';
import { isTouch } from '@vaadin/component-base/src/browser-utils.js';
export function activateItem(target, event = isTouch ? 'click' : 'mouseover') {
const { right, bottom } = target.getBoundingClientRect();
fire(target, event, { x: right, y: bottom });
}
export async function openMenu(target, event = isTouch ? 'click' : 'mouseover') {
// Try to find a submenu first
let menu;
const overlayContent = target.closest('[slot="overlay"]');
if (overlayContent) {
menu = overlayContent.parentElement.querySelector('[slot="submenu"]');
}
// If no submenu is found, use the closest context menu
if (!menu) {
menu = target.closest('vaadin-context-menu');
}
// Disable logic that delays opening submenu
menu.__openListenerActive = true;
// Open the submenu
const wasOpened = menu._overlayElement.opened;
activateItem(target, event);
// Wait for the submenu to open, unless it was already opened for a different item
if (!wasOpened) {
await oneEvent(menu._overlayElement, 'vaadin-overlay-open');
}
}
export function getMenuItems(menu) {
return [...menu.querySelectorAll(':scope > [slot="overlay"] [role="menu"] > *')];
}
export function getSubMenu(menu) {
return menu.querySelector(':scope > vaadin-context-menu[slot="submenu"]');
}
export function pointerMove(x, y) {
document.dispatchEvent(
new PointerEvent('pointermove', {
clientX: x,
clientY: y,
bubbles: true,
pointerType: 'mouse',
}),
);
}
export async function openSubMenus(menu) {
await oneEvent(menu._overlayElement, 'vaadin-overlay-open');
const itemElement = menu.querySelector(':scope > [slot="overlay"] [aria-haspopup="true"]');
if (itemElement) {
itemElement.dispatchEvent(new CustomEvent('mouseover', { bubbles: true, composed: true }));
const subMenu = getSubMenu(menu);
await openSubMenus(subMenu);
}
}