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

Select items #457

Open
wants to merge 7 commits into
base: master
Choose a base branch
from
Open
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
104 changes: 104 additions & 0 deletions iron-list.html
Original file line number Diff line number Diff line change
Expand Up @@ -1500,6 +1500,110 @@
}
},

/**
* 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);
}
},

selectIndexesV1: function(indexes) {
var items = [];
var item = {};
var index = -1;
var model = {};
// 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);
items = indexes.map(function(idx) {
return this.items[idx];
}.bind(this));
} else {
items = this.items;
}
items = items.filter(function(item) {
return !(this.$.selector).isSelected(item);
}.bind(this));
for (var i = 0, l = items.length; i < l; i++) {
item = items[i];
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);
}
}
this.$.selector.selectItems(items);
},

selectIndexesV2: function(indexes) {
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(index);
}
}.bind(this));
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();
},

/**
* Deselects the given item.
*
Expand Down