4
Copyright (c) 2015 The Polymer Project Authors. All rights reserved.
5
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
6
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
7
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
8
Code distributed by Google as part of the polymer project is also
9
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
12
<link rel="import" href="../polymer/polymer.html">
17
* @param {!Function} selectCallback
20
Polymer.IronSelection = function(selectCallback) {
22
this.selectCallback = selectCallback;
25
Polymer.IronSelection.prototype = {
28
* Retrieves the selected item(s).
31
* @returns Returns the selected item(s). If the multi property is true,
32
* `get` will return an array, otherwise it will return
33
* the selected item or undefined if there is no selection.
36
return this.multi ? this.selection.slice() : this.selection[0];
40
* Clears all the selection except the ones indicated.
43
* @param {Array} excludes items to be excluded.
45
clear: function(excludes) {
46
this.selection.slice().forEach(function(item) {
47
if (!excludes || excludes.indexOf(item) < 0) {
48
this.setItemSelected(item, false);
54
* Indicates if a given item is selected.
57
* @param {*} item The item whose selection state should be checked.
58
* @returns Returns true if `item` is selected.
60
isSelected: function(item) {
61
return this.selection.indexOf(item) >= 0;
65
* Sets the selection state for a given item to either selected or deselected.
67
* @method setItemSelected
68
* @param {*} item The item to select.
69
* @param {boolean} isSelected True for selected, false for deselected.
71
setItemSelected: function(item, isSelected) {
73
if (isSelected !== this.isSelected(item)) {
74
// proceed to update selection only if requested state differs from current
76
this.selection.push(item);
78
var i = this.selection.indexOf(item);
80
this.selection.splice(i, 1);
83
if (this.selectCallback) {
84
this.selectCallback(item, isSelected);
91
* Sets the selection state for a given item. If the `multi` property
92
* is true, then the selected state of `item` will be toggled; otherwise
93
* the `item` will be selected.
96
* @param {*} item The item to select.
98
select: function(item) {
101
} else if (this.get() !== item) {
102
this.setItemSelected(this.get(), false);
103
this.setItemSelected(item, true);
108
* Toggles the selection state for `item`.
111
* @param {*} item The item to toggle.
113
toggle: function(item) {
114
this.setItemSelected(item, !this.isSelected(item));