-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathindex.js
85 lines (70 loc) · 1.67 KB
/
index.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
/**
* Dependencies
*/
var store = require('datastore').factory
var walk = require('domwalk')
var bind = require('./lib/bind')
/**
* Create a Legoh brick.
* A brick is a DOM element that updates itself with data changes.
*
* @param {String | Element} tmpl
* @param {Object} data
* @api public
*/
module.exports = function(tmpl, data) {
data = data || {}
var el = domify(tmpl)
var brick = store(function() {
return el
}, data)
walk(el, function(node) {
if(node.nodeType == 1) {
// @note attributes and node type should be handled by bind
attribute(brick, node, data)
} else bind(brick, node, data)
})
return brick
}
function attribute(brick, node, data) {
var attrs = node.attributes
for(var i = 0, l = attrs.length; i < l; i++) {
var attr = attrs[i]
//text(brick, attr, data)
var name = attr.nodeName
if(name.substring(0,2) == 'on') {
var content = attr.nodeValue
attr.nodeValue = ''
listen(brick, node, name.substring(2), content)
}
}
}
/**
* Delegate DOM event to the datastore emitter.
*
* @note we should also register the listener handler
* to a removed event (with mutation observer).
*
* @param {Datastore} brick
* @param {Element} node
* @param {String} type
* @param {String} topic
* @api private
*/
function listen(brick, node, type, topic) {
node.addEventListener(type, function(event) {
brick.emit(type + (topic ? ' ' + topic : ''))
})
}
/**
* Transform template into a DOM element.
*
* @param {String | Element} tmpl
* @return {Element}
* @api private
*/
function domify(tmpl) {
var div = document.createElement('div')
div.innerHTML = tmpl
return div.children[0]
}