OLD | NEW |
| (Empty) |
1 | |
2 | |
3 /** | |
4 * @param {!Function} selectCallback | |
5 * @constructor | |
6 */ | |
7 Polymer.IronSelection = function(selectCallback) { | |
8 this.selection = []; | |
9 this.selectCallback = selectCallback; | |
10 }; | |
11 | |
12 Polymer.IronSelection.prototype = { | |
13 | |
14 /** | |
15 * Retrieves the selected item(s). | |
16 * | |
17 * @method get | |
18 * @returns Returns the selected item(s). If the multi property is true, | |
19 * `get` will return an array, otherwise it will return | |
20 * the selected item or undefined if there is no selection. | |
21 */ | |
22 get: function() { | |
23 return this.multi ? this.selection : this.selection[0]; | |
24 }, | |
25 | |
26 /** | |
27 * Clears all the selection except the ones indicated. | |
28 * | |
29 * @method clear | |
30 * @param {Array} excludes items to be excluded. | |
31 */ | |
32 clear: function(excludes) { | |
33 this.selection.slice().forEach(function(item) { | |
34 if (!excludes || excludes.indexOf(item) < 0) { | |
35 this.setItemSelected(item, false); | |
36 } | |
37 }, this); | |
38 }, | |
39 | |
40 /** | |
41 * Indicates if a given item is selected. | |
42 * | |
43 * @method isSelected | |
44 * @param {*} item The item whose selection state should be checked. | |
45 * @returns Returns true if `item` is selected. | |
46 */ | |
47 isSelected: function(item) { | |
48 return this.selection.indexOf(item) >= 0; | |
49 }, | |
50 | |
51 /** | |
52 * Sets the selection state for a given item to either selected or deselecte
d. | |
53 * | |
54 * @method setItemSelected | |
55 * @param {*} item The item to select. | |
56 * @param {boolean} isSelected True for selected, false for deselected. | |
57 */ | |
58 setItemSelected: function(item, isSelected) { | |
59 if (item != null) { | |
60 if (isSelected) { | |
61 this.selection.push(item); | |
62 } else { | |
63 var i = this.selection.indexOf(item); | |
64 if (i >= 0) { | |
65 this.selection.splice(i, 1); | |
66 } | |
67 } | |
68 if (this.selectCallback) { | |
69 this.selectCallback(item, isSelected); | |
70 } | |
71 } | |
72 }, | |
73 | |
74 /** | |
75 * Sets the selection state for a given item. If the `multi` property | |
76 * is true, then the selected state of `item` will be toggled; otherwise | |
77 * the `item` will be selected. | |
78 * | |
79 * @method select | |
80 * @param {*} item The item to select. | |
81 */ | |
82 select: function(item) { | |
83 if (this.multi) { | |
84 this.toggle(item); | |
85 } else if (this.get() !== item) { | |
86 this.setItemSelected(this.get(), false); | |
87 this.setItemSelected(item, true); | |
88 } | |
89 }, | |
90 | |
91 /** | |
92 * Toggles the selection state for `item`. | |
93 * | |
94 * @method toggle | |
95 * @param {*} item The item to toggle. | |
96 */ | |
97 toggle: function(item) { | |
98 this.setItemSelected(item, !this.isSelected(item)); | |
99 } | |
100 | |
101 }; | |
102 | |
OLD | NEW |