-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathwidget.js
58 lines (48 loc) · 1.29 KB
/
widget.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
module.exports = function (rarry, template) {
var root = document.createElement('div')
template = template || function (val, key, el) {
function pre (val) { return '<pre>'+JSON.stringify(val)+'</pre>' }
if(el)
return el.innerHTML = pre(val), el
var el = document.createElement('span')
el.innerHTML = pre(val)
return el
}
console.log(template)
var elements = {}
rarry.on('update', function (change) {
for(var id in change)
update(id, change[id])
})
function update (key, change) {
var el
if(el = elements[key]) {
if(change == null) {
return root.removeChild(el)
}
var _el = template.call(el, change, key, el)
if(_el != el) {
elements[key] = _el
div.replaceChild(_el, el)
}
//else, template updated el.
return
}
el = template.call(null, change, key)
console.log(el)
//var rarry.indexOf(id)
//insert before the element that is already there...
var before = elements[rarry.keys[rarry.indexOfKey(key) + 1]]
elements[key] = el
if(before) {
root.insertBefore(el, before)
} else {
root.appendChild(el)
}
}
//render the current contents...
rarry.keys.forEach(function (key) {
update(key, rarry.get(key))
})
return root
}