diff --git a/iron-list.html b/iron-list.html index 111e6ae2..0c26330c 100644 --- a/iron-list.html +++ b/iron-list.html @@ -1608,6 +1608,46 @@ this.updateSizeForItem(item); }, + + /** + * 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 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); + } + }, + /** * Select or deselect a given item depending on whether the item * has already been selected. diff --git a/test/selection.html b/test/selection.html index 41795762..237fb6c5 100644 --- a/test/selection.html +++ b/test/selection.html @@ -423,6 +423,52 @@ MockInteractions.tap(item); assert.isNotNull(list.selectedItem); }); + + test('select items: all', function() { + container.useTabIndex = false; + list.items = buildDataSet(5000); + list.multiSelection = true; + Polymer.dom.flush(); + assert.isArray(list.selectedItems); + assert.equal(list.selectedItems.length, 0); + list.selectItems(); + assert.equal(list.selectedItems.length, 5000); + }); + + test('select items: array', function() { + container.useTabIndex = false; + list.items = buildDataSet(100); + list.multiSelection = true; + Polymer.dom.flush(); + assert.isArray(list.selectedItems); + assert.equal(list.selectedItems.length, 0); + var arrayToSelect = [0,3,7,9,30,39,41,42,55,60,67,68,71,82,88,90,97]; + list.selectItems(arrayToSelect); + assert.equal(list.selectedItems.length, arrayToSelect.length); + }); + + test('select items: arguments', function() { + container.useTabIndex = false; + list.items = buildDataSet(100); + list.multiSelection = true; + Polymer.dom.flush(); + assert.isArray(list.selectedItems); + assert.equal(list.selectedItems.length, 0); + var argumentsToSelect = [0,3,7,9,30,39,41,42,55,60,67,68,71,82,88,90,97]; + list.selectItems.apply(list, argumentsToSelect); + assert.equal(list.selectedItems.length, argumentsToSelect.length); + }); + + test('select items: single', function() { + container.useTabIndex = false; + list.items = buildDataSet(100); + list.multiSelection = true; + Polymer.dom.flush(); + assert.isArray(list.selectedItems); + assert.equal(list.selectedItems.length, 0); + list.selectItems(4); + assert.equal(list.selectedItems.length, 1); + }); });