Skip to content

Commit

Permalink
Add array-selector. Use stable key sort if no view sort given.
Browse files Browse the repository at this point in the history
  • Loading branch information
kevinpschaaf committed Feb 26, 2015
1 parent 0a2cc19 commit 6976b0c
Show file tree
Hide file tree
Showing 2 changed files with 108 additions and 3 deletions.
99 changes: 99 additions & 0 deletions src/lib/template/x-array-selector.html
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>
12 changes: 9 additions & 3 deletions src/lib/template/x-repeat.html
Original file line number Diff line number Diff line change
Expand Up @@ -147,7 +147,7 @@
_render: function(splices) {
var c = this.collection;
if (splices) {
if (this._sortFn) {
if (this._sortFn || splices[0].index == null) {
this._applySplicesViewSort(splices);
} else {
this._applySplicesArraySort(splices);
Expand Down Expand Up @@ -193,13 +193,18 @@
}
},

_keySort: function(a, b) {
return this.collection.getKey(a) - this.collection.getKey(b);
},

_applySplicesViewSort: function(splices) {
var c = this.collection;
var keys = this._orderedKeys;
var rows = this.rows;
var removedRows = [];
var addedKeys = [];
var pool = [];
var sortFn = this._sortFn || this._keySort.bind(this);
splices.forEach(function(s) {
// Collect all removed row idx's
for (var i=0; i<s.removed.length; i++) {
Expand Down Expand Up @@ -233,7 +238,7 @@
}
// Sort added keys
addedKeys.sort(function(a, b) {
return this._sortFn(c.getItem(a), c.getItem(b));
return this.sortFn(c.getItem(a), c.getItem(b));
}, this);
// Insert new rows using sort (from pool or newly created)
var start = 0;
Expand All @@ -248,11 +253,12 @@
var item = c.getItem(key);
var end = this.rows.length - 1;
var idx = -1;
var sortFn = this._sortFn || this._keySort.bind(this);
// Binary search for insertion point
while (start <= end) {
var mid = (start + end) >> 1;
var midKey = this._orderedKeys[mid];
var cmp = this._sortFn(c.getItem(midKey), item);
var cmp = sortFn(c.getItem(midKey), item);
if (cmp < 0) {
start = mid + 1;
} else if (cmp > 0) {
Expand Down

0 comments on commit 6976b0c

Please sign in to comment.