forked from soonick/paper-input-autocomplete-chips
-
Notifications
You must be signed in to change notification settings - Fork 1
/
paper-autocomplete-results.html
163 lines (135 loc) · 4.49 KB
/
paper-autocomplete-results.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
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="../paper-menu/paper-menu.html">
<link rel="import" href="../paper-item/paper-item.html">
<link rel="import" href="../paper-material/paper-material.html">
<dom-module id="paper-autocomplete-results">
<template>
<style>
:host {
display: block;
}
:host paper-menu {
padding: 0px;
}
</style>
<paper-menu id="paperMenu" tabindex="-1" on-iron-items-changed="_onIronItemsChanged">
<content></content>
</paper-menu>
</template>
<script>
Polymer({
is: 'paper-autocomplete-results',
behaviors: [
],
hostAttributes: {
'tabindex': -1
},
properties: {
selectableItems: {
type: Array,
value: function() { return []; }
},
/**
* By default navigating of results with the keyboard is circular. i.e.
* when you are in the first item and you click the up arrow you will
* move to the last item. If you are in the last item and then click
* the down arrow, you will move to the first element.
* By setting this property to true, you prevent this behaviour
*/
notCircular: {
type: Boolean,
value: false
}
},
// Element Lifecycle
ready: function() {
},
attached: function() {
},
detached: function() {
},
get items() {
return this.$.paperMenu.items;
},
get focusedItem() {
return this.$.paperMenu.querySelector('.iron-selected');
},
indexOf: function (selectableElement) {
return this.$.paperMenu.indexOf(selectableElement);
},
indexOfSelectable: function (selectableElement) {
return this.selectableItems.indexOf(selectableElement);
},
select: function (index) {
if ((index >= 0) && (this.selectableItems.length > index)) {
var focusedItem = this.focusedItem;
if (focusedItem) {
focusedItem.classList.remove('iron-selected')
focusedItem.active = false;
}
var nextElement = this.selectableItems[index];
nextElement.classList.add('iron-selected')
nextElement.active = true;
var offset = nextElement.offsetTop + nextElement.offsetHeight;
var clientHeightOffsetParent = (this.offsetParent) ? this.offsetParent.clientHeight : 0;
var scrollTopValue = Math.max(0, offset - clientHeightOffsetParent);
if (this.offsetParent) {
this.offsetParent.scrollTop = scrollTopValue;
}
}
},
selectFirst: function () {
this.select(0);
},
selectPrevious: function () {
var selectedItem = this.focusedItem;
if (typeof(selectedItem) !== 'undefined'){
index = Number(this.indexOfSelectable(selectedItem));
index = Math.max(index - 1, -1);
if (index < 0) {
if (!this.notCircular) {
this.selectLast()
}
} else {
this.select(index);
}
}
},
selectNext: function () {
var index = 0;
var selectedItem = this.focusedItem;
if (typeof(selectedItem) !== 'undefined'){
index = Number(this.indexOfSelectable(selectedItem));
index = Math.min(index + 1, this.selectableItems.length);
if (index >= this.selectableItems.length) {
if (!this.notCircular) {
this.selectFirst();
}
} else {
this.select(index);
}
}
},
selectLast: function () {
this.select(this.selectableItems.length - 1);
},
_onIronItemsChanged: function (e) {
if (e.detail) {
var that = this;
if (e.detail.removedNodes) {
e.detail.removedNodes.forEach(function (item) {
that.arrayDelete('selectableItems', item);
});
}
if (e.detail.addedNodes) {
e.detail.addedNodes.forEach(function (item) {
if ((item.hasAttribute) && (item.hasAttribute('selectable-item'))) {
that.push('selectableItems', item);
}
});
}
}
},
});
</script>
</dom-module>