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
|
/**
* Search with chunks
*/
window.Searchable = uki.newClass(uki.view.Observable, new function() {
this.chunkSize = 100;
this.chunkTimeout = 20;
this.init = function(data) {
this.items = data;
};
this.search = function(query, callback) {
stopSearch.call(this);
this._query = query;
var iterator = this.createIterator(query, callback);
this.trigger('search.start', iterator);
filterChunk.call(this, iterator);
};
this.matchRow = function( row, iterator ) { return false; };
this.createIterator = function(query, callback) {
return {
query: query,
iteration: 0,
found: 0,
callback: callback
};
};
this._bindToDom = function() { return true };
function filterChunk(iterator) {
var filtered = 0,
_this = this,
foundInChunk = [],
item;
while(iterator.iteration < this.items.length) {
if (filtered == this.chunkSize) {
if (foundInChunk.length) this.trigger('search.foundInChunk', foundInChunk);
this._searchTimer = setTimeout(function() { filterChunk.call(_this, iterator); }, this.chunkTimeout);
return;
}
item = this.items[iterator.iteration];
if (this.matchRow( item, iterator )) {
iterator.found++;
foundInChunk.push(item);
this.trigger('search.found', item, iterator);
if (iterator.callback) iterator.callback(item, iterator);
} else {
this.trigger('search.missed', item, iterator);
}
iterator.iteration++;
filtered++;
}
this.trigger('search.foundInChunk', foundInChunk);
stopSearch.call(this);
this.trigger('search.finish', iterator);
};
function stopSearch () {
if (this._searchTimer) {
clearTimeout(this._searchTimer);
this._searchTimer = false;
}
}
});
|