-
Notifications
You must be signed in to change notification settings - Fork 92
Expand file tree
/
Copy pathvaadin-context-menu-mixin.js
More file actions
658 lines (566 loc) · 19.6 KB
/
vaadin-context-menu-mixin.js
File metadata and controls
658 lines (566 loc) · 19.6 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
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
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
/**
* @license
* Copyright (c) 2016 - 2026 Vaadin Ltd.
* This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
*/
import { isElementFocusable, isKeyboardActive } from '@vaadin/a11y-base/src/focus-utils.js';
import { isAndroid, isIOS } from '@vaadin/component-base/src/browser-utils.js';
import { addListener, deepTargetFind, gestures, removeListener } from '@vaadin/component-base/src/gestures.js';
import { MediaQueryController } from '@vaadin/component-base/src/media-query-controller.js';
import { ContextMenuTooltipController } from './vaadin-context-menu-tooltip-controller.js';
import { ItemsMixin } from './vaadin-contextmenu-items-mixin.js';
/**
* @polymerMixin
* @mixes ItemsMixin
*/
export const ContextMenuMixin = (superClass) =>
class ContextMenuMixinClass extends ItemsMixin(superClass) {
static get properties() {
return {
/**
* CSS selector that can be used to target any child element
* of the context menu to listen for `openOn` events.
*/
selector: {
type: String,
},
/**
* True if the overlay is currently displayed.
*/
opened: {
type: Boolean,
reflectToAttribute: true,
observer: '_openedChanged',
value: false,
notify: true,
readOnly: true,
},
/**
* Event name to listen for opening the context menu.
* @attr {string} open-on
*/
openOn: {
type: String,
value: 'vaadin-contextmenu',
sync: true,
},
/**
* The target element that's listened to for context menu opening events.
* By default the vaadin-context-menu listens to the target's `vaadin-contextmenu`
* events.
* @type {!HTMLElement}
* @default self
*/
listenOn: {
type: Object,
sync: true,
value() {
return this;
},
},
/**
* Event name to listen for closing the context menu.
* @attr {string} close-on
*/
closeOn: {
type: String,
value: 'click',
observer: '_closeOnChanged',
sync: true,
},
/**
* Custom function for rendering the content of the menu overlay.
* Receives three arguments:
*
* - `root` The root container DOM element. Append your content to it.
* - `contextMenu` The reference to the `<vaadin-context-menu>` element.
* - `context` The object with the menu context, contains:
* - `context.target` the target of the menu opening event,
* - `context.detail` the menu opening event detail.
* @type {ContextMenuRenderer | undefined}
*/
renderer: {
type: Function,
sync: true,
},
/**
* When true, the menu overlay is modeless.
* @protected
*/
_modeless: {
type: Boolean,
sync: true,
},
/** @private */
_context: {
type: Object,
sync: true,
},
/** @private */
_phone: {
type: Boolean,
},
_fullscreen: {
type: Boolean,
},
_fullscreenMediaQuery: {
type: String,
value: '(max-width: 450px), (max-height: 450px)',
},
};
}
static get observers() {
return [
'_targetOrOpenOnChanged(listenOn, openOn)',
'_rendererChanged(renderer, items)',
'_fullscreenChanged(_fullscreen)',
];
}
constructor() {
super();
this._boundOpen = this.open.bind(this);
this._boundClose = this.close.bind(this);
this._boundPreventDefault = this._preventDefault.bind(this);
this._boundOnGlobalContextMenu = this._onGlobalContextMenu.bind(this);
}
/** @protected */
connectedCallback() {
super.connectedCallback();
this.__boundOnScroll = this.__onScroll.bind(this);
window.addEventListener('scroll', this.__boundOnScroll, true);
// Restore opened state if overlay was opened when disconnecting
if (this.__restoreOpened) {
this._setOpened(true);
}
}
/** @protected */
disconnectedCallback() {
super.disconnectedCallback();
window.removeEventListener('scroll', this.__boundOnScroll, true);
// Close overlay and memorize opened state
this.__restoreOpened = this.opened;
this.close();
}
/** @protected */
firstUpdated() {
super.firstUpdated();
this._overlayElement = this.$.overlay;
this.addController(
new MediaQueryController(this._fullscreenMediaQuery, (matches) => {
this._fullscreen = matches;
}),
);
// Sub-menus inherit the tooltip controller from their parent menu
// (assigned before `firstUpdated` runs) to reuse the same slotted
// `<vaadin-tooltip>` across nesting levels. Only create a new one
// when none was inherited, i.e. on the outer host.
if (!this._tooltipController) {
this._tooltipController = new ContextMenuTooltipController(this);
this.addController(this._tooltipController);
}
}
/**
* Runs before overlay is fully rendered
* @private
*/
_onOverlayOpened(event) {
// Ignore events from submenus
if (event.target !== this._overlayElement) {
return;
}
const opened = event.detail.value;
this._setOpened(opened);
if (opened) {
this.__alignOverlayPosition();
}
}
/**
* Runs after overlay is fully rendered
* @private
*/
_onVaadinOverlayOpen(event) {
// Ignore events from submenus
if (event.target !== this._overlayElement) {
return;
}
this.__alignOverlayPosition();
this._overlayElement.style.visibility = '';
this.__forwardFocus();
}
/**
* Runs after overlay's closing animation is finished
* @private
*/
_onVaadinOverlayClosed() {
this.dispatchEvent(new CustomEvent('closed'));
}
/** @private */
_targetOrOpenOnChanged(listenOn, openOn) {
if (this._oldListenOn && this._oldOpenOn) {
this._unlisten(this._oldListenOn, this._oldOpenOn, this._boundOpen);
this._oldListenOn.style.webkitTouchCallout = '';
this._oldListenOn.style.webkitUserSelect = '';
this._oldListenOn.style.userSelect = '';
this._oldListenOn = null;
this._oldOpenOn = null;
}
if (listenOn && openOn) {
this._listen(listenOn, openOn, this._boundOpen);
this._oldListenOn = listenOn;
this._oldOpenOn = openOn;
}
}
/** @private */
_fullscreenChanged(fullScreen) {
this._phone = fullScreen;
}
/** @private */
__setListenOnUserSelect(opened) {
const value = opened ? 'none' : '';
// Note: these styles don't seem to work in Firefox on iOS.
this.listenOn.style.webkitTouchCallout = value;
this.listenOn.style.webkitUserSelect = value; // Chrome, Safari, Firefox
this.listenOn.style.userSelect = value;
// Note: because user-selection is disabled on the overlay
// before opening the menu the text could be already selected
// so we need to clear that selection
if (opened) {
document.getSelection().removeAllRanges();
}
}
/** @private */
_closeOnChanged(closeOn, oldCloseOn) {
// Outside click event from overlay
const evtOverlay = 'vaadin-overlay-outside-click';
const overlay = this._overlayElement;
if (oldCloseOn) {
this._unlisten(overlay, oldCloseOn, this._boundClose);
}
if (closeOn) {
this._listen(overlay, closeOn, this._boundClose);
overlay.removeEventListener(evtOverlay, this._boundPreventDefault);
} else {
overlay.addEventListener(evtOverlay, this._boundPreventDefault);
}
}
/** @private */
_preventDefault(e) {
e.preventDefault();
}
/** @private */
_openedChanged(opened, oldOpened) {
if (opened) {
document.documentElement.addEventListener('contextmenu', this._boundOnGlobalContextMenu, true);
} else if (oldOpened) {
document.documentElement.removeEventListener('contextmenu', this._boundOnGlobalContextMenu, true);
}
this.__setListenOnUserSelect(opened);
}
/**
* Requests an update for the content of the menu overlay.
* While performing the update, it invokes the renderer passed in the `renderer` property.
*
* It is not guaranteed that the update happens immediately (synchronously) after it is requested.
*/
requestContentUpdate() {
if (!this._overlayElement) {
return;
}
// Store state to be restored after re-rendering
this.__preserveMenuState();
// Run overlay renderer to re-create DOM elements
this._overlayElement.requestContentUpdate();
// Restore focused item, update sub-menu if needed
this.__restoreMenuState();
}
/** @private */
_rendererChanged(renderer, items) {
if (items) {
if (renderer) {
throw new Error('The items API cannot be used together with a renderer');
}
if (this.closeOn === 'click') {
this.closeOn = '';
}
}
}
/**
* Closes the overlay.
*/
close() {
this._setOpened(false);
}
/** @private */
_contextTarget(e) {
if (this.selector) {
const targets = this.listenOn.querySelectorAll(this.selector);
return Array.prototype.filter.call(targets, (el) => {
return e.composedPath().indexOf(el) > -1;
})[0];
} else if (this.listenOn && this.listenOn !== this && this.position) {
// If listenOn has been set on a different element than the context menu root, then use listenOn as the target.
return this.listenOn;
}
return e.target;
}
/**
* Opens the overlay.
* @param {!Event | undefined} e used as the context for the menu. Overlay coordinates are taken from this event.
*/
open(e) {
// Ignore events from the overlay
if (this._overlayElement && e.composedPath().includes(this._overlayElement)) {
return;
}
if (e && !this.opened) {
this._context = {
detail: e.detail,
target: this._contextTarget(e),
};
if (this._context.target) {
e.preventDefault();
e.stopPropagation();
// Used in alignment which is delayed until overlay is rendered
this.__x = this._getEventCoordinate(e, 'x');
this.__pageXOffset = window.pageXOffset;
this.__y = this._getEventCoordinate(e, 'y');
this.__pageYOffset = window.pageYOffset;
// Hide overlay until it is fully rendered and positioned
this._overlayElement.style.visibility = 'hidden';
this._setOpened(true);
}
}
}
/** @private */
__preserveMenuState() {
const listBox = this.__getListBox();
if (listBox) {
this.__focusedIndex = listBox.items.indexOf(listBox.focused);
if (this._subMenu && this._subMenu.opened) {
this.__subMenuIndex = listBox.items.indexOf(this._subMenu.listenOn);
}
}
}
/** @private */
__restoreMenuState() {
const focusedIndex = this.__focusedIndex;
const subMenuIndex = this.__subMenuIndex;
const selectedIndex = this.__selectedIndex;
const listBox = this.__getListBox();
if (listBox) {
// Initialize menu items synchronously
listBox._observer.flush();
if (subMenuIndex > -1) {
const itemToOpen = listBox.items[subMenuIndex];
if (itemToOpen) {
if (Array.isArray(itemToOpen._item.children) && itemToOpen._item.children.length) {
// Keep nested sub-menu opened and update it
this.__updateSubMenuForItem(this._subMenu, itemToOpen);
this._subMenu.requestContentUpdate();
} else {
// Item no longer has children, close sub-menu
this._subMenu.close();
this.__focusItem(itemToOpen);
}
} else {
// Item no longer exists, focus the list-box
listBox.focus();
}
}
// Focus previously selected item, in case it was clicked with
// `keepOpen` option, or the previously focused item, if any
this.__focusItem(selectedIndex > -1 ? listBox.children[selectedIndex] : listBox.items[focusedIndex]);
}
this.__focusedIndex = undefined;
this.__subMenuIndex = undefined;
this.__selectedIndex = undefined;
}
/** @private */
__focusItem(item) {
if (item) {
item.focus({ focusVisible: isKeyboardActive() });
}
}
/** @private */
__onScroll() {
if (!this.opened || this.position) {
return;
}
const yDiff = window.pageYOffset - this.__pageYOffset;
const xDiff = window.pageXOffset - this.__pageXOffset;
this.__adjustPosition('left', -xDiff);
this.__adjustPosition('right', xDiff);
this.__adjustPosition('top', -yDiff);
this.__adjustPosition('bottom', yDiff);
this.__pageYOffset += yDiff;
this.__pageXOffset += xDiff;
}
/** @private */
__adjustPosition(coord, diff) {
const overlay = this._overlayElement;
const style = overlay.style;
style[coord] = `${(parseInt(style[coord]) || 0) + diff}px`;
}
/** @private */
__alignOverlayPosition() {
const overlay = this._overlayElement;
if (overlay.positionTarget) {
// The overlay is positioned relative to another node, for example, a
// menu item in a nested submenu structure where this overlay lists
// the items for another submenu.
// It means that the overlay positioning is controlled by
// vaadin-overlay-position-mixin so no manual alignment is needed.
return;
}
const style = overlay.style;
// Reset all properties before measuring
['top', 'right', 'bottom', 'left'].forEach((prop) => style.removeProperty(prop));
['right-aligned', 'end-aligned', 'bottom-aligned'].forEach((attr) => overlay.removeAttribute(attr));
// Maximum x and y values are imposed by content size and overlay limits.
const { xMax, xMin, yMax } = overlay.getBoundaries();
// Reuse saved x and y event values, in order to this method be used async
// in the `vaadin-overlay-change` which guarantees that overlay is ready.
// The valus represent an anchor position on the page where the contextmenu
// event took place.
const x = this.__x;
const y = this.__y;
// Select one overlay corner and move to the event x/y position.
// Then set styling attrs for flex-aligning the content appropriately.
const wdthVport = document.documentElement.clientWidth;
const hghtVport = document.documentElement.clientHeight;
if (!this.__isRTL) {
if (x < wdthVport / 2 || x < xMax) {
// Menu is displayed in the right side of the anchor
style.left = `${x}px`;
} else {
// Menu is displayed in the left side of the anchor
style.right = `${Math.max(0, wdthVport - x)}px`;
this._setEndAligned(overlay);
}
} else if (x > wdthVport / 2 || x > xMin) {
// Menu is displayed in the right side of the anchor
style.right = `${Math.max(0, wdthVport - x)}px`;
} else {
// Menu is displayed in the left side of the anchor
style.left = `${x}px`;
this._setEndAligned(overlay);
}
if (y < hghtVport / 2 || y < yMax) {
style.top = `${y}px`;
} else {
style.bottom = `${Math.max(0, hghtVport - y)}px`;
overlay.setAttribute('bottom-aligned', '');
}
}
/** @private */
_setEndAligned(element) {
element.setAttribute('end-aligned', '');
if (!this.__isRTL) {
element.setAttribute('right-aligned', '');
}
}
/** @private */
_getEventCoordinate(event, coord) {
if (event.detail instanceof Object) {
if (event.detail[coord]) {
// Polymer gesture events, get coordinate from detail
return event.detail[coord];
} else if (event.detail.sourceEvent) {
// Unwrap detailed event
return this._getEventCoordinate(event.detail.sourceEvent, coord);
}
} else {
const prop = `client${coord.toUpperCase()}`;
const position = event.changedTouches ? event.changedTouches[0][prop] : event[prop];
if (position === 0) {
// Native keyboard event
const rect = event.target.getBoundingClientRect();
return coord === 'x' ? rect.left : rect.top + rect.height;
}
// Native mouse or touch event
return position;
}
}
/** @private */
_listen(node, evType, handler) {
if (gestures[evType]) {
addListener(node, evType, handler);
} else {
node.addEventListener(evType, handler);
}
}
/** @private */
_unlisten(node, evType, handler) {
if (gestures[evType]) {
removeListener(node, evType, handler);
} else {
node.removeEventListener(evType, handler);
}
}
/** @private */
__createMouseEvent(name, clientX, clientY) {
return new MouseEvent(name, {
bubbles: true,
composed: true,
cancelable: true,
clientX,
clientY,
});
}
/** @private */
__focusClosestFocusable(target) {
let currentElement = target;
while (currentElement) {
if (currentElement instanceof HTMLElement && isElementFocusable(currentElement)) {
currentElement.focus();
return;
}
currentElement = currentElement.parentNode || currentElement.host;
}
}
/**
* Executes a synthetic contextmenu event on the target under the coordinates.
* @private
*/
__contextMenuAt(x, y) {
// Get the deepest element under the coordinates
const target = deepTargetFind(x, y);
if (target) {
// Need to run asynchronously to avoid timing issues with the Lit-based context menu
queueMicrotask(() => {
// Dispatch mousedown and mouseup to the target (grid cell focus depends on it)
target.dispatchEvent(this.__createMouseEvent('mousedown', x, y));
target.dispatchEvent(this.__createMouseEvent('mouseup', x, y));
// Manually try to focus the closest focusable of the target
this.__focusClosestFocusable(target);
// Dispatch a contextmenu event to the target
target.dispatchEvent(this.__createMouseEvent('contextmenu', x, y));
});
}
}
/** @private */
_onGlobalContextMenu(e) {
if (!e.shiftKey) {
const isTouchDevice = isAndroid || isIOS;
if (!isTouchDevice) {
e.stopPropagation();
// Prevent having the previously focused node auto-focus after closing the overlay
this._overlayElement.__focusRestorationController.focusNode = null;
// Dispatch another contextmenu at the same coordinates after the overlay is closed
this._overlayElement.addEventListener(
'vaadin-overlay-closed',
(closeEvent) => {
if (closeEvent.target === this._overlayElement) {
this.__contextMenuAt(e.clientX, e.clientY);
}
},
{
once: true,
},
);
}
e.preventDefault();
this.close();
}
}
};