Skip to content

Add selectItems and associated tests #437

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 40 additions & 0 deletions iron-list.html
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
46 changes: 46 additions & 0 deletions test/selection.html
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});

</script>
Expand Down