-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add array-selector. Use stable key sort if no view sort given.
- Loading branch information
1 parent
0a2cc19
commit 6976b0c
Showing
2 changed files
with
108 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
<!-- | ||
@license | ||
Copyright (c) 2014 The Polymer Project Authors. All rights reserved. | ||
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt | ||
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt | ||
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt | ||
Code distributed by Google as part of the polymer project is also | ||
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt | ||
--> | ||
|
||
<script> | ||
|
||
using('Collection', function(Collection) { | ||
|
||
Polymer({ | ||
is: 'x-array-selector', | ||
|
||
published: { | ||
items: Array, | ||
selected: { | ||
type: Object, | ||
notify: true | ||
}, | ||
toggle: Boolean, | ||
multi: Boolean | ||
}, | ||
|
||
bind: { | ||
multi: 'update', | ||
items: 'update' | ||
}, | ||
|
||
update: function() { | ||
// Unbind previous selection | ||
if (Array.isArray(this.selected)) { | ||
for (var i=0; i<this.selected.length; i++) { | ||
this.unbindPaths('selected.' + i); | ||
} | ||
} else { | ||
this.unbindPaths('selected'); | ||
} | ||
// Initialize selection | ||
if (this.multi) { | ||
this.selected = []; | ||
this.selectedCollection = Collection.get(this.selected); | ||
} else { | ||
this.selected = null; | ||
} | ||
if (this.items) { | ||
this.itemsCollection = Collection.get(this.items); | ||
} | ||
}, | ||
|
||
deselect: function(item) { | ||
if (this.multi) { | ||
// var sidx = this.selected.indexOf(item); | ||
// if (sidx >= 0) { | ||
var skey = Collection.get(this.selected).getKey(item); | ||
if (skey >= 0) { | ||
// this.selected.splice(sidx, 1); | ||
Collection.get(this.selected).remove(item); | ||
this.unbindPaths('selected.' + skey); | ||
return true; | ||
} | ||
} else { | ||
this.selected = null; | ||
this.unbindPaths('selected'); | ||
} | ||
}, | ||
|
||
select: function(item) { | ||
var key = Collection.get(this.items).getKey(item); | ||
if (this.multi) { | ||
// var sidx = this.selected.indexOf(item); | ||
// if (sidx < 0) { | ||
var skey = Collection.get(this.selected).getKey(item); | ||
if (skey >= 0) { | ||
this.deselect(item); | ||
} else if (this.toggle) { | ||
// this.selected.push(item); | ||
// this.bindPaths('selected.' + sidx, 'items.' + skey); | ||
skey = Collection.get(this.selected).add(item); | ||
this.bindPaths('selected.' + skey, 'items.' + key); | ||
} | ||
} else { | ||
if (this.toggle && item == this.selected) { | ||
this.deselect(); | ||
} else { | ||
this.selected = item; | ||
this.bindPaths('selected', 'items.' + key); | ||
} | ||
} | ||
} | ||
|
||
}); | ||
|
||
}); | ||
|
||
</script> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters