2 Copyright 2013 The Polymer Authors. All rights reserved.
3 Use of this source code is governed by a BSD-style
4 license that can be found in the LICENSE file.
8 * @module Polymer Elements
11 * polymer-selector is used to manage a list of elements that can be selected.
12 * The attribute "selected" indicates which item element is being selected.
13 * The attribute "multi" indicates if multiple items can be selected at once.
14 * Tapping on the item element would fire "polymer-activate" event. Use
15 * "polymer-select" event to listen for selection changes.
19 * <polymer-selector selected="0">
25 * polymer-selector is not styled. So one needs to use "polymer-selected" CSS
26 * class to style the selected element.
29 * .item.polymer-selected {
35 * <div class="item">Item 1</div>
36 * <div class="item">Item 2</div>
37 * <div class="item">Item 3</div>
40 * @class polymer-selector
43 * Fired when an item's selection state is changed. This event is fired both
44 * when an item is selected or deselected. The `isSelected` detail property
45 * contains the selection state.
47 * @event polymer-select
48 * @param {Object} detail
49 * @param {boolean} detail.isSelected true for selection and false for deselection
50 * @param {Object} detail.item the item element
53 * Fired when an item element is tapped.
55 * @event polymer-activate
56 * @param {Object} detail
57 * @param {Object} detail.item the item element
60 <link rel="import" href="../polymer/polymer.html">
61 <link rel="import" href="../polymer-selection/polymer-selection.html">
63 <polymer-element name="polymer-selector"
64 attributes="selected multi valueattr selectedClass selectedProperty selectedItem selectedModel selectedIndex notap target itemsSelector activateEvent">
66 <polymer-selection id="selection" multi="{{multi}}" on-polymer-select="{{selectionSelect}}"></polymer-selection>
67 <content id="items" select="*"></content>
70 Polymer('polymer-selector', {
72 * Gets or sets the selected element. Default to use the index
73 * of the item element.
75 * If you want a specific attribute value of the element to be
76 * used instead of index, set "valueattr" to that attribute name.
80 * <polymer-selector valueattr="label" selected="foo">
81 * <div label="foo"></div>
82 * <div label="bar"></div>
83 * <div label="zot"></div>
86 * In multi-selection this should be an array of values.
90 * <polymer-selector id="selector" valueattr="label" multi>
91 * <div label="foo"></div>
92 * <div label="bar"></div>
93 * <div label="zot"></div>
96 * this.$.selector.selected = ['foo', 'zot'];
104 * If true, multiple selections are allowed.
112 * Specifies the attribute to be used for "selected" attribute.
114 * @attribute valueattr
120 * Specifies the CSS class to be used to add to the selected element.
122 * @attribute selectedClass
124 * @default 'polymer-selected'
126 selectedClass: 'polymer-selected',
128 * Specifies the property to be used to set on the selected element
129 * to indicate its active state.
131 * @attribute selectedProperty
135 selectedProperty: 'active',
137 * Returns the currently selected element. In multi-selection this returns
138 * an array of selected elements.
140 * @attribute selectedItem
146 * In single selection, this returns the model associated with the
149 * @attribute selectedModel
155 * In single selection, this returns the selected index.
157 * @attribute selectedIndex
163 * The target element that contains items. If this is not set
164 * polymer-selector is the container.
172 * This can be used to query nodes from the target node to be used for
173 * selection items. Note this only works if the 'target' property is set.
177 * <polymer-selector target="{{$.myForm}}" itemsSelector="input[type=radio]"></polymer-selector>
179 * <label><input type="radio" name="color" value="red"> Red</label> <br>
180 * <label><input type="radio" name="color" value="green"> Green</label> <br>
181 * <label><input type="radio" name="color" value="blue"> Blue</label> <br>
182 * <p>color = {{color}}</p>
185 * @attribute itemSelector
191 * The event that would be fired from the item element to indicate
192 * it is being selected.
194 * @attribute activateEvent
198 activateEvent: 'tap',
201 this.activateListener = this.activateHandler.bind(this);
202 this.observer = new MutationObserver(this.updateSelected.bind(this));
208 var nodes = this.target !== this ? (this.itemsSelector ?
209 this.target.querySelectorAll(this.itemsSelector) :
210 this.target.children) : this.$.items.getDistributedNodes();
211 return Array.prototype.filter.call(nodes || [], function(n) {
212 return n && n.localName !== 'template';
215 targetChanged: function(old) {
217 this.removeListener(old);
218 this.observer.disconnect();
221 this.addListener(this.target);
222 this.observer.observe(this.target, {childList: true});
225 addListener: function(node) {
226 node.addEventListener(this.activateEvent, this.activateListener);
228 removeListener: function(node) {
229 node.removeEventListener(this.activateEvent, this.activateListener);
232 return this.$.selection.getSelection();
234 selectedChanged: function() {
235 this.updateSelected();
237 updateSelected: function() {
238 this.validateSelected();
240 this.clearSelection();
241 this.selected && this.selected.forEach(function(s) {
242 this.valueToSelection(s);
245 this.valueToSelection(this.selected);
248 validateSelected: function() {
249 // convert to an array for multi-selection
250 if (this.multi && !Array.isArray(this.selected) &&
251 this.selected !== null && this.selected !== undefined) {
252 this.selected = [this.selected];
255 clearSelection: function() {
257 this.selection.slice().forEach(function(s) {
258 this.$.selection.setItemSelected(s, false);
261 this.$.selection.setItemSelected(this.selection, false);
263 this.selectedItem = null;
264 this.$.selection.clear();
266 valueToSelection: function(value) {
267 var item = (value === null || value === undefined) ?
268 null : this.items[this.valueToIndex(value)];
269 this.$.selection.select(item);
271 updateSelectedItem: function() {
272 this.selectedItem = this.selection;
274 selectedItemChanged: function() {
275 if (this.selectedItem) {
276 var t = this.selectedItem.templateInstance;
277 this.selectedModel = t ? t.model : undefined;
279 this.selectedModel = null;
281 this.selectedIndex = this.selectedItem ?
282 parseInt(this.valueToIndex(this.selected)) : -1;
284 valueToIndex: function(value) {
285 // find an item with value == value and return it's index
286 for (var i=0, items=this.items, c; (c=items[i]); i++) {
287 if (this.valueForNode(c) == value) {
291 // if no item found, the value itself is probably the index
294 valueForNode: function(node) {
295 return node[this.valueattr] || node.getAttribute(this.valueattr);
297 // events fired from <polymer-selection> object
298 selectionSelect: function(e, detail) {
299 this.updateSelectedItem();
301 this.applySelection(detail.item, detail.isSelected);
304 applySelection: function(item, isSelected) {
305 if (this.selectedClass) {
306 item.classList.toggle(this.selectedClass, isSelected);
308 if (this.selectedProperty) {
309 item[this.selectedProperty] = isSelected;
312 // event fired from host
313 activateHandler: function(e) {
315 var i = this.findDistributedTarget(e.target, this.items);
317 var item = this.items[i];
318 var s = this.valueForNode(item) || i;
321 this.addRemoveSelected(s);
328 this.asyncFire('polymer-activate', {item: item});
332 addRemoveSelected: function(value) {
333 var i = this.selected.indexOf(value);
335 this.selected.splice(i, 1);
337 this.selected.push(value);
339 this.valueToSelection(value);
341 findDistributedTarget: function(target, nodes) {
342 // find first ancestor of target (including itself) that
343 // is in nodes, if any
344 while (target && target != this) {
345 var i = Array.prototype.indexOf.call(nodes, target);
349 target = target.parentNode;