|
| 1 | +<!-- |
| 2 | +@license |
| 3 | +Copyright (c) 2014 The Polymer Project Authors. All rights reserved. |
| 4 | +This code may only be used under the BSD style license found at http://polymer.github.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/CONTRIBUTORS.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/PATENTS.txt |
| 9 | +--> |
| 10 | + |
| 11 | +<script> |
| 12 | + |
| 13 | +using('Collection', function(Collection) { |
| 14 | + |
| 15 | + Polymer({ |
| 16 | + is: 'x-array-selector', |
| 17 | + |
| 18 | + published: { |
| 19 | + items: Array, |
| 20 | + selected: { |
| 21 | + type: Object, |
| 22 | + notify: true |
| 23 | + }, |
| 24 | + toggle: Boolean, |
| 25 | + multi: Boolean |
| 26 | + }, |
| 27 | + |
| 28 | + bind: { |
| 29 | + multi: 'update', |
| 30 | + items: 'update' |
| 31 | + }, |
| 32 | + |
| 33 | + update: function() { |
| 34 | + // Unbind previous selection |
| 35 | + if (Array.isArray(this.selected)) { |
| 36 | + for (var i=0; i<this.selected.length; i++) { |
| 37 | + this.unbindPaths('selected.' + i); |
| 38 | + } |
| 39 | + } else { |
| 40 | + this.unbindPaths('selected'); |
| 41 | + } |
| 42 | + // Initialize selection |
| 43 | + if (this.multi) { |
| 44 | + this.selected = []; |
| 45 | + this.selectedCollection = Collection.get(this.selected); |
| 46 | + } else { |
| 47 | + this.selected = null; |
| 48 | + } |
| 49 | + if (this.items) { |
| 50 | + this.itemsCollection = Collection.get(this.items); |
| 51 | + } |
| 52 | + }, |
| 53 | + |
| 54 | + deselect: function(item) { |
| 55 | + if (this.multi) { |
| 56 | + // var sidx = this.selected.indexOf(item); |
| 57 | + // if (sidx >= 0) { |
| 58 | + var skey = Collection.get(this.selected).getKey(item); |
| 59 | + if (skey >= 0) { |
| 60 | + // this.selected.splice(sidx, 1); |
| 61 | + Collection.get(this.selected).remove(item); |
| 62 | + this.unbindPaths('selected.' + skey); |
| 63 | + return true; |
| 64 | + } |
| 65 | + } else { |
| 66 | + this.selected = null; |
| 67 | + this.unbindPaths('selected'); |
| 68 | + } |
| 69 | + }, |
| 70 | + |
| 71 | + select: function(item) { |
| 72 | + var key = Collection.get(this.items).getKey(item); |
| 73 | + if (this.multi) { |
| 74 | + // var sidx = this.selected.indexOf(item); |
| 75 | + // if (sidx < 0) { |
| 76 | + var skey = Collection.get(this.selected).getKey(item); |
| 77 | + if (skey >= 0) { |
| 78 | + this.deselect(item); |
| 79 | + } else if (this.toggle) { |
| 80 | + // this.selected.push(item); |
| 81 | + // this.bindPaths('selected.' + sidx, 'items.' + skey); |
| 82 | + skey = Collection.get(this.selected).add(item); |
| 83 | + this.bindPaths('selected.' + skey, 'items.' + key); |
| 84 | + } |
| 85 | + } else { |
| 86 | + if (this.toggle && item == this.selected) { |
| 87 | + this.deselect(); |
| 88 | + } else { |
| 89 | + this.selected = item; |
| 90 | + this.bindPaths('selected', 'items.' + key); |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + |
| 95 | + }); |
| 96 | + |
| 97 | +}); |
| 98 | + |
| 99 | +</script> |
0 commit comments