-
Notifications
You must be signed in to change notification settings - Fork 0
/
dom.js
132 lines (111 loc) · 3.09 KB
/
dom.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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
// https://github.com/Daniel-Hug/DOM-Builder
(function (root, factory) {
'use strict';
if (typeof define === 'function' && define.amd) {
// anonymous AMD module
define(factory);
} else {
// Browser globals
root.dom = factory();
}
})(this, function() {
'use strict';
function bindSet(val, setter) {
// if val is function it's snoopable so the setter should be passed to it.
if (typeof val === 'function') {
val(setter);
} else {
setter(val);
}
}
function createDocFrag(array) {
// build each node and stick in docFrag
var docFrag = document.createDocumentFragment();
array.forEach(function(nodeData, i) {
docFrag.appendChild(dom(nodeData));
});
return docFrag;
}
// snoopable fn should:
// - accept a callback
// - call it right away passing a node value
// - call it again whenever the node value should change
function createDataBoundNode(fn) {
var node;
fn(function(newVal) {
//if (node instanceof HTMLElement && newVal.el === node.tagName) {
// take props from newVal and stick on node
//}
var newNode = dom(newVal);
if (node) node.parentNode.replaceChild(newNode, node);
node = newNode;
});
return node;
}
function createEl(elData) {
var el = elData.el instanceof HTMLElement ? elData.el :
document.createElement(elData.el || 'div');
Object.keys(elData).forEach(function(key) {
if (['el', 'text', 'kids'].indexOf(key) === -1) {
// set JS properties
if (key[0] === '_') {
var prop = key.slice(1);
bindSet(elData[key], function(newVal) {
el[prop] = newVal;
});
}
// add event listener(s)
else if (key.slice(0, 3) === 'on_') {
var eventName = key.slice(3);
var handlers = elData[key];
// accept a function
if (typeof handlers === 'function') handlers = [handlers];
// or an array of functions
for (var i = 0; i < handlers.length; i++) {
el.addEventListener(eventName, handlers[i], false);
}
}
// class toggle
else if (key.slice(0, 6) === 'class_') {
var className = key.slice(6);
bindSet(elData[key], function(classEnabled) {
el.classList[classEnabled ? 'add' : 'remove'](className);
});
}
// add html attributes
else {
bindSet(elData[key], function(newVal) {
if (newVal == null) {
el.removeAttribute(key);
} else {
el.setAttribute(key, newVal);
}
});
}
}
});
// set text
if (elData.text) {
bindSet(elData.text, function(newVal) {
el.textContent = newVal;
});
}
// otherwise add child nodes
else if (elData.kids) el.appendChild(createDocFrag(elData.kids));
return el;
}
return function dom(nodeData) {
var type = typeof nodeData;
// string -> text node
return type === 'string' || type === 'number' ?
document.createTextNode(nodeData) :
// DOM node -> DOM node
nodeData instanceof Node ? nodeData :
// array -> document fragment
Array.isArray(nodeData) ? createDocFrag(nodeData) :
// function -> data bound DOM node
type === 'function' ? createDataBoundNode(nodeData) :
// object -> element
createEl(nodeData);
};
});