UNPKG

five-bells-visualization

Version:
180 lines (158 loc) 4.66 kB
<!-- @license Copyright (c) 2014 The Polymer Project Authors. All rights reserved. This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt Code distributed by Google as part of the polymer project is also subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt --> <link rel="import" href="array-observe.html"> <link rel="import" href="debounce.html"> <script> Polymer._collections = new WeakMap(); Polymer.Collection = function(userArray, noObserve) { Polymer._collections.set(userArray, this); this.userArray = userArray; this.store = userArray.slice(); this.callbacks = []; this.debounce = null; this.map = null; this.added = []; this.removed = []; if (!noObserve) { Polymer.ArrayObserve.observe(userArray, this.applySplices.bind(this)); this.initMap(); } }; Polymer.Collection.prototype = { constructor: Polymer.Collection, initMap: function() { var map = this.map = new WeakMap(); var s = this.store; var u = this.userArray; for (var i=0; i<s.length; i++) { var v = s[i]; if (v) { switch (typeof v) { case 'string': v = s[i] = u[i]= new String(v); break; case 'number': v = s[i] = u[i]= new Number(v); break; case 'boolean': v = s[i] = u[i]= new Boolean(v); break; } map.set(v, i); } } }, add: function(item, squelch) { var key = this.store.push(item) - 1; if (item != null && this.map) { this.map.set(item, key); } if (!squelch) { this.added.push(key); this.debounce = Polymer.Debounce(this.debounce, this.notify.bind(this)); } return key; }, removeKey: function(key) { if (this.map) { this.map.delete(this.store[key]); } delete this.store[key]; this.removed.push(key); this.debounce = Polymer.Debounce(this.debounce, this.notify.bind(this)); }, remove: function(item, squelch) { var key = this.getKey(item); if (item != null && this.map) { this.map.delete(item); } delete this.store[key]; if (!squelch) { this.removed.push(key); this.debounce = Polymer.Debounce(this.debounce, this.notify.bind(this)); } return key; }, notify: function(splices) { if (!splices) { splices = [{ added: this.added, removed: this.removed }]; this.added = []; this.removed = []; } this.callbacks.forEach(function(cb) { cb(splices); }, this); }, observe: function(callback) { this.callbacks.push(callback); }, unobserve: function(callback) { this.callbacks.splice(this.callbacks.indexOf(callback), 1); }, getKey: function(item) { if (item != null && this.map) { return this.map.get(item); } else { return this.store.indexOf(item); } }, getKeys: function() { return Object.keys(this.store); }, setItem: function(key, value) { this.store[key] = value; }, getItem: function(key) { return this.store[key]; }, getItems: function() { var items = [], store = this.store; for (var key in store) { items.push(store[key]); } return items; }, applySplices: function(splices) { var map = this.map; var keySplices = []; for (var i=0; i<splices.length; i++) { var j, o, key, s = splices[i]; // Removed keys var removed = []; for (j=0; j<s.removed.length; j++) { o = s.removed[j]; key = this.remove(o, true); removed.push(key); } // Added keys var added = []; for (j=0; j<s.addedCount; j++) { o = this.userArray[s.index + j]; key = this.add(o, true); added.push(key); } // Record splice keySplices.push({ index: s.index, removed: removed, added: added }); } this.notify(keySplices); } }; Polymer.Collection.get = function(userArray, noObserve) { return Polymer._collections.get(userArray) || new Polymer.Collection(userArray, noObserve); }; </script>