Skip to content

Commit 6976b0c

Browse files
committed
Add array-selector. Use stable key sort if no view sort given.
1 parent 0a2cc19 commit 6976b0c

File tree

2 files changed

+108
-3
lines changed

2 files changed

+108
-3
lines changed
+99
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
<!--
2+
@license
3+
Copyright (c) 2014 The Polymer Project Authors. All rights reserved.
4+
This code may only be used under the BSD style license found at http://polymer.github.io/LICENSE.txt
5+
The complete set of authors may be found at http://polymer.github.io/AUTHORS.txt
6+
The complete set of contributors may be found at http://polymer.github.io/CONTRIBUTORS.txt
7+
Code distributed by Google as part of the polymer project is also
8+
subject to an additional IP rights grant found at http://polymer.github.io/PATENTS.txt
9+
-->
10+
11+
<script>
12+
13+
using('Collection', function(Collection) {
14+
15+
Polymer({
16+
is: 'x-array-selector',
17+
18+
published: {
19+
items: Array,
20+
selected: {
21+
type: Object,
22+
notify: true
23+
},
24+
toggle: Boolean,
25+
multi: Boolean
26+
},
27+
28+
bind: {
29+
multi: 'update',
30+
items: 'update'
31+
},
32+
33+
update: function() {
34+
// Unbind previous selection
35+
if (Array.isArray(this.selected)) {
36+
for (var i=0; i<this.selected.length; i++) {
37+
this.unbindPaths('selected.' + i);
38+
}
39+
} else {
40+
this.unbindPaths('selected');
41+
}
42+
// Initialize selection
43+
if (this.multi) {
44+
this.selected = [];
45+
this.selectedCollection = Collection.get(this.selected);
46+
} else {
47+
this.selected = null;
48+
}
49+
if (this.items) {
50+
this.itemsCollection = Collection.get(this.items);
51+
}
52+
},
53+
54+
deselect: function(item) {
55+
if (this.multi) {
56+
// var sidx = this.selected.indexOf(item);
57+
// if (sidx >= 0) {
58+
var skey = Collection.get(this.selected).getKey(item);
59+
if (skey >= 0) {
60+
// this.selected.splice(sidx, 1);
61+
Collection.get(this.selected).remove(item);
62+
this.unbindPaths('selected.' + skey);
63+
return true;
64+
}
65+
} else {
66+
this.selected = null;
67+
this.unbindPaths('selected');
68+
}
69+
},
70+
71+
select: function(item) {
72+
var key = Collection.get(this.items).getKey(item);
73+
if (this.multi) {
74+
// var sidx = this.selected.indexOf(item);
75+
// if (sidx < 0) {
76+
var skey = Collection.get(this.selected).getKey(item);
77+
if (skey >= 0) {
78+
this.deselect(item);
79+
} else if (this.toggle) {
80+
// this.selected.push(item);
81+
// this.bindPaths('selected.' + sidx, 'items.' + skey);
82+
skey = Collection.get(this.selected).add(item);
83+
this.bindPaths('selected.' + skey, 'items.' + key);
84+
}
85+
} else {
86+
if (this.toggle && item == this.selected) {
87+
this.deselect();
88+
} else {
89+
this.selected = item;
90+
this.bindPaths('selected', 'items.' + key);
91+
}
92+
}
93+
}
94+
95+
});
96+
97+
});
98+
99+
</script>

src/lib/template/x-repeat.html

+9-3
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@
147147
_render: function(splices) {
148148
var c = this.collection;
149149
if (splices) {
150-
if (this._sortFn) {
150+
if (this._sortFn || splices[0].index == null) {
151151
this._applySplicesViewSort(splices);
152152
} else {
153153
this._applySplicesArraySort(splices);
@@ -193,13 +193,18 @@
193193
}
194194
},
195195

196+
_keySort: function(a, b) {
197+
return this.collection.getKey(a) - this.collection.getKey(b);
198+
},
199+
196200
_applySplicesViewSort: function(splices) {
197201
var c = this.collection;
198202
var keys = this._orderedKeys;
199203
var rows = this.rows;
200204
var removedRows = [];
201205
var addedKeys = [];
202206
var pool = [];
207+
var sortFn = this._sortFn || this._keySort.bind(this);
203208
splices.forEach(function(s) {
204209
// Collect all removed row idx's
205210
for (var i=0; i<s.removed.length; i++) {
@@ -233,7 +238,7 @@
233238
}
234239
// Sort added keys
235240
addedKeys.sort(function(a, b) {
236-
return this._sortFn(c.getItem(a), c.getItem(b));
241+
return this.sortFn(c.getItem(a), c.getItem(b));
237242
}, this);
238243
// Insert new rows using sort (from pool or newly created)
239244
var start = 0;
@@ -248,11 +253,12 @@
248253
var item = c.getItem(key);
249254
var end = this.rows.length - 1;
250255
var idx = -1;
256+
var sortFn = this._sortFn || this._keySort.bind(this);
251257
// Binary search for insertion point
252258
while (start <= end) {
253259
var mid = (start + end) >> 1;
254260
var midKey = this._orderedKeys[mid];
255-
var cmp = this._sortFn(c.getItem(midKey), item);
261+
var cmp = sortFn(c.getItem(midKey), item);
256262
if (cmp < 0) {
257263
start = mid + 1;
258264
} else if (cmp > 0) {

0 commit comments

Comments
 (0)