-
Notifications
You must be signed in to change notification settings - Fork 65
/
array-datasource.js
60 lines (51 loc) · 1.85 KB
/
array-datasource.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
function ArrayDataSource(arr) {
function _filter(items, filter) {
if (filter.length === 0) {
return items;
}
return Array.prototype.filter.call(items, function(item, index) {
for (var i = 0; i < filter.length; i++) {
var value = Polymer.Base.get(filter[i].path, item);
if ([undefined, null, ''].indexOf(filter[i].filter) > -1) {
continue;
} else if ([undefined, null].indexOf(value) > -1 ||
value.toString().toLowerCase().indexOf(filter[i].filter.toString().toLowerCase()) === -1) {
return false;
}
}
return true;
});
}
function _compare(a, b) {
return (a===undefined)-(b===undefined) || (a==='')-(b==='') || (a===null)-(b===null) || +(a>b)||-(a<b);
}
function _sort(items, sortOrder) {
if (!sortOrder || sortOrder.length === 0) {
return items;
}
var multiSort = function() {
return function(a, b) {
return sortOrder.map(function(sort) {
if (sort.direction === 'asc') {
return _compare(Polymer.Base.get(sort.path, a), Polymer.Base.get(sort.path, b));
} else if (sort.direction === 'desc') {
return _compare(Polymer.Base.get(sort.path, b), Polymer.Base.get(sort.path, a));
}
return 0;
}).reduce(function firstNonZeroValue(p, n) {
return p ? p : n;
}, 0);
};
};
// make sure a copy is used so that original array is unaffected.
return Array.prototype.sort.call(items.slice(0), multiSort(sortOrder));
}
return function(opts, cb, err) {
var filteredItems = _filter(arr, opts.filter);
var sortedItems = _sort(filteredItems, opts.sortOrder);
var start = opts.page * opts.pageSize;
var end = start + opts.pageSize;
var slice = sortedItems.slice(start, end);
cb(slice, filteredItems.length);
};
}