forked from Saulis/iron-data-table
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patharray-datasource.js
65 lines (56 loc) · 1.75 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
61
62
63
64
65
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 (!filter[i].filter) {
continue;
} else if (!value || value.toString().toLowerCase().indexOf(filter[i].filter.toString().toLowerCase()) === -1) {
return false;
}
}
return true;
});
}
function _compare(a, b) {
if (a < b) {
return -1;
}
if (a > b) {
return 1;
}
return 0;
}
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);
};
}