Skip to content

Commit

Permalink
Default selected to empty array. Add isSelected API.
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinpschaaf committed Aug 7, 2015
1 parent a370860 commit d4e7140
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
45 changes: 23 additions & 22 deletions src/lib/template/array-selector.html
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@
* An array containing items from which selection will be made.
*/
items: {
type: Array
type: Array,
observer: '_resetSelection'
},

/**
Expand Down Expand Up @@ -102,15 +103,11 @@
*/
multi: {
type: Boolean,
value: false
observer: '_resetSelection'
}
},

observers: [
'_resetSelection(items, multi)'
],

_resetSelection: function(items, multi) {
_resetSelection: function() {
// Unbind previous selection
if (Array.isArray(this.selected)) {
for (var i=0; i<this.selected.length; i++) {
Expand All @@ -120,10 +117,22 @@
this.unlinkPaths('selected');
}
// Initialize selection
if (multi && items) {
this.selected = [];
if (this.multi) {
if (!this.selected || this.selected.length) {
this.selected = [];
this._selectedColl = Polymer.Collection.get(this.selected);
}
} else {
this.selected = null;
this._selectedColl = null;
}
},

isSelected: function(item) {
if (this.multi) {
return this._selectedColl.getKey(item) !== undefined;
} else {
return this.selected == item;
}
},

Expand All @@ -132,14 +141,9 @@
*/
deselect: function(item) {
if (this.multi) {
var scol = Polymer.Collection.get(this.selected);
// var skey = scol.getKey(item);
// if (skey >= 0) {
var sidx = this.selected.indexOf(item);
if (sidx >= 0) {
var skey = scol.getKey(item);
this.splice('selected', sidx, 1);
// scol.remove(item);
if (this.isSelected(item)) {
var skey = this._selectedColl.getKey(item);
this.arrayDelete('selected', item);
this.unlinkPaths('selected.' + skey);
return true;
}
Expand All @@ -157,16 +161,13 @@
var icol = Polymer.Collection.get(this.items);
var key = icol.getKey(item);
if (this.multi) {
// var sidx = this.selected.indexOf(item);
// if (sidx < 0) {
var scol = Polymer.Collection.get(this.selected);
if (scol.getKey(item) !== undefined) {
if (this.isSelected(item)) {
if (this.toggle) {
this.deselect(item);
}
} else {
this.push('selected', item);
skey = scol.getKey(item);
skey = this._selectedColl.getKey(item);

This comment has been minimized.

Copy link
@viart

viart Aug 27, 2015

@kevinpschaaf seems like var is missing here :) (unfortunately, don't have a time for creation PR).

this.linkPaths('selected.' + skey, 'items.' + key);
}
} else {
Expand Down
2 changes: 1 addition & 1 deletion test/unit/array-selector.html
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@

test('bound defaults', function() {
assert.equal(bind.$.observer.singleSelected, null);
assert.equal(bind.$.observer.multiSelected, null);
assert.sameMembers(bind.$.observer.multiSelected, []);
bind.items = [
{name: 'one'},
{name: 'two'},
Expand Down

0 comments on commit d4e7140

Please sign in to comment.