forked from vaadin/vaadin-combo-box
-
Notifications
You must be signed in to change notification settings - Fork 0
/
vaadin-combo-box-overlay.html
270 lines (229 loc) · 6.83 KB
/
vaadin-combo-box-overlay.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
<!--
@element vaadin-combo-box-overlay
-->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../iron-list/iron-list.html">
<link rel="import" href="../paper-styles/shadow.html">
<link rel="import" href="vaadin-overlay-behavior.html">
<dom-module id="vaadin-combo-box-overlay">
<style>
:host {
position: absolute;
@apply(--shadow-elevation-2dp);
background: #fff;
border-radius: 0 0 2px 2px;
/**
* Keep the vaadin-combo-box-overlay above paper-dialogs.
* iron-overlay-manager.html: Polymer.IronOverlayManagerClass.prototype._applyOverlayZ
*/
z-index: 200;
overflow: hidden;
}
#scroller {
overflow: auto;
max-height: var(--vaadin-combo-box-overlay-max-height, 65vh);
/* Fixes item background from getting on top of scrollbars on Safari */
transform: translate3d(0,0,0);
}
#selector {
padding-top: 8px;
padding-bottom: 8px;
}
#selector .item {
cursor: pointer;
padding: 13px 16px;
color: var(--primary-text-color);
white-space: nowrap;
overflow: hidden;
text-overflow: ellipsis;
}
#selector:not([touch-device]) .item:hover,
#selector .item[focused] {
background: #eee;
}
#selector .item[selected] {
color: var(--default-primary-color);
}
</style>
<template>
<div id="scroller" scroller="[[_getScroller()]]">
<iron-list
id="selector"
touch-device$="[[touchDevice]]"
role="listbox"
data-selection$="[[_ariaActiveIndex]]"
on-touchend="_preventDefault"
selected-item="{{_selectedItem}}"
items="[[_items]]"
selection-enabled>
<template>
<div class="item"
id$="it[[index]]"
selected$="[[selected]]"
role$="[[_getAriaRole(index)]]"
aria-selected$="[[_getAriaSelected(_focusedIndex,index)]]"
focused$="[[_isItemFocused(_focusedIndex,index)]]">
[[getItemLabel(item)]]
</div>
</template>
</iron-list>
</div>
</template>
</dom-module>
<script>
Polymer({
is: 'vaadin-combo-box-overlay',
behaviors: [
vaadin.elements.combobox.OverlayBehavior
],
properties: {
/**
* True if the device supports touch events.
*/
touchDevice: {
type: Boolean,
reflectToAttribute: true,
value: function() {
try {
document.createEvent('TouchEvent');
return true;
} catch (e) {
return false;
}
}
},
_selectedItem: {
type: String,
notify: true
},
_items: {
type: Object
},
_focusedIndex: {
type: Number,
notify: true,
value: -1,
observer: '_focusedIndexChanged'
},
_focusedItem: {
type: String,
computed: '_getFocusedItem(_focusedIndex)'
},
_ariaActiveIndex: {
type: Number,
notify: true,
computed: '_getAriaActiveIndex(_focusedIndex)'
},
_itemLabelPath: {
type: String,
value: 'label'
},
_itemValuePath: {
type: String,
value: 'value'
}
},
ready: function() {
this._patchWheelOverScrolling();
},
_getFocusedItem: function(focusedIndex) {
if (focusedIndex >= 0) {
return this._items[focusedIndex];
}
},
/**
* Gets the index of the item with the provided label.
* @return {Number}
*/
indexOfLabel: function(label) {
if (this._items && label) {
for (var i = 0; i < this._items.length; i++) {
if (this.getItemLabel(this._items[i]).toString().toLowerCase() ===
label.toString().toLowerCase()) {
return i;
}
}
}
return -1;
},
/**
* Gets the label string for the item based on the `_itemLabelPath`.
* @return {String}
*/
getItemLabel: function(item) {
var label = this.get(this._itemLabelPath, item);
if (label === undefined || label === null) {
label = item ? item.toString() : '';
}
return label;
},
_isItemFocused: function(focusedIndex, itemIndex) {
return focusedIndex == itemIndex;
},
_getAriaActiveIndex: function(focusedIndex) {
if (focusedIndex >= 0) {
return 'it' + focusedIndex;
}
return false;
},
_getAriaSelected: function(focusedIndex, itemIndex) {
return this._isItemFocused(focusedIndex, itemIndex).toString();
},
_getAriaRole: function(itemIndex) {
return itemIndex !== undefined ? 'option' : false;
},
_focusedIndexChanged: function(index) {
if (index >= 0) {
this._scrollIntoView(index);
}
},
_scrollIntoView: function(index) {
if (index <= this.$.selector.firstVisibleIndex) {
this.$.selector.scrollToIndex(index);
} else if (index > this._lastVisibleIndex()) {
this.$.selector.scrollToIndex(index - this._visibleItemsCount() + 1);
}
},
_getScroller: function() {
return this.$.scroller;
},
/**
* We want to prevent the kinetic scrolling energy from being transferred from the overlay contents over to the parent.
* Further improvement ideas: after the contents have been scrolled to the top or bottom and scrolling has stopped, it could allow
* scrolling the parent similarily to touch scrolling.
*/
_patchWheelOverScrolling: function() {
var selector = this.$.selector;
selector.addEventListener('wheel', function(e) {
var scrolledToTop = selector._scroller.scrollTop === 0;
var scrolledToBottom = (selector._scroller.scrollHeight - selector._scroller.scrollTop - selector._scroller.clientHeight) <= 1;
if (scrolledToTop && e.deltaY < 0) {
e.preventDefault();
} else if (scrolledToBottom && e.deltaY > 0) {
e.preventDefault();
}
});
},
// TODO: PR for iron-list: https://github.com/PolymerElements/iron-list/pull/150
_visibleItemsCount: function() {
var firstItemIndex = this.$.selector._physicalStart;
var firstItemHeight = this.$.selector._physicalSizes[firstItemIndex];
if (firstItemHeight) {
var visibleItems = this.$.selector._viewportSize / firstItemHeight;
return Math.floor(visibleItems);
}
},
// TODO: PR for iron-list: https://github.com/PolymerElements/iron-list/pull/150
_lastVisibleIndex: function() {
if (this._visibleItemsCount()) {
return this.$.selector.firstVisibleIndex + this._visibleItemsCount() - 1;
}
},
_selectItem: function(item) {
this.$.selector.selectItem(item);
},
_preventDefault: function(e) {
e.preventDefault();
}
});
</script>