73bcce88
luigser
COMPONENTS
|
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
<!--
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
Code distributed by Google as part of the polymer project is also
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
-->
<link rel="import" href="../polymer/polymer.html">
<link rel="import" href="iron-selection.html">
<script>
/** @polymerBehavior */
Polymer.IronSelectableBehavior = {
/**
|
e619a3b0
Luigi Serra
Controllet cross ...
|
19
20
21
|
* Fired when iron-selector is activated (selected or deselected).
* It is fired before the selected items are changed.
* Cancel the event to abort selection.
|
73bcce88
luigser
COMPONENTS
|
22
23
|
*
* @event iron-activate
|
e619a3b0
Luigi Serra
Controllet cross ...
|
24
25
|
*/
|
73bcce88
luigser
COMPONENTS
|
26
|
/**
|
e619a3b0
Luigi Serra
Controllet cross ...
|
27
|
* Fired when an item is selected
|
73bcce88
luigser
COMPONENTS
|
28
29
|
*
* @event iron-select
|
e619a3b0
Luigi Serra
Controllet cross ...
|
30
31
|
*/
|
73bcce88
luigser
COMPONENTS
|
32
|
/**
|
e619a3b0
Luigi Serra
Controllet cross ...
|
33
|
* Fired when an item is deselected
|
73bcce88
luigser
COMPONENTS
|
34
35
|
*
* @event iron-deselect
|
e619a3b0
Luigi Serra
Controllet cross ...
|
36
37
38
39
40
41
|
*/
/**
* Fired when the list of selectable items changes (e.g., items are
* added or removed). The detail of the event is a list of mutation
* records that describe what changed.
|
73bcce88
luigser
COMPONENTS
|
42
|
*
|
e619a3b0
Luigi Serra
Controllet cross ...
|
43
44
|
* @event iron-items-changed
*/
|
73bcce88
luigser
COMPONENTS
|
45
46
47
48
49
50
|
properties: {
/**
* If you want to use the attribute value of an element for `selected` instead of the index,
* set this to the name of the attribute.
|
73bcce88
luigser
COMPONENTS
|
51
52
53
54
55
56
57
58
|
*/
attrForSelected: {
type: String,
value: null
},
/**
* Gets or sets the selected element. The default is to use the index of the item.
|
73bcce88
luigser
COMPONENTS
|
59
60
61
62
63
64
65
66
|
*/
selected: {
type: String,
notify: true
},
/**
* Returns the currently selected item.
|
73bcce88
luigser
COMPONENTS
|
67
68
69
70
71
72
73
74
75
76
77
|
*/
selectedItem: {
type: Object,
readOnly: true,
notify: true
},
/**
* The event that fires from items when they are selected. Selectable
* will listen for this event from items and update the selection state.
* Set to empty string to listen to no events.
|
73bcce88
luigser
COMPONENTS
|
78
79
80
81
82
83
84
85
86
87
|
*/
activateEvent: {
type: String,
value: 'tap',
observer: '_activateEventChanged'
},
/**
* This is a CSS selector string. If this is set, only items that match the CSS selector
* are selectable.
|
73bcce88
luigser
COMPONENTS
|
88
89
90
91
92
|
*/
selectable: String,
/**
* The class to set on elements when selected.
|
73bcce88
luigser
COMPONENTS
|
93
94
95
96
97
98
99
100
|
*/
selectedClass: {
type: String,
value: 'iron-selected'
},
/**
* The attribute to set on elements when selected.
|
73bcce88
luigser
COMPONENTS
|
101
102
103
104
105
106
107
|
*/
selectedAttribute: {
type: String,
value: null
},
/**
|
e619a3b0
Luigi Serra
Controllet cross ...
|
108
|
* The set of excluded elements where the key is the `localName`
|
73bcce88
luigser
COMPONENTS
|
109
110
|
* of the element that will be ignored from the item list.
*
|
c5169e0e
Renato De Donato
a new hope
|
111
|
* @type {object}
|
73bcce88
luigser
COMPONENTS
|
112
113
|
* @default {template: 1}
*/
|
eb240478
Luigi Serra
public room cards...
|
114
|
_excludedLocalNames: {
|
73bcce88
luigser
COMPONENTS
|
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
|
type: Object,
value: function() {
return {
'template': 1
};
}
}
},
observers: [
'_updateSelected(attrForSelected, selected)'
],
created: function() {
this._bindFilterItem = this._filterItem.bind(this);
this._selection = new Polymer.IronSelection(this._applySelection.bind(this));
|
c5169e0e
Renato De Donato
a new hope
|
131
132
133
|
// TODO(cdata): When polymer/polymer#2535 lands, we do not need to do this
// book keeping anymore:
this.__listeningForActivate = false;
|
73bcce88
luigser
COMPONENTS
|
134
135
136
137
|
},
attached: function() {
this._observer = this._observeItems(this);
|
c5169e0e
Renato De Donato
a new hope
|
138
139
|
this._contentObserver = this._observeContent(this);
if (!this.selectedItem && this.selected) {
|
e619a3b0
Luigi Serra
Controllet cross ...
|
140
141
|
this._updateSelected(this.attrForSelected,this.selected)
}
|
eb240478
Luigi Serra
public room cards...
|
142
|
this._addListener(this.activateEvent);
|
73bcce88
luigser
COMPONENTS
|
143
144
145
146
|
},
detached: function() {
if (this._observer) {
|
c5169e0e
Renato De Donato
a new hope
|
147
148
149
150
|
this._observer.disconnect();
}
if (this._contentObserver) {
this._contentObserver.disconnect();
|
73bcce88
luigser
COMPONENTS
|
151
152
153
154
155
|
}
this._removeListener(this.activateEvent);
},
/**
|
c5169e0e
Renato De Donato
a new hope
|
156
157
158
159
160
161
162
163
164
165
166
|
* Returns an array of selectable items.
*
* @property items
* @type Array
*/
get items() {
var nodes = Polymer.dom(this).queryDistributedElements(this.selectable || '*');
return Array.prototype.filter.call(nodes, this._bindFilterItem);
},
/**
|
73bcce88
luigser
COMPONENTS
|
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
|
* Returns the index of the given item.
*
* @method indexOf
* @param {Object} item
* @returns Returns the index of the item
*/
indexOf: function(item) {
return this.items.indexOf(item);
},
/**
* Selects the given value.
*
* @method select
* @param {string} value the value to select.
*/
select: function(value) {
this.selected = value;
},
/**
* Selects the previous item.
*
* @method selectPrevious
*/
selectPrevious: function() {
var length = this.items.length;
var index = (Number(this._valueToIndex(this.selected)) - 1 + length) % length;
this.selected = this._indexToValue(index);
},
/**
* Selects the next item.
*
* @method selectNext
*/
selectNext: function() {
var index = (Number(this._valueToIndex(this.selected)) + 1) % this.items.length;
this.selected = this._indexToValue(index);
},
|
a1a3bc73
Luigi Serra
graphs updates
|
208
|
_addListener: function(eventName) {
|
c5169e0e
Renato De Donato
a new hope
|
209
210
211
212
213
|
if (!this.isAttached || this.__listeningForActivate) {
return;
}
this.__listeningForActivate = true;
|
73bcce88
luigser
COMPONENTS
|
214
215
216
217
218
|
this.listen(this, eventName, '_activateHandler');
},
_removeListener: function(eventName) {
this.unlisten(this, eventName, '_activateHandler');
|
c5169e0e
Renato De Donato
a new hope
|
219
|
this.__listeningForActivate = false;
|
73bcce88
luigser
COMPONENTS
|
220
221
222
223
224
225
226
|
},
_activateEventChanged: function(eventName, old) {
this._removeListener(old);
this._addListener(eventName);
},
|
73bcce88
luigser
COMPONENTS
|
227
228
229
230
231
232
233
234
235
|
_updateSelected: function() {
this._selectSelected(this.selected);
},
_selectSelected: function(selected) {
this._selection.select(this._valueToItem(this.selected));
},
_filterItem: function(node) {
|
eb240478
Luigi Serra
public room cards...
|
236
|
return !this._excludedLocalNames[node.localName];
|
73bcce88
luigser
COMPONENTS
|
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
|
},
_valueToItem: function(value) {
return (value == null) ? null : this.items[this._valueToIndex(value)];
},
_valueToIndex: function(value) {
if (this.attrForSelected) {
for (var i = 0, item; item = this.items[i]; i++) {
if (this._valueForItem(item) == value) {
return i;
}
}
} else {
return Number(value);
}
},
_indexToValue: function(index) {
if (this.attrForSelected) {
var item = this.items[index];
if (item) {
return this._valueForItem(item);
}
} else {
return index;
}
},
_valueForItem: function(item) {
return item[this.attrForSelected] || item.getAttribute(this.attrForSelected);
},
_applySelection: function(item, isSelected) {
if (this.selectedClass) {
this.toggleClass(this.selectedClass, isSelected, item);
}
if (this.selectedAttribute) {
this.toggleAttribute(this.selectedAttribute, isSelected, item);
}
this._selectionChange();
this.fire('iron-' + (isSelected ? 'select' : 'deselect'), {item: item});
},
_selectionChange: function() {
this._setSelectedItem(this._selection.get());
},
|
c5169e0e
Renato De Donato
a new hope
|
285
286
287
288
289
290
291
292
|
// observe content changes under the given node.
_observeContent: function(node) {
var content = node.querySelector('content');
if (content && content.parentElement === node) {
return this._observeItems(node.domHost);
}
},
|
73bcce88
luigser
COMPONENTS
|
293
294
|
// observe items change under the given node.
_observeItems: function(node) {
|
c5169e0e
Renato De Donato
a new hope
|
295
296
|
// TODO(cdata): Update this when we get distributed children changed.
var observer = new MutationObserver(function(mutations) {
|
e619a3b0
Luigi Serra
Controllet cross ...
|
297
298
299
300
301
302
303
|
// Let other interested parties know about the change so that
// we don't have to recreate mutation observers everywher.
this.fire('iron-items-changed', mutations, {
bubbles: false,
cancelable: false
});
|
c5169e0e
Renato De Donato
a new hope
|
304
|
if (this.selected != null) {
|
73bcce88
luigser
COMPONENTS
|
305
306
|
this._updateSelected();
}
|
c5169e0e
Renato De Donato
a new hope
|
307
308
309
310
|
}.bind(this));
observer.observe(node, {
childList: true,
subtree: true
|
73bcce88
luigser
COMPONENTS
|
311
|
});
|
c5169e0e
Renato De Donato
a new hope
|
312
|
return observer;
|
73bcce88
luigser
COMPONENTS
|
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
|
},
_activateHandler: function(e) {
var t = e.target;
var items = this.items;
while (t && t != this) {
var i = items.indexOf(t);
if (i >= 0) {
var value = this._indexToValue(i);
this._itemActivate(value, t);
return;
}
t = t.parentNode;
}
},
_itemActivate: function(value, item) {
if (!this.fire('iron-activate',
{selected: value, item: item}, {cancelable: true}).defaultPrevented) {
this.select(value);
}
}
};
</script>
|