/
vaadin-combo-box-dropdown.html
334 lines (280 loc) · 10.1 KB
/
vaadin-combo-box-dropdown.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
<!--
@license
Copyright (c) 2017 Vaadin Ltd.
This program is available under Apache License Version 2.0, available at https://vaadin.com/license/
-->
<link rel="import" href="../../polymer/polymer-element.html">
<link rel="import" href="../../polymer/lib/mixins/disable-upgrade-mixin.html">
<link rel="import" href="../../vaadin-overlay/src/vaadin-overlay.html">
<link rel="import" href="../../iron-resizable-behavior/iron-resizable-behavior.html">
<dom-module id="vaadin-combo-box-dropdown">
<template>
<style>
:host {
display: block;
}
:host > #overlay {
display: none;
}
</style>
<vaadin-combo-box-overlay
id="overlay"
hidden$="[[hidden]]"
position-target="[[positionTarget]]"
opened="{{opened}}"
template="{{template}}"
style="align-items: stretch; margin: 0;"
theme$="[[theme]]">
<slot></slot>
</vaadin-combo-box-overlay>
</template>
</dom-module>
<dom-module id="vaadin-combo-box-overlay-styles" theme-for="vaadin-combo-box-overlay">
<template>
<style>
:host {
width: var(--vaadin-combo-box-overlay-width, var(--_vaadin-combo-box-overlay-default-width, auto));
}
</style>
</template>
</dom-module>
<script>
(function() {
let memoizedTemplate;
/**
* The overlay element.
*
* ### Styling
*
* See [`<vaadin-overlay>` documentation](https://github.com/vaadin/vaadin-overlay/blob/master/src/vaadin-overlay.html)
* for `<vaadin-combo-box-overlay>` parts.
*
* See [ThemableMixin – how to apply styles for shadow parts](https://github.com/vaadin/vaadin-themable-mixin/wiki)
*
* @memberof Vaadin
* @private
*/
class ComboBoxOverlayElement extends Vaadin.OverlayElement {
static get is() {
return 'vaadin-combo-box-overlay';
}
static get properties() {
return {
/**
* The element to position/align the dropdown by.
*/
positionTarget: {
type: Object
}
};
}
static get template() {
if (!memoizedTemplate) {
memoizedTemplate = super.template.cloneNode(true);
memoizedTemplate.content.querySelector('[part~="overlay"]').removeAttribute('tabindex');
}
return memoizedTemplate;
}
connectedCallback() {
super.connectedCallback();
const dropdown = this.__dataHost;
const comboBoxOverlay = dropdown.getRootNode().host;
const comboBox = comboBoxOverlay && comboBoxOverlay.getRootNode().host;
const hostDir = comboBox && comboBox.getAttribute('dir');
if (hostDir) {
this.setAttribute('dir', hostDir);
}
}
ready() {
super.ready();
const loader = document.createElement('div');
loader.setAttribute('part', 'loader');
const content = this.shadowRoot.querySelector('[part~="content"]');
content.parentNode.insertBefore(loader, content);
}
/**
* Override OverlayElement._outsideClickListener to prevent closing the
* overlay when clicking into the combo box input element
* @param event
* @private
*/
_outsideClickListener(event) {
const eventPath = event.composedPath();
if (eventPath.indexOf(this.positionTarget) < 0 && eventPath.indexOf(this) < 0) {
this.close();
}
}
}
customElements.define(ComboBoxOverlayElement.is, ComboBoxOverlayElement);
/**
* Element for internal use only.
*
* @memberof Vaadin
* @private
*/
class ComboBoxDropdownElement extends Polymer.DisableUpgradeMixin(
Polymer.mixinBehaviors(Polymer.IronResizableBehavior, Polymer.Element)) {
static get is() {
return 'vaadin-combo-box-dropdown';
}
static get properties() {
return {
opened: {
type: Boolean,
observer: '_openedChanged'
},
template: {
type: Object,
notify: true
},
/**
* The element to position/align the dropdown by.
*/
positionTarget: {
type: Object
},
/**
* If `true`, overlay is aligned above the `positionTarget`
*/
alignedAbove: {
type: Boolean,
value: false
},
/**
* Used to propagate the `theme` attribute from the host element.
*/
theme: String
};
}
constructor() {
super();
this._boundSetPosition = this._setPosition.bind(this);
}
connectedCallback() {
super.connectedCallback();
this.addEventListener('iron-resize', this._boundSetPosition);
}
ready() {
super.ready();
// Preventing the default modal behaviour of the overlay on input clicking
this.$.overlay.addEventListener('vaadin-overlay-outside-click', e => {
e.preventDefault();
});
}
disconnectedCallback() {
super.disconnectedCallback();
this.removeEventListener('iron-resize', this._boundSetPosition);
// Making sure the overlay is closed and removed from DOM after detaching the dropdown.
this.opened = false;
}
notifyResize() {
super.notifyResize();
if (this.positionTarget && this.opened) {
this._setPosition();
// Schedule another position update (to cover virtual keyboard opening for example)
requestAnimationFrame(this._setPosition.bind(this));
}
}
/**
* Fired after the `vaadin-combo-box-dropdown` opens.
*
* @event vaadin-combo-box-dropdown-opened
*/
/**
* Fired after the `vaadin-combo-box-dropdown` closes.
*
* @event vaadin-combo-box-dropdown-closed
*/
_openedChanged(opened, oldValue) {
if (!!opened === !!oldValue) {
return;
}
if (opened) {
this.$.overlay.style.position = this._isPositionFixed(this.positionTarget) ? 'fixed' : 'absolute';
this._setPosition();
window.addEventListener('scroll', this._boundSetPosition, true);
this.dispatchEvent(new CustomEvent('vaadin-combo-box-dropdown-opened', {bubbles: true, composed: true}));
} else if (!this.__emptyItems) {
window.removeEventListener('scroll', this._boundSetPosition, true);
this.dispatchEvent(new CustomEvent('vaadin-combo-box-dropdown-closed', {bubbles: true, composed: true}));
}
}
_isPositionFixed(element) {
const offsetParent = this._getOffsetParent(element);
return window.getComputedStyle(element).position === 'fixed' ||
(offsetParent && this._isPositionFixed(offsetParent));
}
_getOffsetParent(element) {
if (element.assignedSlot) {
return element.assignedSlot.parentElement;
} else if (element.parentElement) {
return element.offsetParent;
}
const parent = element.parentNode;
if (parent && parent.nodeType === 11 && parent.host) {
return parent.host; // parent is #shadowRoot
}
}
_verticalOffset(overlayRect, targetRect) {
return this.alignedAbove ? -overlayRect.height : targetRect.height;
}
_shouldAlignAbove(targetRect) {
const spaceBelow = (
window.innerHeight -
targetRect.bottom -
Math.min(document.body.scrollTop, 0)
) / window.innerHeight;
return spaceBelow < 0.30;
}
_getCustomWidth() {
return window.ShadyCSS ?
window.ShadyCSS.getComputedStyleValue(this, '--vaadin-combo-box-overlay-width') :
getComputedStyle(this).getPropertyValue('--vaadin-combo-box-overlay-width');
}
_setOverlayWidth() {
const inputWidth = this.positionTarget.clientWidth + 'px';
const customWidth = this._getCustomWidth();
if (window.ShadyCSS && !window.ShadyCSS.nativeCss) {
window.ShadyCSS.styleSubtree(this.$.overlay, {
'--vaadin-combo-box-overlay-width': customWidth,
'--_vaadin-combo-box-overlay-default-width': inputWidth
});
} else {
this.$.overlay.style.setProperty('--_vaadin-combo-box-overlay-default-width', inputWidth);
if (customWidth === '') {
this.$.overlay.style.removeProperty('--vaadin-combo-box-overlay-width');
} else {
this.$.overlay.style.setProperty('--vaadin-combo-box-overlay-width', customWidth);
}
}
}
_setPosition(e) {
if (this.hidden) {
return;
}
if (e && e.target) {
const target = e.target === document ? document.body : e.target;
const parent = this.$.overlay.parentElement;
if (!(target.contains(this.$.overlay) || target.contains(this.positionTarget)) || parent !== document.body) {
return;
}
}
const targetRect = this.positionTarget.getBoundingClientRect();
this.alignedAbove = this._shouldAlignAbove(targetRect);
const overlayRect = this.$.overlay.getBoundingClientRect();
this._translateX = targetRect.left - overlayRect.left + (this._translateX || 0);
this._translateY = targetRect.top - overlayRect.top + (this._translateY || 0) +
this._verticalOffset(overlayRect, targetRect);
const _devicePixelRatio = window.devicePixelRatio || 1;
this._translateX = Math.round(this._translateX * _devicePixelRatio) / _devicePixelRatio;
this._translateY = Math.round(this._translateY * _devicePixelRatio) / _devicePixelRatio;
this.$.overlay.style.transform = `translate3d(${this._translateX}px, ${this._translateY}px, 0)`;
this.$.overlay.style.justifyContent = this.alignedAbove ? 'flex-end' : 'flex-start';
this._setOverlayWidth();
// TODO: fire only when position actually changes changes
this.dispatchEvent(new CustomEvent('position-changed'));
}
}
customElements.define(ComboBoxDropdownElement.is, ComboBoxDropdownElement);
})();
</script>