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
13 * The polymer-selection element is used to manage selection state. It has no
14 * visual appearance and is typically used in conjuneciton with another element.
15 * For example, <a href="polymer-selector.html">polymer-selector</a>
16 * use a polymer-selection to manage selection.
18 * To mark an item as selected, call the select(item) method on
19 * polymer-selection. Notice that the item itself is an argument to this method.
20 * The polymer-selection element manages selection state for any given set of
21 * items. When an item is selected, the `polymer-select` event is fired.
22 * The attribute "multi" indicates if multiple items can be selected at once.
26 * <polymer-element name="selection-example">
29 * ::-webkit-distributed(> .selected) {
34 * <ul on-tap="{{itemTapAction}}">
37 * <polymer-selection id="selection" multi on-polymer-select="{{selectAction}}"></polymer-selection>
40 * Polymer('selection-example', {
41 * itemTapAction: function(e) {
42 * this.$.selection.select(e.target);
44 * selectAction: function(e, detail) {
45 * detail.item.classList.toggle('selected', detail.isSelected);
55 * </selection-example>
57 * @class polymer-selection
60 * Fired when an item's selection state is changed. This event is fired both
61 * when an item is selected or deselected. The `isSelected` detail property
62 * contains the selection state.
64 * @event polymer-select
65 * @param {Object} detail
66 * @param {boolean} detail.isSelected true for selection and false for deselection
67 * @param {Object} detail.item the item element
70 <link rel="import" href="../polymer/polymer.html">
72 <polymer-element name="polymer-selection" attributes="multi">
76 display: none !important;
81 Polymer('polymer-selection', {
83 * If true, multiple selections are allowed.
97 * Retrieves the selected item(s).
98 * @method getSelection
99 * @returns Returns the selected item(s). If the multi property is true,
100 * getSelection will return an array, otherwise it will return
101 * the selected item or undefined if there is no selection.
103 getSelection: function() {
104 return this.multi ? this.selection : this.selection[0];
107 * Indicates if a given item is selected.
109 * @param {any} item The item whose selection state should be checked.
110 * @returns Returns true if `item` is selected.
112 isSelected: function(item) {
113 return this.selection.indexOf(item) >= 0;
115 setItemSelected: function(item, isSelected) {
116 if (item !== undefined && item !== null) {
118 this.selection.push(item);
120 var i = this.selection.indexOf(item);
122 this.selection.splice(i, 1);
125 this.fire("polymer-select", {isSelected: isSelected, item: item});
129 * Set the selection state for a given `item`. If the multi property
130 * is true, then the selected state of `item` will be toggled; otherwise
131 * the `item` will be selected.
133 * @param {any} item: The item to select.
135 select: function(item) {
138 } else if (this.getSelection() !== item) {
139 this.setItemSelected(this.getSelection(), false);
140 this.setItemSelected(item, true);
144 * Toggles the selection state for `item`.
146 * @param {any} item: The item to toggle.
148 toggle: function(item) {
149 this.setItemSelected(item, !this.isSelected(item));