OLD | NEW |
(Empty) | |
| 1 <!-- |
| 2 Copyright 2013 The Polymer Authors. All rights reserved. |
| 3 Use of this source code is governed by a BSD-style |
| 4 license that can be found in the LICENSE file. |
| 5 --> |
| 6 <!-- |
| 7 /** |
| 8 * @module Polymer Elements |
| 9 */ |
| 10 /** |
| 11 * polymer-selector is used to manage a list of elements that can be selected. |
| 12 * The attribute "selected" indicates which item element is being selected. |
| 13 * The attribute "multi" indicates if multiple items can be selected at once. |
| 14 * Tapping on the item element would fire "polymer-activate" event. Use |
| 15 * "polymer-select" event to listen for selection changes. |
| 16 * |
| 17 * Example: |
| 18 * |
| 19 * <polymer-selector selected="0"> |
| 20 * <div>Item 1</div> |
| 21 * <div>Item 2</div> |
| 22 * <div>Item 3</div> |
| 23 * </polymer-selector> |
| 24 * |
| 25 * polymer-selector is not styled. So one needs to use "polymer-selected" CSS |
| 26 * class to style the selected element. |
| 27 * |
| 28 * <style> |
| 29 * .item.polymer-selected { |
| 30 * background: #eee; |
| 31 * } |
| 32 * </style> |
| 33 * ... |
| 34 * <polymer-selector> |
| 35 * <div class="item">Item 1</div> |
| 36 * <div class="item">Item 2</div> |
| 37 * <div class="item">Item 3</div> |
| 38 * </polymer-selector> |
| 39 * |
| 40 * @class polymer-selector |
| 41 * @status stable |
| 42 */ |
| 43 /** |
| 44 * Fired when an item's selection state is changed. This event is fired both |
| 45 * when an item is selected or deselected. The `isSelected` detail property |
| 46 * contains the selection state. |
| 47 * |
| 48 * @event polymer-select |
| 49 * @param {Object} detail |
| 50 * @param {boolean} detail.isSelected true for selection and false for deselec
tion |
| 51 * @param {Object} detail.item the item element |
| 52 */ |
| 53 /** |
| 54 * Fired when an item element is tapped. |
| 55 * |
| 56 * @event polymer-activate |
| 57 * @param {Object} detail |
| 58 * @param {Object} detail.item the item element |
| 59 */ |
| 60 --> |
| 61 <link rel="import" href="../polymer/polymer.html"> |
| 62 <link rel="import" href="../polymer-selection/polymer-selection.html"> |
| 63 |
| 64 <polymer-element name="polymer-selector" |
| 65 attributes="selected multi valueattr selectedClass selectedProperty selected
Attribute selectedItem selectedModel selectedIndex notap target itemsSelector ac
tivateEvent"> |
| 66 <template> |
| 67 <polymer-selection id="selection" multi="{{multi}}" on-polymer-select="{{sel
ectionSelect}}"></polymer-selection> |
| 68 <content id="items" select="*"></content> |
| 69 </template> |
| 70 <script> |
| 71 Polymer('polymer-selector', { |
| 72 /** |
| 73 * Gets or sets the selected element. Default to use the index |
| 74 * of the item element. |
| 75 * |
| 76 * If you want a specific attribute value of the element to be |
| 77 * used instead of index, set "valueattr" to that attribute name. |
| 78 * |
| 79 * Example: |
| 80 * |
| 81 * <polymer-selector valueattr="label" selected="foo"> |
| 82 * <div label="foo"></div> |
| 83 * <div label="bar"></div> |
| 84 * <div label="zot"></div> |
| 85 * </polymer-selector> |
| 86 * |
| 87 * In multi-selection this should be an array of values. |
| 88 * |
| 89 * Example: |
| 90 * |
| 91 * <polymer-selector id="selector" valueattr="label" multi> |
| 92 * <div label="foo"></div> |
| 93 * <div label="bar"></div> |
| 94 * <div label="zot"></div> |
| 95 * </polymer-selector> |
| 96 * |
| 97 * this.$.selector.selected = ['foo', 'zot']; |
| 98 * |
| 99 * @attribute selected |
| 100 * @type Object |
| 101 * @default null |
| 102 */ |
| 103 selected: null, |
| 104 /** |
| 105 * If true, multiple selections are allowed. |
| 106 * |
| 107 * @attribute multi |
| 108 * @type boolean |
| 109 * @default false |
| 110 */ |
| 111 multi: false, |
| 112 /** |
| 113 * Specifies the attribute to be used for "selected" attribute. |
| 114 * |
| 115 * @attribute valueattr |
| 116 * @type string |
| 117 * @default 'name' |
| 118 */ |
| 119 valueattr: 'name', |
| 120 /** |
| 121 * Specifies the CSS class to be used to add to the selected element. |
| 122 * |
| 123 * @attribute selectedClass |
| 124 * @type string |
| 125 * @default 'polymer-selected' |
| 126 */ |
| 127 selectedClass: 'polymer-selected', |
| 128 /** |
| 129 * Specifies the property to be used to set on the selected element |
| 130 * to indicate its active state. |
| 131 * |
| 132 * @attribute selectedProperty |
| 133 * @type string |
| 134 * @default '' |
| 135 */ |
| 136 selectedProperty: '', |
| 137 /** |
| 138 * Specifies the property to be used to set on the selected element |
| 139 * to indicate its active state. |
| 140 * |
| 141 * @attribute selectedProperty |
| 142 * @type string |
| 143 * @default 'active' |
| 144 */ |
| 145 selectedAttribute: 'active', |
| 146 /** |
| 147 * Returns the currently selected element. In multi-selection this returns |
| 148 * an array of selected elements. |
| 149 * |
| 150 * @attribute selectedItem |
| 151 * @type Object |
| 152 * @default null |
| 153 */ |
| 154 selectedItem: null, |
| 155 /** |
| 156 * In single selection, this returns the model associated with the |
| 157 * selected element. |
| 158 * |
| 159 * @attribute selectedModel |
| 160 * @type Object |
| 161 * @default null |
| 162 */ |
| 163 selectedModel: null, |
| 164 /** |
| 165 * In single selection, this returns the selected index. |
| 166 * |
| 167 * @attribute selectedIndex |
| 168 * @type number |
| 169 * @default -1 |
| 170 */ |
| 171 selectedIndex: -1, |
| 172 /** |
| 173 * The target element that contains items. If this is not set |
| 174 * polymer-selector is the container. |
| 175 * |
| 176 * @attribute target |
| 177 * @type Object |
| 178 * @default null |
| 179 */ |
| 180 target: null, |
| 181 /** |
| 182 * This can be used to query nodes from the target node to be used for |
| 183 * selection items. Note this only works if the 'target' property is set. |
| 184 * |
| 185 * Example: |
| 186 * |
| 187 * <polymer-selector target="{{$.myForm}}" itemsSelector="input[type=r
adio]"></polymer-selector> |
| 188 * <form id="myForm"> |
| 189 * <label><input type="radio" name="color" value="red"> Red</label>
<br> |
| 190 * <label><input type="radio" name="color" value="green"> Green</lab
el> <br> |
| 191 * <label><input type="radio" name="color" value="blue"> Blue</label
> <br> |
| 192 * <p>color = {{color}}</p> |
| 193 * </form> |
| 194 * |
| 195 * @attribute itemSelector |
| 196 * @type string |
| 197 * @default '' |
| 198 */ |
| 199 itemsSelector: '', |
| 200 /** |
| 201 * The event that would be fired from the item element to indicate |
| 202 * it is being selected. |
| 203 * |
| 204 * @attribute activateEvent |
| 205 * @type string |
| 206 * @default 'tap' |
| 207 */ |
| 208 activateEvent: 'tap', |
| 209 notap: false, |
| 210 ready: function() { |
| 211 this.activateListener = this.activateHandler.bind(this); |
| 212 this.observer = new MutationObserver(this.updateSelected.bind(this)); |
| 213 if (!this.target) { |
| 214 this.target = this; |
| 215 } |
| 216 }, |
| 217 get items() { |
| 218 var nodes = this.target !== this ? (this.itemsSelector ? |
| 219 this.target.querySelectorAll(this.itemsSelector) : |
| 220 this.target.children) : this.$.items.getDistributedNodes(); |
| 221 return Array.prototype.filter.call(nodes || [], function(n) { |
| 222 return n && n.localName !== 'template'; |
| 223 }); |
| 224 }, |
| 225 targetChanged: function(old) { |
| 226 if (old) { |
| 227 this.removeListener(old); |
| 228 this.observer.disconnect(); |
| 229 } |
| 230 if (this.target) { |
| 231 this.addListener(this.target); |
| 232 this.observer.observe(this.target, {childList: true}); |
| 233 } |
| 234 }, |
| 235 addListener: function(node) { |
| 236 node.addEventListener(this.activateEvent, this.activateListener); |
| 237 }, |
| 238 removeListener: function(node) { |
| 239 node.removeEventListener(this.activateEvent, this.activateListener); |
| 240 }, |
| 241 get selection() { |
| 242 return this.$.selection.getSelection(); |
| 243 }, |
| 244 selectedChanged: function() { |
| 245 this.updateSelected(); |
| 246 }, |
| 247 updateSelected: function() { |
| 248 this.validateSelected(); |
| 249 if (this.multi) { |
| 250 this.clearSelection(); |
| 251 this.selected && this.selected.forEach(function(s) { |
| 252 this.valueToSelection(s); |
| 253 }, this); |
| 254 } else { |
| 255 this.valueToSelection(this.selected); |
| 256 } |
| 257 }, |
| 258 validateSelected: function() { |
| 259 // convert to an array for multi-selection |
| 260 if (this.multi && !Array.isArray(this.selected) && |
| 261 this.selected !== null && this.selected !== undefined) { |
| 262 this.selected = [this.selected]; |
| 263 } |
| 264 }, |
| 265 clearSelection: function() { |
| 266 if (this.multi) { |
| 267 this.selection.slice().forEach(function(s) { |
| 268 this.$.selection.setItemSelected(s, false); |
| 269 }, this); |
| 270 } else { |
| 271 this.$.selection.setItemSelected(this.selection, false); |
| 272 } |
| 273 this.selectedItem = null; |
| 274 this.$.selection.clear(); |
| 275 }, |
| 276 valueToSelection: function(value) { |
| 277 var item = (value === null || value === undefined) ? |
| 278 null : this.items[this.valueToIndex(value)]; |
| 279 this.$.selection.select(item); |
| 280 }, |
| 281 updateSelectedItem: function() { |
| 282 this.selectedItem = this.selection; |
| 283 }, |
| 284 selectedItemChanged: function() { |
| 285 if (this.selectedItem) { |
| 286 var t = this.selectedItem.templateInstance; |
| 287 this.selectedModel = t ? t.model : undefined; |
| 288 } else { |
| 289 this.selectedModel = null; |
| 290 } |
| 291 this.selectedIndex = this.selectedItem ? |
| 292 parseInt(this.valueToIndex(this.selected)) : -1; |
| 293 }, |
| 294 valueToIndex: function(value) { |
| 295 // find an item with value == value and return it's index |
| 296 for (var i=0, items=this.items, c; (c=items[i]); i++) { |
| 297 if (this.valueForNode(c) == value) { |
| 298 return i; |
| 299 } |
| 300 } |
| 301 // if no item found, the value itself is probably the index |
| 302 return value; |
| 303 }, |
| 304 valueForNode: function(node) { |
| 305 return node[this.valueattr] || node.getAttribute(this.valueattr); |
| 306 }, |
| 307 // events fired from <polymer-selection> object |
| 308 selectionSelect: function(e, detail) { |
| 309 this.updateSelectedItem(); |
| 310 if (detail.item) { |
| 311 this.applySelection(detail.item, detail.isSelected); |
| 312 } |
| 313 }, |
| 314 applySelection: function(item, isSelected) { |
| 315 if (this.selectedClass) { |
| 316 item.classList.toggle(this.selectedClass, isSelected); |
| 317 } |
| 318 if (this.selectedProperty) { |
| 319 item[this.selectedProperty] = isSelected; |
| 320 } |
| 321 if (this.selectedAttribute && item.setAttribute) { |
| 322 if (isSelected) { |
| 323 item.setAttribute(this.selectedAttribute, ''); |
| 324 } else { |
| 325 item.removeAttribute(this.selectedAttribute); |
| 326 } |
| 327 } |
| 328 }, |
| 329 // event fired from host |
| 330 activateHandler: function(e) { |
| 331 if (!this.notap) { |
| 332 var i = this.findDistributedTarget(e.target, this.items); |
| 333 if (i >= 0) { |
| 334 var item = this.items[i]; |
| 335 var s = this.valueForNode(item) || i; |
| 336 if (this.multi) { |
| 337 if (this.selected) { |
| 338 this.addRemoveSelected(s); |
| 339 } else { |
| 340 this.selected = [s]; |
| 341 } |
| 342 } else { |
| 343 this.selected = s; |
| 344 } |
| 345 this.asyncFire('polymer-activate', {item: item}); |
| 346 } |
| 347 } |
| 348 }, |
| 349 addRemoveSelected: function(value) { |
| 350 var i = this.selected.indexOf(value); |
| 351 if (i >= 0) { |
| 352 this.selected.splice(i, 1); |
| 353 } else { |
| 354 this.selected.push(value); |
| 355 } |
| 356 this.valueToSelection(value); |
| 357 }, |
| 358 findDistributedTarget: function(target, nodes) { |
| 359 // find first ancestor of target (including itself) that |
| 360 // is in nodes, if any |
| 361 while (target && target != this) { |
| 362 var i = Array.prototype.indexOf.call(nodes, target); |
| 363 if (i >= 0) { |
| 364 return i; |
| 365 } |
| 366 target = target.parentNode; |
| 367 } |
| 368 } |
| 369 }); |
| 370 </script> |
| 371 </polymer-element> |
OLD | NEW |