OLD | NEW |
(Empty) | |
| 1 <!-- |
| 2 Copyright (c) 2015 The Polymer Project Authors. All rights reserved. |
| 3 This code may only be used under the BSD style license found at http://polymer.g
ithub.io/LICENSE.txt |
| 4 The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt |
| 5 The complete set of contributors may be found at http://polymer.github.io/CONTRI
BUTORS.txt |
| 6 Code distributed by Google as part of the polymer project is also |
| 7 subject to an additional IP rights grant found at http://polymer.github.io/PATEN
TS.txt |
| 8 --> |
| 9 |
| 10 <link rel="import" href="../polymer/polymer.html"> |
| 11 <link rel="import" href="iron-selectable.html"> |
| 12 |
| 13 <script> |
| 14 |
| 15 Polymer.IronMultiSelectableBehavior = [ |
| 16 Polymer.IronSelectableBehavior, { |
| 17 |
| 18 properties: { |
| 19 |
| 20 /** |
| 21 * If true, multiple selections are allowed. |
| 22 * |
| 23 * @attribute multi |
| 24 * @type Boolean |
| 25 * @default false |
| 26 */ |
| 27 multi: { |
| 28 type: Boolean, |
| 29 value: false, |
| 30 observer: 'multiChanged' |
| 31 }, |
| 32 |
| 33 /** |
| 34 * Gets or sets the selected elements. This is used instead of `selected
` when `multi` |
| 35 * is true. |
| 36 * |
| 37 * @attribute selectedValues |
| 38 * @type Array |
| 39 */ |
| 40 selectedValues: { |
| 41 type: Array, |
| 42 notify: true |
| 43 }, |
| 44 |
| 45 /** |
| 46 * Returns an array of currently selected items. |
| 47 * |
| 48 * @attribute selectedItems |
| 49 * @type Array |
| 50 */ |
| 51 selectedItems: { |
| 52 type: Array, |
| 53 readOnly: true, |
| 54 notify: true |
| 55 }, |
| 56 |
| 57 }, |
| 58 |
| 59 observers: [ |
| 60 '_updateSelected(attrForSelected, selectedValues)' |
| 61 ], |
| 62 |
| 63 /** |
| 64 * Selects the given value. If the `multi` property is true, then the sele
cted state of the |
| 65 * `value` will be toggled; otherwise the `value` will be selected. |
| 66 * |
| 67 * @method select |
| 68 * @param {String} value the value to select. |
| 69 */ |
| 70 select: function(value) { |
| 71 if (this.multi) { |
| 72 if (this.selectedValues) { |
| 73 this._toggleSelected(value); |
| 74 } else { |
| 75 this.selectedValues = [value]; |
| 76 } |
| 77 } else { |
| 78 this.selected = value; |
| 79 } |
| 80 }, |
| 81 |
| 82 multiChanged: function(multi) { |
| 83 this._selection.multi = multi; |
| 84 }, |
| 85 |
| 86 _updateSelected: function() { |
| 87 if (this.multi) { |
| 88 this._selectMulti(this.selectedValues); |
| 89 } else { |
| 90 this._selectSelected(this.selected); |
| 91 } |
| 92 }, |
| 93 |
| 94 _selectMulti: function(values) { |
| 95 this._selection.clear(); |
| 96 if (values) { |
| 97 for (var i = 0; i < values.length; i++) { |
| 98 this._selection.setItemSelected(this._valueToItem(values[i]), true); |
| 99 } |
| 100 } |
| 101 }, |
| 102 |
| 103 _selectionChange: function() { |
| 104 var s = this._selection.get(); |
| 105 if (this.multi) { |
| 106 this._setSelectedItems(s); |
| 107 } else { |
| 108 this._setSelectedItems([s]); |
| 109 this._setSelectedItem(s); |
| 110 } |
| 111 }, |
| 112 |
| 113 _toggleSelected: function(value) { |
| 114 var i = this.selectedValues.indexOf(value); |
| 115 var unselected = i < 0; |
| 116 if (unselected) { |
| 117 this.selectedValues.push(value); |
| 118 } else { |
| 119 this.selectedValues.splice(i, 1); |
| 120 } |
| 121 this._selection.setItemSelected(this._valueToItem(value), unselected); |
| 122 } |
| 123 |
| 124 } |
| 125 ]; |
| 126 |
| 127 </script> |
OLD | NEW |