OLD | NEW |
(Empty) | |
| 1 |
| 2 <!-- |
| 3 Copyright (c) 2015 The Polymer Project Authors. All rights reserved. |
| 4 This code may only be used under the BSD style license found at http://polymer.g
ithub.io/LICENSE.txt |
| 5 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt |
| 6 The complete set of contributors may be found at http://polymer.github.io/CONTRI
BUTORS.txt |
| 7 Code distributed by Google as part of the polymer project is also |
| 8 subject to an additional IP rights grant found at http://polymer.github.io/PATEN
TS.txt |
| 9 --> |
| 10 |
| 11 <link rel="import" href="../polymer/polymer.html"> |
| 12 |
| 13 <script> |
| 14 |
| 15 Polymer.IronSelection = function(selectCallback) { |
| 16 this.selection = []; |
| 17 this.selectCallback = selectCallback; |
| 18 }; |
| 19 |
| 20 Polymer.IronSelection.prototype = { |
| 21 |
| 22 /** |
| 23 * Retrieves the selected item(s). |
| 24 * |
| 25 * @method get |
| 26 * @returns Returns the selected item(s). If the multi property is true, |
| 27 * `get` will return an array, otherwise it will return |
| 28 * the selected item or undefined if there is no selection. |
| 29 */ |
| 30 get: function() { |
| 31 return this.multi ? this.selection : this.selection[0]; |
| 32 }, |
| 33 |
| 34 /** |
| 35 * Clears all the selection except the ones indicated. |
| 36 * |
| 37 * @method clear |
| 38 * @param {Array} excludes items to be excluded. |
| 39 */ |
| 40 clear: function(excludes) { |
| 41 this.selection.slice().forEach(function(item) { |
| 42 if (!excludes || excludes.indexOf(item) < 0) { |
| 43 this.setItemSelected(item, false); |
| 44 } |
| 45 }, this); |
| 46 }, |
| 47 |
| 48 /** |
| 49 * Indicates if a given item is selected. |
| 50 * |
| 51 * @method isSelected |
| 52 * @param {any} item The item whose selection state should be checked. |
| 53 * @returns Returns true if `item` is selected. |
| 54 */ |
| 55 isSelected: function(item) { |
| 56 return this.selection.indexOf(item) >= 0; |
| 57 }, |
| 58 |
| 59 /** |
| 60 * Sets the selection state for a given item to either selected or deselecte
d. |
| 61 * |
| 62 * @method setItemSelected |
| 63 * @param {any} item The item to select. |
| 64 * @param {Boolean} isSelected True for selected, false for deselected. |
| 65 */ |
| 66 setItemSelected: function(item, isSelected) { |
| 67 if (item != null) { |
| 68 if (isSelected) { |
| 69 this.selection.push(item); |
| 70 } else { |
| 71 var i = this.selection.indexOf(item); |
| 72 if (i >= 0) { |
| 73 this.selection.splice(i, 1); |
| 74 } |
| 75 } |
| 76 if (this.selectCallback) { |
| 77 this.selectCallback(item, isSelected); |
| 78 } |
| 79 } |
| 80 }, |
| 81 |
| 82 /** |
| 83 * Sets the selection state for a given item. If the `multi` property |
| 84 * is true, then the selected state of `item` will be toggled; otherwise |
| 85 * the `item` will be selected. |
| 86 * |
| 87 * @method select |
| 88 * @param {any} item The item to select. |
| 89 */ |
| 90 select: function(item) { |
| 91 if (this.multi) { |
| 92 this.toggle(item); |
| 93 } else if (this.get() !== item) { |
| 94 this.setItemSelected(this.get(), false); |
| 95 this.setItemSelected(item, true); |
| 96 } |
| 97 }, |
| 98 |
| 99 /** |
| 100 * Toggles the selection state for `item`. |
| 101 * |
| 102 * @method toggle |
| 103 * @param {any} item The item to toggle. |
| 104 */ |
| 105 toggle: function(item) { |
| 106 this.setItemSelected(item, !this.isSelected(item)); |
| 107 } |
| 108 |
| 109 }; |
| 110 |
| 111 </script> |
OLD | NEW |