-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathindex.js
340 lines (283 loc) · 7 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
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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
/**
* Module dependencies.
*/
var Emitter = require('emitter');
var query = require('query');
var domify = require('domify');
var debug = require('debug')('reactive');
var Adapter = require('./adapter');
var AttrBinding = require('./attr-binding');
var TextBinding = require('./text-binding');
var bindings = require('./bindings');
var Binding = require('./binding');
var utils = require('./utils');
var walk = require('./walk');
/**
* Expose `Reactive`.
*/
exports = module.exports = Reactive;
/**
* Initialize a reactive template for `el` and `obj`.
*
* @param {Element} el
* @param {Element} obj
* @param {Object} options
* @api public
*/
function Reactive(el, model, opt) {
if (!(this instanceof Reactive)) return new Reactive(el, model, opt);
opt = opt || {};
if (typeof el === 'string') {
el = domify(el);
}
var self = this;
self.opt = opt || {};
self.model = model || {};
self.adapter = (opt.adapter || Adapter)(self.model);
self.el = el;
self.view = opt.delegate || Object.create(null);
self.bindings = opt.bindings || Object.create(null);
// TODO undo this crap and just export bindings regularly
// not that binding order matters!!
bindings({
bind: function(name, fn) {
self.bindings[name] = fn;
}
});
self._bind(this.el, []);
}
Emitter(Reactive.prototype);
/**
* Subscribe to changes on `prop`.
*
* @param {String} prop
* @param {Function} fn
* @return {Reactive}
* @api private
*/
Reactive.prototype.sub = function(prop, fn){
var self = this;
debug('subscribe %s', prop);
// if we have parts, we need to subscribe to the parent as well
// TODO (defunctzombie) multiple levels of properties
var parts = prop.split('.');
if (parts.length > 1) {
self.sub(parts[0], function() {
// use self.get(prop) here because we wanted the value of the nested
// property but the subscription is for the parent
fn(self.get(prop));
});
}
// for when reactive changes the property
self.on('change ' + prop, fn);
// for when the property changed within the adapter
self.adapter.subscribe(prop, function() {
// skip items set internally from calling function twice
if (self._internal_set) return;
fn.apply(this, arguments);
});
return self;
};
/**
* Unsubscribe to changes from `prop`.
*
* @param {String} prop
* @param {Function} fn
* @return {Reactive}
* @api private
*/
Reactive.prototype.unsub = function(prop, fn){
this.off('change ' + prop, fn);
this.adapter.unsubscribe(prop, fn);
return this;
};
/**
* Get a `prop`
*
* @param {String} prop
* @param {Mixed} val
* @return {Mixed}
* @api private
*/
Reactive.prototype.get = function(prop) {
if (prop === 'this') {
return this.model;
}
// model takes precedence
var modelVal = this.adapter.get(prop);
if (modelVal !== undefined) {
return modelVal;
}
var view = this.view;
var viewVal = view[prop];
if ('function' == typeof viewVal) {
return viewVal.call(view);
}
else if (viewVal) {
return viewVal;
}
return undefined;
};
/**
* Set a `prop`
*
* @param {String} prop
* @param {Mixed} val
* @return {Reactive}
* @api private
*/
Reactive.prototype.set = function(prop, val) {
var self = this;
// internal set flag lets reactive updates know to avoid triggering
// updates for the Adapter#set call
// we will already trigger updates with the change event
self._internal_set = true;
if( "object" == typeof prop) {
Object.keys(prop).forEach(function(name){
self.set(name, prop[name]);
});
}
else {
self.adapter.set(prop, val);
self.emit('change ' + prop, val);
}
self._internal_set = false;
return self;
};
/**
* Traverse and bind all interpolation within attributes and text.
*
* @param {Element} el
* @api private
*/
Reactive.prototype.bindInterpolation = function(el, els){
// element
if (el.nodeType == 1) {
for (var i = 0; i < el.attributes.length; i++) {
var attr = el.attributes[i];
if (utils.hasInterpolation(attr.value)) {
new AttrBinding(this, el, attr);
}
}
}
// text node
if (el.nodeType == 3) {
if (utils.hasInterpolation(el.data)) {
debug('bind text "%s"', el.data);
new TextBinding(this, el);
}
}
// walk nodes
for (var i = 0; i < el.childNodes.length; i++) {
var node = el.childNodes[i];
this.bindInterpolation(node, els);
}
};
Reactive.prototype._bind = function() {
var self = this;
var bindings = self.bindings;
walk(self.el, function(el, next) {
// element
if (el.nodeType == 1) {
var skip = false;
var attrs = {};
for (var i = 0; i < el.attributes.length; ++i) {
var attr = el.attributes[i];
var name = attr.name;
attrs[name] = attr;
}
// bindings must be iterated first
// to see if any request skipping
// only then can we see about attributes
Object.keys(bindings).forEach(function(name) {
if (!attrs[name] || skip) {
return;
}
debug('bind [%s]', name);
var prop = attrs[name].value;
var binding_fn = bindings[name];
if (!binding_fn) {
return;
}
var binding = new Binding(name, self, el, binding_fn);
binding.bind();
if (binding.skip) {
skip = true;
}
});
if (skip) {
return next(skip);
}
// if we are not skipping
// bind any interpolation attrs
for (var i = 0; i < el.attributes.length; ++i) {
var attr = el.attributes[i];
var name = attr.name;
if (utils.hasInterpolation(attr.value)) {
new AttrBinding(self, el, attr);
}
}
return next(skip);
}
// text
else if (el.nodeType == 3 && el.parentNode) {
if (utils.hasInterpolation(el.data)) {
debug('bind text "%s"', el.data);
new TextBinding(self, el);
}
}
next();
});
};
/**
* Bind `name` to `fn`.
*
* @param {String|Object} name or object
* @param {Function} fn
* @api public
*/
Reactive.prototype.bind = function(name, fn) {
var self = this;
if ('object' == typeof name) {
for (var key in name) {
this.bind(key, name[key]);
}
return;
}
var els = query.all('[' + name + ']', this.el);
if (this.el.hasAttribute && this.el.hasAttribute(name)) {
els = [].slice.call(els);
els.unshift(this.el);
}
if (!els.length) return;
debug('bind [%s] (%d elements)', name, els.length);
for (var i = 0; i < els.length; i++) {
var binding = new Binding(name, this, els[i], fn);
binding.bind();
}
};
/**
* Destroy the binding
* - Removes the element from the dom (if inserted)
* - unbinds any event listeners
*
* @api public
*/
Reactive.prototype.destroy = function() {
var self = this;
if (self.el.parentNode) {
self.el.parentNode.removeChild(self.el);
}
self.adapter.unsubscribeAll();
self.emit('destroyed');
self.removeAllListeners();
};
/**
* Use middleware
*
* @api public
*/
Reactive.prototype.use = function(fn) {
fn(this);
return this;
};