Skip to content
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

selectItems, selectAll, and tests #4705

Closed
wants to merge 1 commit 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
35 changes: 35 additions & 0 deletions src/lib/template/array-selector.html
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,41 @@
this.linkPaths('selectedItem', 'items.' + key);
}
}
},

/**
* Selects the given items.
*
* @param {array} items Items from `items` array to select
*/
selectItems: function(items) {
if (arguments.length > 1 || (typeof items !== 'undefined' && !Array.isArray(items))) {
items = Array.from(arguments);
}
var icol = Polymer.Collection.get(this.items);
var key;
var skey;
var item;
if (this.multi) {
this.set('selected', items);
this._selectedColl = Polymer.Collection.get(this.selected);
for (var i=0; i<items; i++) {
item = items[i];
key = icol.getKey(item);
skey = this._selectedColl.getKey(item);
this.linkPaths('selected.' + skey, 'items.' + key);
}
} else if (items.length){
this.select(items[0]);
}
},

/**
* Selects all of the items.
*
*/
selectAll: function() {
this.selectItems(this.items);
}

});
Expand Down
44 changes: 44 additions & 0 deletions test/unit/array-selector.html
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,50 @@
assert.equal(called, true);
});

test('select items', function() {
var el = document.querySelector('#multiConfigured');
el.clearSelection();
// Nothing selected
assert.sameMembers(el.selected, []);
assert.isFalse(el.isSelected(el.items[0]));
assert.isFalse(el.isSelected(el.items[1]));
assert.isFalse(el.isSelected(el.items[2]));
// Select 0, 2
el.selectItems([el.items[0], el.items[2]]);
assert.sameMembers(el.selected, [el.items[0], el.items[2]]);
assert.isTrue(el.isSelected(el.items[0]));
assert.isFalse(el.isSelected(el.items[1]));
assert.isTrue(el.isSelected(el.items[2]));
el.clearSelection();
// Nothing selected
assert.sameMembers(el.selected, []);
assert.isFalse(el.isSelected(el.items[0]));
assert.isFalse(el.isSelected(el.items[1]));
assert.isFalse(el.isSelected(el.items[2]));
// Select 1, 2
el.selectItems(el.items[1], el.items[2]);
assert.sameMembers(el.selected, [el.items[1], el.items[2]]);
assert.isFalse(el.isSelected(el.items[0]));
assert.isTrue(el.isSelected(el.items[1]));
assert.isTrue(el.isSelected(el.items[2]));
});

test('select all', function() {
var el = document.querySelector('#multiConfigured');
el.clearSelection();
// Nothing selected
assert.sameMembers(el.selected, []);
assert.isFalse(el.isSelected(el.items[0]));
assert.isFalse(el.isSelected(el.items[1]));
assert.isFalse(el.isSelected(el.items[2]));
// Select All
el.selectAll();
assert.sameMembers(el.selected, [el.items[0], el.items[1], el.items[2]]);
assert.isTrue(el.isSelected(el.items[0]));
assert.isTrue(el.isSelected(el.items[1]));
assert.isTrue(el.isSelected(el.items[2]));
});

});
</script>

Expand Down