-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapollo-select-grid-dropdown.html
320 lines (301 loc) · 10.6 KB
/
apollo-select-grid-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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8"></meta>
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../vaadin-components/vaadin-grid/vaadin-grid.html">
</head>
<body>
<dom-module id="apollo-select-grid-dropdown">
<style>
#dropdown {
background-color: white;
border: 1px solid #aaa;
padding:8px 0px;
margin:0;
position: absolute;
top: 32px;
left: 0;
right:0;
box-shadow: 1px 1px 5px #aaa;
z-index: 1;
overflow: hidden;
max-height: 0;
visibility: hidden;
/* transition: <property> <duration> <timing-function> <delay>; */
transition: visibility 0s linear 0.3s,
max-height 0.30s ease-in-out 0s;
}
#dropdown.open {
max-height: 192px;
visibility:visible;
transition: visibility 0s linear 0s,
max-height 0.30s ease-in-out 0s;
}
#dropdown > ::content li {
list-style-type: none;
text-overflow: ellipsis;
white-space: nowrap;
overflow: hidden;
padding:0 24px;
margin:0;
cursor: pointer;
font-size:16px;
line-height: 48px;
-webkit-user-select: none;
-moz-user-select: none;
-ms-user-select: none;
user-select: none;
}
#dropdown > ::content li.highlight {
background-color: #eeeeee;
}
#dropdown > ::content li.selected {
background-color: #eeeeee;
}
#dropdown.overflow-auto {
overflow:auto;
}
</style>
<template>
<ul id="dropdown">
<v-grid id="grid" style="height:180px">
<table>
<colgroup>
<col header-text="Name">
<col header-text="Value">
<col header-text="Progress">
</colgroup>
<tbody id="tbody">
</tbody>
</table>
</v-grid>
<content></content>
</ul>
</template>
</dom-module>
<script>
Polymer({
is: "apollo-select-grid-dropdown",
properties: {
value: { type: String },
values: {
type: String,
observer: 'valuesChanged'
},
search: {
type: String,
observer: 'searchChanged'
},
apolloselect: {
type: Object
},
opening: false
},
closeJob: null,
created: function() {
this.filteredValues = [];
this.opening = false;
},
ready: function() {
var component = this;
this.$.grid.addEventListener('select', function() {
if(this.opening){
return;
}
var newValue = component.filteredValues[component.$.grid.selection.selected()];
component.fire('value-selected', {selectedValue : newValue});
component.close();
});
},
selectItemWithText: function(text){
if(!this.filteredValues || !text){
return;
}
for (var i = 0; i < this.filteredValues.length; i++) {
if(this.filteredValues[i].toString() == text.toString()){
this.$.grid.selection.select(i);
break;
}
}
},
/* TODO: this should probably check if 'value' is valid after values have changed */
valuesChanged: function(){
this.filteredValues = this.values.slice();
},
renderList: function(){
var component = this;
this.$.grid.data.source = function(req) {
if(typeof component.filteredValues === 'undefined'){
req.success([], 0);
} else {
var array = component.filteredValues.slice(req.index, req.index + req.count);
req.success(array, component.filteredValues.length);
}
};
this.selectItemWithText(this.value);
},
toggleOpen: function() {
var isOpen = this.$.dropdown.classList.contains('open');
if(isOpen){
this.close();
} else {
this.open();
}
},
open: function(){
opening = true;
this.$.dropdown.classList.add('open');
this.selectItemWithText(this.value);
var component = this;
this.async(function() {
component.addTapListeners();
});
setTimeout(function() {
component.$.dropdown.classList.add('overflow-auto');
}, 300);
opening = false;
},
close: function(){
this.$.dropdown.classList.remove('open');
this.removeTapListeners();
var component = this;
component.$.dropdown.classList.remove('overflow-auto');
setTimeout(function() {
component.search = null;
component.fire('reset-search-value', {});
}, 300);
},
clickedOutsideListener: null,
clickedInsideListener: null,
clickedInsideSelect: function(dropdown) {
if(dropdown.closeJob){
clearTimeout(dropdown.closeJob);
dropdown.closeJob = null;
}
},
clickedOutsideDropdown: function(dropdown) {
/* Shadow dom effectively hides away the nodes from travesing and gives you the first parent element in the main html file. This means that the target for the click will always look like it is outside of the element. This is a workaround to that problem where you have one tap listener on document and a second one listens only on the apollo-select component only. The first one fires always where ever you click and starts a deffered job to close the overlay. The second one fires if clicked within the component and cancelles the job immediately before it can execute.
*/
var context = this;
dropdown.closeJob = setTimeout(function() {
dropdown.fire('close', {});
}, 0);
},
addTapListeners: function(){
var component = this;
document.currentApolloSelect = this;
this.clickedOutsideListener = function clickedOutsideListener() {
component.clickedOutsideDropdown(component);
}
this.clickedInsideListener = function clickedInsideListener() {
component.clickedInsideSelect(component);
}
setTimeout(function(){
document.addEventListener('click', component.clickedOutsideListener, true);
component.apolloselect.addEventListener('click', component.clickedInsideListener, false);
}, 0);
},
removeTapListeners: function(){
document.removeEventListener('click', this.clickedOutsideListener, true);
this.apolloselect.removeEventListener('click', this.clickedInsideListener, false);
},
_makeBoundListener: function(methodName) {
var self = this, method = this[methodName];
if (!method) {
return;
}
var bound = '_bound' + methodName;
if (!this[bound]) {
this[bound] = function(e) {
method.call(self, e);
};
}
return this[bound];
},
searchChanged: function(newvalue) {
if(newvalue !== null){
this.open();
var newArray = [];
for (var i = 0; i < this.values.length; i++) {
var value = this.values[i];
if(value.indexOf(newvalue) > -1){
newArray.push(value);
}
}
this.filteredValues = newArray;
} else {
this.filteredValues = this.values;
}
this.renderList();
},
navigate: function(event){
if(!this.$.dropdown.classList.contains('open')){
this.open();
} else{
var keyCode = event.detail.keyCode;
if(keyCode == 38){ this.move(-1); } // up
else if(keyCode == 40){ this.move(1); } // down
else if(keyCode == 33){ this.move(-10); } // page up
else if(keyCode == 34){ this.move(10); } // page down
else if(keyCode == 13){ this.selectOnEnterPress(); } // enter
}
},
move: function(steps){
var listItems = this.$.dropdown.getElementsByTagName('li');
var highlighted = this.$.dropdown.getElementsByClassName('highlight');
var selected = this.$.dropdown.getElementsByClassName('selected');
var array = Array.prototype.slice.call(listItems);
if(array.length === 0){
return;
}
var newIndex = 0;
if(highlighted[0]){
newIndex = array.indexOf(highlighted[0]) + steps;
} else if(selected[0]){
newIndex = array.indexOf(selected[0]) + steps;
}
if(newIndex <= 0){
newIndex = 0;
} else if(newIndex > array.length-1){
newIndex = array.length-1;
}
this.highlightRow(array[newIndex], true);
this.fire('display', {value : array[newIndex].textContent});
},
highlightRow: function(element, scroll){
var highlighted = this.$.dropdown.getElementsByClassName('highlight');
for (var i = 0; i < highlighted.length; i++) {
highlighted[i].classList.remove('highlight');
}
if(element){
element.classList.add("highlight");
if(scroll){
this.scrollIntoView(element);
}
}
},
scrollIntoView: function(element){
var elementRect = element.getBoundingClientRect();
var dropdownRect = this.$.dropdown.getBoundingClientRect();
if((elementRect.top - dropdownRect.top)<0){
element.scrollIntoView(true);
} else if((elementRect.bottom - dropdownRect.top - 1) > this.$.dropdown.clientHeight){
element.scrollIntoView(false);
}
},
resetHighlight: function(){
var selected = this.$.dropdown.getElementsByClassName('selected');
this.highlightRow(selected[0], true);
},
selectOnEnterPress: function(){
var selected = this.$.dropdown.getElementsByClassName('highlight');
if(selected.length > 0){
this.fire('value-selected', {selectedValue : selected[0].textContent});
this.close();
}
}
});
</script>
</body>
</html>