forked from dgraham/stache-bind
-
Notifications
You must be signed in to change notification settings - Fork 0
/
stache-bind.js
190 lines (168 loc) · 4.45 KB
/
stache-bind.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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
// Maps context object to observer tree.
const trees = new WeakMap();
function escape(text) {
const node = document.createElement('p');
node.textContent = text;
return node.innerHTML;
}
function resolve(obj, prop) {
if (typeof obj !== 'undefined') {
const value = obj[prop];
if (typeof value === 'function') {
return value.call();
}
return value;
}
}
function pluck(context, key) {
const properties = key.split('.');
const value = properties.reduce(resolve, context) || '';
return escape(value);
}
function queryAll(selectors, root = document) {
return Array.from(root.querySelectorAll(selectors));
}
function bindings(node) {
const results = [];
for (const child of node.childNodes) {
switch (child.nodeType) {
case 1:
results.push(...bindAttributes(child));
results.push(...bindings(child));
break;
case 3:
results.push(...bindText(child));
break;
}
}
return results;
}
function notifyTree(node) {
for (const fn of node.observers) fn();
for (const name in node.children) {
notifyTree(node.children[name]);
}
}
function appendChild(parent, name) {
let child = parent.children[name];
if (!child) {
child = {name: name, children: {}, observers: []};
parent.children[name] = child;
}
return child;
}
function compose(a, b) {
return function composed() {
return a(b.apply(this, arguments));
};
}
function descend(node, key, visitor) {
const fn = compose(visitor, appendChild);
return key.split('.').reduce(fn, node);
}
// Finds or builds the observer tree for a context object.
function tree(context) {
let root = trees.get(context);
if (!root) {
root = {children: {}, observers: []};
trees.set(context, root);
}
return root;
}
function bind(fragment, context) {
const root = tree(context);
for (const {target, key, update} of bindings(fragment)) {
const observer = update.bind(target, context, key);
const leaf = descend(root, key, proxyOnce(context));
leaf.observers.push(observer);
update.call(target, context, key);
}
return fragment;
}
// Returns a visitor function to descend through a context object's
// attributes, proxying assignment operators to notify the observer
// tree of changes.
function proxyOnce(context) {
let current = context;
return function visit(node) {
if (!node.proxied) {
proxy(current, node.name, notifyTree.bind(null, node));
node.proxied = true;
}
current = current[node.name];
return node;
};
}
function proxy(target, property, after) {
let current = target[property];
Object.defineProperty(target, property, {
get: () => current,
set: value => {
current = value;
after();
}
});
}
function updateText(context, key) {
this.textContent = pluck(context, key);
}
function updateAttribute(context, key) {
this.value = pluck(context, key);
}
function parse(text) {
return text.split(/({{.+?}})/).map(token => {
const stache = token.startsWith('{{') && token.endsWith('}}');
return stache
? {type: 1, value: token.slice(2, -2).trim()}
: {type: 0, value: token};
});
}
function bindAttributes(node) {
const bindings = [];
for (const attr of Array.from(node.attributes)) {
for (const token of parse(attr.value)) {
if (token.type === 1) {
bindings.push({
target: attr,
key: token.value,
update: updateAttribute
});
}
}
}
return bindings;
}
function bindText(node) {
const tokens = parse(node.textContent);
if (tokens.length === 1 && tokens[0].type === 0) {
return [];
}
const bindings = [];
for (const token of tokens) {
const text = document.createTextNode(token.value);
node.parentNode.insertBefore(text, node);
if (token.type === 1) {
bindings.push({
target: text,
key: token.value,
update: updateText
});
}
}
node.parentNode.removeChild(node);
return bindings;
}
export function template(name) {
const template = document.querySelector(`template[data-name="${name}"]`);
const fragment = template.content;
return function evaluate(context) {
return bind(fragment.cloneNode(true), context);
};
}
export function install(global = 'Templates') {
const registry = {};
const templates = queryAll('template[data-name]');
const names = templates.map(el => el.getAttribute('data-name'));
for (const name of names) registry[name] = template(name);
window[global] = registry;
}