Index: third_party/polymer/v0_8/components-chromium/polymer/src/lib/collection-extracted.js |
diff --git a/third_party/polymer/v0_8/components-chromium/polymer/src/lib/collection-extracted.js b/third_party/polymer/v0_8/components-chromium/polymer/src/lib/collection-extracted.js |
new file mode 100644 |
index 0000000000000000000000000000000000000000..76546478a777de6dfa2a8e8b19a09849272c60e9 |
--- /dev/null |
+++ b/third_party/polymer/v0_8/components-chromium/polymer/src/lib/collection-extracted.js |
@@ -0,0 +1,165 @@ |
+ |
+ |
+ 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); |
+ }; |
+ |