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