2 Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
3 This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
4 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
5 The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
6 Code distributed by Google as part of the polymer project is also
7 subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
10 @group Polymer Core Elements
12 The `<core-selection>` element is used to manage selection state. It has no
13 visual appearance and is typically used in conjunction with another element.
14 For example, [core-selector](#core-selector)
15 uses a `<core-selection>` to manage selection.
17 To mark an item as selected, call the `select(item)` method on
18 `<core-selection>`. The item itself is an argument to this method.
20 The `<core-selection>`element manages selection state for any given set of
21 items. When an item is selected, the `core-select` event is fired.
23 The attribute `multi` indicates if multiple items can be selected at once.
27 <polymer-element name="selection-example">
30 polyfill-next-selector { content: ':host > .selected'; }
31 ::content > .selected {
36 <ul on-tap="{{itemTapAction}}">
39 <core-selection id="selection" multi
40 on-core-select="{{selectAction}}"></core-selection>
43 Polymer('selection-example', {
44 itemTapAction: function(e, detail, sender) {
45 this.$.selection.select(e.target);
47 selectAction: function(e, detail, sender) {
48 detail.item.classList.toggle('selected', detail.isSelected);
60 @element core-selection
64 Fired when an item's selection state is changed. This event is fired both
65 when an item is selected or deselected. The `isSelected` detail property
66 contains the selection state.
69 @param {Object} detail
70 @param {boolean} detail.isSelected true for selection and false for de-selection
71 @param {Object} detail.item the item element
73 <link rel="import" href="../polymer/polymer.html">
75 <polymer-element name="core-selection" attributes="multi" hidden>
77 Polymer('core-selection', {
79 * If true, multiple selections are allowed.
93 * Retrieves the selected item(s).
94 * @method getSelection
95 * @returns Returns the selected item(s). If the multi property is true,
96 * getSelection will return an array, otherwise it will return
97 * the selected item or undefined if there is no selection.
99 getSelection: function() {
100 return this.multi ? this.selection : this.selection[0];
103 * Indicates if a given item is selected.
105 * @param {any} item The item whose selection state should be checked.
106 * @returns Returns true if `item` is selected.
108 isSelected: function(item) {
109 return this.selection.indexOf(item) >= 0;
111 setItemSelected: function(item, isSelected) {
112 if (item !== undefined && item !== null) {
114 this.selection.push(item);
116 var i = this.selection.indexOf(item);
118 this.selection.splice(i, 1);
121 this.fire("core-select", {isSelected: isSelected, item: item});
125 * Set the selection state for a given `item`. If the multi property
126 * is true, then the selected state of `item` will be toggled; otherwise
127 * the `item` will be selected.
129 * @param {any} item: The item to select.
131 select: function(item) {
134 } else if (this.getSelection() !== item) {
135 this.setItemSelected(this.getSelection(), false);
136 this.setItemSelected(item, true);
140 * Toggles the selection state for `item`.
142 * @param {any} item: The item to toggle.
144 toggle: function(item) {
145 this.setItemSelected(item, !this.isSelected(item));