forked from javve/list.js
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathparse.js
47 lines (41 loc) · 1.24 KB
/
parse.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
module.exports = function(list) {
var Item = require('./item')(list);
var getChildren = function(parent) {
var nodes = parent.childNodes,
items = [];
for (var i = 0, il = nodes.length; i < il; i++) {
// Only textnodes have a data attribute
if (nodes[i].data === undefined) {
items.push(nodes[i]);
}
}
return items;
};
var parse = function(itemElements, valueNames) {
for (var i = 0, il = itemElements.length; i < il; i++) {
list.items.push(new Item(valueNames, itemElements[i]));
}
};
var parseAsync = function(itemElements, valueNames) {
var itemsToIndex = itemElements.splice(0, 50); // TODO: If < 100 items, what happens in IE etc?
parse(itemsToIndex, valueNames);
if (itemElements.length > 0) {
setTimeout(function() {
parseAsync(itemElements, valueNames);
}, 1);
} else {
list.update();
list.trigger('parseComplete');
}
};
list.handlers.parseComplete = list.handlers.parseComplete || [];
return function() {
var itemsToIndex = getChildren(list.list),
valueNames = list.valueNames;
if (list.indexAsync) {
parseAsync(itemsToIndex, valueNames);
} else {
parse(itemsToIndex, valueNames);
}
};
};