rivets.js
19.3 KB
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
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
// rivets.js
// version: 0.4.6
// author: Michael Richards
// license: MIT
(function() {
var Rivets, bindEvent, factory, getInputValue, unbindEvent,
__bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; },
__slice = [].slice,
__indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; };
Rivets = {};
if (!String.prototype.trim) {
String.prototype.trim = function() {
return this.replace(/^\s+|\s+$/g, '');
};
}
Rivets.Binding = (function() {
function Binding(el, type, model, keypath, options) {
var identifier, regexp, value, _ref;
this.el = el;
this.type = type;
this.model = model;
this.keypath = keypath;
this.options = options != null ? options : {};
this.unbind = __bind(this.unbind, this);
this.bind = __bind(this.bind, this);
this.publish = __bind(this.publish, this);
this.sync = __bind(this.sync, this);
this.set = __bind(this.set, this);
this.formattedValue = __bind(this.formattedValue, this);
if (!(this.binder = Rivets.binders[type])) {
_ref = Rivets.binders;
for (identifier in _ref) {
value = _ref[identifier];
if (identifier !== '*' && identifier.indexOf('*') !== -1) {
regexp = new RegExp("^" + (identifier.replace('*', '.+')) + "$");
if (regexp.test(type)) {
this.binder = value;
this.args = new RegExp("^" + (identifier.replace('*', '(.+)')) + "$").exec(type);
this.args.shift();
}
}
}
}
this.binder || (this.binder = Rivets.binders['*']);
if (this.binder instanceof Function) {
this.binder = {
routine: this.binder
};
}
this.formatters = this.options.formatters || [];
}
Binding.prototype.formattedValue = function(value) {
var args, formatter, id, _i, _len, _ref;
_ref = this.formatters;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
formatter = _ref[_i];
args = formatter.split(/\s+/);
id = args.shift();
formatter = this.model[id] instanceof Function ? this.model[id] : Rivets.formatters[id];
if ((formatter != null ? formatter.read : void 0) instanceof Function) {
value = formatter.read.apply(formatter, [value].concat(__slice.call(args)));
} else if (formatter instanceof Function) {
value = formatter.apply(null, [value].concat(__slice.call(args)));
}
}
return value;
};
Binding.prototype.set = function(value) {
var _ref;
value = value instanceof Function && !this.binder["function"] ? this.formattedValue(value.call(this.model)) : this.formattedValue(value);
return (_ref = this.binder.routine) != null ? _ref.call(this, this.el, value) : void 0;
};
Binding.prototype.sync = function() {
return this.set(this.options.bypass ? this.model[this.keypath] : Rivets.config.adapter.read(this.model, this.keypath));
};
Binding.prototype.publish = function() {
var args, formatter, id, value, _i, _len, _ref, _ref1, _ref2;
value = getInputValue(this.el);
_ref = this.formatters.slice(0).reverse();
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
formatter = _ref[_i];
args = formatter.split(/\s+/);
id = args.shift();
if ((_ref1 = Rivets.formatters[id]) != null ? _ref1.publish : void 0) {
value = (_ref2 = Rivets.formatters[id]).publish.apply(_ref2, [value].concat(__slice.call(args)));
}
}
return Rivets.config.adapter.publish(this.model, this.keypath, value);
};
Binding.prototype.bind = function() {
var dependency, keypath, model, _i, _len, _ref, _ref1, _ref2, _results;
if ((_ref = this.binder.bind) != null) {
_ref.call(this, this.el);
}
if (this.options.bypass) {
this.sync();
} else {
Rivets.config.adapter.subscribe(this.model, this.keypath, this.sync);
if (Rivets.config.preloadData) {
this.sync();
}
}
if ((_ref1 = this.options.dependencies) != null ? _ref1.length : void 0) {
_ref2 = this.options.dependencies;
_results = [];
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
dependency = _ref2[_i];
if (/^\./.test(dependency)) {
model = this.model;
keypath = dependency.substr(1);
} else {
dependency = dependency.split('.');
model = this.view.models[dependency.shift()];
keypath = dependency.join('.');
}
_results.push(Rivets.config.adapter.subscribe(model, keypath, this.sync));
}
return _results;
}
};
Binding.prototype.unbind = function() {
var dependency, keypath, model, _i, _len, _ref, _ref1, _ref2, _results;
if ((_ref = this.binder.unbind) != null) {
_ref.call(this, this.el);
}
if (!this.options.bypass) {
Rivets.config.adapter.unsubscribe(this.model, this.keypath, this.sync);
}
if ((_ref1 = this.options.dependencies) != null ? _ref1.length : void 0) {
_ref2 = this.options.dependencies;
_results = [];
for (_i = 0, _len = _ref2.length; _i < _len; _i++) {
dependency = _ref2[_i];
if (/^\./.test(dependency)) {
model = this.model;
keypath = dependency.substr(1);
} else {
dependency = dependency.split('.');
model = this.view.models[dependency.shift()];
keypath = dependency.join('.');
}
_results.push(Rivets.config.adapter.unsubscribe(model, keypath, this.sync));
}
return _results;
}
};
return Binding;
})();
Rivets.View = (function() {
function View(els, models) {
this.els = els;
this.models = models;
this.publish = __bind(this.publish, this);
this.sync = __bind(this.sync, this);
this.unbind = __bind(this.unbind, this);
this.bind = __bind(this.bind, this);
this.select = __bind(this.select, this);
this.build = __bind(this.build, this);
this.bindingRegExp = __bind(this.bindingRegExp, this);
if (!(this.els.jquery || this.els instanceof Array)) {
this.els = [this.els];
}
this.build();
}
View.prototype.bindingRegExp = function() {
var prefix;
prefix = Rivets.config.prefix;
if (prefix) {
return new RegExp("^data-" + prefix + "-");
} else {
return /^data-/;
}
};
View.prototype.build = function() {
var bindingRegExp, el, node, parse, skipNodes, _i, _j, _len, _len1, _ref, _ref1,
_this = this;
this.bindings = [];
skipNodes = [];
bindingRegExp = this.bindingRegExp();
parse = function(node) {
var attribute, attributes, binder, binding, context, ctx, dependencies, identifier, keypath, model, n, options, path, pipe, pipes, regexp, splitPath, type, value, _i, _j, _k, _len, _len1, _len2, _ref, _ref1, _ref2, _ref3;
if (__indexOf.call(skipNodes, node) < 0) {
_ref = node.attributes;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
attribute = _ref[_i];
if (bindingRegExp.test(attribute.name)) {
type = attribute.name.replace(bindingRegExp, '');
if (!(binder = Rivets.binders[type])) {
_ref1 = Rivets.binders;
for (identifier in _ref1) {
value = _ref1[identifier];
if (identifier !== '*' && identifier.indexOf('*') !== -1) {
regexp = new RegExp("^" + (identifier.replace('*', '.+')) + "$");
if (regexp.test(type)) {
binder = value;
}
}
}
}
binder || (binder = Rivets.binders['*']);
if (binder.block) {
_ref2 = node.getElementsByTagName('*');
for (_j = 0, _len1 = _ref2.length; _j < _len1; _j++) {
n = _ref2[_j];
skipNodes.push(n);
}
attributes = [attribute];
}
}
}
_ref3 = attributes || node.attributes;
for (_k = 0, _len2 = _ref3.length; _k < _len2; _k++) {
attribute = _ref3[_k];
if (bindingRegExp.test(attribute.name)) {
options = {};
type = attribute.name.replace(bindingRegExp, '');
pipes = (function() {
var _l, _len3, _ref4, _results;
_ref4 = attribute.value.split('|');
_results = [];
for (_l = 0, _len3 = _ref4.length; _l < _len3; _l++) {
pipe = _ref4[_l];
_results.push(pipe.trim());
}
return _results;
})();
context = (function() {
var _l, _len3, _ref4, _results;
_ref4 = pipes.shift().split('<');
_results = [];
for (_l = 0, _len3 = _ref4.length; _l < _len3; _l++) {
ctx = _ref4[_l];
_results.push(ctx.trim());
}
return _results;
})();
path = context.shift();
splitPath = path.split(/\.|:/);
options.formatters = pipes;
options.bypass = path.indexOf(':') !== -1;
if (splitPath[0]) {
model = _this.models[splitPath.shift()];
} else {
model = _this.models;
splitPath.shift();
}
keypath = splitPath.join('.');
if (model) {
if (dependencies = context.shift()) {
options.dependencies = dependencies.split(/\s+/);
}
binding = new Rivets.Binding(node, type, model, keypath, options);
binding.view = _this;
_this.bindings.push(binding);
}
}
}
if (attributes) {
attributes = null;
}
}
};
_ref = this.els;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
el = _ref[_i];
parse(el);
_ref1 = el.getElementsByTagName('*');
for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
node = _ref1[_j];
if (node.attributes != null) {
parse(node);
}
}
}
};
View.prototype.select = function(fn) {
var binding, _i, _len, _ref, _results;
_ref = this.bindings;
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
binding = _ref[_i];
if (fn(binding)) {
_results.push(binding);
}
}
return _results;
};
View.prototype.bind = function() {
var binding, _i, _len, _ref, _results;
_ref = this.bindings;
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
binding = _ref[_i];
_results.push(binding.bind());
}
return _results;
};
View.prototype.unbind = function() {
var binding, _i, _len, _ref, _results;
_ref = this.bindings;
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
binding = _ref[_i];
_results.push(binding.unbind());
}
return _results;
};
View.prototype.sync = function() {
var binding, _i, _len, _ref, _results;
_ref = this.bindings;
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
binding = _ref[_i];
_results.push(binding.sync());
}
return _results;
};
View.prototype.publish = function() {
var binding, _i, _len, _ref, _results;
_ref = this.select(function(b) {
return b.binder.publishes;
});
_results = [];
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
binding = _ref[_i];
_results.push(binding.publish());
}
return _results;
};
return View;
})();
bindEvent = function(el, event, handler, context) {
var fn;
fn = function(e) {
return handler.call(context, e);
};
if (window.jQuery != null) {
el = jQuery(el);
if (el.on != null) {
el.on(event, fn);
} else {
el.bind(event, fn);
}
} else if (window.addEventListener != null) {
el.addEventListener(event, fn, false);
} else {
event = 'on' + event;
el.attachEvent(event, fn);
}
return fn;
};
unbindEvent = function(el, event, fn) {
if (window.jQuery != null) {
el = jQuery(el);
if (el.off != null) {
return el.off(event, fn);
} else {
return el.unbind(event, fn);
}
} else if (window.removeEventListener) {
return el.removeEventListener(event, fn, false);
} else {
event = 'on' + event;
return el.detachEvent(event, fn);
}
};
getInputValue = function(el) {
var o, _i, _len, _results;
switch (el.type) {
case 'checkbox':
return el.checked;
case 'select-multiple':
_results = [];
for (_i = 0, _len = el.length; _i < _len; _i++) {
o = el[_i];
if (o.selected) {
_results.push(o.value);
}
}
return _results;
break;
default:
return el.value;
}
};
Rivets.binders = {
enabled: function(el, value) {
return el.disabled = !value;
},
disabled: function(el, value) {
return el.disabled = !!value;
},
checked: {
publishes: true,
bind: function(el) {
return this.currentListener = bindEvent(el, 'change', this.publish);
},
unbind: function(el) {
return unbindEvent(el, 'change', this.currentListener);
},
routine: function(el, value) {
if (el.type === 'radio') {
return el.checked = el.value === value;
} else {
return el.checked = !!value;
}
}
},
unchecked: {
publishes: true,
bind: function(el) {
return this.currentListener = bindEvent(el, 'change', this.publish);
},
unbind: function(el) {
return unbindEvent(el, 'change', this.currentListener);
},
routine: function(el, value) {
if (el.type === 'radio') {
return el.checked = el.value !== value;
} else {
return el.checked = !value;
}
}
},
show: function(el, value) {
return el.style.display = value ? '' : 'none';
},
hide: function(el, value) {
return el.style.display = value ? 'none' : '';
},
html: function(el, value) {
return el.innerHTML = value != null ? value : '';
},
value: {
publishes: true,
bind: function(el) {
return this.currentListener = bindEvent(el, 'change', this.publish);
},
unbind: function(el) {
return unbindEvent(el, 'change', this.currentListener);
},
routine: function(el, value) {
var o, _i, _len, _ref, _ref1, _results;
if (el.type === 'select-multiple') {
if (value != null) {
_results = [];
for (_i = 0, _len = el.length; _i < _len; _i++) {
o = el[_i];
_results.push(o.selected = (_ref = o.value, __indexOf.call(value, _ref) >= 0));
}
return _results;
}
} else if ((value != null ? value.toString() : void 0) !== ((_ref1 = el.value) != null ? _ref1.toString() : void 0)) {
return el.value = value != null ? value : '';
}
}
},
text: function(el, value) {
if (el.innerText != null) {
return el.innerText = value != null ? value : '';
} else {
return el.textContent = value != null ? value : '';
}
},
"on-*": {
"function": true,
routine: function(el, value) {
if (this.currentListener) {
unbindEvent(el, this.args[0], this.currentListener);
}
return this.currentListener = bindEvent(el, this.args[0], value, this.model);
}
},
"each-*": {
block: true,
bind: function(el, collection) {
return el.removeAttribute(['data', rivets.config.prefix, this.type].join('-').replace('--', '-'));
},
routine: function(el, collection) {
var data, e, item, itemEl, m, n, previous, view, _i, _j, _k, _len, _len1, _len2, _ref, _ref1, _ref2, _ref3, _results;
if (this.iterated != null) {
_ref = this.iterated;
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
view = _ref[_i];
view.unbind();
_ref1 = view.els;
for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) {
e = _ref1[_j];
e.parentNode.removeChild(e);
}
}
} else {
this.marker = document.createComment(" rivets: " + this.type + " ");
el.parentNode.insertBefore(this.marker, el);
el.parentNode.removeChild(el);
}
this.iterated = [];
if (collection) {
_results = [];
for (_k = 0, _len2 = collection.length; _k < _len2; _k++) {
item = collection[_k];
data = {};
_ref2 = this.view.models;
for (n in _ref2) {
m = _ref2[n];
data[n] = m;
}
data[this.args[0]] = item;
itemEl = el.cloneNode(true);
if (this.iterated.length > 0) {
previous = this.iterated[this.iterated.length - 1].els[0];
} else {
previous = this.marker;
}
this.marker.parentNode.insertBefore(itemEl, (_ref3 = previous.nextSibling) != null ? _ref3 : null);
_results.push(this.iterated.push(rivets.bind(itemEl, data)));
}
return _results;
}
}
},
"class-*": function(el, value) {
var elClass;
elClass = " " + el.className + " ";
if (!value === (elClass.indexOf(" " + this.args[0] + " ") !== -1)) {
return el.className = value ? "" + el.className + " " + this.args[0] : elClass.replace(" " + this.args[0] + " ", ' ').trim();
}
},
"*": function(el, value) {
if (value) {
return el.setAttribute(this.type, value);
} else {
return el.removeAttribute(this.type);
}
}
};
Rivets.config = {
preloadData: true
};
Rivets.formatters = {};
factory = function(exports) {
exports.binders = Rivets.binders;
exports.formatters = Rivets.formatters;
exports.config = Rivets.config;
exports.configure = function(options) {
var property, value;
if (options == null) {
options = {};
}
for (property in options) {
value = options[property];
Rivets.config[property] = value;
}
};
return exports.bind = function(el, models) {
var view;
if (models == null) {
models = {};
}
view = new Rivets.View(el, models);
view.bind();
return view;
};
};
if (typeof exports === 'object') {
factory(exports);
} else if (typeof define === 'function' && define.amd) {
define(['exports'], function(exports) {
return this.rivets = factory(exports);
});
} else {
factory(this.rivets = {});
}
}).call(this);