-
Notifications
You must be signed in to change notification settings - Fork 4
/
paper-autocomplete.html
479 lines (423 loc) · 15.2 KB
/
paper-autocomplete.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
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
<!--
`paper-autocomplete` extends `paper-input`, adding autocomplete functionality.
The current value is used to prefix-match against the labels of this element's
child nodes (either `core-item` or `paper-item`). Those that match are shown,
while those that fail to match are hidden. The suggested items can be selected
using keyboard and mouse input.
Example:
<paper-autocomplete label="Country" on-change="{{changeHandler}}">
<core-item label="United States></core-item>
<core-item label="Canada"></core-item>
</paper-autocomplete>
All attributes and events of paper-input are inherited by paper-autocomplete.
New Attributes:
maxItems
caseSensitive
delimiter
allowWhitespace
New Events:
none yet
-->
<link href="../polymer/polymer.html" rel="import">
<link href="../paper-input/paper-input.html" rel="import">
<link href="../core-selector/core-selector.html" rel="import">
<polymer-element name="paper-autocomplete" extends="paper-input" attributes="maxItems caseSensitive delimiter">
<template>
<link href="paper-autocomplete.css" rel="stylesheet">
<div id="frame">
<div id="top-input">
<shadow></shadow>
</div>
<div id="bottom-suggest">
<core-selector id="suggest-box" class="suggest-box-style hidden"
on-core-activate="{{selectAction}}">
<content id="items" select="core-item,paper-item"></content>
</core-selector>
</div>
</div>
</template>
<script>
Polymer({
/**
* Maximum number of items to show in the suggestion box.
* A null value indicates that there is no limit.
*
* @attribute: maxItems
* @type: integer | null
*/
maxItems: null,
/**
* Flag that indicates the case-sensitivity of prefix-matching.
*
* @attribute: caseSensitive
* @type: boolean
*/
caseSensitive: false,
/**
* If null, this element acts like a standard autocomplete unit.
* If given a value, then instead of replacing the full text string,
* this element will replace the substring after the last occurrence of
* the delimiter. In effect, making this like a list selector.
*
* Example: delimiter === '' (Append-only)
* When the text box has 'abcd', selecting 'ef' makes this 'abcdef'
* This choice of delimiter prevents suggestions from being filtered.
*
* Example: delimiter === ',' (CSV-style list)
* If the text box has 'red,orange,g', and 'green' is selected,
* then the text box will have 'red,orange,green'.
*
* Some delimiters work well with dynamic child node suggestions.
*
* Example: delimiter === '/' (Directory path)
* If the text box has 'drive/google/photos/' and 'May2012' is selected,
* then the text box will have 'drive/google/photos/May2012'.
*
* @attribute: delimiter
* @type: string | null
*/
delimiter: null,
/**
* Set to true to tolerate whitespace when filtering suggestions.
* Whitespace will also be maintained when a suggestion is selected.
* Note: This attribute is only useful when there is a delimiter.
*
* Example: delimiter === ',' and allowWhitespace === true
* If the text box has 'red, orange, g' and 'green' is selected,
* then the text box will have 'red, orange, green'.
*
* @attribute: allowWhitespace
* @type: boolean
*/
allowWhitespace: false,
/**
* Whether this element is currently being edited or not. Becomes true
* when the internal paper-input gains focus or receives input.
*
* @type: boolean
*/
editing: false,
/**
* A special flag indicating that the suggestion box is going to close.
*/
closing: false,
/**
* List of indexes of children that can be shown. The children are
* filtered by the current value.
*
* @type: Array<integer>
*/
showing: [],
/**
* Tracks the highlight position in the showing array. The corresponding
* child node is highlighted with the 'highlighted' class.
*
* @type: int | null
*/
highlightIndex: null,
/**
* Called when this node is added to the DOM. Sets up event listeners
* through the shadow DOM to its internal paper-input. Additionally,
* watches for mutations in its light DOM children.
*/
ready: function() {
this.super();
// Add listener to the suggestBox to cancel the suggestion box from
// closing if the user starts to click on it.
var suggestBox = this.$['suggest-box'];
suggestBox.addEventListener(
'mousedown',
this.cancelClosing.bind(this)
);
// Listen to inputs to determine editing status.
this.addEventListener('input', this.inputAction.bind(this));
this.addEventListener('keydown', this.keydownAction.bind(this));
this.addEventListener('focus', this.inputFocusAction.bind(this));
this.addEventListener('blur', this.inputBlurAction.bind(this));
// Capture changes to children
this.watchMutation();
},
/**
* Whenever the light DOM children change, then rerender this element.
*/
watchMutation: function() {
var change = function() {
this.filterSuggestions();
// Note: onMutation unbinds after each use, so we need to rebind.
this.onMutation(this, change.bind(this));
}
this.onMutation(this, change.bind(this));
},
/**
* Set the editing flag to true upon gaining focus.
*/
inputFocusAction: function(e) {
this.super(e);
if (!this.closing) {
this.editing = true;
}
this.closing = false;
this.filterSuggestions();
},
/**
* When blurring, potentially set editing to false.
* Has a delay before the suggestion box is closed in case the user starts
* to click on the suggestion box.
*/
inputBlurAction: function(e) {
this.super(e);
this.closing = true;
this.async(function() {
if (this.closing) {
this.showSuggestions(false);
this.editing = false;
}
}, null, 100);
},
/**
* If the mouse is pressed down on the suggestion box, set the closing
* flag to false.
*/
cancelClosing: function(e) {
this.async(function() {
if (this.closing) {
this.closing = false;
}
}, null, 0);
},
/**
* On the hidden input's 'input' event, the fact that data changed means
* that the user is editing. Re-render the suggestions.
* Note: Fires this element's 'input' event.
*/
inputAction: function(e) {
this.closing = false;
this.editing = true;
this.filterSuggestions();
// Convert a standard Event into the Custom Event for consistency.
if (!(e instanceof CustomEvent)) {
e.stopPropagation();
this.fireInput();
}
},
/**
* Filters the potential list of suggestions drawn from child node labels.
* Use a prefixMatch to decide which child nodes to show/hide.
* Finally, show the suggestion box if the conditions are right.
*/
filterSuggestions: function() {
var showing = [];
var childNodes = this.$.items.getDistributedNodes();
for (var i = 0; i < childNodes.length; i++) {
var item = childNodes[i];
// Remove style if necessary.
if (this.highlightIndex !== i) {
item.classList.toggle('highlighted', false);
}
// Show nodes with a prefix match, as long
// as the maxItems limit has not been reached.
if ((this.maxItems === null || showing.length < this.maxItems) &&
this.prefixMatch(this.getItemLabel(item))) {
showing.push(i);
item.style.display = '';
} else {
item.style.display = 'none';
}
}
this.showing = showing;
this.showSuggestions(this.shouldShowSuggestions());
},
/**
* Depending on the caseSensitive attribute, returns whether or not the
* given word has a prefix match with the current value of this element.
*/
prefixMatch: function(word) {
var curValue = this.getInputValueSuffix();
if (!this.caseSensitive) {
return word.toLowerCase().indexOf(curValue.toLowerCase()) === 0;
}
return word.indexOf(curValue) === 0;
},
/**
* Get the suffix of the current input value. If there is no delimiter,
* then the full input value is used instead.
*/
getInputValueSuffix: function() {
var value = this.value || '';
if (this.delimiter === '') {
return '';
} else if (!this.delimiter) {
return value;
}
// Find the suffix.
var parts = value.split(this.delimiter);
var suffix = parts[parts.length - 1];
// Handle excess whitespace.
if (this.allowWhitespace) {
return suffix.trimLeft();
}
return suffix;
},
/**
* Gets the label out of the item for prefix-matching and replacement.
* Note: paper-item and core-item mismatch; label is not available on the
* former anymore.
*/
getItemLabel: function(item) {
return item.textContent || item.label;
},
/**
* Determine whether or not the suggestBox should be shown.
* Show if this node is being edited and there are valid suggestions.
*/
shouldShowSuggestions: function() {
return this.editing && this.showing.length > 0;
},
/**
* The suggestion box is shown/hidden. If hidden, then additional internal
* values are reset.
*/
showSuggestions: function(shouldShow) {
this.$['suggest-box'].classList.toggle('hidden', !shouldShow);
if (!shouldShow) {
this.highlightReset();
this.closing = false;
}
},
/**
* Process the 'core-activate' event when a suggestion was tapped.
*/
selectAction: function(e) {
this.selectItem(e, e.detail.item);
},
/**
* When an item in the suggestion list is clicked or 'enter' is pressed
* while it is being highlighted, then replace this node's value.
*
* This node is no longer being edited, so re-render will hide it.
* Note: Fires this element's 'input' and 'change' events.
* Special: Click loses the element's focus, but pressing enter does not.
*/
selectItem: function(e, item) {
this.highlightReset();
this.replaceValue(this.getItemLabel(item));
this.fireInput();
this.changeAction(e);
this.showSuggestions(false);
this.editing = false;
},
/**
* Replace the current value and inputValue of this element.
* If there is a delimiter, replace only the suffix.
*/
replaceValue: function(newValue) {
if (this.delimiter === '') {
this.value += newValue;
} else if (!this.delimiter) {
this.value = newValue;
} else {
// Find the suffix.
var parts = this.value.split(this.delimiter);
var suffix = parts[parts.length - 1];
// Compute the replacement, taking into account whitespace.
var keepLength = 0;
if (this.allowWhitespace) {
keepLength = suffix.length - suffix.trimLeft().length
}
parts[parts.length - 1] = suffix.substring(0, keepLength) + newValue;
// Replace the input value.
this.value = parts.join(this.delimiter);
}
},
/**
* Fire the input event. (The invocation matches core-input's.)
*/
fireInput: function() {
this.fire('input', null, this);
},
/**
* Remove highlight off the current item and stop tracking it.
*/
highlightReset: function() {
this.highlightItem(false);
this.highlightIndex = null;
},
/**
* Add/remove the highlighted class off the currently highlighted item.
*/
highlightItem: function(shouldHighlight) {
if (this.highlightIndex !== null) {
var selected = this.showing[this.highlightIndex];
this.$.items.getDistributedNodes()[selected].classList.toggle(
'highlighted',
shouldHighlight
);
}
},
/**
* Pressing up and down change the currently highlighted item.
* Pressing enter will select that item.
* Pressing escape will close the suggestBox.
* Pressing any other key will reset highlight.
*/
keydownAction: function(e) {
var KEY_UP = 38;
var KEY_DOWN = 40;
var KEY_ENTER = 13;
var KEY_ESC = 27;
// The suggest box is not open.
if (!this.shouldShowSuggestions()) {
// If up or down was hit, potentially open the suggest box.
if (e.keyCode === KEY_UP || e.keyCode === KEY_DOWN) {
this.editing = true;
this.showSuggestions(this.shouldShowSuggestions());
}
return;
}
// The suggest box is open. Handle the keypresses accordingly.
switch(e.keyCode) {
case KEY_UP: // Highlight the item above the current one.
e.preventDefault();
// unhighlight, change selection, highlight
this.highlightItem(false);
if (this.highlightIndex === null || this.highlightIndex === 0) {
this.highlightIndex = this.showing.length - 1;
} else {
this.highlightIndex--;
}
this.highlightItem(true);
break;
case KEY_DOWN: // Highlight the item below the current one.
e.preventDefault();
// unhighlight, change selection, highlight
this.highlightItem(false);
if (this.highlightIndex === null ||
this.highlightIndex === this.showing.length - 1) {
this.highlightIndex = 0;
} else {
this.highlightIndex++;
}
this.highlightItem(true);
break;
case KEY_ENTER: // Activate the highlighted node, if any.
if (this.highlightIndex !== null) {
e.preventDefault(); // do not commit a change
var highlightIndex = this.showing[this.highlightIndex];
var item = this.$.items.getDistributedNodes()[highlightIndex];
this.selectItem(e, item);
} else {
this.showSuggestions(false);
this.editing = false;
}
break;
case KEY_ESC: // Hide the suggestions and reset the current highlight.
this.showSuggestions(false);
this.editing = false;
break;
default: // Reset the current highlight.
this.highlightReset();
}
}
});
</script>
</polymer-element>