-
Notifications
You must be signed in to change notification settings - Fork 2
/
progressivefilter.js
91 lines (81 loc) · 2.82 KB
/
progressivefilter.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
(function(factory) {"use strict";
if (typeof require === "function" && typeof exports === "object" && typeof module === "object") {
// CommonJS or Node: hard-coded dependency on "knockout"
factory(require("knockout"), exports);
} else if (typeof define === "function" && define["amd"]) {
// AMD anonymous module with hard-coded dependency on "knockout"
define(["knockout", "exports"], factory);
} else {
// <script> tag: use the global `ko` object
factory(ko, {});
}
}(function(ko, exports) {"use strict";
ko.extenders.progressivefilter = function(target, args) {
var requestAnimationFrame = window.requestAnimationFrame || window.mozRequestAnimationFrame || window.webkitRequestAnimationFrame || window.msRequestAnimationFrame || function(callback) { setTimeout(callback, 4); },
currentCount = 0,
props = {};
args = args || {};
target.progressivefilter = props;
props.unfilteredCollection = [];
props.unfilteredCollectionIndex = 0;
props.isFiltering = ko.observable(false);
props.filterFunction = args.filterFunction;
props.batchSize = Math.max(parseInt(args.batchSize, 10), 1);
props.add = args.addFunction || function(item) { target.peek().push(item); };
props.clear = args.clearFunction || function() { target([]); };
target.isFiltered = function(item) {
return !props.filterFunction || props.filterFunction(item);
};
target.filter = function(unfilteredCollection) {
var filteredCollection = [],
i;
for (i = 0; i < unfilteredCollection.length; i++) {
if (target.isFiltered(unfilteredCollection[i])) {
filteredCollection.push(unfilteredCollection[i]);
}
}
props.clear();
target(filteredCollection);
};
target.filterProgressive = function(unfilteredCollection) {
props.unfilteredCollection = unfilteredCollection.slice(0);
props.unfilteredCollectionIndex = 0;
currentCount = 0;
props.clear();
if (!props.isFiltering.peek()) {
props.isFiltering(true);
requestAnimationFrame(doFilter);
}
};
function doFilter() {
var item;
for (props.unfilteredCollectionIndex; props.unfilteredCollectionIndex < props.unfilteredCollection.length; props.unfilteredCollectionIndex++) {
item = props.unfilteredCollection[props.unfilteredCollectionIndex];
if (item && target.isFiltered(item)) {
props.add(item);
break;
}
}
currentCount++;
props.unfilteredCollectionIndex++;
if (props.unfilteredCollectionIndex < props.unfilteredCollection.length) {
if (currentCount >= props.batchSize) {
target.valueHasMutated();
currentCount = 0;
requestAnimationFrame(doFilter);
}
else {
currentCount++;
doFilter();
}
return;
}
else {
target.valueHasMutated();
currentCount = 0;
props.unfilteredCollectionIndex = 0;
props.isFiltering(false);
}
}
};
}));