@vaadin/context-menu 22.0.0-alpha7

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,733 @@
1
+ /**
2
+ * @license
3
+ * Copyright (c) 2021 Vaadin Ltd.
4
+ * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
5
+ */
6
+ import { PolymerElement, html } from '@polymer/polymer/polymer-element.js';
7
+ import { gestures, addListener, removeListener } from '@polymer/polymer/lib/utils/gestures.js';
8
+ import { GestureEventListeners } from '@polymer/polymer/lib/mixins/gesture-event-listeners.js';
9
+ import { ElementMixin } from '@vaadin/component-base/src/element-mixin.js';
10
+ import { processTemplates } from '@vaadin/component-base/src/templates.js';
11
+ import { ThemePropertyMixin } from '@vaadin/vaadin-themable-mixin/vaadin-theme-property-mixin.js';
12
+ import { ItemsMixin } from './vaadin-contextmenu-items-mixin.js';
13
+ import './vaadin-contextmenu-event.js';
14
+ import './vaadin-device-detector.js';
15
+ import './vaadin-context-menu-overlay.js';
16
+
17
+ /**
18
+ * `<vaadin-context-menu>` is a Web Component for creating context menus.
19
+ *
20
+ * ### Items
21
+ *
22
+ * Items is a higher level convenience API for defining a (hierarchical) menu structure for the component.
23
+ * If a menu item has a non-empty `children` set, a sub-menu with the child items is opened
24
+ * next to the parent menu on mouseover, tap or a right arrow keypress.
25
+ *
26
+ * When an item is selected, `<vaadin-context-menu>` dispatches an "item-selected" event
27
+ * with the selected item as `event.detail.value` property.
28
+ *
29
+ * ```javascript
30
+ * contextMenu.items = [
31
+ * {text: 'Menu Item 1', theme: 'primary', children:
32
+ * [
33
+ * {text: 'Menu Item 1-1', checked: true},
34
+ * {text: 'Menu Item 1-2'}
35
+ * ]
36
+ * },
37
+ * {component: 'hr'},
38
+ * {text: 'Menu Item 2', children:
39
+ * [
40
+ * {text: 'Menu Item 2-1'},
41
+ * {text: 'Menu Item 2-2', disabled: true}
42
+ * ]
43
+ * },
44
+ * {text: 'Menu Item 3', disabled: true}
45
+ * ];
46
+ *
47
+ * contextMenu.addEventListener('item-selected', e => {
48
+ * const item = e.detail.value;
49
+ * console.log(`${item.text} selected`);
50
+ * });
51
+ * ```
52
+ *
53
+ * **NOTE:** when the `items` array is defined, the renderer cannot be used.
54
+ *
55
+ * ### Rendering
56
+ *
57
+ * The content of the menu can be populated by using the renderer callback function.
58
+ *
59
+ * The renderer function provides `root`, `contextMenu`, `model` arguments when applicable.
60
+ * Generate DOM content by using `model` object properties if needed, append it to the `root`
61
+ * element and control the state of the host element by accessing `contextMenu`. Before generating
62
+ * new content, the renderer function should check if there is already content in `root` for reusing it.
63
+ *
64
+ * ```html
65
+ * <vaadin-context-menu id="contextMenu">
66
+ * <p>This paragraph has a context menu.</p>
67
+ * </vaadin-context-menu>
68
+ * ```
69
+ * ```js
70
+ * const contextMenu = document.querySelector('#contextMenu');
71
+ * contextMenu.renderer = (root, contextMenu, context) => {
72
+ * let listBox = root.firstElementChild;
73
+ * if (!listBox) {
74
+ * listBox = document.createElement('vaadin-list-box');
75
+ * root.appendChild(listBox);
76
+ * }
77
+ *
78
+ * let item = listBox.querySelector('vaadin-item');
79
+ * if (!item) {
80
+ * item = document.createElement('vaadin-item');
81
+ * listBox.appendChild(item);
82
+ * }
83
+ * item.textContent = 'Content of the selector: ' + context.target.textContent;
84
+ * };
85
+ * ```
86
+ *
87
+ * You can access the menu context inside the renderer using
88
+ * `context.target` and `context.detail`.
89
+ *
90
+ * Renderer is called on the opening of the context-menu and each time the related context is updated.
91
+ * DOM generated during the renderer call can be reused
92
+ * in the next renderer call and will be provided with the `root` argument.
93
+ * On first call it will be empty.
94
+ *
95
+ * ### “vaadin-contextmenu” Gesture Event
96
+ *
97
+ * `vaadin-contextmenu` is a gesture event (a custom event),
98
+ * which is dispatched after either `contextmenu` or long touch events.
99
+ * This enables support for both mouse and touch environments in a uniform way.
100
+ *
101
+ * `<vaadin-context-menu>` opens the menu overlay on the `vaadin-contextmenu`
102
+ * event by default.
103
+ *
104
+ * ### Menu Listener
105
+ *
106
+ * By default, the `<vaadin-context-menu>` element listens for the menu opening
107
+ * event on itself. In case if you do not want to wrap the target, you can listen for
108
+ * events on an element outside the `<vaadin-context-menu>` by setting the
109
+ * `listenOn` property:
110
+ *
111
+ * ```html
112
+ * <vaadin-context-menu id="contextMenu"></vaadin-context-menu>
113
+ *
114
+ * <div id="menuListener">The element that listens for the contextmenu event.</div>
115
+ * ```
116
+ * ```javascript
117
+ * const contextMenu = document.querySelector('#contextMenu');
118
+ * contextMenu.listenOn = document.querySelector('#menuListener');
119
+ * ```
120
+ *
121
+ * ### Filtering Menu Targets
122
+ *
123
+ * By default, the listener element and all its descendants open the context
124
+ * menu. You can filter the menu targets to a smaller set of elements inside
125
+ * the listener element by setting the `selector` property.
126
+ *
127
+ * In the following example, only the elements matching `.has-menu` will open the context menu:
128
+ *
129
+ * ```html
130
+ * <vaadin-context-menu selector=".has-menu">
131
+ * <p class="has-menu">This paragraph opens the context menu</p>
132
+ * <p>This paragraph does not open the context menu</p>
133
+ * </vaadin-context-menu>
134
+ * ```
135
+ *
136
+ * ### Menu Context
137
+ *
138
+ * The following properties are available in the `context` argument:
139
+ *
140
+ * - `target` is the menu opening event target, which is the element that
141
+ * the user has called the context menu for
142
+ * - `detail` is the menu opening event detail
143
+ *
144
+ * In the following example, the menu item text is composed with the contents
145
+ * of the element that opened the menu:
146
+ *
147
+ * ```html
148
+ * <vaadin-context-menu selector="li" id="contextMenu">
149
+ * <ul>
150
+ * <li>Foo</li>
151
+ * <li>Bar</li>
152
+ * <li>Baz</li>
153
+ * </ul>
154
+ * </vaadin-context-menu>
155
+ * ```
156
+ * ```js
157
+ * const contextMenu = document.querySelector('#contextMenu');
158
+ * contextMenu.renderer = (root, contextMenu, context) => {
159
+ * let listBox = root.firstElementChild;
160
+ * if (!listBox) {
161
+ * listBox = document.createElement('vaadin-list-box');
162
+ * root.appendChild(listBox);
163
+ * }
164
+ *
165
+ * let item = listBox.querySelector('vaadin-item');
166
+ * if (!item) {
167
+ * item = document.createElement('vaadin-item');
168
+ * listBox.appendChild(item);
169
+ * }
170
+ * item.textContent = 'The menu target: ' + context.target.textContent;
171
+ * };
172
+ * ```
173
+ *
174
+ * ### Styling
175
+ *
176
+ * `<vaadin-context-menu>` uses `<vaadin-context-menu-overlay>` internal
177
+ * themable component as the actual visible context menu overlay.
178
+ *
179
+ * See [`<vaadin-overlay>`](#/elements/vaadin-overlay)
180
+ * documentation for `<vaadin-context-menu-overlay>` stylable parts.
181
+ *
182
+ * See [Styling Components](https://vaadin.com/docs/latest/ds/customization/styling-components) documentation.
183
+ *
184
+ * ### Internal components
185
+ *
186
+ * When using `items` API, in addition `<vaadin-context-menu-overlay>`, the following
187
+ * internal components are themable:
188
+ *
189
+ * - `<vaadin-context-menu-item>` - has the same API as [`<vaadin-item>`](#/elements/vaadin-item).
190
+ * - `<vaadin-context-menu-list-box>` - has the same API as [`<vaadin-list-box>`](#/elements/vaadin-list-box).
191
+ *
192
+ * Note: the `theme` attribute value set on `<vaadin-context-menu>` is
193
+ * propagated to the internal components listed above.
194
+ *
195
+ * @fires {CustomEvent} opened-changed - Fired when the `opened` property changes.
196
+ * @fires {CustomEvent} item-selected - Fired when an item is selected when the context menu is populated using the `items` API.
197
+ *
198
+ * @extends HTMLElement
199
+ * @mixes ElementMixin
200
+ * @mixes ThemePropertyMixin
201
+ * @mixes ItemsMixin
202
+ * @mixes GestureEventListeners
203
+ */
204
+ class ContextMenu extends ElementMixin(ThemePropertyMixin(ItemsMixin(GestureEventListeners(PolymerElement)))) {
205
+ static get template() {
206
+ return html`
207
+ <style>
208
+ :host {
209
+ display: block;
210
+ }
211
+
212
+ :host([hidden]) {
213
+ display: none !important;
214
+ }
215
+ </style>
216
+
217
+ <slot id="slot"></slot>
218
+
219
+ <vaadin-device-detector phone="{{_phone}}" touch="{{_touch}}"></vaadin-device-detector>
220
+
221
+ <vaadin-context-menu-overlay
222
+ id="overlay"
223
+ on-opened-changed="_onOverlayOpened"
224
+ on-vaadin-overlay-open="_onVaadinOverlayOpen"
225
+ with-backdrop="[[_phone]]"
226
+ phone$="[[_phone]]"
227
+ model="[[_context]]"
228
+ theme$="[[theme]]"
229
+ >
230
+ </vaadin-context-menu-overlay>
231
+ `;
232
+ }
233
+
234
+ static get is() {
235
+ return 'vaadin-context-menu';
236
+ }
237
+
238
+ static get properties() {
239
+ return {
240
+ /**
241
+ * CSS selector that can be used to target any child element
242
+ * of the context menu to listen for `openOn` events.
243
+ */
244
+ selector: {
245
+ type: String
246
+ },
247
+
248
+ /**
249
+ * True if the overlay is currently displayed.
250
+ * @type {boolean}
251
+ */
252
+ opened: {
253
+ type: Boolean,
254
+ value: false,
255
+ notify: true,
256
+ readOnly: true
257
+ },
258
+
259
+ /**
260
+ * Event name to listen for opening the context menu.
261
+ * @attr {string} open-on
262
+ * @type {string}
263
+ */
264
+ openOn: {
265
+ type: String,
266
+ value: 'vaadin-contextmenu'
267
+ },
268
+
269
+ /**
270
+ * The target element that's listened to for context menu opening events.
271
+ * By default the vaadin-context-menu listens to the target's `vaadin-contextmenu`
272
+ * events.
273
+ * @type {!HTMLElement}
274
+ * @default self
275
+ */
276
+ listenOn: {
277
+ type: Object,
278
+ value: function () {
279
+ return this;
280
+ }
281
+ },
282
+
283
+ /**
284
+ * Event name to listen for closing the context menu.
285
+ * @attr {string} close-on
286
+ * @type {string}
287
+ */
288
+ closeOn: {
289
+ type: String,
290
+ value: 'click',
291
+ observer: '_closeOnChanged'
292
+ },
293
+
294
+ /**
295
+ * Custom function for rendering the content of the menu overlay.
296
+ * Receives three arguments:
297
+ *
298
+ * - `root` The root container DOM element. Append your content to it.
299
+ * - `contextMenu` The reference to the `<vaadin-context-menu>` element.
300
+ * - `context` The object with the menu context, contains:
301
+ * - `context.target` the target of the menu opening event,
302
+ * - `context.detail` the menu opening event detail.
303
+ * @type {ContextMenuRenderer | undefined}
304
+ */
305
+ renderer: {
306
+ type: Function
307
+ },
308
+
309
+ /** @private */
310
+ _context: Object,
311
+
312
+ /** @private */
313
+ _boundClose: Object,
314
+
315
+ /** @private */
316
+ _boundOpen: Object,
317
+
318
+ /** @private */
319
+ _touch: Boolean
320
+ };
321
+ }
322
+
323
+ static get observers() {
324
+ return ['_openedChanged(opened)', '_targetOrOpenOnChanged(listenOn, openOn)', '_rendererChanged(renderer, items)'];
325
+ }
326
+
327
+ constructor() {
328
+ super();
329
+ this._boundOpen = this.open.bind(this);
330
+ this._boundClose = this.close.bind(this);
331
+ this._boundOnGlobalContextMenu = this._onGlobalContextMenu.bind(this);
332
+ }
333
+
334
+ /** @protected */
335
+ connectedCallback() {
336
+ super.connectedCallback();
337
+
338
+ this.__boundOnScroll = this.__onScroll.bind(this);
339
+ window.addEventListener('scroll', this.__boundOnScroll, true);
340
+ }
341
+
342
+ /** @protected */
343
+ disconnectedCallback() {
344
+ super.disconnectedCallback();
345
+
346
+ window.removeEventListener('scroll', this.__boundOnScroll, true);
347
+ this.close();
348
+ }
349
+
350
+ /** @protected */
351
+ ready() {
352
+ super.ready();
353
+
354
+ processTemplates(this);
355
+ }
356
+
357
+ /**
358
+ * Runs before overlay is fully rendered
359
+ * @private
360
+ */
361
+ _onOverlayOpened(e) {
362
+ this._setOpened(e.detail.value);
363
+ this.__alignOverlayPosition();
364
+ }
365
+
366
+ /**
367
+ * Runs after overlay is fully rendered
368
+ * @private
369
+ */
370
+ _onVaadinOverlayOpen() {
371
+ this.__alignOverlayPosition();
372
+ this.$.overlay.style.opacity = '';
373
+ this.__forwardFocus();
374
+ }
375
+
376
+ /** @private */
377
+ _targetOrOpenOnChanged(listenOn, openOn) {
378
+ if (this._oldListenOn && this._oldOpenOn) {
379
+ this._unlisten(this._oldListenOn, this._oldOpenOn, this._boundOpen);
380
+
381
+ this._oldListenOn.style.webkitTouchCallout = '';
382
+ this._oldListenOn.style.webkitUserSelect = '';
383
+ this._oldListenOn.style.userSelect = '';
384
+
385
+ this._oldListenOn = null;
386
+ this._oldOpenOn = null;
387
+ }
388
+
389
+ if (listenOn && openOn) {
390
+ this._listen(listenOn, openOn, this._boundOpen);
391
+
392
+ this._oldListenOn = listenOn;
393
+ this._oldOpenOn = openOn;
394
+ }
395
+ }
396
+
397
+ /** @private */
398
+ _setListenOnUserSelect(value) {
399
+ // note: these styles don't seem to work in Firefox on iOS.
400
+ this.listenOn.style.webkitTouchCallout = value;
401
+ this.listenOn.style.webkitUserSelect = value; // Chrome, Safari, Firefox
402
+ this.listenOn.style.userSelect = value;
403
+
404
+ // note: because user-selection is disabled on the overlay
405
+ // before opening the menu the text could be already selected
406
+ // so we need to clear that selection
407
+ document.getSelection().removeAllRanges();
408
+ }
409
+
410
+ /** @private */
411
+ _closeOnChanged(closeOn, oldCloseOn) {
412
+ // Listen on this.$.overlay.root to workaround issue on
413
+ // ShadyDOM polyfill: https://github.com/webcomponents/shadydom/issues/159
414
+
415
+ // Outside click event from overlay
416
+ const evtOverlay = 'vaadin-overlay-outside-click';
417
+
418
+ if (oldCloseOn) {
419
+ this._unlisten(this.$.overlay, oldCloseOn, this._boundClose);
420
+ this._unlisten(this.$.overlay.root, oldCloseOn, this._boundClose);
421
+ }
422
+ if (closeOn) {
423
+ this._listen(this.$.overlay, closeOn, this._boundClose);
424
+ this._listen(this.$.overlay.root, closeOn, this._boundClose);
425
+ this._unlisten(this.$.overlay, evtOverlay, this._preventDefault);
426
+ } else {
427
+ this._listen(this.$.overlay, evtOverlay, this._preventDefault);
428
+ }
429
+ }
430
+
431
+ /** @private */
432
+ _preventDefault(e) {
433
+ e.preventDefault();
434
+ }
435
+
436
+ /** @private */
437
+ _openedChanged(opened) {
438
+ if (opened) {
439
+ document.documentElement.addEventListener('contextmenu', this._boundOnGlobalContextMenu, true);
440
+ this._setListenOnUserSelect('none');
441
+ } else {
442
+ document.documentElement.removeEventListener('contextmenu', this._boundOnGlobalContextMenu, true);
443
+ this._setListenOnUserSelect('');
444
+ }
445
+
446
+ // Has to be set after instance has been created
447
+ this.$.overlay.opened = opened;
448
+ }
449
+
450
+ /**
451
+ * Requests an update for the content of the menu overlay.
452
+ * While performing the update, it invokes the renderer passed in the `renderer` property.
453
+ *
454
+ * It is not guaranteed that the update happens immediately (synchronously) after it is requested.
455
+ */
456
+ requestContentUpdate() {
457
+ this.$.overlay.requestContentUpdate();
458
+ }
459
+
460
+ /**
461
+ * Manually invoke existing renderer.
462
+ *
463
+ * @deprecated Since Vaadin 21, `render()` is deprecated. Please use `requestContentUpdate()` instead.
464
+ */
465
+ render() {
466
+ console.warn('WARNING: Since Vaadin 21, render() is deprecated. Please use requestContentUpdate() instead.');
467
+
468
+ this.requestContentUpdate();
469
+ }
470
+
471
+ /** @private */
472
+ _rendererChanged(renderer, items) {
473
+ if (items) {
474
+ if (renderer) {
475
+ throw new Error('The items API cannot be used together with a renderer');
476
+ }
477
+
478
+ if (this.closeOn === 'click') {
479
+ this.closeOn = '';
480
+ }
481
+
482
+ renderer = this.__itemsRenderer;
483
+ }
484
+
485
+ this.$.overlay.setProperties({ owner: this, renderer });
486
+ }
487
+
488
+ /**
489
+ * Closes the overlay.
490
+ */
491
+ close() {
492
+ this._setOpened(false);
493
+ }
494
+
495
+ /** @private */
496
+ _contextTarget(e) {
497
+ if (this.selector) {
498
+ const targets = this.listenOn.querySelectorAll(this.selector);
499
+
500
+ return Array.prototype.filter.call(targets, (el) => {
501
+ return e.composedPath().indexOf(el) > -1;
502
+ })[0];
503
+ } else {
504
+ return e.target;
505
+ }
506
+ }
507
+
508
+ /**
509
+ * Opens the overlay.
510
+ * @param {!Event | undefined} e used as the context for the menu. Overlay coordinates are taken from this event.
511
+ */
512
+ open(e) {
513
+ if (e && !this.opened) {
514
+ this._context = {
515
+ detail: e.detail,
516
+ target: this._contextTarget(e)
517
+ };
518
+
519
+ if (this._context.target) {
520
+ this._preventDefault(e);
521
+ e.stopPropagation();
522
+
523
+ // Used in alignment which is delayed until overlay is rendered
524
+ this.__x = this._getEventCoordinate(e, 'x');
525
+ this.__pageXOffset = window.pageXOffset;
526
+
527
+ this.__y = this._getEventCoordinate(e, 'y');
528
+ this.__pageYOffset = window.pageYOffset;
529
+
530
+ this.$.overlay.style.opacity = '0';
531
+ this._setOpened(true);
532
+ }
533
+ }
534
+ }
535
+
536
+ /** @private */
537
+ __onScroll() {
538
+ if (!this.opened) {
539
+ return;
540
+ }
541
+
542
+ const yDiff = window.pageYOffset - this.__pageYOffset;
543
+ const xDiff = window.pageXOffset - this.__pageXOffset;
544
+
545
+ this.__adjustPosition('left', -xDiff);
546
+ this.__adjustPosition('right', xDiff);
547
+
548
+ this.__adjustPosition('top', -yDiff);
549
+ this.__adjustPosition('bottom', yDiff);
550
+
551
+ this.__pageYOffset += yDiff;
552
+ this.__pageXOffset += xDiff;
553
+ }
554
+
555
+ /** @private */
556
+ __adjustPosition(coord, diff) {
557
+ const overlay = this.$.overlay;
558
+ const style = overlay.style;
559
+
560
+ style[coord] = (parseInt(style[coord]) || 0) + diff + 'px';
561
+ }
562
+
563
+ /** @private */
564
+ __alignOverlayPosition() {
565
+ const overlay = this.$.overlay;
566
+ const style = overlay.style;
567
+
568
+ // Reset all properties before measuring
569
+ ['top', 'right', 'bottom', 'left'].forEach((prop) => style.removeProperty(prop));
570
+ ['right-aligned', 'end-aligned', 'bottom-aligned'].forEach((attr) => overlay.removeAttribute(attr));
571
+
572
+ // Maximum x and y values are imposed by content size and overlay limits.
573
+ const { xMax, xMin, yMax, left, right, top, width } = overlay.getBoundaries();
574
+ // Reuse saved x and y event values, in order to this method be used async
575
+ // in the `vaadin-overlay-change` which guarantees that overlay is ready
576
+ let x = this.__x || (!this.__isRTL ? left : right);
577
+ const y = this.__y || top;
578
+
579
+ // Select one overlay corner and move to the event x/y position.
580
+ // Then set styling attrs for flex-aligning the content appropriately.
581
+ const wdthVport = document.documentElement.clientWidth;
582
+ const hghtVport = document.documentElement.clientHeight;
583
+
584
+ // Align to the parent menu overlay, if any.
585
+ const parent = overlay.parentOverlay;
586
+ let alignedToParent = false;
587
+ let parentContentRect;
588
+ if (parent) {
589
+ parentContentRect = parent.$.overlay.getBoundingClientRect();
590
+ if (parent.hasAttribute('right-aligned') || parent.hasAttribute('end-aligned')) {
591
+ const parentStyle = getComputedStyle(parent);
592
+ const getPadding = (el, direction) => {
593
+ return parseFloat(getComputedStyle(el.$.content)['padding' + direction]);
594
+ };
595
+ const dimensionToSet = parseFloat(parentStyle[this.__isRTL ? 'left' : 'right']) + parentContentRect.width;
596
+ const padding = getPadding(parent, 'Left') + getPadding(overlay, 'Right');
597
+
598
+ // Preserve end-aligned, if possible.
599
+ if (wdthVport - (dimensionToSet - padding) > width) {
600
+ this._setEndAligned(overlay);
601
+ style[this.__isRTL ? 'left' : 'right'] = dimensionToSet + 'px';
602
+ alignedToParent = true;
603
+ }
604
+ } else if (x < parentContentRect.x) {
605
+ // Check if sub menu opens on the left side and the parent menu is not right aligned.
606
+ // If so, use actual width of the submenu content instead of the parent menu content.
607
+ x = x - (width - parentContentRect.width);
608
+ }
609
+ }
610
+
611
+ if (!alignedToParent) {
612
+ if (!this.__isRTL) {
613
+ // Sub-menu is displayed in the right side of root menu
614
+ if ((x < wdthVport / 2 || x < xMax) && !parent) {
615
+ style.left = x + 'px';
616
+ } else if (parent && wdthVport - parentContentRect.width - parentContentRect.left >= parentContentRect.width) {
617
+ // Sub-menu is displayed in the right side of root menu If it is nested menu
618
+ style.left = parentContentRect.left + parentContentRect.width + 'px';
619
+ } else if (parent) {
620
+ // Sub-menu is displayed in the left side of root menu If it is nested menu
621
+ style.right = 'auto';
622
+ style.left =
623
+ Math.max(
624
+ overlay.getBoundingClientRect().left,
625
+ parentContentRect.left - overlay.getBoundingClientRect().width
626
+ ) + 'px';
627
+ this._setEndAligned(overlay);
628
+ } else {
629
+ // Sub-menu is displayed in the left side of root menu
630
+ style.right = Math.max(0, wdthVport - x) + 'px';
631
+ this._setEndAligned(overlay);
632
+ }
633
+ } else {
634
+ // Sub-menu is displayed in the left side of root menu
635
+ if ((x > wdthVport / 2 || x > xMin) && !parent) {
636
+ style.right = Math.max(0, wdthVport - x) + 'px';
637
+ } else if (parent && parentContentRect.left >= parentContentRect.width) {
638
+ // Sub-menu is displayed in the left side of root menu If it is nested menu
639
+ style.right = wdthVport - parentContentRect.right + parentContentRect.width + 'px';
640
+ } else if (parent) {
641
+ // Sub-menu is displayed in the right side of root menu If it is nested menu
642
+ style.right = 'auto';
643
+ style.left =
644
+ Math.max(
645
+ overlay.getBoundingClientRect().left - overlay.getBoundingClientRect().width,
646
+ parentContentRect.right
647
+ ) + 'px';
648
+ this._setEndAligned(overlay);
649
+ } else {
650
+ // Sub-menu is displayed in the left side of root menu
651
+ style.left = x + 'px';
652
+ this._setEndAligned(overlay);
653
+ }
654
+ }
655
+ }
656
+ if (y < hghtVport / 2 || y < yMax) {
657
+ style.top = y + 'px';
658
+ } else {
659
+ style.bottom = Math.max(0, hghtVport - y) + 'px';
660
+ overlay.setAttribute('bottom-aligned', '');
661
+ }
662
+ }
663
+
664
+ /** @private */
665
+ _setEndAligned(element) {
666
+ element.setAttribute('end-aligned', '');
667
+ if (!this.__isRTL) {
668
+ element.setAttribute('right-aligned', '');
669
+ }
670
+ }
671
+
672
+ /** @private */
673
+ _getEventCoordinate(event, coord) {
674
+ if (event.detail instanceof Object) {
675
+ if (event.detail[coord]) {
676
+ // Polymer gesture events, get coordinate from detail
677
+ return event.detail[coord];
678
+ } else if (event.detail.sourceEvent) {
679
+ // Unwrap detailed event
680
+ return this._getEventCoordinate(event.detail.sourceEvent, coord);
681
+ }
682
+ } else {
683
+ const prop = 'client' + coord.toUpperCase();
684
+ const position = event.changedTouches ? event.changedTouches[0][prop] : event[prop];
685
+
686
+ if (position === 0) {
687
+ // Native keyboard event
688
+ const rect = event.target.getBoundingClientRect();
689
+ return coord === 'x' ? rect.left : rect.top + rect.height;
690
+ } else {
691
+ // Native mouse or touch event
692
+ return position;
693
+ }
694
+ }
695
+ }
696
+
697
+ /** @private */
698
+ _listen(node, evType, handler) {
699
+ if (gestures[evType]) {
700
+ addListener(node, evType, handler);
701
+ } else {
702
+ node.addEventListener(evType, handler);
703
+ }
704
+ }
705
+
706
+ /** @private */
707
+ _unlisten(node, evType, handler) {
708
+ if (gestures[evType]) {
709
+ removeListener(node, evType, handler);
710
+ } else {
711
+ node.removeEventListener(evType, handler);
712
+ }
713
+ }
714
+
715
+ /** @private */
716
+ _onGlobalContextMenu(e) {
717
+ if (!e.shiftKey) {
718
+ e.preventDefault();
719
+ this.close();
720
+ }
721
+ }
722
+
723
+ /**
724
+ * Fired when an item is selected when the context menu is populated using the `items` API.
725
+ *
726
+ * @event item-selected
727
+ * @param {Object} detail
728
+ * @param {Object} detail.value the selected menu item
729
+ */
730
+ }
731
+
732
+ customElements.define(ContextMenu.is, ContextMenu);
733
+ export { ContextMenu };