From 0f8b01567fab6e9c9175683fc49add51fa5a4186 Mon Sep 17 00:00:00 2001 From: Westbrook Johnson Date: Sun, 18 Jun 2017 22:49:11 -0400 Subject: [PATCH 1/7] Everything local --- iron-list.html | 80 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 80 insertions(+) diff --git a/iron-list.html b/iron-list.html index 4a0fe385..fb2e143e 100644 --- a/iron-list.html +++ b/iron-list.html @@ -1500,6 +1500,86 @@ } }, + /** + * Select the array of list items provided or + * select the entire list. + * + * @method selectItems + * @param {array} items The array of item objects or indexes + */ + selectItems: function(items) { + var indexes = []; + if (arguments.length > 1 || (typeof items !== 'undefined' && !Array.isArray(items))) { + items = !!Array.from ? Array.from(arguments) : [].slice.call(arguments); + } + items = items || this.items; + indexes = items.map(function(item) { + return (typeof item === 'number') ? item : this.items.indexOf(item); + }.bind(this)); + return this.selectIndexes(indexes); + }, + + selectIndexes: function(indexes) { + if (this.$.selector.selectIndex) { + // v2 + this.selectIndexesV2(indexes); + } else { + // v1 + this.selectIndexesV1(indexes); + } + }, + + // TODO: finish me!! + selectIndexesV1: function(indexes) { + var item = {}; + var model = {}; + var skey = ''; + var icol = Polymer.Collection.get((this.$.selector).items); + var key = ''; + // Cast an arguments list that doesn't consist of a single array to an array + if (arguments.length > 1 || (typeof items !== 'undefined' && !Array.isArray(items))) { + items = !!Array.from ? Array.from(arguments) : [].slice.call(arguments); + } + items = items || this.items; + items = items.map(function(item) { + return this._getNormalizedItem(item); + }.bind(this)); + this.$.selector.set('selected', items); + items = items.filter(function(item) { + return !(this.$.selector).isSelected(item); + }.bind(this)); + this.$.selector._selectedColl = Polymer.Collection.get((this.$.selector).selected); + for (var i = 0, l = items.length; i < l; i++) { + item = items[i]; + model = this._getModelFromItem(item); + if (model) { + model[this.selectedAs] = true; + } + key = icol.getKey(item); + skey = (this.$.selector)._selectedColl.getKey(item); + (this.$.selector).linkPaths("selected." + skey, "items." + key); + this.updateSizeForItem(item); + } + }, + + selectIndexesV2: function(indexes) { + this.$.selector.__selectedMap.clear(); + var items = indexes.map(function(idx) { + var item = this.items[idx]; + if (this._isIndexRendered(idx)) { + var model = this.modelForElement(this._physicalItems[this._getPhysicalIndex(idx)]); + if (model) { + model[this.selectedAs] = true; + } + this.updateSizeForIndex(idx); + } + this.$.selector.__selectedMap.set(item, idx); + return item; + }.bind(this)); + this.$.selector.__updateLinks(); + this.$.selector.set('selected', items); + }, + /** * Deselects the given item. * From 7c3dbdf4b2b48e02a4a2a19ecd88bb26f52c5be1 Mon Sep 17 00:00:00 2001 From: Westbrook Johnson Date: Mon, 19 Jun 2017 19:26:44 -0400 Subject: [PATCH 2/7] Select All separately --- iron-list.html | 46 ++++++++++++++++++++++++++++++++++++---------- 1 file changed, 36 insertions(+), 10 deletions(-) diff --git a/iron-list.html b/iron-list.html index fb2e143e..6c6c5b63 100644 --- a/iron-list.html +++ b/iron-list.html @@ -1563,21 +1563,47 @@ }, selectIndexesV2: function(indexes) { - this.$.selector.__selectedMap.clear(); - var items = indexes.map(function(idx) { - var item = this.items[idx]; - if (this._isIndexRendered(idx)) { - var model = this.modelForElement(this._physicalItems[this._getPhysicalIndex(idx)]); + this.selectedItems.map(function(item){ + var index = this.items.indexOf(item); + if (this._isIndexRendered(index)) { + var model = this.modelForElement(this._physicalItems[this._getPhysicalIndex(index)]); + model[this.selectedAs] = false; + this.updateSizeForIndex(index); + } + }.bind(this)); + indexes.forEach(function(index) { + if (this._isIndexRendered(index)) { + var model = this.modelForElement(this._physicalItems[this._getPhysicalIndex(index)]); if (model) { model[this.selectedAs] = true; } - this.updateSizeForIndex(idx); + this.updateSizeForIndex(index); } - this.$.selector.__selectedMap.set(item, idx); - return item; }.bind(this)); - this.$.selector.__updateLinks(); - this.$.selector.set('selected', items); + this.$.selector.selectIndexes(indexes); + }, + + selectAll: function() { + if (this.$.selector.selectIndex) { + // v2 + this.selectAllV2(); + } else { + // v1 + this.selectIndexes(); + } + }, + + selectAllV2: function() { + this.items.forEach(function(item, index) { + if (this._isIndexRendered(index)) { + var model = this.modelForElement(this._physicalItems[this._getPhysicalIndex(index)]); + if (model) { + model[this.selectedAs] = true; + } + this.updateSizeForIndex(index); + } + }.bind(this)); + this.$.selector.selectAll(); }, /** From 04ca1bc06d85d5d35a15575b77377ef72212ee27 Mon Sep 17 00:00:00 2001 From: Westbrook Johnson Date: Mon, 19 Jun 2017 20:59:03 -0400 Subject: [PATCH 3/7] Remove _getNormalizedItem --- iron-list.html | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/iron-list.html b/iron-list.html index 6c6c5b63..a27436be 100644 --- a/iron-list.html +++ b/iron-list.html @@ -1531,6 +1531,7 @@ // TODO: finish me!! selectIndexesV1: function(indexes) { + var items = []; var item = {}; var model = {}; var skey = ''; @@ -1540,10 +1541,13 @@ if (arguments.length > 1 || (typeof items !== 'undefined' && !Array.isArray(items))) { items = !!Array.from ? Array.from(arguments) : [].slice.call(arguments); } - items = items || this.items; - items = items.map(function(item) { - return this._getNormalizedItem(item); - }.bind(this)); + if (!indexes.length) { + items = this.items; + } else { + item = indexes.map(function(idx) { + return this.items[idx]; + }.bind(this)); + } this.$.selector.set('selected', items); items = items.filter(function(item) { return !(this.$.selector).isSelected(item); From 67b587ba5dd753beb1e138ac25c1684bbea0ad97 Mon Sep 17 00:00:00 2001 From: Westbrook Johnson Date: Mon, 19 Jun 2017 21:00:47 -0400 Subject: [PATCH 4/7] Undefined check --- iron-list.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/iron-list.html b/iron-list.html index a27436be..ca5a971c 100644 --- a/iron-list.html +++ b/iron-list.html @@ -1541,7 +1541,7 @@ if (arguments.length > 1 || (typeof items !== 'undefined' && !Array.isArray(items))) { items = !!Array.from ? Array.from(arguments) : [].slice.call(arguments); } - if (!indexes.length) { + if (typeof indexes === 'undefined' || !indexes.length) { items = this.items; } else { item = indexes.map(function(idx) { From 9aa358cc5c9b8c8bd56b851076bab0738029f002 Mon Sep 17 00:00:00 2001 From: Westbrook Johnson Date: Mon, 19 Jun 2017 21:02:31 -0400 Subject: [PATCH 5/7] Switch logic --- iron-list.html | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/iron-list.html b/iron-list.html index ca5a971c..52ebae4c 100644 --- a/iron-list.html +++ b/iron-list.html @@ -1538,15 +1538,13 @@ var icol = Polymer.Collection.get((this.$.selector).items); var key = ''; // Cast an arguments list that doesn't consist of a single array to an array - if (arguments.length > 1 || (typeof items !== 'undefined' && !Array.isArray(items))) { - items = !!Array.from ? Array.from(arguments) : [].slice.call(arguments); - } - if (typeof indexes === 'undefined' || !indexes.length) { - items = this.items; - } else { + if (arguments.length > 1 || (typeof indexes !== 'undefined' && !Array.isArray(indexes))) { + indexes = !!Array.from ? Array.from(arguments) : [].slice.call(arguments); item = indexes.map(function(idx) { return this.items[idx]; }.bind(this)); + } else { + items = this.items; } this.$.selector.set('selected', items); items = items.filter(function(item) { From e15dc6a5a11e84683f2c2d9613a243b26840cbe3 Mon Sep 17 00:00:00 2001 From: Westbrook Johnson Date: Mon, 19 Jun 2017 21:09:16 -0400 Subject: [PATCH 6/7] Update to modelForElement --- iron-list.html | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/iron-list.html b/iron-list.html index 52ebae4c..9563c1a6 100644 --- a/iron-list.html +++ b/iron-list.html @@ -1533,6 +1533,7 @@ selectIndexesV1: function(indexes) { var items = []; var item = {}; + var index = -1; var model = {}; var skey = ''; var icol = Polymer.Collection.get((this.$.selector).items); @@ -1553,14 +1554,17 @@ this.$.selector._selectedColl = Polymer.Collection.get((this.$.selector).selected); for (var i = 0, l = items.length; i < l; i++) { item = items[i]; - model = this._getModelFromItem(item); - if (model) { - model[this.selectedAs] = true; + index = (indexes) ? indexes[i] : i; + if (this._isIndexRendered(index)) { + model = this.modelForElement(this._physicalItems[this._getPhysicalIndex(index)]); + if (model) { + model[this.selectedAs] = true; + } + this.updateSizeForIndex(index); } key = icol.getKey(item); skey = (this.$.selector)._selectedColl.getKey(item); (this.$.selector).linkPaths("selected." + skey, "items." + key); - this.updateSizeForItem(item); } }, From c9c30afb80f261fb6d7597da2e7dcf5a5793e6ce Mon Sep 17 00:00:00 2001 From: Westbrook Johnson Date: Wed, 21 Jun 2017 20:45:11 -0400 Subject: [PATCH 7/7] V1 versions --- iron-list.html | 12 ++---------- 1 file changed, 2 insertions(+), 10 deletions(-) diff --git a/iron-list.html b/iron-list.html index 9563c1a6..93c41396 100644 --- a/iron-list.html +++ b/iron-list.html @@ -1529,29 +1529,23 @@ } }, - // TODO: finish me!! selectIndexesV1: function(indexes) { var items = []; var item = {}; var index = -1; var model = {}; - var skey = ''; - var icol = Polymer.Collection.get((this.$.selector).items); - var key = ''; // Cast an arguments list that doesn't consist of a single array to an array if (arguments.length > 1 || (typeof indexes !== 'undefined' && !Array.isArray(indexes))) { indexes = !!Array.from ? Array.from(arguments) : [].slice.call(arguments); - item = indexes.map(function(idx) { + items = indexes.map(function(idx) { return this.items[idx]; }.bind(this)); } else { items = this.items; } - this.$.selector.set('selected', items); items = items.filter(function(item) { return !(this.$.selector).isSelected(item); }.bind(this)); - this.$.selector._selectedColl = Polymer.Collection.get((this.$.selector).selected); for (var i = 0, l = items.length; i < l; i++) { item = items[i]; index = (indexes) ? indexes[i] : i; @@ -1562,10 +1556,8 @@ } this.updateSizeForIndex(index); } - key = icol.getKey(item); - skey = (this.$.selector)._selectedColl.getKey(item); - (this.$.selector).linkPaths("selected." + skey, "items." + key); } + this.$.selector.selectItems(items); }, selectIndexesV2: function(indexes) {