@everymatrix/general-input 1.10.2 → 1.15.0

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.
Files changed (96) hide show
  1. package/dist/cjs/checkbox-group-input_10.cjs.entry.js +35539 -0
  2. package/dist/cjs/general-input.cjs.entry.js +34 -13
  3. package/dist/cjs/general-input.cjs.js +2 -2
  4. package/dist/cjs/{index-64a5cb7f.js → index-132a0774.js} +119 -6
  5. package/dist/cjs/loader.cjs.js +2 -2
  6. package/dist/cjs/locale.utils-7665b010.js +71 -0
  7. package/dist/cjs/toggle-checkbox-input.cjs.entry.js +85 -0
  8. package/dist/cjs/tooltipIcon-092a795f.js +5 -0
  9. package/dist/collection/collection-manifest.json +3 -1
  10. package/dist/collection/components/checkbox-group-input/checkbox-group-input.css +62 -0
  11. package/dist/collection/components/checkbox-group-input/checkbox-group-input.js +366 -0
  12. package/dist/collection/components/checkbox-input/checkbox-input.css +47 -1
  13. package/dist/collection/components/checkbox-input/checkbox-input.js +143 -16
  14. package/dist/collection/components/date-input/date-input.css +64 -30
  15. package/dist/collection/components/date-input/date-input.js +194 -13
  16. package/dist/collection/components/email-input/email-input.css +63 -28
  17. package/dist/collection/components/email-input/email-input.js +197 -21
  18. package/dist/collection/components/general-input/general-input.js +163 -21
  19. package/dist/collection/components/number-input/number-input.css +65 -30
  20. package/dist/collection/components/number-input/number-input.js +174 -19
  21. package/dist/collection/components/password-input/password-input.css +120 -29
  22. package/dist/collection/components/password-input/password-input.js +341 -25
  23. package/dist/collection/components/radio-input/radio-input.css +22 -1
  24. package/dist/collection/components/radio-input/radio-input.js +89 -10
  25. package/dist/collection/components/select-input/select-input.css +75 -22
  26. package/dist/collection/components/select-input/select-input.js +180 -37
  27. package/dist/collection/components/tel-input/tel-input.css +91 -33
  28. package/dist/collection/components/tel-input/tel-input.js +202 -24
  29. package/dist/collection/components/text-input/text-input.css +63 -28
  30. package/dist/collection/components/text-input/text-input.js +208 -39
  31. package/dist/collection/components/toggle-checkbox-input/toggle-checkbox-input.css +76 -0
  32. package/dist/collection/components/toggle-checkbox-input/toggle-checkbox-input.js +324 -0
  33. package/dist/collection/utils/locale.utils.js +52 -13
  34. package/dist/collection/utils/tooltipIcon.svg +5 -0
  35. package/dist/components/active-mixin.js +975 -0
  36. package/dist/components/checkbox-group-input.d.ts +11 -0
  37. package/dist/components/checkbox-group-input.js +6 -0
  38. package/dist/components/checkbox-group-input2.js +1125 -0
  39. package/dist/components/checkbox-input2.js +62 -12
  40. package/dist/components/date-input2.js +10247 -15
  41. package/dist/components/email-input2.js +98 -21
  42. package/dist/components/field-mixin.js +12712 -0
  43. package/dist/components/general-input.js +1 -118
  44. package/dist/components/general-input2.js +331 -0
  45. package/dist/components/input-field-shared-styles.js +1114 -0
  46. package/dist/components/number-input2.js +92 -16
  47. package/dist/components/password-input2.js +924 -24
  48. package/dist/components/pattern-mixin.js +85 -0
  49. package/dist/components/radio-input2.js +45 -11
  50. package/dist/components/select-input2.js +87 -27
  51. package/dist/components/tel-input2.js +122 -22
  52. package/dist/components/text-input2.js +120 -34
  53. package/dist/components/toggle-checkbox-input.d.ts +11 -0
  54. package/dist/components/toggle-checkbox-input.js +6 -0
  55. package/dist/components/tooltipIcon.js +70 -0
  56. package/dist/components/vaadin-button.js +461 -0
  57. package/dist/components/vaadin-combo-box.js +4329 -0
  58. package/dist/components/virtual-keyboard-controller.js +2658 -0
  59. package/dist/esm/checkbox-group-input_10.entry.js +35526 -0
  60. package/dist/esm/general-input.entry.js +34 -13
  61. package/dist/esm/general-input.js +2 -2
  62. package/dist/esm/{index-df80f936.js → index-db76d5b5.js} +118 -7
  63. package/dist/esm/loader.js +2 -2
  64. package/dist/esm/locale.utils-95ea2605.js +68 -0
  65. package/dist/esm/toggle-checkbox-input.entry.js +81 -0
  66. package/dist/esm/tooltipIcon-99c1c7b7.js +3 -0
  67. package/dist/general-input/general-input.esm.js +1 -1
  68. package/dist/general-input/p-0966f523.entry.js +3581 -0
  69. package/dist/general-input/p-916a1319.entry.js +1 -0
  70. package/dist/general-input/p-b408093e.js +1 -0
  71. package/dist/general-input/p-c2d4d6ac.entry.js +1 -0
  72. package/dist/general-input/p-f4f4ccda.js +1 -0
  73. package/dist/general-input/p-f6132f1d.js +1 -0
  74. package/dist/types/components/checkbox-group-input/checkbox-group-input.d.ts +74 -0
  75. package/dist/types/components/checkbox-input/checkbox-input.d.ts +28 -2
  76. package/dist/types/components/date-input/date-input.d.ts +43 -1
  77. package/dist/types/components/email-input/email-input.d.ts +37 -3
  78. package/dist/types/components/general-input/general-input.d.ts +30 -0
  79. package/dist/types/components/number-input/number-input.d.ts +34 -3
  80. package/dist/types/components/password-input/password-input.d.ts +56 -7
  81. package/dist/types/components/radio-input/radio-input.d.ts +17 -1
  82. package/dist/types/components/select-input/select-input.d.ts +36 -3
  83. package/dist/types/components/tel-input/tel-input.d.ts +40 -5
  84. package/dist/types/components/text-input/text-input.d.ts +84 -0
  85. package/dist/types/components/toggle-checkbox-input/toggle-checkbox-input.d.ts +67 -0
  86. package/dist/types/components.d.ts +568 -9
  87. package/dist/types/utils/locale.utils.d.ts +9 -0
  88. package/dist/types/utils/types.d.ts +41 -9
  89. package/package.json +8 -1
  90. package/dist/cjs/checkbox-input_9.cjs.entry.js +0 -623
  91. package/dist/components/locale.utils.js +0 -29
  92. package/dist/esm/checkbox-input_9.entry.js +0 -611
  93. package/dist/general-input/p-1703fce3.entry.js +0 -1
  94. package/dist/general-input/p-d9f7fa2e.js +0 -1
  95. package/dist/general-input/p-dea0a4ac.entry.js +0 -1
  96. /package/dist/types/Users/{user/workspace/everymatrix → adrian.pripon/Documents/Work}/widgets-stencil/packages/general-input/.stencil/packages/general-input/stencil.config.d.ts +0 -0
