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