@@ -0,0 +1,2658 @@
1
+ import { i, r as registerStyles, d as dedupingMixin, B as PropertyEffects, G as strictTemplatePolicy, w as wrap, H as legacyWarnings, J as getFocusableElements, b as isElementFocused, T as ThemableMixin, A as DirMixin, C as ControllerMixin, P as PolymerElement, h as html, m as FlattenedNodesObserver, M as getAncestorRootNodes } from './field-mixin.js';
2
+ import { b as isIOS } from './input-field-shared-styles.js';
3
+
4
+ /**
5
+ * @license
6
+ * Copyright (c) 2017 - 2022 Vaadin Ltd.
7
+ * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
8
+ */
9
+
10
+ const overlay = i`
11
+ :host {
12
+ top: var(--lumo-space-m);
13
+ right: var(--lumo-space-m);
14
+ bottom: var(--lumo-space-m);
15
+ left: var(--lumo-space-m);
16
+ /* Workaround for Edge issue (only on Surface), where an overflowing vaadin-list-box inside vaadin-select-overlay makes the overlay transparent */
17
+ /* stylelint-disable-next-line */
18
+ outline: 0px solid transparent;
19
+ }
20
+
21
+ [part='overlay'] {
22
+ background-color: var(--lumo-base-color);
23
+ background-image: linear-gradient(var(--lumo-tint-5pct), var(--lumo-tint-5pct));
24
+ border-radius: var(--lumo-border-radius-m);
25
+ box-shadow: 0 0 0 1px var(--lumo-shade-5pct), var(--lumo-box-shadow-m);
26
+ color: var(--lumo-body-text-color);
27
+ font-family: var(--lumo-font-family);
28
+ font-size: var(--lumo-font-size-m);
29
+ font-weight: 400;
30
+ line-height: var(--lumo-line-height-m);
31
+ letter-spacing: 0;
32
+ text-transform: none;
33
+ -webkit-text-size-adjust: 100%;
34
+ -webkit-font-smoothing: antialiased;
35
+ -moz-osx-font-smoothing: grayscale;
36
+ }
37
+
38
+ [part='content'] {
39
+ padding: var(--lumo-space-xs);
40
+ }
41
+
42
+ [part='backdrop'] {
43
+ background-color: var(--lumo-shade-20pct);
44
+ animation: 0.2s lumo-overlay-backdrop-enter both;
45
+ will-change: opacity;
46
+ }
47
+
48
+ @keyframes lumo-overlay-backdrop-enter {
49
+ 0% {
50
+ opacity: 0;
51
+ }
52
+ }
53
+
54
+ :host([closing]) [part='backdrop'] {
55
+ animation: 0.2s lumo-overlay-backdrop-exit both;
56
+ }
57
+
58
+ @keyframes lumo-overlay-backdrop-exit {
59
+ 100% {
60
+ opacity: 0;
61
+ }
62
+ }
63
+
64
+ @keyframes lumo-overlay-dummy-animation {
65
+ 0% {
66
+ opacity: 1;
67
+ }
68
+
69
+ 100% {
70
+ opacity: 1;
71
+ }
72
+ }
73
+ `;
74
+
75
+ registerStyles('', overlay, { moduleId: 'lumo-overlay' });
76
+
77
+ registerStyles('vaadin-overlay', overlay, { moduleId: 'lumo-vaadin-overlay' });
78
+
79
+ /**
80
+ @license
81
+ Copyright (c) 2017 The Polymer Project Authors. All rights reserved.
82
+ This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
83
+ The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
84
+ The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
85
+ Code distributed by Google as part of the polymer project is also
86
+ subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
87
+ */
88
+
89
+ let scheduled = false;
90
+ let beforeRenderQueue = [];
91
+ let afterRenderQueue = [];
92
+
93
+ function schedule() {
94
+ scheduled = true;
95
+ // before next render
96
+ requestAnimationFrame(function() {
97
+ scheduled = false;
98
+ flushQueue(beforeRenderQueue);
99
+ // after the render
100
+ setTimeout(function() {
101
+ runQueue(afterRenderQueue);
102
+ });
103
+ });
104
+ }
105
+
106
+ function flushQueue(queue) {
107
+ while (queue.length) {
108
+ callMethod(queue.shift());
109
+ }
110
+ }
111
+
112
+ function runQueue(queue) {
113
+ for (let i=0, l=queue.length; i < l; i++) {
114
+ callMethod(queue.shift());
115
+ }
116
+ }
117
+
118
+ function callMethod(info) {
119
+ const context = info[0];
120
+ const callback = info[1];
121
+ const args = info[2];
122
+ try {
123
+ callback.apply(context, args);
124
+ } catch(e) {
125
+ setTimeout(() => {
126
+ throw e;
127
+ });
128
+ }
129
+ }
130
+
131
+ /**
132
+ * Enqueues a callback which will be run after the next render, equivalent
133
+ * to one task (`setTimeout`) after the next `requestAnimationFrame`.
134
+ *
135
+ * This method is useful for tuning the first-render performance of an
136
+ * element or application by deferring non-critical work until after the
137
+ * first paint. Typical non-render-critical work may include adding UI
138
+ * event listeners and aria attributes.
139
+ *
140
+ * @param {*} context Context object the callback function will be bound to
141
+ * @param {function(...*):void} callback Callback function
142
+ * @param {!Array=} args An array of arguments to call the callback function with
143
+ * @return {void}
144
+ */
145
+ function afterNextRender(context, callback, args) {
146
+ if (!scheduled) {
147
+ schedule();
148
+ }
149
+ afterRenderQueue.push([context, callback, args]);
150
+ }
151
+
152
+ /**
153
+ @license
154
+ Copyright (c) 2017 The Polymer Project Authors. All rights reserved.
155
+ This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
156
+ The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
157
+ The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
158
+ Code distributed by Google as part of the polymer project is also
159
+ subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
160
+ */
161
+
162
+ // Common implementation for mixin & behavior
163
+ function mutablePropertyChange(inst, property, value, old, mutableData) {
164
+ let isObject;
165
+ if (mutableData) {
166
+ isObject = (typeof value === 'object' && value !== null);
167
+ // Pull `old` for Objects from temp cache, but treat `null` as a primitive
168
+ if (isObject) {
169
+ old = inst.__dataTemp[property];
170
+ }
171
+ }
172
+ // Strict equality check, but return false for NaN===NaN
173
+ let shouldChange = (old !== value && (old === old || value === value));
174
+ // Objects are stored in temporary cache (cleared at end of
175
+ // turn), which is used for dirty-checking
176
+ if (isObject && shouldChange) {
177
+ inst.__dataTemp[property] = value;
178
+ }
179
+ return shouldChange;
180
+ }
181
+
182
+ /**
183
+ * Element class mixin to skip strict dirty-checking for objects and arrays
184
+ * (always consider them to be "dirty"), for use on elements utilizing
185
+ * `PropertyEffects`
186
+ *
187
+ * By default, `PropertyEffects` performs strict dirty checking on
188
+ * objects, which means that any deep modifications to an object or array will
189
+ * not be propagated unless "immutable" data patterns are used (i.e. all object
190
+ * references from the root to the mutation were changed).
191
+ *
192
+ * Polymer also provides a proprietary data mutation and path notification API
193
+ * (e.g. `notifyPath`, `set`, and array mutation API's) that allow efficient
194
+ * mutation and notification of deep changes in an object graph to all elements
195
+ * bound to the same object graph.
196
+ *
197
+ * In cases where neither immutable patterns nor the data mutation API can be
198
+ * used, applying this mixin will cause Polymer to skip dirty checking for
199
+ * objects and arrays (always consider them to be "dirty"). This allows a
200
+ * user to make a deep modification to a bound object graph, and then either
201
+ * simply re-set the object (e.g. `this.items = this.items`) or call `notifyPath`
202
+ * (e.g. `this.notifyPath('items')`) to update the tree. Note that all
203
+ * elements that wish to be updated based on deep mutations must apply this
204
+ * mixin or otherwise skip strict dirty checking for objects/arrays.
205
+ * Specifically, any elements in the binding tree between the source of a
206
+ * mutation and the consumption of it must apply this mixin or enable the
207
+ * `OptionalMutableData` mixin.
208
+ *
209
+ * In order to make the dirty check strategy configurable, see
210
+ * `OptionalMutableData`.
211
+ *
212
+ * Note, the performance characteristics of propagating large object graphs
213
+ * will be worse as opposed to using strict dirty checking with immutable
214
+ * patterns or Polymer's path notification API.
215
+ *
216
+ * @mixinFunction
217
+ * @polymer
218
+ * @summary Element class mixin to skip strict dirty-checking for objects
219
+ * and arrays
220
+ * @template T
221
+ * @param {function(new:T)} superClass Class to apply mixin to.
222
+ * @return {function(new:T)} superClass with mixin applied.
223
+ */
224
+ const MutableData = dedupingMixin(superClass => {
225
+
226
+ /**
227
+ * @polymer
228
+ * @mixinClass
229
+ * @implements {Polymer_MutableData}
230
+ */
231
+ class MutableData extends superClass {
232
+ /**
233
+ * Overrides `PropertyEffects` to provide option for skipping
234
+ * strict equality checking for Objects and Arrays.
235
+ *
236
+ * This method pulls the value to dirty check against from the `__dataTemp`
237
+ * cache (rather than the normal `__data` cache) for Objects. Since the temp
238
+ * cache is cleared at the end of a turn, this implementation allows
239
+ * side-effects of deep object changes to be processed by re-setting the
240
+ * same object (using the temp cache as an in-turn backstop to prevent
241
+ * cycles due to 2-way notification).
242
+ *
243
+ * @param {string} property Property name
244
+ * @param {*} value New property value
245
+ * @param {*} old Previous property value
246
+ * @return {boolean} Whether the property should be considered a change
247
+ * @protected
248
+ */
249
+ _shouldPropertyChange(property, value, old) {
250
+ return mutablePropertyChange(this, property, value, old, true);
251
+ }
252
+
253
+ }
254
+
255
+ return MutableData;
256
+
257
+ });
258
+
259
+ /**
260
+ * Element class mixin to add the optional ability to skip strict
261
+ * dirty-checking for objects and arrays (always consider them to be
262
+ * "dirty") by setting a `mutable-data` attribute on an element instance.
263
+ *
264
+ * By default, `PropertyEffects` performs strict dirty checking on
265
+ * objects, which means that any deep modifications to an object or array will
266
+ * not be propagated unless "immutable" data patterns are used (i.e. all object
267
+ * references from the root to the mutation were changed).
268
+ *
269
+ * Polymer also provides a proprietary data mutation and path notification API
270
+ * (e.g. `notifyPath`, `set`, and array mutation API's) that allow efficient
271
+ * mutation and notification of deep changes in an object graph to all elements
272
+ * bound to the same object graph.
273
+ *
274
+ * In cases where neither immutable patterns nor the data mutation API can be
275
+ * used, applying this mixin will allow Polymer to skip dirty checking for
276
+ * objects and arrays (always consider them to be "dirty"). This allows a
277
+ * user to make a deep modification to a bound object graph, and then either
278
+ * simply re-set the object (e.g. `this.items = this.items`) or call `notifyPath`
279
+ * (e.g. `this.notifyPath('items')`) to update the tree. Note that all
280
+ * elements that wish to be updated based on deep mutations must apply this
281
+ * mixin or otherwise skip strict dirty checking for objects/arrays.
282
+ * Specifically, any elements in the binding tree between the source of a
283
+ * mutation and the consumption of it must enable this mixin or apply the
284
+ * `MutableData` mixin.
285
+ *
286
+ * While this mixin adds the ability to forgo Object/Array dirty checking,
287
+ * the `mutableData` flag defaults to false and must be set on the instance.
288
+ *
289
+ * Note, the performance characteristics of propagating large object graphs
290
+ * will be worse by relying on `mutableData: true` as opposed to using
291
+ * strict dirty checking with immutable patterns or Polymer's path notification
292
+ * API.
293
+ *
294
+ * @mixinFunction
295
+ * @polymer
296
+ * @summary Element class mixin to optionally skip strict dirty-checking
297
+ * for objects and arrays
298
+ */
299
+ const OptionalMutableData = dedupingMixin(superClass => {
300
+
301
+ /**
302
+ * @mixinClass
303
+ * @polymer
304
+ * @implements {Polymer_OptionalMutableData}
305
+ */
306
+ class OptionalMutableData extends superClass {
307
+
308
+ /** @nocollapse */
309
+ static get properties() {
310
+ return {
311
+ /**
312
+ * Instance-level flag for configuring the dirty-checking strategy
313
+ * for this element. When true, Objects and Arrays will skip dirty
314
+ * checking, otherwise strict equality checking will be used.
315
+ */
316
+ mutableData: Boolean
317
+ };
318
+ }
319
+
320
+ /**
321
+ * Overrides `PropertyEffects` to provide option for skipping
322
+ * strict equality checking for Objects and Arrays.
323
+ *
324
+ * When `this.mutableData` is true on this instance, this method
325
+ * pulls the value to dirty check against from the `__dataTemp` cache
326
+ * (rather than the normal `__data` cache) for Objects. Since the temp
327
+ * cache is cleared at the end of a turn, this implementation allows
328
+ * side-effects of deep object changes to be processed by re-setting the
329
+ * same object (using the temp cache as an in-turn backstop to prevent
330
+ * cycles due to 2-way notification).
331
+ *
332
+ * @param {string} property Property name
333
+ * @param {*} value New property value
334
+ * @param {*} old Previous property value
335
+ * @return {boolean} Whether the property should be considered a change
336
+ * @protected
337
+ */
338
+ _shouldPropertyChange(property, value, old) {
339
+ return mutablePropertyChange(this, property, value, old, this.mutableData);
340
+ }
341
+ }
342
+
343
+ return OptionalMutableData;
344
+
345
+ });
346
+
347
+ // Export for use by legacy behavior
348
+ MutableData._mutablePropertyChange = mutablePropertyChange;
349
+
350
+ /**
351
+ @license
352
+ Copyright (c) 2017 The Polymer Project Authors. All rights reserved.
353
+ This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
354
+ The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
355
+ The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
356
+ Code distributed by Google as part of the polymer project is also
357
+ subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
358
+ */
359
+
360
+ // Base class for HTMLTemplateElement extension that has property effects
361
+ // machinery for propagating host properties to children. This is an ES5
362
+ // class only because Babel (incorrectly) requires super() in the class
363
+ // constructor even though no `this` is used and it returns an instance.
364
+ let newInstance = null;
365
+
366
+ /**
367
+ * @constructor
368
+ * @extends {HTMLTemplateElement}
369
+ * @private
370
+ */
371
+ function HTMLTemplateElementExtension() { return newInstance; }
372
+ HTMLTemplateElementExtension.prototype = Object.create(HTMLTemplateElement.prototype, {
373
+ constructor: {
374
+ value: HTMLTemplateElementExtension,
375
+ writable: true
376
+ }
377
+ });
378
+
379
+ /**
380
+ * @constructor
381
+ * @implements {Polymer_PropertyEffects}
382
+ * @extends {HTMLTemplateElementExtension}
383
+ * @private
384
+ */
385
+ const DataTemplate = PropertyEffects(HTMLTemplateElementExtension);
386
+
387
+ /**
388
+ * @constructor
389
+ * @implements {Polymer_MutableData}
390
+ * @extends {DataTemplate}
391
+ * @private
392
+ */
393
+ const MutableDataTemplate = MutableData(DataTemplate);
394
+
395
+ // Applies a DataTemplate subclass to a <template> instance
396
+ function upgradeTemplate(template, constructor) {
397
+ newInstance = template;
398
+ Object.setPrototypeOf(template, constructor.prototype);
399
+ new constructor();
400
+ newInstance = null;
401
+ }
402
+
403
+ /**
404
+ * Base class for TemplateInstance.
405
+ * @constructor
406
+ * @extends {HTMLElement}
407
+ * @implements {Polymer_PropertyEffects}
408
+ * @private
409
+ */
410
+ const templateInstanceBase = PropertyEffects(class {});
411
+
412
+ function showHideChildren(hide, children) {
413
+ for (let i=0; i<children.length; i++) {
414
+ let n = children[i];
415
+ // Ignore non-changes
416
+ if (Boolean(hide) != Boolean(n.__hideTemplateChildren__)) {
417
+ // clear and restore text
418
+ if (n.nodeType === Node.TEXT_NODE) {
419
+ if (hide) {
420
+ n.__polymerTextContent__ = n.textContent;
421
+ n.textContent = '';
422
+ } else {
423
+ n.textContent = n.__polymerTextContent__;
424
+ }
425
+ // remove and replace slot
426
+ } else if (n.localName === 'slot') {
427
+ if (hide) {
428
+ n.__polymerReplaced__ = document.createComment('hidden-slot');
429
+ wrap(wrap(n).parentNode).replaceChild(n.__polymerReplaced__, n);
430
+ } else {
431
+ const replace = n.__polymerReplaced__;
432
+ if (replace) {
433
+ wrap(wrap(replace).parentNode).replaceChild(n, replace);
434
+ }
435
+ }
436
+ }
437
+ // hide and show nodes
438
+ else if (n.style) {
439
+ if (hide) {
440
+ n.__polymerDisplay__ = n.style.display;
441
+ n.style.display = 'none';
442
+ } else {
443
+ n.style.display = n.__polymerDisplay__;
444
+ }
445
+ }
446
+ }
447
+ n.__hideTemplateChildren__ = hide;
448
+ if (n._showHideChildren) {
449
+ n._showHideChildren(hide);
450
+ }
451
+ }
452
+ }
453
+
454
+ /**
455
+ * @polymer
456
+ * @customElement
457
+ * @appliesMixin PropertyEffects
458
+ * @unrestricted
459
+ */
460
+ class TemplateInstanceBase extends templateInstanceBase {
461
+ constructor(props) {
462
+ super();
463
+ this._configureProperties(props);
464
+ /** @type {!StampedTemplate} */
465
+ this.root = this._stampTemplate(this.__dataHost);
466
+ // Save list of stamped children
467
+ let children = [];
468
+ /** @suppress {invalidCasts} */
469
+ this.children = /** @type {!NodeList} */ (children);
470
+ // Polymer 1.x did not use `Polymer.dom` here so not bothering.
471
+ for (let n = this.root.firstChild; n; n=n.nextSibling) {
472
+ children.push(n);
473
+ n.__templatizeInstance = this;
474
+ }
475
+ if (this.__templatizeOwner &&
476
+ this.__templatizeOwner.__hideTemplateChildren__) {
477
+ this._showHideChildren(true);
478
+ }
479
+ // Flush props only when props are passed if instance props exist
480
+ // or when there isn't instance props.
481
+ let options = this.__templatizeOptions;
482
+ if ((props && options.instanceProps) || !options.instanceProps) {
483
+ this._enableProperties();
484
+ }
485
+ }
486
+ /**
487
+ * Configure the given `props` by calling `_setPendingProperty`. Also
488
+ * sets any properties stored in `__hostProps`.
489
+ * @private
490
+ * @param {Object} props Object of property name-value pairs to set.
491
+ * @return {void}
492
+ */
493
+ _configureProperties(props) {
494
+ let options = this.__templatizeOptions;
495
+ if (options.forwardHostProp) {
496
+ for (let hprop in this.__hostProps) {
497
+ this._setPendingProperty(hprop, this.__dataHost['_host_' + hprop]);
498
+ }
499
+ }
500
+ // Any instance props passed in the constructor will overwrite host props;
501
+ // normally this would be a user error but we don't specifically filter them
502
+ for (let iprop in props) {
503
+ this._setPendingProperty(iprop, props[iprop]);
504
+ }
505
+ }
506
+ /**
507
+ * Forwards a host property to this instance. This method should be
508
+ * called on instances from the `options.forwardHostProp` callback
509
+ * to propagate changes of host properties to each instance.
510
+ *
511
+ * Note this method enqueues the change, which are flushed as a batch.
512
+ *
513
+ * @param {string} prop Property or path name
514
+ * @param {*} value Value of the property to forward
515
+ * @return {void}
516
+ */
517
+ forwardHostProp(prop, value) {
518
+ if (this._setPendingPropertyOrPath(prop, value, false, true)) {
519
+ this.__dataHost._enqueueClient(this);
520
+ }
521
+ }
522
+
523
+ /**
524
+ * Override point for adding custom or simulated event handling.
525
+ *
526
+ * @override
527
+ * @param {!Node} node Node to add event listener to
528
+ * @param {string} eventName Name of event
529
+ * @param {function(!Event):void} handler Listener function to add
530
+ * @return {void}
531
+ */
532
+ _addEventListenerToNode(node, eventName, handler) {
533
+ if (this._methodHost && this.__templatizeOptions.parentModel) {
534
+ // If this instance should be considered a parent model, decorate
535
+ // events this template instance as `model`
536
+ this._methodHost._addEventListenerToNode(node, eventName, (e) => {
537
+ e.model = this;
538
+ handler(e);
539
+ });
540
+ } else {
541
+ // Otherwise delegate to the template's host (which could be)
542
+ // another template instance
543
+ let templateHost = this.__dataHost.__dataHost;
544
+ if (templateHost) {
545
+ templateHost._addEventListenerToNode(node, eventName, handler);
546
+ }
547
+ }
548
+ }
549
+ /**
550
+ * Shows or hides the template instance top level child elements. For
551
+ * text nodes, `textContent` is removed while "hidden" and replaced when
552
+ * "shown."
553
+ * @param {boolean} hide Set to true to hide the children;
554
+ * set to false to show them.
555
+ * @return {void}
556
+ * @protected
557
+ */
558
+ _showHideChildren(hide) {
559
+ showHideChildren(hide, this.children);
560
+ }
561
+ /**
562
+ * Overrides default property-effects implementation to intercept
563
+ * textContent bindings while children are "hidden" and cache in
564
+ * private storage for later retrieval.
565
+ *
566
+ * @override
567
+ * @param {!Node} node The node to set a property on
568
+ * @param {string} prop The property to set
569
+ * @param {*} value The value to set
570
+ * @return {void}
571
+ * @protected
572
+ */
573
+ _setUnmanagedPropertyToNode(node, prop, value) {
574
+ if (node.__hideTemplateChildren__ &&
575
+ node.nodeType == Node.TEXT_NODE && prop == 'textContent') {
576
+ node.__polymerTextContent__ = value;
577
+ } else {
578
+ super._setUnmanagedPropertyToNode(node, prop, value);
579
+ }
580
+ }
581
+ /**
582
+ * Find the parent model of this template instance. The parent model
583
+ * is either another templatize instance that had option `parentModel: true`,
584
+ * or else the host element.
585
+ *
586
+ * @return {!Polymer_PropertyEffects} The parent model of this instance
587
+ */
588
+ get parentModel() {
589
+ let model = this.__parentModel;
590
+ if (!model) {
591
+ let options;
592
+ model = this;
593
+ do {
594
+ // A template instance's `__dataHost` is a <template>
595
+ // `model.__dataHost.__dataHost` is the template's host
596
+ model = model.__dataHost.__dataHost;
597
+ } while ((options = model.__templatizeOptions) && !options.parentModel);
598
+ this.__parentModel = model;
599
+ }
600
+ return model;
601
+ }
602
+
603
+ /**
604
+ * Stub of HTMLElement's `dispatchEvent`, so that effects that may
605
+ * dispatch events safely no-op.
606
+ *
607
+ * @param {Event} event Event to dispatch
608
+ * @return {boolean} Always true.
609
+ * @override
610
+ */
611
+ dispatchEvent(event) { // eslint-disable-line no-unused-vars
612
+ return true;
613
+ }
614
+ }
615
+
616
+ /**
617
+ * @constructor
618
+ * @extends {TemplateInstanceBase}
619
+ * @implements {Polymer_MutableData}
620
+ * @private
621
+ */
622
+ const MutableTemplateInstanceBase = MutableData(
623
+ // This cast shouldn't be neccessary, but Closure doesn't understand that
624
+ // TemplateInstanceBase is a constructor function.
625
+ /** @type {function(new:TemplateInstanceBase)} */ (TemplateInstanceBase));
626
+
627
+ function findMethodHost(template) {
628
+ // Technically this should be the owner of the outermost template.
629
+ // In shadow dom, this is always getRootNode().host, but we can
630
+ // approximate this via cooperation with our dataHost always setting
631
+ // `_methodHost` as long as there were bindings (or id's) on this
632
+ // instance causing it to get a dataHost.
633
+ let templateHost = template.__dataHost;
634
+ return templateHost && templateHost._methodHost || templateHost;
635
+ }
636
+
637
+ /* eslint-disable valid-jsdoc */
638
+ /**
639
+ * @suppress {missingProperties} class.prototype is not defined for some reason
640
+ */
641
+ function createTemplatizerClass(template, templateInfo, options) {
642
+ /**
643
+ * @constructor
644
+ * @extends {TemplateInstanceBase}
645
+ */
646
+ let templatizerBase = options.mutableData ?
647
+ MutableTemplateInstanceBase : TemplateInstanceBase;
648
+
649
+ // Affordance for global mixins onto TemplatizeInstance
650
+ if (templatize.mixin) {
651
+ templatizerBase = templatize.mixin(templatizerBase);
652
+ }
653
+
654
+ /**
655
+ * Anonymous class created by the templatize
656
+ * @constructor
657
+ * @private
658
+ */
659
+ let klass = class extends templatizerBase { };
660
+ /** @override */
661
+ klass.prototype.__templatizeOptions = options;
662
+ klass.prototype._bindTemplate(template);
663
+ addNotifyEffects(klass, template, templateInfo, options);
664
+ return klass;
665
+ }
666
+
667
+ /**
668
+ * Adds propagate effects from the template to the template instance for
669
+ * properties that the host binds to the template using the `_host_` prefix.
670
+ *
671
+ * @suppress {missingProperties} class.prototype is not defined for some reason
672
+ */
673
+ function addPropagateEffects(target, templateInfo, options, methodHost) {
674
+ let userForwardHostProp = options.forwardHostProp;
675
+ if (userForwardHostProp && templateInfo.hasHostProps) {
676
+ // Under the `removeNestedTemplates` optimization, a custom element like
677
+ // `dom-if` or `dom-repeat` can itself be treated as the "template"; this
678
+ // flag is used to switch between upgrading a `<template>` to be a property
679
+ // effects client vs. adding the effects directly to the custom element
680
+ const isTemplate = target.localName == 'template';
681
+ // Provide data API and property effects on memoized template class
682
+ let klass = templateInfo.templatizeTemplateClass;
683
+ if (!klass) {
684
+ if (isTemplate) {
685
+ /**
686
+ * @constructor
687
+ * @extends {DataTemplate}
688
+ */
689
+ let templatizedBase =
690
+ options.mutableData ? MutableDataTemplate : DataTemplate;
691
+
692
+ // NOTE: due to https://github.com/google/closure-compiler/issues/2928,
693
+ // combining the next two lines into one assignment causes a spurious
694
+ // type error.
695
+ /** @private */
696
+ class TemplatizedTemplate extends templatizedBase {}
697
+ klass = templateInfo.templatizeTemplateClass = TemplatizedTemplate;
698
+ } else {
699
+ /**
700
+ * @constructor
701
+ * @extends {PolymerElement}
702
+ */
703
+ const templatizedBase = target.constructor;
704
+
705
+ // Create a cached subclass of the base custom element class onto which
706
+ // to put the template-specific propagate effects
707
+ // NOTE: due to https://github.com/google/closure-compiler/issues/2928,
708
+ // combining the next two lines into one assignment causes a spurious
709
+ // type error.
710
+ /** @private */
711
+ class TemplatizedTemplateExtension extends templatizedBase {}
712
+ klass = templateInfo.templatizeTemplateClass =
713
+ TemplatizedTemplateExtension;
714
+ }
715
+ // Add template - >instances effects
716
+ // and host <- template effects
717
+ let hostProps = templateInfo.hostProps;
718
+ for (let prop in hostProps) {
719
+ klass.prototype._addPropertyEffect('_host_' + prop,
720
+ klass.prototype.PROPERTY_EFFECT_TYPES.PROPAGATE,
721
+ {fn: createForwardHostPropEffect(prop, userForwardHostProp)});
722
+ klass.prototype._createNotifyingProperty('_host_' + prop);
723
+ }
724
+ if (legacyWarnings && methodHost) {
725
+ warnOnUndeclaredProperties(templateInfo, options, methodHost);
726
+ }
727
+ }
728
+ // Mix any pre-bound data into __data; no need to flush this to
729
+ // instances since they pull from the template at instance-time
730
+ if (target.__dataProto) {
731
+ // Note, generally `__dataProto` could be chained, but it's guaranteed
732
+ // to not be since this is a vanilla template we just added effects to
733
+ Object.assign(target.__data, target.__dataProto);
734
+ }
735
+ if (isTemplate) {
736
+ upgradeTemplate(target, klass);
737
+ // Clear any pending data for performance
738
+ target.__dataTemp = {};
739
+ target.__dataPending = null;
740
+ target.__dataOld = null;
741
+ target._enableProperties();
742
+ } else {
743
+ // Swizzle the cached subclass prototype onto the custom element
744
+ Object.setPrototypeOf(target, klass.prototype);
745
+ // Check for any pre-bound instance host properties, and do the
746
+ // instance property delete/assign dance for those (directly into data;
747
+ // not need to go through accessor since they are pulled at instance time)
748
+ const hostProps = templateInfo.hostProps;
749
+ for (let prop in hostProps) {
750
+ prop = '_host_' + prop;
751
+ if (prop in target) {
752
+ const val = target[prop];
753
+ delete target[prop];
754
+ target.__data[prop] = val;
755
+ }
756
+ }
757
+ }
758
+ }
759
+ }
760
+ /* eslint-enable valid-jsdoc */
761
+
762
+ function createForwardHostPropEffect(hostProp, userForwardHostProp) {
763
+ return function forwardHostProp(template, prop, props) {
764
+ userForwardHostProp.call(template.__templatizeOwner,
765
+ prop.substring('_host_'.length), props[prop]);
766
+ };
767
+ }
768
+
769
+ function addNotifyEffects(klass, template, templateInfo, options) {
770
+ let hostProps = templateInfo.hostProps || {};
771
+ for (let iprop in options.instanceProps) {
772
+ delete hostProps[iprop];
773
+ let userNotifyInstanceProp = options.notifyInstanceProp;
774
+ if (userNotifyInstanceProp) {
775
+ klass.prototype._addPropertyEffect(iprop,
776
+ klass.prototype.PROPERTY_EFFECT_TYPES.NOTIFY,
777
+ {fn: createNotifyInstancePropEffect(iprop, userNotifyInstanceProp)});
778
+ }
779
+ }
780
+ if (options.forwardHostProp && template.__dataHost) {
781
+ for (let hprop in hostProps) {
782
+ // As we're iterating hostProps in this function, note whether
783
+ // there were any, for an optimization in addPropagateEffects
784
+ if (!templateInfo.hasHostProps) {
785
+ templateInfo.hasHostProps = true;
786
+ }
787
+ klass.prototype._addPropertyEffect(hprop,
788
+ klass.prototype.PROPERTY_EFFECT_TYPES.NOTIFY,
789
+ {fn: createNotifyHostPropEffect()});
790
+ }
791
+ }
792
+ }
793
+
794
+ function createNotifyInstancePropEffect(instProp, userNotifyInstanceProp) {
795
+ return function notifyInstanceProp(inst, prop, props) {
796
+ userNotifyInstanceProp.call(inst.__templatizeOwner,
797
+ inst, prop, props[prop]);
798
+ };
799
+ }
800
+
801
+ function createNotifyHostPropEffect() {
802
+ return function notifyHostProp(inst, prop, props) {
803
+ inst.__dataHost._setPendingPropertyOrPath('_host_' + prop, props[prop], true, true);
804
+ };
805
+ }
806
+
807
+
808
+ /**
809
+ * Returns an anonymous `PropertyEffects` class bound to the
810
+ * `<template>` provided. Instancing the class will result in the
811
+ * template being stamped into a document fragment stored as the instance's
812
+ * `root` property, after which it can be appended to the DOM.
813
+ *
814
+ * Templates may utilize all Polymer data-binding features as well as
815
+ * declarative event listeners. Event listeners and inline computing
816
+ * functions in the template will be called on the host of the template.
817
+ *
818
+ * The constructor returned takes a single argument dictionary of initial
819
+ * property values to propagate into template bindings. Additionally
820
+ * host properties can be forwarded in, and instance properties can be
821
+ * notified out by providing optional callbacks in the `options` dictionary.
822
+ *
823
+ * Valid configuration in `options` are as follows:
824
+ *
825
+ * - `forwardHostProp(property, value)`: Called when a property referenced
826
+ * in the template changed on the template's host. As this library does
827
+ * not retain references to templates instanced by the user, it is the
828
+ * templatize owner's responsibility to forward host property changes into
829
+ * user-stamped instances. The `instance.forwardHostProp(property, value)`
830
+ * method on the generated class should be called to forward host
831
+ * properties into the template to prevent unnecessary property-changed
832
+ * notifications. Any properties referenced in the template that are not
833
+ * defined in `instanceProps` will be notified up to the template's host
834
+ * automatically.
835
+ * - `instanceProps`: Dictionary of property names that will be added
836
+ * to the instance by the templatize owner. These properties shadow any
837
+ * host properties, and changes within the template to these properties
838
+ * will result in `notifyInstanceProp` being called.
839
+ * - `mutableData`: When `true`, the generated class will skip strict
840
+ * dirty-checking for objects and arrays (always consider them to be
841
+ * "dirty").
842
+ * - `notifyInstanceProp(instance, property, value)`: Called when
843
+ * an instance property changes. Users may choose to call `notifyPath`
844
+ * on e.g. the owner to notify the change.
845
+ * - `parentModel`: When `true`, events handled by declarative event listeners
846
+ * (`on-event="handler"`) will be decorated with a `model` property pointing
847
+ * to the template instance that stamped it. It will also be returned
848
+ * from `instance.parentModel` in cases where template instance nesting
849
+ * causes an inner model to shadow an outer model.
850
+ *
851
+ * All callbacks are called bound to the `owner`. Any context
852
+ * needed for the callbacks (such as references to `instances` stamped)
853
+ * should be stored on the `owner` such that they can be retrieved via
854
+ * `this`.
855
+ *
856
+ * When `options.forwardHostProp` is declared as an option, any properties
857
+ * referenced in the template will be automatically forwarded from the host of
858
+ * the `<template>` to instances, with the exception of any properties listed in
859
+ * the `options.instanceProps` object. `instanceProps` are assumed to be
860
+ * managed by the owner of the instances, either passed into the constructor
861
+ * or set after the fact. Note, any properties passed into the constructor will
862
+ * always be set to the instance (regardless of whether they would normally
863
+ * be forwarded from the host).
864
+ *
865
+ * Note that `templatize()` can be run only once for a given `<template>`.
866
+ * Further calls will result in an error. Also, there is a special
867
+ * behavior if the template was duplicated through a mechanism such as
868
+ * `<dom-repeat>` or `<test-fixture>`. In this case, all calls to
869
+ * `templatize()` return the same class for all duplicates of a template.
870
+ * The class returned from `templatize()` is generated only once using
871
+ * the `options` from the first call. This means that any `options`
872
+ * provided to subsequent calls will be ignored. Therefore, it is very
873
+ * important not to close over any variables inside the callbacks. Also,
874
+ * arrow functions must be avoided because they bind the outer `this`.
875
+ * Inside the callbacks, any contextual information can be accessed
876
+ * through `this`, which points to the `owner`.
877
+ *
878
+ * @param {!HTMLTemplateElement} template Template to templatize
879
+ * @param {Polymer_PropertyEffects=} owner Owner of the template instances;
880
+ * any optional callbacks will be bound to this owner.
881
+ * @param {Object=} options Options dictionary (see summary for details)
882
+ * @return {function(new:TemplateInstanceBase, Object=)} Generated class bound
883
+ * to the template provided
884
+ * @suppress {invalidCasts}
885
+ */
886
+ function templatize(template, owner, options) {
887
+ // Under strictTemplatePolicy, the templatized element must be owned
888
+ // by a (trusted) Polymer element, indicated by existence of _methodHost;
889
+ // e.g. for dom-if & dom-repeat in main document, _methodHost is null
890
+ if (strictTemplatePolicy && !findMethodHost(template)) {
891
+ throw new Error('strictTemplatePolicy: template owner not trusted');
892
+ }
893
+ options = /** @type {!TemplatizeOptions} */(options || {});
894
+ if (template.__templatizeOwner) {
895
+ throw new Error('A <template> can only be templatized once');
896
+ }
897
+ template.__templatizeOwner = owner;
898
+ const ctor = owner ? owner.constructor : TemplateInstanceBase;
899
+ let templateInfo = ctor._parseTemplate(template);
900
+ // Get memoized base class for the prototypical template, which
901
+ // includes property effects for binding template & forwarding
902
+ /**
903
+ * @constructor
904
+ * @extends {TemplateInstanceBase}
905
+ */
906
+ let baseClass = templateInfo.templatizeInstanceClass;
907
+ if (!baseClass) {
908
+ baseClass = createTemplatizerClass(template, templateInfo, options);
909
+ templateInfo.templatizeInstanceClass = baseClass;
910
+ }
911
+ const methodHost = findMethodHost(template);
912
+ // Host property forwarding must be installed onto template instance
913
+ addPropagateEffects(template, templateInfo, options, methodHost);
914
+ // Subclass base class and add reference for this specific template
915
+ /** @private */
916
+ let klass = class TemplateInstance extends baseClass {};
917
+ /** @override */
918
+ klass.prototype._methodHost = methodHost;
919
+ /** @override */
920
+ klass.prototype.__dataHost = /** @type {!DataTemplate} */ (template);
921
+ /** @override */
922
+ klass.prototype.__templatizeOwner = /** @type {!Object} */ (owner);
923
+ /** @override */
924
+ klass.prototype.__hostProps = templateInfo.hostProps;
925
+ klass = /** @type {function(new:TemplateInstanceBase)} */(klass); //eslint-disable-line no-self-assign
926
+ return klass;
927
+ }
928
+
929
+ function warnOnUndeclaredProperties(templateInfo, options, methodHost) {
930
+ const declaredProps = methodHost.constructor._properties;
931
+ const {propertyEffects} = templateInfo;
932
+ const {instanceProps} = options;
933
+ for (let prop in propertyEffects) {
934
+ // Ensure properties with template effects are declared on the outermost
935
+ // host (`methodHost`), unless they are instance props or static functions
936
+ if (!declaredProps[prop] && !(instanceProps && instanceProps[prop])) {
937
+ const effects = propertyEffects[prop];
938
+ for (let i=0; i<effects.length; i++) {
939
+ const {part} = effects[i].info;
940
+ if (!(part.signature && part.signature.static)) {
941
+ console.warn(`Property '${prop}' used in template but not ` +
942
+ `declared in 'properties'; attribute will not be observed.`);
943
+ break;
944
+ }
945
+ }
946
+ }
947
+ }
948
+ }
949
+
950
+ /**
951
+ * Returns the template "model" associated with a given element, which
952
+ * serves as the binding scope for the template instance the element is
953
+ * contained in. A template model is an instance of
954
+ * `TemplateInstanceBase`, and should be used to manipulate data
955
+ * associated with this template instance.
956
+ *
957
+ * Example:
958
+ *
959
+ * let model = modelForElement(el);
960
+ * if (model.index < 10) {
961
+ * model.set('item.checked', true);
962
+ * }
963
+ *
964
+ * @param {HTMLElement} template The model will be returned for
965
+ * elements stamped from this template (accepts either an HTMLTemplateElement)
966
+ * or a `<dom-if>`/`<dom-repeat>` element when using `removeNestedTemplates`
967
+ * optimization.
968
+ * @param {Node=} node Node for which to return a template model.
969
+ * @return {TemplateInstanceBase} Template instance representing the
970
+ * binding scope for the element
971
+ */
972
+ function modelForElement(template, node) {
973
+ let model;
974
+ while (node) {
975
+ // An element with a __templatizeInstance marks the top boundary
976
+ // of a scope; walk up until we find one, and then ensure that
977
+ // its __dataHost matches `this`, meaning this dom-repeat stamped it
978
+ if ((model = node.__dataHost ? node : node.__templatizeInstance)) {
979
+ // Found an element stamped by another template; keep walking up
980
+ // from its __dataHost
981
+ if (model.__dataHost != template) {
982
+ node = model.__dataHost;
983
+ } else {
984
+ return model;
985
+ }
986
+ } else {
987
+ // Still in a template scope, keep going up until
988
+ // a __templatizeInstance is found
989
+ node = wrap(node).parentNode;
990
+ }
991
+ }
992
+ return null;
993
+ }
994
+
995
+ /**
996
+ * @license
997
+ * Copyright (c) 2021 - 2022 Vaadin Ltd.
998
+ * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
999
+ */
1000
+
1001
+ const instances = [];
1002
+
1003
+ /**
1004
+ * A controller for trapping focus within a DOM node.
1005
+ */
1006
+ class FocusTrapController {
1007
+ /**
1008
+ * @param {HTMLElement} host
1009
+ */
1010
+ constructor(host) {
1011
+ /**
1012
+ * The controller host element.
1013
+ *
1014
+ * @type {HTMLElement}
1015
+ */
1016
+ this.host = host;
1017
+
1018
+ /**
1019
+ * A node for trapping focus in.
1020
+ *
1021
+ * @type {HTMLElement | null}
1022
+ * @private
1023
+ */
1024
+ this.__trapNode = null;
1025
+
1026
+ this.__onKeyDown = this.__onKeyDown.bind(this);
1027
+ }
1028
+
1029
+ hostConnected() {
1030
+ document.addEventListener('keydown', this.__onKeyDown);
1031
+ }
1032
+
1033
+ hostDisconnected() {
1034
+ document.removeEventListener('keydown', this.__onKeyDown);
1035
+ }
1036
+
1037
+ /**
1038
+ * Activates a focus trap for a DOM node that will prevent focus from escaping the node.
1039
+ * The trap can be deactivated with the `.releaseFocus()` method.
1040
+ *
1041
+ * If focus is initially outside the trap, the method will move focus inside,
1042
+ * on the first focusable element of the trap in the tab order.
1043
+ * The first focusable element can be the trap node itself if it is focusable
1044
+ * and comes first in the tab order.
1045
+ *
1046
+ * If there are no focusable elements, the method will throw an exception
1047
+ * and the trap will not be set.
1048
+ *
1049
+ * @param {HTMLElement} trapNode
1050
+ */
1051
+ trapFocus(trapNode) {
1052
+ this.__trapNode = trapNode;
1053
+
1054
+ if (this.__focusableElements.length === 0) {
1055
+ this.__trapNode = null;
1056
+ throw new Error('The trap node should have at least one focusable descendant or be focusable itself.');
1057
+ }
1058
+
1059
+ instances.push(this);
1060
+
1061
+ if (this.__focusedElementIndex === -1) {
1062
+ this.__focusableElements[0].focus();
1063
+ }
1064
+ }
1065
+
1066
+ /**
1067
+ * Deactivates the focus trap set with the `.trapFocus()` method
1068
+ * so that it becomes possible to tab outside the trap node.
1069
+ */
1070
+ releaseFocus() {
1071
+ this.__trapNode = null;
1072
+
1073
+ instances.pop();
1074
+ }
1075
+
1076
+ /**
1077
+ * A `keydown` event handler that manages tabbing navigation when the trap is enabled.
1078
+ *
1079
+ * - Moves focus to the next focusable element of the trap on `Tab` press.
1080
+ * When no next element to focus, the method moves focus to the first focusable element.
1081
+ * - Moves focus to the prev focusable element of the trap on `Shift+Tab` press.
1082
+ * When no prev element to focus, the method moves focus to the last focusable element.
1083
+ *
1084
+ * @param {KeyboardEvent} event
1085
+ * @private
1086
+ */
1087
+ __onKeyDown(event) {
1088
+ if (!this.__trapNode) {
1089
+ return;
1090
+ }
1091
+
1092
+ // Only handle events for the last instance
1093
+ if (this !== Array.from(instances).pop()) {
1094
+ return;
1095
+ }
1096
+
1097
+ if (event.key === 'Tab') {
1098
+ event.preventDefault();
1099
+
1100
+ const backward = event.shiftKey;
1101
+ this.__focusNextElement(backward);
1102
+ }
1103
+ }
1104
+
1105
+ /**
1106
+ * - Moves focus to the next focusable element if `backward === false`.
1107
+ * When no next element to focus, the method moves focus to the first focusable element.
1108
+ * - Moves focus to the prev focusable element if `backward === true`.
1109
+ * When no prev element to focus the method moves focus to the last focusable element.
1110
+ *
1111
+ * If no focusable elements, the method returns immediately.
1112
+ *
1113
+ * @param {boolean} backward
1114
+ * @private
1115
+ */
1116
+ __focusNextElement(backward = false) {
1117
+ const focusableElements = this.__focusableElements;
1118
+ const step = backward ? -1 : 1;
1119
+ const currentIndex = this.__focusedElementIndex;
1120
+ const nextIndex = (focusableElements.length + currentIndex + step) % focusableElements.length;
1121
+ const element = focusableElements[nextIndex];
1122
+ element.focus();
1123
+ if (element.localName === 'input') {
1124
+ element.select();
1125
+ }
1126
+ }
1127
+
1128
+ /**
1129
+ * An array of tab-ordered focusable elements inside the trap node.
1130
+ *
1131
+ * @return {HTMLElement[]}
1132
+ * @private
1133
+ */
1134
+ get __focusableElements() {
1135
+ return getFocusableElements(this.__trapNode);
1136
+ }
1137
+
1138
+ /**
1139
+ * The index of the element inside the trap node that currently has focus.
1140
+ *
1141
+ * @return {HTMLElement | undefined}
1142
+ * @private
1143
+ */
1144
+ get __focusedElementIndex() {
1145
+ const focusableElements = this.__focusableElements;
1146
+ return focusableElements.indexOf(focusableElements.filter(isElementFocused).pop());
1147
+ }
1148
+ }
1149
+
1150
+ /**
1151
+ * @license
1152
+ * Copyright (c) 2017 - 2022 Vaadin Ltd.
1153
+ * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
1154
+ */
1155
+
1156
+ /**
1157
+ *
1158
+ * `<vaadin-overlay>` is a Web Component for creating overlays. The content of the overlay
1159
+ * can be populated in two ways: imperatively by using renderer callback function and
1160
+ * declaratively by using Polymer's Templates.
1161
+ *
1162
+ * ### Rendering
1163
+ *
1164
+ * By default, the overlay uses the content provided by using the renderer callback function.
1165
+ *
1166
+ * The renderer function provides `root`, `owner`, `model` arguments when applicable.
1167
+ * Generate DOM content by using `model` object properties if needed, append it to the `root`
1168
+ * element and control the state of the host element by accessing `owner`. Before generating new
1169
+ * content, users are able to check if there is already content in `root` for reusing it.
1170
+ *
1171
+ * ```html
1172
+ * <vaadin-overlay id="overlay"></vaadin-overlay>
1173
+ * ```
1174
+ * ```js
1175
+ * const overlay = document.querySelector('#overlay');
1176
+ * overlay.renderer = function(root) {
1177
+ * root.textContent = "Overlay content";
1178
+ * };
1179
+ * ```
1180
+ *
1181
+ * Renderer is called on the opening of the overlay and each time the related model is updated.
1182
+ * DOM generated during the renderer call can be reused
1183
+ * in the next renderer call and will be provided with the `root` argument.
1184
+ * On first call it will be empty.
1185
+ *
1186
+ * **NOTE:** when the renderer property is defined, the `<template>` content is not used.
1187
+ *
1188
+ * ### Templating
1189
+ *
1190
+ * Alternatively, the content can be provided with Polymer Template.
1191
+ * Overlay finds the first child template and uses that in case renderer callback function
1192
+ * is not provided. You can also set a custom template using the `template` property.
1193
+ *
1194
+ * After the content from the template is stamped, the `content` property
1195
+ * points to the content container.
1196
+ *
1197
+ * The overlay provides `forwardHostProp` when calling
1198
+ * `Polymer.Templatize.templatize` for the template, so that the bindings
1199
+ * from the parent scope propagate to the content.
1200
+ *
1201
+ * ### Styling
1202
+ *
1203
+ * To style the overlay content, use styles in the parent scope:
1204
+ *
1205
+ * - If the overlay is used in a component, then the component styles
1206
+ * apply the overlay content.
1207
+ * - If the overlay is used in the global DOM scope, then global styles
1208
+ * apply to the overlay content.
1209
+ *
1210
+ * See examples for styling the overlay content in the live demos.
1211
+ *
1212
+ * The following Shadow DOM parts are available for styling the overlay component itself:
1213
+ *
1214
+ * Part name | Description
1215
+ * -----------|---------------------------------------------------------|
1216
+ * `backdrop` | Backdrop of the overlay
1217
+ * `overlay` | Container for position/sizing/alignment of the content
1218
+ * `content` | Content of the overlay
1219
+ *
1220
+ * The following state attributes are available for styling:
1221
+ *
1222
+ * Attribute | Description | Part
1223
+ * ---|---|---
1224
+ * `opening` | Applied just after the overlay is attached to the DOM. You can apply a CSS @keyframe animation for this state. | `:host`
1225
+ * `closing` | Applied just before the overlay is detached from the DOM. You can apply a CSS @keyframe animation for this state. | `:host`
1226
+ *
1227
+ * The following custom CSS properties are available for styling:
1228
+ *
1229
+ * Custom CSS property | Description | Default value
1230
+ * ---|---|---
1231
+ * `--vaadin-overlay-viewport-bottom` | Bottom offset of the visible viewport area | `0` or detected offset
1232
+ *
1233
+ * See [Styling Components](https://vaadin.com/docs/latest/styling/custom-theme/styling-components) documentation.
1234
+ *
1235
+ * @fires {CustomEvent} opened-changed - Fired when the `opened` property changes.
1236
+ * @fires {CustomEvent} vaadin-overlay-open - Fired after the overlay is opened.
1237
+ * @fires {CustomEvent} vaadin-overlay-close - Fired before the overlay will be closed. If canceled the closing of the overlay is canceled as well.
1238
+ * @fires {CustomEvent} vaadin-overlay-closing - Fired when the overlay will be closed.
1239
+ * @fires {CustomEvent} vaadin-overlay-outside-click - Fired before the overlay will be closed on outside click. If canceled the closing of the overlay is canceled as well.
1240
+ * @fires {CustomEvent} vaadin-overlay-escape-press - Fired before the overlay will be closed on ESC button press. If canceled the closing of the overlay is canceled as well.
1241
+ *
1242
+ * @extends HTMLElement
1243
+ * @mixes ThemableMixin
1244
+ * @mixes DirMixin
1245
+ * @mixes ControllerMixin
1246
+ */
1247
+ class Overlay extends ThemableMixin(DirMixin(ControllerMixin(PolymerElement))) {
1248
+ static get template() {
1249
+ return html`
1250
+ <style>
1251
+ :host {
1252
+ z-index: 200;
1253
+ position: fixed;
1254
+
1255
+ /* Despite of what the names say, <vaadin-overlay> is just a container
1256
+ for position/sizing/alignment. The actual overlay is the overlay part. */
1257
+
1258
+ /* Default position constraints: the entire viewport. Note: themes can
1259
+ override this to introduce gaps between the overlay and the viewport. */
1260
+ top: 0;
1261
+ right: 0;
1262
+ bottom: var(--vaadin-overlay-viewport-bottom);
1263
+ left: 0;
1264
+
1265
+ /* Use flexbox alignment for the overlay part. */
1266
+ display: flex;
1267
+ flex-direction: column; /* makes dropdowns sizing easier */
1268
+ /* Align to center by default. */
1269
+ align-items: center;
1270
+ justify-content: center;
1271
+
1272
+ /* Allow centering when max-width/max-height applies. */
1273
+ margin: auto;
1274
+
1275
+ /* The host is not clickable, only the overlay part is. */
1276
+ pointer-events: none;
1277
+
1278
+ /* Remove tap highlight on touch devices. */
1279
+ -webkit-tap-highlight-color: transparent;
1280
+
1281
+ /* CSS API for host */
1282
+ --vaadin-overlay-viewport-bottom: 0;
1283
+ }
1284
+
1285
+ :host([hidden]),
1286
+ :host(:not([opened]):not([closing])) {
1287
+ display: none !important;
1288
+ }
1289
+
1290
+ [part='overlay'] {
1291
+ -webkit-overflow-scrolling: touch;
1292
+ overflow: auto;
1293
+ pointer-events: auto;
1294
+
1295
+ /* Prevent overflowing the host in MSIE 11 */
1296
+ max-width: 100%;
1297
+ box-sizing: border-box;
1298
+
1299
+ -webkit-tap-highlight-color: initial; /* reenable tap highlight inside */
1300
+ }
1301
+
1302
+ [part='backdrop'] {
1303
+ z-index: -1;
1304
+ content: '';
1305
+ background: rgba(0, 0, 0, 0.5);
1306
+ position: fixed;
1307
+ top: 0;
1308
+ left: 0;
1309
+ bottom: 0;
1310
+ right: 0;
1311
+ pointer-events: auto;
1312
+ }
1313
+ </style>
1314
+
1315
+ <div id="backdrop" part="backdrop" hidden$="[[!withBackdrop]]"></div>
1316
+ <div part="overlay" id="overlay" tabindex="0">
1317
+ <div part="content" id="content">
1318
+ <slot></slot>
1319
+ </div>
1320
+ </div>
1321
+ `;
1322
+ }
1323
+
1324
+ static get is() {
1325
+ return 'vaadin-overlay';
1326
+ }
1327
+
1328
+ static get properties() {
1329
+ return {
1330
+ /**
1331
+ * When true, the overlay is visible and attached to body.
1332
+ */
1333
+ opened: {
1334
+ type: Boolean,
1335
+ notify: true,
1336
+ observer: '_openedChanged',
1337
+ reflectToAttribute: true,
1338
+ },
1339
+
1340
+ /**
1341
+ * Owner element passed with renderer function
1342
+ * @type {HTMLElement}
1343
+ */
1344
+ owner: Element,
1345
+
1346
+ /**
1347
+ * Custom function for rendering the content of the overlay.
1348
+ * Receives three arguments:
1349
+ *
1350
+ * - `root` The root container DOM element. Append your content to it.
1351
+ * - `owner` The host element of the renderer function.
1352
+ * - `model` The object with the properties related with rendering.
1353
+ * @type {OverlayRenderer | null | undefined}
1354
+ */
1355
+ renderer: Function,
1356
+
1357
+ /**
1358
+ * The template of the overlay content.
1359
+ * @type {HTMLTemplateElement | null | undefined}
1360
+ */
1361
+ template: {
1362
+ type: Object,
1363
+ notify: true,
1364
+ },
1365
+
1366
+ /**
1367
+ * References the content container after the template is stamped.
1368
+ * @type {!HTMLElement | undefined}
1369
+ */
1370
+ content: {
1371
+ type: Object,
1372
+ notify: true,
1373
+ },
1374
+
1375
+ /**
1376
+ * When true the overlay has backdrop on top of content when opened.
1377
+ * @type {boolean}
1378
+ */
1379
+ withBackdrop: {
1380
+ type: Boolean,
1381
+ value: false,
1382
+ reflectToAttribute: true,
1383
+ },
1384
+
1385
+ /**
1386
+ * Object with properties that is passed to `renderer` function
1387
+ */
1388
+ model: Object,
1389
+
1390
+ /**
1391
+ * When true the overlay won't disable the main content, showing
1392
+ * it doesn’t change the functionality of the user interface.
1393
+ * @type {boolean}
1394
+ */
1395
+ modeless: {
1396
+ type: Boolean,
1397
+ value: false,
1398
+ reflectToAttribute: true,
1399
+ observer: '_modelessChanged',
1400
+ },
1401
+
1402
+ /**
1403
+ * When set to true, the overlay is hidden. This also closes the overlay
1404
+ * immediately in case there is a closing animation in progress.
1405
+ * @type {boolean}
1406
+ */
1407
+ hidden: {
1408
+ type: Boolean,
1409
+ reflectToAttribute: true,
1410
+ observer: '_hiddenChanged',
1411
+ },
1412
+
1413
+ /**
1414
+ * When true move focus to the first focusable element in the overlay,
1415
+ * or to the overlay if there are no focusable elements.
1416
+ * @type {boolean}
1417
+ */
1418
+ focusTrap: {
1419
+ type: Boolean,
1420
+ value: false,
1421
+ },
1422
+
1423
+ /**
1424
+ * Set to true to enable restoring of focus when overlay is closed.
1425
+ * @type {boolean}
1426
+ */
1427
+ restoreFocusOnClose: {
1428
+ type: Boolean,
1429
+ value: false,
1430
+ },
1431
+
1432
+ /**
1433
+ * Set to specify the element which should be focused on overlay close,
1434
+ * if `restoreFocusOnClose` is set to true.
1435
+ * @type {HTMLElement}
1436
+ */
1437
+ restoreFocusNode: {
1438
+ type: HTMLElement,
1439
+ },
1440
+
1441
+ /** @private */
1442
+ _mouseDownInside: {
1443
+ type: Boolean,
1444
+ },
1445
+
1446
+ /** @private */
1447
+ _mouseUpInside: {
1448
+ type: Boolean,
1449
+ },
1450
+
1451
+ /** @private */
1452
+ _instance: {
1453
+ type: Object,
1454
+ },
1455
+
1456
+ /** @private */
1457
+ _originalContentPart: Object,
1458
+
1459
+ /** @private */
1460
+ _contentNodes: Array,
1461
+
1462
+ /** @private */
1463
+ _oldOwner: Element,
1464
+
1465
+ /** @private */
1466
+ _oldModel: Object,
1467
+
1468
+ /** @private */
1469
+ _oldTemplate: Object,
1470
+
1471
+ /** @private */
1472
+ _oldRenderer: Object,
1473
+
1474
+ /** @private */
1475
+ _oldOpened: Boolean,
1476
+ };
1477
+ }
1478
+
1479
+ static get observers() {
1480
+ return ['_templateOrRendererChanged(template, renderer, owner, model, opened)'];
1481
+ }
1482
+
1483
+ constructor() {
1484
+ super();
1485
+ this._boundMouseDownListener = this._mouseDownListener.bind(this);
1486
+ this._boundMouseUpListener = this._mouseUpListener.bind(this);
1487
+ this._boundOutsideClickListener = this._outsideClickListener.bind(this);
1488
+ this._boundKeydownListener = this._keydownListener.bind(this);
1489
+
1490
+ this._observer = new FlattenedNodesObserver(this, (info) => {
1491
+ this._setTemplateFromNodes(info.addedNodes);
1492
+ });
1493
+
1494
+ // Listener for preventing closing of the paper-dialog and all components extending `iron-overlay-behavior`.
1495
+ this._boundIronOverlayCanceledListener = this._ironOverlayCanceled.bind(this);
1496
+
1497
+ /* c8 ignore next 3 */
1498
+ if (isIOS) {
1499
+ this._boundIosResizeListener = () => this._detectIosNavbar();
1500
+ }
1501
+
1502
+ this.__focusTrapController = new FocusTrapController(this);
1503
+ }
1504
+
1505
+ /** @protected */
1506
+ ready() {
1507
+ super.ready();
1508
+
1509
+ this._observer.flush();
1510
+
1511
+ // Need to add dummy click listeners to this and the backdrop or else
1512
+ // the document click event listener (_outsideClickListener) may never
1513
+ // get invoked on iOS Safari (reproducible in <vaadin-dialog>
1514
+ // and <vaadin-context-menu>).
1515
+ this.addEventListener('click', () => {});
1516
+ this.$.backdrop.addEventListener('click', () => {});
1517
+
1518
+ this.addController(this.__focusTrapController);
1519
+ }
1520
+
1521
+ /** @private */
1522
+ _detectIosNavbar() {
1523
+ /* c8 ignore next 15 */
1524
+ if (!this.opened) {
1525
+ return;
1526
+ }
1527
+
1528
+ const innerHeight = window.innerHeight;
1529
+ const innerWidth = window.innerWidth;
1530
+
1531
+ const landscape = innerWidth > innerHeight;
1532
+
1533
+ const clientHeight = document.documentElement.clientHeight;
1534
+
1535
+ if (landscape && clientHeight > innerHeight) {
1536
+ this.style.setProperty('--vaadin-overlay-viewport-bottom', `${clientHeight - innerHeight}px`);
1537
+ } else {
1538
+ this.style.setProperty('--vaadin-overlay-viewport-bottom', '0');
1539
+ }
1540
+ }
1541
+
1542
+ /**
1543
+ * @param {!Array<!Element>} nodes
1544
+ * @protected
1545
+ */
1546
+ _setTemplateFromNodes(nodes) {
1547
+ this.template = nodes.find((node) => node.localName && node.localName === 'template') || this.template;
1548
+ }
1549
+
1550
+ /**
1551
+ * @param {Event=} sourceEvent
1552
+ * @event vaadin-overlay-close
1553
+ * fired before the `vaadin-overlay` will be closed. If canceled the closing of the overlay is canceled as well.
1554
+ */
1555
+ close(sourceEvent) {
1556
+ const evt = new CustomEvent('vaadin-overlay-close', {
1557
+ bubbles: true,
1558
+ cancelable: true,
1559
+ detail: { sourceEvent },
1560
+ });
1561
+ this.dispatchEvent(evt);
1562
+ if (!evt.defaultPrevented) {
1563
+ this.opened = false;
1564
+ }
1565
+ }
1566
+
1567
+ /** @protected */
1568
+ connectedCallback() {
1569
+ super.connectedCallback();
1570
+
1571
+ /* c8 ignore next 3 */
1572
+ if (this._boundIosResizeListener) {
1573
+ this._detectIosNavbar();
1574
+ window.addEventListener('resize', this._boundIosResizeListener);
1575
+ }
1576
+ }
1577
+
1578
+ /** @protected */
1579
+ disconnectedCallback() {
1580
+ super.disconnectedCallback();
1581
+
1582
+ /* c8 ignore next 3 */
1583
+ if (this._boundIosResizeListener) {
1584
+ window.removeEventListener('resize', this._boundIosResizeListener);
1585
+ }
1586
+ }
1587
+
1588
+ /**
1589
+ * Requests an update for the content of the overlay.
1590
+ * While performing the update, it invokes the renderer passed in the `renderer` property.
1591
+ *
1592
+ * It is not guaranteed that the update happens immediately (synchronously) after it is requested.
1593
+ */
1594
+ requestContentUpdate() {
1595
+ if (this.renderer) {
1596
+ this.renderer.call(this.owner, this.content, this.owner, this.model);
1597
+ }
1598
+ }
1599
+
1600
+ /** @private */
1601
+ _ironOverlayCanceled(event) {
1602
+ event.preventDefault();
1603
+ }
1604
+
1605
+ /** @private */
1606
+ _mouseDownListener(event) {
1607
+ this._mouseDownInside = event.composedPath().indexOf(this.$.overlay) >= 0;
1608
+ }
1609
+
1610
+ /** @private */
1611
+ _mouseUpListener(event) {
1612
+ this._mouseUpInside = event.composedPath().indexOf(this.$.overlay) >= 0;
1613
+ }
1614
+
1615
+ /**
1616
+ * We need to listen on 'click' / 'tap' event and capture it and close the overlay before
1617
+ * propagating the event to the listener in the button. Otherwise, if the clicked button would call
1618
+ * open(), this would happen: https://www.youtube.com/watch?v=Z86V_ICUCD4
1619
+ *
1620
+ * @event vaadin-overlay-outside-click
1621
+ * fired before the `vaadin-overlay` will be closed on outside click. If canceled the closing of the overlay is canceled as well.
1622
+ *
1623
+ * @private
1624
+ */
1625
+ _outsideClickListener(event) {
1626
+ if (event.composedPath().includes(this.$.overlay) || this._mouseDownInside || this._mouseUpInside) {
1627
+ this._mouseDownInside = false;
1628
+ this._mouseUpInside = false;
1629
+ return;
1630
+ }
1631
+ if (!this._last) {
1632
+ return;
1633
+ }
1634
+
1635
+ const evt = new CustomEvent('vaadin-overlay-outside-click', {
1636
+ bubbles: true,
1637
+ cancelable: true,
1638
+ detail: { sourceEvent: event },
1639
+ });
1640
+ this.dispatchEvent(evt);
1641
+
1642
+ if (this.opened && !evt.defaultPrevented) {
1643
+ this.close(event);
1644
+ }
1645
+ }
1646
+
1647
+ /**
1648
+ * @event vaadin-overlay-escape-press
1649
+ * fired before the `vaadin-overlay` will be closed on ESC button press. If canceled the closing of the overlay is canceled as well.
1650
+ *
1651
+ * @private
1652
+ */
1653
+ _keydownListener(event) {
1654
+ if (!this._last) {
1655
+ return;
1656
+ }
1657
+
1658
+ // Only close modeless overlay on Esc press when it contains focus
1659
+ if (this.modeless && !event.composedPath().includes(this.$.overlay)) {
1660
+ return;
1661
+ }
1662
+
1663
+ if (event.key === 'Escape') {
1664
+ const evt = new CustomEvent('vaadin-overlay-escape-press', {
1665
+ bubbles: true,
1666
+ cancelable: true,
1667
+ detail: { sourceEvent: event },
1668
+ });
1669
+ this.dispatchEvent(evt);
1670
+
1671
+ if (this.opened && !evt.defaultPrevented) {
1672
+ this.close(event);
1673
+ }
1674
+ }
1675
+ }
1676
+
1677
+ /** @protected */
1678
+ _ensureTemplatized() {
1679
+ this._setTemplateFromNodes(Array.from(this.children));
1680
+ }
1681
+
1682
+ /**
1683
+ * @event vaadin-overlay-open
1684
+ * fired after the `vaadin-overlay` is opened.
1685
+ *
1686
+ * @private
1687
+ */
1688
+ _openedChanged(opened, wasOpened) {
1689
+ if (!this._instance) {
1690
+ this._ensureTemplatized();
1691
+ }
1692
+
1693
+ if (opened) {
1694
+ // Store focused node.
1695
+ this.__restoreFocusNode = this._getActiveElement();
1696
+ this._animatedOpening();
1697
+
1698
+ afterNextRender(this, () => {
1699
+ if (this.focusTrap) {
1700
+ this.__focusTrapController.trapFocus(this.$.overlay);
1701
+ }
1702
+
1703
+ const evt = new CustomEvent('vaadin-overlay-open', { bubbles: true });
1704
+ this.dispatchEvent(evt);
1705
+ });
1706
+
1707
+ document.addEventListener('keydown', this._boundKeydownListener);
1708
+
1709
+ if (!this.modeless) {
1710
+ this._addGlobalListeners();
1711
+ }
1712
+ } else if (wasOpened) {
1713
+ if (this.focusTrap) {
1714
+ this.__focusTrapController.releaseFocus();
1715
+ }
1716
+
1717
+ this._animatedClosing();
1718
+
1719
+ document.removeEventListener('keydown', this._boundKeydownListener);
1720
+
1721
+ if (!this.modeless) {
1722
+ this._removeGlobalListeners();
1723
+ }
1724
+ }
1725
+ }
1726
+
1727
+ /** @private */
1728
+ _hiddenChanged(hidden) {
1729
+ if (hidden && this.hasAttribute('closing')) {
1730
+ this._flushAnimation('closing');
1731
+ }
1732
+ }
1733
+
1734
+ /**
1735
+ * @return {boolean}
1736
+ * @protected
1737
+ */
1738
+ _shouldAnimate() {
1739
+ const name = getComputedStyle(this).getPropertyValue('animation-name');
1740
+ const hidden = getComputedStyle(this).getPropertyValue('display') === 'none';
1741
+ return !hidden && name && name !== 'none';
1742
+ }
1743
+
1744
+ /**
1745
+ * @param {string} type
1746
+ * @param {Function} callback
1747
+ * @protected
1748
+ */
1749
+ _enqueueAnimation(type, callback) {
1750
+ const handler = `__${type}Handler`;
1751
+ const listener = (event) => {
1752
+ if (event && event.target !== this) {
1753
+ return;
1754
+ }
1755
+ callback();
1756
+ this.removeEventListener('animationend', listener);
1757
+ delete this[handler];
1758
+ };
1759
+ this[handler] = listener;
1760
+ this.addEventListener('animationend', listener);
1761
+ }
1762
+
1763
+ /**
1764
+ * @param {string} type
1765
+ * @protected
1766
+ */
1767
+ _flushAnimation(type) {
1768
+ const handler = `__${type}Handler`;
1769
+ if (typeof this[handler] === 'function') {
1770
+ this[handler]();
1771
+ }
1772
+ }
1773
+
1774
+ /** @protected */
1775
+ _animatedOpening() {
1776
+ if (this.parentNode === document.body && this.hasAttribute('closing')) {
1777
+ this._flushAnimation('closing');
1778
+ }
1779
+ this._attachOverlay();
1780
+ if (!this.modeless) {
1781
+ this._enterModalState();
1782
+ }
1783
+ this.setAttribute('opening', '');
1784
+
1785
+ if (this._shouldAnimate()) {
1786
+ this._enqueueAnimation('opening', () => {
1787
+ this._finishOpening();
1788
+ });
1789
+ } else {
1790
+ this._finishOpening();
1791
+ }
1792
+ }
1793
+
1794
+ /** @protected */
1795
+ _attachOverlay() {
1796
+ this._placeholder = document.createComment('vaadin-overlay-placeholder');
1797
+ this.parentNode.insertBefore(this._placeholder, this);
1798
+ document.body.appendChild(this);
1799
+ this.bringToFront();
1800
+ }
1801
+
1802
+ /** @protected */
1803
+ _finishOpening() {
1804
+ document.addEventListener('iron-overlay-canceled', this._boundIronOverlayCanceledListener);
1805
+ this.removeAttribute('opening');
1806
+ }
1807
+
1808
+ /** @protected */
1809
+ _finishClosing() {
1810
+ document.removeEventListener('iron-overlay-canceled', this._boundIronOverlayCanceledListener);
1811
+ this._detachOverlay();
1812
+ this.$.overlay.style.removeProperty('pointer-events');
1813
+ this.removeAttribute('closing');
1814
+ }
1815
+
1816
+ /**
1817
+ * @event vaadin-overlay-closing
1818
+ * Fired when the overlay will be closed.
1819
+ *
1820
+ * @protected
1821
+ */
1822
+ _animatedClosing() {
1823
+ if (this.hasAttribute('opening')) {
1824
+ this._flushAnimation('opening');
1825
+ }
1826
+ if (this._placeholder) {
1827
+ this._exitModalState();
1828
+
1829
+ // Use this.restoreFocusNode if specified, otherwise fallback to the node
1830
+ // which was focused before opening the overlay.
1831
+ const restoreFocusNode = this.restoreFocusNode || this.__restoreFocusNode;
1832
+
1833
+ if (this.restoreFocusOnClose && restoreFocusNode) {
1834
+ // If the activeElement is `<body>` or inside the overlay,
1835
+ // we are allowed to restore the focus. In all the other
1836
+ // cases focus might have been moved elsewhere by another
1837
+ // component or by the user interaction (e.g. click on a
1838
+ // button outside the overlay).
1839
+ const activeElement = this._getActiveElement();
1840
+
1841
+ if (activeElement === document.body || this._deepContains(activeElement)) {
1842
+ // Focusing the restoreFocusNode doesn't always work synchronously on Firefox and Safari
1843
+ // (e.g. combo-box overlay close on outside click).
1844
+ setTimeout(() => restoreFocusNode.focus());
1845
+ }
1846
+ this.__restoreFocusNode = null;
1847
+ }
1848
+
1849
+ this.setAttribute('closing', '');
1850
+ this.dispatchEvent(new CustomEvent('vaadin-overlay-closing'));
1851
+
1852
+ if (this._shouldAnimate()) {
1853
+ this._enqueueAnimation('closing', () => {
1854
+ this._finishClosing();
1855
+ });
1856
+ } else {
1857
+ this._finishClosing();
1858
+ }
1859
+ }
1860
+ }
1861
+
1862
+ /** @protected */
1863
+ _detachOverlay() {
1864
+ this._placeholder.parentNode.insertBefore(this, this._placeholder);
1865
+ this._placeholder.parentNode.removeChild(this._placeholder);
1866
+ }
1867
+
1868
+ /**
1869
+ * Returns all attached overlays in visual stacking order.
1870
+ * @private
1871
+ */
1872
+ static get __attachedInstances() {
1873
+ return Array.from(document.body.children)
1874
+ .filter((el) => el instanceof Overlay && !el.hasAttribute('closing'))
1875
+ .sort((a, b) => a.__zIndex - b.__zIndex || 0);
1876
+ }
1877
+
1878
+ /**
1879
+ * Returns true if this is the last one in the opened overlays stack
1880
+ * @return {boolean}
1881
+ * @protected
1882
+ */
1883
+ get _last() {
1884
+ return this === Overlay.__attachedInstances.pop();
1885
+ }
1886
+
1887
+ /** @private */
1888
+ _modelessChanged(modeless) {
1889
+ if (!modeless) {
1890
+ if (this.opened) {
1891
+ this._addGlobalListeners();
1892
+ this._enterModalState();
1893
+ }
1894
+ } else {
1895
+ this._removeGlobalListeners();
1896
+ this._exitModalState();
1897
+ }
1898
+ }
1899
+
1900
+ /** @protected */
1901
+ _addGlobalListeners() {
1902
+ document.addEventListener('mousedown', this._boundMouseDownListener);
1903
+ document.addEventListener('mouseup', this._boundMouseUpListener);
1904
+ // Firefox leaks click to document on contextmenu even if prevented
1905
+ // https://bugzilla.mozilla.org/show_bug.cgi?id=990614
1906
+ document.documentElement.addEventListener('click', this._boundOutsideClickListener, true);
1907
+ }
1908
+
1909
+ /** @protected */
1910
+ _enterModalState() {
1911
+ if (document.body.style.pointerEvents !== 'none') {
1912
+ // Set body pointer-events to 'none' to disable mouse interactions with
1913
+ // other document nodes.
1914
+ this._previousDocumentPointerEvents = document.body.style.pointerEvents;
1915
+ document.body.style.pointerEvents = 'none';
1916
+ }
1917
+
1918
+ // Disable pointer events in other attached overlays
1919
+ Overlay.__attachedInstances.forEach((el) => {
1920
+ if (el !== this) {
1921
+ el.shadowRoot.querySelector('[part="overlay"]').style.pointerEvents = 'none';
1922
+ }
1923
+ });
1924
+ }
1925
+
1926
+ /** @protected */
1927
+ _removeGlobalListeners() {
1928
+ document.removeEventListener('mousedown', this._boundMouseDownListener);
1929
+ document.removeEventListener('mouseup', this._boundMouseUpListener);
1930
+ document.documentElement.removeEventListener('click', this._boundOutsideClickListener, true);
1931
+ }
1932
+
1933
+ /** @protected */
1934
+ _exitModalState() {
1935
+ if (this._previousDocumentPointerEvents !== undefined) {
1936
+ // Restore body pointer-events
1937
+ document.body.style.pointerEvents = this._previousDocumentPointerEvents;
1938
+ delete this._previousDocumentPointerEvents;
1939
+ }
1940
+
1941
+ // Restore pointer events in the previous overlay(s)
1942
+ const instances = Overlay.__attachedInstances;
1943
+ let el;
1944
+ // Use instances.pop() to ensure the reverse order
1945
+ while ((el = instances.pop())) {
1946
+ if (el === this) {
1947
+ // Skip the current instance
1948
+ continue;
1949
+ }
1950
+ el.shadowRoot.querySelector('[part="overlay"]').style.removeProperty('pointer-events');
1951
+ if (!el.modeless) {
1952
+ // Stop after the last modal
1953
+ break;
1954
+ }
1955
+ }
1956
+ }
1957
+
1958
+ /** @protected */
1959
+ _removeOldContent() {
1960
+ if (!this.content || !this._contentNodes) {
1961
+ return;
1962
+ }
1963
+
1964
+ this._observer.disconnect();
1965
+
1966
+ this._contentNodes.forEach((node) => {
1967
+ if (node.parentNode === this.content) {
1968
+ this.content.removeChild(node);
1969
+ }
1970
+ });
1971
+
1972
+ if (this._originalContentPart) {
1973
+ // Restore the original <div part="content">
1974
+ this.$.content.parentNode.replaceChild(this._originalContentPart, this.$.content);
1975
+ this.$.content = this._originalContentPart;
1976
+ this._originalContentPart = undefined;
1977
+ }
1978
+
1979
+ this._observer.connect();
1980
+
1981
+ this._contentNodes = undefined;
1982
+ this.content = undefined;
1983
+ }
1984
+
1985
+ /**
1986
+ * @param {!HTMLTemplateElement} template
1987
+ * @protected
1988
+ */
1989
+ _stampOverlayTemplate(template) {
1990
+ this._removeOldContent();
1991
+
1992
+ if (!template._Templatizer) {
1993
+ template._Templatizer = templatize(template, this, {
1994
+ forwardHostProp(prop, value) {
1995
+ if (this._instance) {
1996
+ this._instance.forwardHostProp(prop, value);
1997
+ }
1998
+ },
1999
+ });
2000
+ }
2001
+
2002
+ this._instance = new template._Templatizer({});
2003
+ this._contentNodes = Array.from(this._instance.root.childNodes);
2004
+
2005
+ const templateRoot = template._templateRoot || (template._templateRoot = template.getRootNode());
2006
+
2007
+ if (templateRoot !== document) {
2008
+ if (!this.$.content.shadowRoot) {
2009
+ this.$.content.attachShadow({ mode: 'open' });
2010
+ }
2011
+
2012
+ let scopeCssText = Array.from(templateRoot.querySelectorAll('style')).reduce(
2013
+ (result, style) => result + style.textContent,
2014
+ '',
2015
+ );
2016
+
2017
+ // The overlay root’s :host styles should not apply inside the overlay
2018
+ scopeCssText = scopeCssText.replace(/:host/g, ':host-nomatch');
2019
+
2020
+ if (scopeCssText) {
2021
+ // Append a style to the content shadowRoot
2022
+ const style = document.createElement('style');
2023
+ style.textContent = scopeCssText;
2024
+ this.$.content.shadowRoot.appendChild(style);
2025
+ this._contentNodes.unshift(style);
2026
+ }
2027
+
2028
+ this.$.content.shadowRoot.appendChild(this._instance.root);
2029
+ this.content = this.$.content.shadowRoot;
2030
+ } else {
2031
+ this.appendChild(this._instance.root);
2032
+ this.content = this;
2033
+ }
2034
+ }
2035
+
2036
+ /** @private */
2037
+ _removeNewRendererOrTemplate(template, oldTemplate, renderer, oldRenderer) {
2038
+ if (template !== oldTemplate) {
2039
+ this.template = undefined;
2040
+ } else if (renderer !== oldRenderer) {
2041
+ this.renderer = undefined;
2042
+ }
2043
+ }
2044
+
2045
+ /** @private */
2046
+ // eslint-disable-next-line max-params
2047
+ _templateOrRendererChanged(template, renderer, owner, model, opened) {
2048
+ if (template && renderer) {
2049
+ this._removeNewRendererOrTemplate(template, this._oldTemplate, renderer, this._oldRenderer);
2050
+ throw new Error('You should only use either a renderer or a template for overlay content');
2051
+ }
2052
+
2053
+ const ownerOrModelChanged = this._oldOwner !== owner || this._oldModel !== model;
2054
+ this._oldModel = model;
2055
+ this._oldOwner = owner;
2056
+
2057
+ const templateChanged = this._oldTemplate !== template;
2058
+ this._oldTemplate = template;
2059
+
2060
+ const rendererChanged = this._oldRenderer !== renderer;
2061
+ this._oldRenderer = renderer;
2062
+
2063
+ const openedChanged = this._oldOpened !== opened;
2064
+ this._oldOpened = opened;
2065
+
2066
+ if (rendererChanged) {
2067
+ this.content = this;
2068
+ this.content.innerHTML = '';
2069
+ // Whenever a Lit-based renderer is used, it assigns a Lit part to the node it was rendered into.
2070
+ // When clearing the rendered content, this part needs to be manually disposed of.
2071
+ // Otherwise, using a Lit-based renderer on the same node will throw an exception or render nothing afterward.
2072
+ delete this.content._$litPart$;
2073
+ }
2074
+
2075
+ if (template && templateChanged) {
2076
+ this._stampOverlayTemplate(template);
2077
+ } else if (renderer && (rendererChanged || openedChanged || ownerOrModelChanged)) {
2078
+ if (opened) {
2079
+ this.requestContentUpdate();
2080
+ }
2081
+ }
2082
+ }
2083
+
2084
+ /**
2085
+ * @return {!Element}
2086
+ * @protected
2087
+ */
2088
+ _getActiveElement() {
2089
+ // Document.activeElement can be null
2090
+ // https://developer.mozilla.org/en-US/docs/Web/API/Document/activeElement
2091
+ let active = document.activeElement || document.body;
2092
+ while (active.shadowRoot && active.shadowRoot.activeElement) {
2093
+ active = active.shadowRoot.activeElement;
2094
+ }
2095
+ return active;
2096
+ }
2097
+
2098
+ /**
2099
+ * @param {!Node} node
2100
+ * @return {boolean}
2101
+ * @protected
2102
+ */
2103
+ _deepContains(node) {
2104
+ if (this.contains(node)) {
2105
+ return true;
2106
+ }
2107
+ let n = node;
2108
+ const doc = node.ownerDocument;
2109
+ // Walk from node to `this` or `document`
2110
+ while (n && n !== doc && n !== this) {
2111
+ n = n.parentNode || n.host;
2112
+ }
2113
+ return n === this;
2114
+ }
2115
+
2116
+ /**
2117
+ * Brings the overlay as visually the frontmost one
2118
+ */
2119
+ bringToFront() {
2120
+ let zIndex = '';
2121
+ const frontmost = Overlay.__attachedInstances.filter((o) => o !== this).pop();
2122
+ if (frontmost) {
2123
+ const frontmostZIndex = frontmost.__zIndex;
2124
+ zIndex = frontmostZIndex + 1;
2125
+ }
2126
+ this.style.zIndex = zIndex;
2127
+ this.__zIndex = zIndex || parseFloat(getComputedStyle(this).zIndex);
2128
+ }
2129
+ }
2130
+
2131
+ customElements.define(Overlay.is, Overlay);
2132
+
2133
+ /**
2134
+ * @license
2135
+ * Copyright (c) 2017 - 2022 Vaadin Ltd.
2136
+ * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
2137
+ */
2138
+
2139
+ const menuOverlayCore = i`
2140
+ :host([opening]),
2141
+ :host([closing]) {
2142
+ animation: 0.14s lumo-overlay-dummy-animation;
2143
+ }
2144
+
2145
+ [part='overlay'] {
2146
+ will-change: opacity, transform;
2147
+ }
2148
+
2149
+ :host([opening]) [part='overlay'] {
2150
+ animation: 0.1s lumo-menu-overlay-enter ease-out both;
2151
+ }
2152
+
2153
+ @keyframes lumo-menu-overlay-enter {
2154
+ 0% {
2155
+ opacity: 0;
2156
+ transform: translateY(-4px);
2157
+ }
2158
+ }
2159
+
2160
+ :host([closing]) [part='overlay'] {
2161
+ animation: 0.1s lumo-menu-overlay-exit both;
2162
+ }
2163
+
2164
+ @keyframes lumo-menu-overlay-exit {
2165
+ 100% {
2166
+ opacity: 0;
2167
+ }
2168
+ }
2169
+ `;
2170
+
2171
+ registerStyles('', menuOverlayCore, { moduleId: 'lumo-menu-overlay-core' });
2172
+
2173
+ const menuOverlayExt = i`
2174
+ /* Small viewport (bottom sheet) styles */
2175
+ /* Use direct media queries instead of the state attributes ([phone] and [fullscreen]) provided by the elements */
2176
+ @media (max-width: 420px), (max-height: 420px) {
2177
+ :host {
2178
+ top: 0 !important;
2179
+ right: 0 !important;
2180
+ bottom: var(--vaadin-overlay-viewport-bottom, 0) !important;
2181
+ left: 0 !important;
2182
+ align-items: stretch !important;
2183
+ justify-content: flex-end !important;
2184
+ }
2185
+
2186
+ [part='overlay'] {
2187
+ max-height: 50vh;
2188
+ width: 100vw;
2189
+ border-radius: 0;
2190
+ box-shadow: var(--lumo-box-shadow-xl);
2191
+ }
2192
+
2193
+ /* The content part scrolls instead of the overlay part, because of the gradient fade-out */
2194
+ [part='content'] {
2195
+ padding: 30px var(--lumo-space-m);
2196
+ max-height: inherit;
2197
+ box-sizing: border-box;
2198
+ -webkit-overflow-scrolling: touch;
2199
+ overflow: auto;
2200
+ -webkit-mask-image: linear-gradient(transparent, #000 40px, #000 calc(100% - 40px), transparent);
2201
+ mask-image: linear-gradient(transparent, #000 40px, #000 calc(100% - 40px), transparent);
2202
+ }
2203
+
2204
+ [part='backdrop'] {
2205
+ display: block;
2206
+ }
2207
+
2208
+ /* Animations */
2209
+
2210
+ :host([opening]) [part='overlay'] {
2211
+ animation: 0.2s lumo-mobile-menu-overlay-enter cubic-bezier(0.215, 0.61, 0.355, 1) both;
2212
+ }
2213
+
2214
+ :host([closing]),
2215
+ :host([closing]) [part='backdrop'] {
2216
+ animation-delay: 0.14s;
2217
+ }
2218
+
2219
+ :host([closing]) [part='overlay'] {
2220
+ animation: 0.14s 0.14s lumo-mobile-menu-overlay-exit cubic-bezier(0.55, 0.055, 0.675, 0.19) both;
2221
+ }
2222
+ }
2223
+
2224
+ @keyframes lumo-mobile-menu-overlay-enter {
2225
+ 0% {
2226
+ transform: translateY(150%);
2227
+ }
2228
+ }
2229
+
2230
+ @keyframes lumo-mobile-menu-overlay-exit {
2231
+ 100% {
2232
+ transform: translateY(150%);
2233
+ }
2234
+ }
2235
+ `;
2236
+
2237
+ const menuOverlay = [overlay, menuOverlayCore, menuOverlayExt];
2238
+
2239
+ registerStyles('', menuOverlay, { moduleId: 'lumo-menu-overlay' });
2240
+
2241
+ /**
2242
+ * @license
2243
+ * Copyright (c) 2017 - 2022 Vaadin Ltd.
2244
+ * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
2245
+ */
2246
+
2247
+ const PROP_NAMES_VERTICAL = {
2248
+ start: 'top',
2249
+ end: 'bottom',
2250
+ };
2251
+
2252
+ const PROP_NAMES_HORIZONTAL = {
2253
+ start: 'left',
2254
+ end: 'right',
2255
+ };
2256
+
2257
+ const targetResizeObserver = new ResizeObserver((entries) => {
2258
+ setTimeout(() => {
2259
+ entries.forEach((entry) => {
2260
+ if (entry.target.__overlay) {
2261
+ entry.target.__overlay._updatePosition();
2262
+ }
2263
+ });
2264
+ });
2265
+ });
2266
+
2267
+ /**
2268
+ * @polymerMixin
2269
+ */
2270
+ const PositionMixin = (superClass) =>
2271
+ class PositionMixin extends superClass {
2272
+ static get properties() {
2273
+ return {
2274
+ /**
2275
+ * The element next to which this overlay should be aligned.
2276
+ * The position of the overlay relative to the positionTarget can be adjusted
2277
+ * with properties `horizontalAlign`, `verticalAlign`, `noHorizontalOverlap`
2278
+ * and `noVerticalOverlap`.
2279
+ */
2280
+ positionTarget: {
2281
+ type: Object,
2282
+ value: null,
2283
+ },
2284
+
2285
+ /**
2286
+ * When `positionTarget` is set, this property defines whether to align the overlay's
2287
+ * left or right side to the target element by default.
2288
+ * Possible values are `start` and `end`.
2289
+ * RTL is taken into account when interpreting the value.
2290
+ * The overlay is automatically flipped to the opposite side when it doesn't fit into
2291
+ * the default side defined by this property.
2292
+ *
2293
+ * @attr {start|end} horizontal-align
2294
+ */
2295
+ horizontalAlign: {
2296
+ type: String,
2297
+ value: 'start',
2298
+ },
2299
+
2300
+ /**
2301
+ * When `positionTarget` is set, this property defines whether to align the overlay's
2302
+ * top or bottom side to the target element by default.
2303
+ * Possible values are `top` and `bottom`.
2304
+ * The overlay is automatically flipped to the opposite side when it doesn't fit into
2305
+ * the default side defined by this property.
2306
+ *
2307
+ * @attr {top|bottom} vertical-align
2308
+ */
2309
+ verticalAlign: {
2310
+ type: String,
2311
+ value: 'top',
2312
+ },
2313
+
2314
+ /**
2315
+ * When `positionTarget` is set, this property defines whether the overlay should overlap
2316
+ * the target element in the x-axis, or be positioned right next to it.
2317
+ *
2318
+ * @attr {boolean} no-horizontal-overlap
2319
+ */
2320
+ noHorizontalOverlap: {
2321
+ type: Boolean,
2322
+ value: false,
2323
+ },
2324
+
2325
+ /**
2326
+ * When `positionTarget` is set, this property defines whether the overlay should overlap
2327
+ * the target element in the y-axis, or be positioned right above/below it.
2328
+ *
2329
+ * @attr {boolean} no-vertical-overlap
2330
+ */
2331
+ noVerticalOverlap: {
2332
+ type: Boolean,
2333
+ value: false,
2334
+ },
2335
+
2336
+ /**
2337
+ * If the overlay content has no intrinsic height, this property can be used to set
2338
+ * the minimum vertical space (in pixels) required by the overlay. Setting a value to
2339
+ * the property effectively disables the content measurement in favor of using this
2340
+ * fixed value for determining the open direction.
2341
+ *
2342
+ * @attr {number} required-vertical-space
2343
+ */
2344
+ requiredVerticalSpace: {
2345
+ type: Number,
2346
+ value: 0,
2347
+ },
2348
+ };
2349
+ }
2350
+
2351
+ static get observers() {
2352
+ return [
2353
+ '__positionSettingsChanged(horizontalAlign, verticalAlign, noHorizontalOverlap, noVerticalOverlap, requiredVerticalSpace)',
2354
+ '__overlayOpenedChanged(opened, positionTarget)',
2355
+ ];
2356
+ }
2357
+
2358
+ constructor() {
2359
+ super();
2360
+
2361
+ this.__onScroll = this.__onScroll.bind(this);
2362
+ this._updatePosition = this._updatePosition.bind(this);
2363
+ }
2364
+
2365
+ /** @protected */
2366
+ connectedCallback() {
2367
+ super.connectedCallback();
2368
+
2369
+ if (this.opened) {
2370
+ this.__addUpdatePositionEventListeners();
2371
+ }
2372
+ }
2373
+
2374
+ /** @protected */
2375
+ disconnectedCallback() {
2376
+ super.disconnectedCallback();
2377
+ this.__removeUpdatePositionEventListeners();
2378
+ }
2379
+
2380
+ /** @private */
2381
+ __addUpdatePositionEventListeners() {
2382
+ window.addEventListener('resize', this._updatePosition);
2383
+
2384
+ this.__positionTargetAncestorRootNodes = getAncestorRootNodes(this.positionTarget);
2385
+ this.__positionTargetAncestorRootNodes.forEach((node) => {
2386
+ node.addEventListener('scroll', this.__onScroll, true);
2387
+ });
2388
+ }
2389
+
2390
+ /** @private */
2391
+ __removeUpdatePositionEventListeners() {
2392
+ window.removeEventListener('resize', this._updatePosition);
2393
+
2394
+ if (this.__positionTargetAncestorRootNodes) {
2395
+ this.__positionTargetAncestorRootNodes.forEach((node) => {
2396
+ node.removeEventListener('scroll', this.__onScroll, true);
2397
+ });
2398
+ this.__positionTargetAncestorRootNodes = null;
2399
+ }
2400
+ }
2401
+
2402
+ /** @private */
2403
+ __overlayOpenedChanged(opened, positionTarget) {
2404
+ this.__removeUpdatePositionEventListeners();
2405
+
2406
+ if (positionTarget) {
2407
+ positionTarget.__overlay = null;
2408
+ targetResizeObserver.unobserve(positionTarget);
2409
+
2410
+ if (opened) {
2411
+ this.__addUpdatePositionEventListeners();
2412
+ positionTarget.__overlay = this;
2413
+ targetResizeObserver.observe(positionTarget);
2414
+ }
2415
+ }
2416
+
2417
+ if (opened) {
2418
+ const computedStyle = getComputedStyle(this);
2419
+ if (!this.__margins) {
2420
+ this.__margins = {};
2421
+ ['top', 'bottom', 'left', 'right'].forEach((propName) => {
2422
+ this.__margins[propName] = parseInt(computedStyle[propName], 10);
2423
+ });
2424
+ }
2425
+ this.setAttribute('dir', computedStyle.direction);
2426
+
2427
+ this._updatePosition();
2428
+ // Schedule another position update (to cover virtual keyboard opening for example)
2429
+ requestAnimationFrame(() => this._updatePosition());
2430
+ }
2431
+ }
2432
+
2433
+ get __isRTL() {
2434
+ return this.getAttribute('dir') === 'rtl';
2435
+ }
2436
+
2437
+ __positionSettingsChanged() {
2438
+ this._updatePosition();
2439
+ }
2440
+
2441
+ /** @private */
2442
+ __onScroll(e) {
2443
+ // If the scroll event occurred inside the overlay, ignore it.
2444
+ if (!this.contains(e.target)) {
2445
+ this._updatePosition();
2446
+ }
2447
+ }
2448
+
2449
+ _updatePosition() {
2450
+ if (!this.positionTarget || !this.opened) {
2451
+ return;
2452
+ }
2453
+
2454
+ const targetRect = this.positionTarget.getBoundingClientRect();
2455
+
2456
+ // Detect the desired alignment and update the layout accordingly
2457
+ const shouldAlignStartVertically = this.__shouldAlignStartVertically(targetRect);
2458
+ this.style.justifyContent = shouldAlignStartVertically ? 'flex-start' : 'flex-end';
2459
+
2460
+ const shouldAlignStartHorizontally = this.__shouldAlignStartHorizontally(targetRect, this.__isRTL);
2461
+ const flexStart =
2462
+ (!this.__isRTL && shouldAlignStartHorizontally) || (this.__isRTL && !shouldAlignStartHorizontally);
2463
+ this.style.alignItems = flexStart ? 'flex-start' : 'flex-end';
2464
+
2465
+ // Get the overlay rect after possible overlay alignment changes
2466
+ const overlayRect = this.getBoundingClientRect();
2467
+
2468
+ // Obtain vertical positioning properties
2469
+ const verticalProps = this.__calculatePositionInOneDimension(
2470
+ targetRect,
2471
+ overlayRect,
2472
+ this.noVerticalOverlap,
2473
+ PROP_NAMES_VERTICAL,
2474
+ this,
2475
+ shouldAlignStartVertically,
2476
+ );
2477
+
2478
+ // Obtain horizontal positioning properties
2479
+ const horizontalProps = this.__calculatePositionInOneDimension(
2480
+ targetRect,
2481
+ overlayRect,
2482
+ this.noHorizontalOverlap,
2483
+ PROP_NAMES_HORIZONTAL,
2484
+ this,
2485
+ shouldAlignStartHorizontally,
2486
+ );
2487
+
2488
+ // Apply the positioning properties to the overlay
2489
+ Object.assign(this.style, verticalProps, horizontalProps);
2490
+
2491
+ this.toggleAttribute('bottom-aligned', !shouldAlignStartVertically);
2492
+ this.toggleAttribute('top-aligned', shouldAlignStartVertically);
2493
+
2494
+ this.toggleAttribute('end-aligned', !flexStart);
2495
+ this.toggleAttribute('start-aligned', flexStart);
2496
+ }
2497
+
2498
+ __shouldAlignStartHorizontally(targetRect, rtl) {
2499
+ // Using previous size to fix a case where window resize may cause the overlay to be squeezed
2500
+ // smaller than its current space before the fit-calculations.
2501
+ const contentWidth = Math.max(this.__oldContentWidth || 0, this.$.overlay.offsetWidth);
2502
+ this.__oldContentWidth = this.$.overlay.offsetWidth;
2503
+
2504
+ const viewportWidth = Math.min(window.innerWidth, document.documentElement.clientWidth);
2505
+ const defaultAlignLeft = (!rtl && this.horizontalAlign === 'start') || (rtl && this.horizontalAlign === 'end');
2506
+
2507
+ return this.__shouldAlignStart(
2508
+ targetRect,
2509
+ contentWidth,
2510
+ viewportWidth,
2511
+ this.__margins,
2512
+ defaultAlignLeft,
2513
+ this.noHorizontalOverlap,
2514
+ PROP_NAMES_HORIZONTAL,
2515
+ );
2516
+ }
2517
+
2518
+ __shouldAlignStartVertically(targetRect) {
2519
+ // Using previous size to fix a case where window resize may cause the overlay to be squeezed
2520
+ // smaller than its current space before the fit-calculations.
2521
+ const contentHeight =
2522
+ this.requiredVerticalSpace || Math.max(this.__oldContentHeight || 0, this.$.overlay.offsetHeight);
2523
+ this.__oldContentHeight = this.$.overlay.offsetHeight;
2524
+
2525
+ const viewportHeight = Math.min(window.innerHeight, document.documentElement.clientHeight);
2526
+ const defaultAlignTop = this.verticalAlign === 'top';
2527
+
2528
+ return this.__shouldAlignStart(
2529
+ targetRect,
2530
+ contentHeight,
2531
+ viewportHeight,
2532
+ this.__margins,
2533
+ defaultAlignTop,
2534
+ this.noVerticalOverlap,
2535
+ PROP_NAMES_VERTICAL,
2536
+ );
2537
+ }
2538
+
2539
+ // eslint-disable-next-line max-params
2540
+ __shouldAlignStart(targetRect, contentSize, viewportSize, margins, defaultAlignStart, noOverlap, propNames) {
2541
+ const spaceForStartAlignment =
2542
+ viewportSize - targetRect[noOverlap ? propNames.end : propNames.start] - margins[propNames.end];
2543
+ const spaceForEndAlignment = targetRect[noOverlap ? propNames.start : propNames.end] - margins[propNames.start];
2544
+
2545
+ const spaceForDefaultAlignment = defaultAlignStart ? spaceForStartAlignment : spaceForEndAlignment;
2546
+ const spaceForOtherAlignment = defaultAlignStart ? spaceForEndAlignment : spaceForStartAlignment;
2547
+
2548
+ const shouldGoToDefaultSide =
2549
+ spaceForDefaultAlignment > spaceForOtherAlignment || spaceForDefaultAlignment > contentSize;
2550
+
2551
+ return defaultAlignStart === shouldGoToDefaultSide;
2552
+ }
2553
+
2554
+ /**
2555
+ * Returns an adjusted value after resizing the browser window,
2556
+ * to avoid wrong calculations when e.g. previously set `bottom`
2557
+ * CSS property value is larger than the updated viewport height.
2558
+ * See https://github.com/vaadin/web-components/issues/4604
2559
+ */
2560
+ __adjustBottomProperty(cssPropNameToSet, propNames, currentValue) {
2561
+ let adjustedProp;
2562
+
2563
+ if (cssPropNameToSet === propNames.end) {
2564
+ // Adjust horizontally
2565
+ if (propNames.end === PROP_NAMES_VERTICAL.end) {
2566
+ const viewportHeight = Math.min(window.innerHeight, document.documentElement.clientHeight);
2567
+
2568
+ if (currentValue > viewportHeight && this.__oldViewportHeight) {
2569
+ const heightDiff = this.__oldViewportHeight - viewportHeight;
2570
+ adjustedProp = currentValue - heightDiff;
2571
+ }
2572
+
2573
+ this.__oldViewportHeight = viewportHeight;
2574
+ }
2575
+
2576
+ // Adjust vertically
2577
+ if (propNames.end === PROP_NAMES_HORIZONTAL.end) {
2578
+ const viewportWidth = Math.min(window.innerWidth, document.documentElement.clientWidth);
2579
+
2580
+ if (currentValue > viewportWidth && this.__oldViewportWidth) {
2581
+ const widthDiff = this.__oldViewportWidth - viewportWidth;
2582
+ adjustedProp = currentValue - widthDiff;
2583
+ }
2584
+
2585
+ this.__oldViewportWidth = viewportWidth;
2586
+ }
2587
+ }
2588
+
2589
+ return adjustedProp;
2590
+ }
2591
+
2592
+ /**
2593
+ * Returns an object with CSS position properties to set,
2594
+ * e.g. { top: "100px" }
2595
+ */
2596
+ // eslint-disable-next-line max-params
2597
+ __calculatePositionInOneDimension(targetRect, overlayRect, noOverlap, propNames, overlay, shouldAlignStart) {
2598
+ const cssPropNameToSet = shouldAlignStart ? propNames.start : propNames.end;
2599
+ const cssPropNameToClear = shouldAlignStart ? propNames.end : propNames.start;
2600
+
2601
+ const currentValue = parseFloat(overlay.style[cssPropNameToSet] || getComputedStyle(overlay)[cssPropNameToSet]);
2602
+ const adjustedValue = this.__adjustBottomProperty(cssPropNameToSet, propNames, currentValue);
2603
+
2604
+ const diff =
2605
+ overlayRect[shouldAlignStart ? propNames.start : propNames.end] -
2606
+ targetRect[noOverlap === shouldAlignStart ? propNames.end : propNames.start];
2607
+
2608
+ const valueToSet = adjustedValue
2609
+ ? `${adjustedValue}px`
2610
+ : `${currentValue + diff * (shouldAlignStart ? -1 : 1)}px`;
2611
+
2612
+ return {
2613
+ [cssPropNameToSet]: valueToSet,
2614
+ [cssPropNameToClear]: '',
2615
+ };
2616
+ }
2617
+ };
2618
+
2619
+ /**
2620
+ * @license
2621
+ * Copyright (c) 2021 - 2022 Vaadin Ltd.
2622
+ * This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
2623
+ */
2624
+
2625
+ /**
2626
+ * A controller which prevents the virtual keyboard from showing up on mobile devices
2627
+ * when the field's overlay is closed.
2628
+ */
2629
+ class VirtualKeyboardController {
2630
+ /**
2631
+ * @param {{ inputElement?: HTMLElement; opened: boolean } & HTMLElement} host
2632
+ */
2633
+ constructor(host) {
2634
+ this.host = host;
2635
+
2636
+ host.addEventListener('opened-changed', () => {
2637
+ if (!host.opened) {
2638
+ // Prevent opening the virtual keyboard when the input gets re-focused on dropdown close
2639
+ this.__setVirtualKeyboardEnabled(false);
2640
+ }
2641
+ });
2642
+
2643
+ // Re-enable virtual keyboard on blur, so it gets opened when the field is focused again
2644
+ host.addEventListener('blur', () => this.__setVirtualKeyboardEnabled(true));
2645
+
2646
+ // Re-enable the virtual keyboard whenever the field is touched
2647
+ host.addEventListener('touchstart', () => this.__setVirtualKeyboardEnabled(true));
2648
+ }
2649
+
2650
+ /** @private */
2651
+ __setVirtualKeyboardEnabled(value) {
2652
+ if (this.host.inputElement) {
2653
+ this.host.inputElement.inputMode = value ? '' : 'none';
2654
+ }
2655
+ }
2656
+ }
2657
+
2658
+ export { Overlay as O, PositionMixin as P, VirtualKeyboardController as V, OptionalMutableData as a, modelForElement as b, afterNextRender as c, menuOverlayCore as d, menuOverlay as m, overlay as o, templatize as t };