mirror of
https://github.com/wahyd4/cdnjs.git
synced 2026-08-19 09:47:48 +10:00
add old versions of rivets
This commit is contained in:
@@ -0,0 +1,720 @@
|
||||
// rivets.js
|
||||
// version: 0.5.0
|
||||
// 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(view, el, type, key, keypath, options) {
|
||||
var identifier, regexp, value, _ref;
|
||||
|
||||
this.view = view;
|
||||
this.el = el;
|
||||
this.type = type;
|
||||
this.key = key;
|
||||
this.keypath = keypath;
|
||||
this.options = options != null ? options : {};
|
||||
this.update = __bind(this.update, this);
|
||||
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 = this.view.binders[type])) {
|
||||
_ref = this.view.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 = this.view.binders['*']);
|
||||
if (this.binder instanceof Function) {
|
||||
this.binder = {
|
||||
routine: this.binder
|
||||
};
|
||||
}
|
||||
this.formatters = this.options.formatters || [];
|
||||
this.model = this.view.models[this.key];
|
||||
}
|
||||
|
||||
Binding.prototype.formattedValue = function(value) {
|
||||
var args, formatter, id, _i, _len, _ref, _ref1, _ref2, _ref3;
|
||||
|
||||
_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] : ((_ref1 = this.options) != null ? (_ref2 = _ref1.bindingOptions) != null ? (_ref3 = _ref2.formatters) != null ? _ref3[id] : void 0 : void 0 : void 0) instanceof Function ? this.options.bindingOptions.formatters[id] : this.view.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] : this.view.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 = this.view.formatters[id]) != null ? _ref1.publish : void 0) {
|
||||
value = (_ref2 = this.view.formatters[id]).publish.apply(_ref2, [value].concat(__slice.call(args)));
|
||||
}
|
||||
}
|
||||
return this.view.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 {
|
||||
this.view.config.adapter.subscribe(this.model, this.keypath, this.sync);
|
||||
if (this.view.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(this.view.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) {
|
||||
this.view.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(this.view.config.adapter.unsubscribe(model, keypath, this.sync));
|
||||
}
|
||||
return _results;
|
||||
}
|
||||
};
|
||||
|
||||
Binding.prototype.update = function() {
|
||||
this.unbind();
|
||||
this.model = this.view.models[this.key];
|
||||
return this.bind();
|
||||
};
|
||||
|
||||
return Binding;
|
||||
|
||||
})();
|
||||
|
||||
Rivets.View = (function() {
|
||||
function View(els, models, options) {
|
||||
var k, option, v, _base, _i, _len, _ref, _ref1, _ref2, _ref3;
|
||||
|
||||
this.els = els;
|
||||
this.models = models;
|
||||
this.options = options != null ? options : {};
|
||||
this.update = __bind(this.update, this);
|
||||
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];
|
||||
}
|
||||
_ref = ['config', 'binders', 'formatters'];
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
option = _ref[_i];
|
||||
this[option] = {};
|
||||
if (this.options[option]) {
|
||||
_ref1 = this.options[option];
|
||||
for (k in _ref1) {
|
||||
v = _ref1[k];
|
||||
this[option][k] = v;
|
||||
}
|
||||
}
|
||||
_ref2 = Rivets[option];
|
||||
for (k in _ref2) {
|
||||
v = _ref2[k];
|
||||
if ((_ref3 = (_base = this[option])[k]) == null) {
|
||||
_base[k] = v;
|
||||
}
|
||||
}
|
||||
}
|
||||
this.build();
|
||||
}
|
||||
|
||||
View.prototype.bindingRegExp = function() {
|
||||
var prefix;
|
||||
|
||||
prefix = this.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, context, ctx, dependencies, identifier, key, keypath, 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 = _this.binders[type])) {
|
||||
_ref1 = _this.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 = _this.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]) {
|
||||
key = splitPath.shift();
|
||||
} else {
|
||||
key = null;
|
||||
splitPath.shift();
|
||||
}
|
||||
keypath = splitPath.join('.');
|
||||
if (_this.models[key] != null) {
|
||||
if (dependencies = context.shift()) {
|
||||
options.dependencies = dependencies.split(/\s+/);
|
||||
}
|
||||
_this.bindings.push(new Rivets.Binding(_this, node, type, key, keypath, options));
|
||||
}
|
||||
}
|
||||
}
|
||||
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;
|
||||
};
|
||||
|
||||
View.prototype.update = function(models) {
|
||||
var binding, key, model, _results;
|
||||
|
||||
if (models == null) {
|
||||
models = {};
|
||||
}
|
||||
_results = [];
|
||||
for (key in models) {
|
||||
model = models[key];
|
||||
this.models[key] = model;
|
||||
_results.push((function() {
|
||||
var _i, _len, _ref, _results1;
|
||||
|
||||
_ref = this.select(function(b) {
|
||||
return b.key === key;
|
||||
});
|
||||
_results1 = [];
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
binding = _ref[_i];
|
||||
_results1.push(binding.update());
|
||||
}
|
||||
return _results1;
|
||||
}).call(this));
|
||||
}
|
||||
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;
|
||||
|
||||
if (window.jQuery != null) {
|
||||
el = jQuery(el);
|
||||
switch (el[0].type) {
|
||||
case 'checkbox':
|
||||
return el.is(':checked');
|
||||
default:
|
||||
return el.val();
|
||||
}
|
||||
} else {
|
||||
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) {
|
||||
var _ref;
|
||||
|
||||
if (el.type === 'radio') {
|
||||
return el.checked = ((_ref = el.value) != null ? _ref.toString() : void 0) === (value != null ? value.toString() : void 0);
|
||||
} 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) {
|
||||
var _ref;
|
||||
|
||||
if (el.type === 'radio') {
|
||||
return el.checked = ((_ref = el.value) != null ? _ref.toString() : void 0) !== (value != null ? value.toString() : void 0);
|
||||
} 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, _ref2, _results;
|
||||
|
||||
if (window.jQuery != null) {
|
||||
el = jQuery(el);
|
||||
if ((value != null ? value.toString() : void 0) !== ((_ref = el.val()) != null ? _ref.toString() : void 0)) {
|
||||
return el.val(value != null ? value : '');
|
||||
}
|
||||
} else {
|
||||
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 = (_ref1 = o.value, __indexOf.call(value, _ref1) >= 0));
|
||||
}
|
||||
return _results;
|
||||
}
|
||||
} else if ((value != null ? value.toString() : void 0) !== ((_ref2 = el.value) != null ? _ref2.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', this.view.config.prefix, this.type].join('-').replace('--', '-'));
|
||||
},
|
||||
unbind: function(el, collection) {
|
||||
var view, _i, _len, _ref, _results;
|
||||
|
||||
if (this.iterated != null) {
|
||||
_ref = this.iterated;
|
||||
_results = [];
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
view = _ref[_i];
|
||||
_results.push(view.unbind());
|
||||
}
|
||||
return _results;
|
||||
}
|
||||
},
|
||||
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);
|
||||
previous = this.iterated.length ? this.iterated[this.iterated.length - 1].els[0] : this.marker;
|
||||
this.marker.parentNode.insertBefore(itemEl, (_ref3 = previous.nextSibling) != null ? _ref3 : null);
|
||||
view = new Rivets.View(itemEl, data, this.view.options);
|
||||
view.bind();
|
||||
_results.push(this.iterated.push(view));
|
||||
}
|
||||
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, options) {
|
||||
var view;
|
||||
|
||||
if (models == null) {
|
||||
models = {};
|
||||
}
|
||||
if (options == null) {
|
||||
options = {};
|
||||
}
|
||||
view = new Rivets.View(el, models, options);
|
||||
view.bind();
|
||||
return view;
|
||||
};
|
||||
};
|
||||
|
||||
if (typeof exports === 'object') {
|
||||
factory(exports);
|
||||
} else if (typeof define === 'function' && define.amd) {
|
||||
define(['exports'], function(exports) {
|
||||
factory(this.rivets = exports);
|
||||
return exports;
|
||||
});
|
||||
} else {
|
||||
factory(this.rivets = {});
|
||||
}
|
||||
|
||||
}).call(this);
|
||||
Vendored
+5
File diff suppressed because one or more lines are too long
@@ -0,0 +1,720 @@
|
||||
// Rivets.js
|
||||
// version: 0.5.1
|
||||
// author: Michael Richards
|
||||
// license: MIT
|
||||
(function() {
|
||||
var Rivets,
|
||||
__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(view, el, type, key, keypath, options) {
|
||||
var identifier, regexp, value, _ref;
|
||||
|
||||
this.view = view;
|
||||
this.el = el;
|
||||
this.type = type;
|
||||
this.key = key;
|
||||
this.keypath = keypath;
|
||||
this.options = options != null ? options : {};
|
||||
this.update = __bind(this.update, this);
|
||||
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 = this.view.binders[type])) {
|
||||
_ref = this.view.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 = this.view.binders['*']);
|
||||
if (this.binder instanceof Function) {
|
||||
this.binder = {
|
||||
routine: this.binder
|
||||
};
|
||||
}
|
||||
this.formatters = this.options.formatters || [];
|
||||
this.model = this.view.models[this.key];
|
||||
}
|
||||
|
||||
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] : this.view.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] : this.view.config.adapter.read(this.model, this.keypath));
|
||||
};
|
||||
|
||||
Binding.prototype.publish = function() {
|
||||
var args, formatter, id, value, _i, _len, _ref, _ref1, _ref2;
|
||||
|
||||
value = Rivets.Util.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 = this.view.formatters[id]) != null ? _ref1.publish : void 0) {
|
||||
value = (_ref2 = this.view.formatters[id]).publish.apply(_ref2, [value].concat(__slice.call(args)));
|
||||
}
|
||||
}
|
||||
return this.view.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 {
|
||||
this.view.config.adapter.subscribe(this.model, this.keypath, this.sync);
|
||||
if (this.view.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(this.view.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) {
|
||||
this.view.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(this.view.config.adapter.unsubscribe(model, keypath, this.sync));
|
||||
}
|
||||
return _results;
|
||||
}
|
||||
};
|
||||
|
||||
Binding.prototype.update = function() {
|
||||
this.unbind();
|
||||
this.model = this.view.models[this.key];
|
||||
return this.bind();
|
||||
};
|
||||
|
||||
return Binding;
|
||||
|
||||
})();
|
||||
|
||||
Rivets.View = (function() {
|
||||
function View(els, models, options) {
|
||||
var k, option, v, _base, _i, _len, _ref, _ref1, _ref2, _ref3;
|
||||
|
||||
this.els = els;
|
||||
this.models = models;
|
||||
this.options = options != null ? options : {};
|
||||
this.update = __bind(this.update, this);
|
||||
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];
|
||||
}
|
||||
_ref = ['config', 'binders', 'formatters'];
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
option = _ref[_i];
|
||||
this[option] = {};
|
||||
if (this.options[option]) {
|
||||
_ref1 = this.options[option];
|
||||
for (k in _ref1) {
|
||||
v = _ref1[k];
|
||||
this[option][k] = v;
|
||||
}
|
||||
}
|
||||
_ref2 = Rivets[option];
|
||||
for (k in _ref2) {
|
||||
v = _ref2[k];
|
||||
if ((_ref3 = (_base = this[option])[k]) == null) {
|
||||
_base[k] = v;
|
||||
}
|
||||
}
|
||||
}
|
||||
this.build();
|
||||
}
|
||||
|
||||
View.prototype.bindingRegExp = function() {
|
||||
var prefix;
|
||||
|
||||
prefix = this.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, context, ctx, dependencies, identifier, key, keypath, 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 = _this.binders[type])) {
|
||||
_ref1 = _this.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 = _this.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]) {
|
||||
key = splitPath.shift();
|
||||
} else {
|
||||
key = null;
|
||||
splitPath.shift();
|
||||
}
|
||||
keypath = splitPath.join('.');
|
||||
if (_this.models[key] != null) {
|
||||
if (dependencies = context.shift()) {
|
||||
options.dependencies = dependencies.split(/\s+/);
|
||||
}
|
||||
_this.bindings.push(new Rivets.Binding(_this, node, type, key, keypath, options));
|
||||
}
|
||||
}
|
||||
}
|
||||
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;
|
||||
};
|
||||
|
||||
View.prototype.update = function(models) {
|
||||
var binding, key, model, _results;
|
||||
|
||||
if (models == null) {
|
||||
models = {};
|
||||
}
|
||||
_results = [];
|
||||
for (key in models) {
|
||||
model = models[key];
|
||||
this.models[key] = model;
|
||||
_results.push((function() {
|
||||
var _i, _len, _ref, _results1;
|
||||
|
||||
_ref = this.select(function(b) {
|
||||
return b.key === key;
|
||||
});
|
||||
_results1 = [];
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
binding = _ref[_i];
|
||||
_results1.push(binding.update());
|
||||
}
|
||||
return _results1;
|
||||
}).call(this));
|
||||
}
|
||||
return _results;
|
||||
};
|
||||
|
||||
return View;
|
||||
|
||||
})();
|
||||
|
||||
Rivets.Util = {
|
||||
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;
|
||||
|
||||
if (window.jQuery != null) {
|
||||
el = jQuery(el);
|
||||
switch (el[0].type) {
|
||||
case 'checkbox':
|
||||
return el.is(':checked');
|
||||
default:
|
||||
return el.val();
|
||||
}
|
||||
} else {
|
||||
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 = Rivets.Util.bindEvent(el, 'change', this.publish);
|
||||
},
|
||||
unbind: function(el) {
|
||||
return Rivets.Util.unbindEvent(el, 'change', this.currentListener);
|
||||
},
|
||||
routine: function(el, value) {
|
||||
var _ref;
|
||||
|
||||
if (el.type === 'radio') {
|
||||
return el.checked = ((_ref = el.value) != null ? _ref.toString() : void 0) === (value != null ? value.toString() : void 0);
|
||||
} else {
|
||||
return el.checked = !!value;
|
||||
}
|
||||
}
|
||||
},
|
||||
unchecked: {
|
||||
publishes: true,
|
||||
bind: function(el) {
|
||||
return this.currentListener = Rivets.Util.bindEvent(el, 'change', this.publish);
|
||||
},
|
||||
unbind: function(el) {
|
||||
return Rivets.Util.unbindEvent(el, 'change', this.currentListener);
|
||||
},
|
||||
routine: function(el, value) {
|
||||
var _ref;
|
||||
|
||||
if (el.type === 'radio') {
|
||||
return el.checked = ((_ref = el.value) != null ? _ref.toString() : void 0) !== (value != null ? value.toString() : void 0);
|
||||
} 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 = Rivets.Util.bindEvent(el, 'change', this.publish);
|
||||
},
|
||||
unbind: function(el) {
|
||||
return Rivets.Util.unbindEvent(el, 'change', this.currentListener);
|
||||
},
|
||||
routine: function(el, value) {
|
||||
var o, _i, _len, _ref, _ref1, _ref2, _results;
|
||||
|
||||
if (window.jQuery != null) {
|
||||
el = jQuery(el);
|
||||
if ((value != null ? value.toString() : void 0) !== ((_ref = el.val()) != null ? _ref.toString() : void 0)) {
|
||||
return el.val(value != null ? value : '');
|
||||
}
|
||||
} else {
|
||||
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 = (_ref1 = o.value, __indexOf.call(value, _ref1) >= 0));
|
||||
}
|
||||
return _results;
|
||||
}
|
||||
} else if ((value != null ? value.toString() : void 0) !== ((_ref2 = el.value) != null ? _ref2.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) {
|
||||
Rivets.Util.unbindEvent(el, this.args[0], this.currentListener);
|
||||
}
|
||||
return this.currentListener = Rivets.Util.bindEvent(el, this.args[0], value, this.model);
|
||||
}
|
||||
},
|
||||
"each-*": {
|
||||
block: true,
|
||||
bind: function(el, collection) {
|
||||
return el.removeAttribute(['data', this.view.config.prefix, this.type].join('-').replace('--', '-'));
|
||||
},
|
||||
unbind: function(el, collection) {
|
||||
var view, _i, _len, _ref, _results;
|
||||
|
||||
if (this.iterated != null) {
|
||||
_ref = this.iterated;
|
||||
_results = [];
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
view = _ref[_i];
|
||||
_results.push(view.unbind());
|
||||
}
|
||||
return _results;
|
||||
}
|
||||
},
|
||||
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);
|
||||
previous = this.iterated.length ? this.iterated[this.iterated.length - 1].els[0] : this.marker;
|
||||
this.marker.parentNode.insertBefore(itemEl, (_ref3 = previous.nextSibling) != null ? _ref3 : null);
|
||||
view = new Rivets.View(itemEl, data, this.view.options);
|
||||
view.bind();
|
||||
_results.push(this.iterated.push(view));
|
||||
}
|
||||
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 = {};
|
||||
|
||||
Rivets.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, options) {
|
||||
var view;
|
||||
|
||||
if (models == null) {
|
||||
models = {};
|
||||
}
|
||||
if (options == null) {
|
||||
options = {};
|
||||
}
|
||||
view = new Rivets.View(el, models, options);
|
||||
view.bind();
|
||||
return view;
|
||||
};
|
||||
};
|
||||
|
||||
if (typeof exports === 'object') {
|
||||
Rivets.factory(exports);
|
||||
} else if (typeof define === 'function' && define.amd) {
|
||||
define(['exports'], function(exports) {
|
||||
Rivets.factory(this.rivets = exports);
|
||||
return exports;
|
||||
});
|
||||
} else {
|
||||
Rivets.factory(this.rivets = {});
|
||||
}
|
||||
|
||||
}).call(this);
|
||||
Vendored
+5
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
+5
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
+5
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
+5
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
+5
File diff suppressed because one or more lines are too long
@@ -0,0 +1,733 @@
|
||||
// Rivets.js
|
||||
// version: 0.5.2
|
||||
// author: Michael Richards
|
||||
// license: MIT
|
||||
(function() {
|
||||
var Rivets,
|
||||
__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(view, el, type, key, keypath, options) {
|
||||
var identifier, regexp, value, _ref;
|
||||
|
||||
this.view = view;
|
||||
this.el = el;
|
||||
this.type = type;
|
||||
this.key = key;
|
||||
this.keypath = keypath;
|
||||
this.options = options != null ? options : {};
|
||||
this.update = __bind(this.update, this);
|
||||
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 = this.view.binders[type])) {
|
||||
_ref = this.view.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 = this.view.binders['*']);
|
||||
if (this.binder instanceof Function) {
|
||||
this.binder = {
|
||||
routine: this.binder
|
||||
};
|
||||
}
|
||||
this.formatters = this.options.formatters || [];
|
||||
this.model = this.view.models[this.key];
|
||||
}
|
||||
|
||||
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] : this.view.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] : this.view.config.adapter.read(this.model, this.keypath));
|
||||
};
|
||||
|
||||
Binding.prototype.publish = function() {
|
||||
var args, formatter, id, value, _i, _len, _ref, _ref1, _ref2;
|
||||
|
||||
value = Rivets.Util.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 = this.view.formatters[id]) != null ? _ref1.publish : void 0) {
|
||||
value = (_ref2 = this.view.formatters[id]).publish.apply(_ref2, [value].concat(__slice.call(args)));
|
||||
}
|
||||
}
|
||||
return this.view.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 {
|
||||
this.view.config.adapter.subscribe(this.model, this.keypath, this.sync);
|
||||
if (this.view.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(this.view.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) {
|
||||
this.view.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(this.view.config.adapter.unsubscribe(model, keypath, this.sync));
|
||||
}
|
||||
return _results;
|
||||
}
|
||||
};
|
||||
|
||||
Binding.prototype.update = function() {
|
||||
this.unbind();
|
||||
this.model = this.view.models[this.key];
|
||||
return this.bind();
|
||||
};
|
||||
|
||||
return Binding;
|
||||
|
||||
})();
|
||||
|
||||
Rivets.View = (function() {
|
||||
function View(els, models, options) {
|
||||
var k, option, v, _base, _i, _len, _ref, _ref1, _ref2, _ref3;
|
||||
|
||||
this.els = els;
|
||||
this.models = models;
|
||||
this.options = options != null ? options : {};
|
||||
this.update = __bind(this.update, this);
|
||||
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];
|
||||
}
|
||||
_ref = ['config', 'binders', 'formatters'];
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
option = _ref[_i];
|
||||
this[option] = {};
|
||||
if (this.options[option]) {
|
||||
_ref1 = this.options[option];
|
||||
for (k in _ref1) {
|
||||
v = _ref1[k];
|
||||
this[option][k] = v;
|
||||
}
|
||||
}
|
||||
_ref2 = Rivets[option];
|
||||
for (k in _ref2) {
|
||||
v = _ref2[k];
|
||||
if ((_ref3 = (_base = this[option])[k]) == null) {
|
||||
_base[k] = v;
|
||||
}
|
||||
}
|
||||
}
|
||||
this.build();
|
||||
}
|
||||
|
||||
View.prototype.bindingRegExp = function() {
|
||||
var prefix;
|
||||
|
||||
prefix = this.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, context, ctx, dependencies, identifier, key, keypath, 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 = _this.binders[type])) {
|
||||
_ref1 = _this.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 = _this.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]) {
|
||||
key = splitPath.shift();
|
||||
} else {
|
||||
key = null;
|
||||
splitPath.shift();
|
||||
}
|
||||
keypath = splitPath.join('.');
|
||||
if (_this.models[key] != null) {
|
||||
if (dependencies = context.shift()) {
|
||||
options.dependencies = dependencies.split(/\s+/);
|
||||
}
|
||||
_this.bindings.push(new Rivets.Binding(_this, node, type, key, keypath, options));
|
||||
}
|
||||
}
|
||||
}
|
||||
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;
|
||||
};
|
||||
|
||||
View.prototype.update = function(models) {
|
||||
var binding, key, model, _results;
|
||||
|
||||
if (models == null) {
|
||||
models = {};
|
||||
}
|
||||
_results = [];
|
||||
for (key in models) {
|
||||
model = models[key];
|
||||
this.models[key] = model;
|
||||
_results.push((function() {
|
||||
var _i, _len, _ref, _results1;
|
||||
|
||||
_ref = this.select(function(b) {
|
||||
return b.key === key;
|
||||
});
|
||||
_results1 = [];
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
binding = _ref[_i];
|
||||
_results1.push(binding.update());
|
||||
}
|
||||
return _results1;
|
||||
}).call(this));
|
||||
}
|
||||
return _results;
|
||||
};
|
||||
|
||||
return View;
|
||||
|
||||
})();
|
||||
|
||||
Rivets.Util = {
|
||||
bindEvent: function(el, event, handler, view) {
|
||||
var fn;
|
||||
|
||||
fn = function(ev) {
|
||||
return handler.call(this, ev, view);
|
||||
};
|
||||
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;
|
||||
|
||||
if (window.jQuery != null) {
|
||||
el = jQuery(el);
|
||||
switch (el[0].type) {
|
||||
case 'checkbox':
|
||||
return el.is(':checked');
|
||||
default:
|
||||
return el.val();
|
||||
}
|
||||
} else {
|
||||
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 = Rivets.Util.bindEvent(el, 'change', this.publish);
|
||||
},
|
||||
unbind: function(el) {
|
||||
return Rivets.Util.unbindEvent(el, 'change', this.currentListener);
|
||||
},
|
||||
routine: function(el, value) {
|
||||
var _ref;
|
||||
|
||||
if (el.type === 'radio') {
|
||||
return el.checked = ((_ref = el.value) != null ? _ref.toString() : void 0) === (value != null ? value.toString() : void 0);
|
||||
} else {
|
||||
return el.checked = !!value;
|
||||
}
|
||||
}
|
||||
},
|
||||
unchecked: {
|
||||
publishes: true,
|
||||
bind: function(el) {
|
||||
return this.currentListener = Rivets.Util.bindEvent(el, 'change', this.publish);
|
||||
},
|
||||
unbind: function(el) {
|
||||
return Rivets.Util.unbindEvent(el, 'change', this.currentListener);
|
||||
},
|
||||
routine: function(el, value) {
|
||||
var _ref;
|
||||
|
||||
if (el.type === 'radio') {
|
||||
return el.checked = ((_ref = el.value) != null ? _ref.toString() : void 0) !== (value != null ? value.toString() : void 0);
|
||||
} 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 = Rivets.Util.bindEvent(el, 'change', this.publish);
|
||||
},
|
||||
unbind: function(el) {
|
||||
return Rivets.Util.unbindEvent(el, 'change', this.currentListener);
|
||||
},
|
||||
routine: function(el, value) {
|
||||
var o, _i, _len, _ref, _ref1, _ref2, _results;
|
||||
|
||||
if (window.jQuery != null) {
|
||||
el = jQuery(el);
|
||||
if ((value != null ? value.toString() : void 0) !== ((_ref = el.val()) != null ? _ref.toString() : void 0)) {
|
||||
return el.val(value != null ? value : '');
|
||||
}
|
||||
} else {
|
||||
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 = (_ref1 = o.value, __indexOf.call(value, _ref1) >= 0));
|
||||
}
|
||||
return _results;
|
||||
}
|
||||
} else if ((value != null ? value.toString() : void 0) !== ((_ref2 = el.value) != null ? _ref2.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) {
|
||||
Rivets.Util.unbindEvent(el, this.args[0], this.currentListener);
|
||||
}
|
||||
return this.currentListener = Rivets.Util.bindEvent(el, this.args[0], value, this.view);
|
||||
}
|
||||
},
|
||||
"each-*": {
|
||||
block: true,
|
||||
bind: function(el, collection) {
|
||||
return el.removeAttribute(['data', this.view.config.prefix, this.type].join('-').replace('--', '-'));
|
||||
},
|
||||
unbind: function(el, collection) {
|
||||
var view, _i, _len, _ref, _results;
|
||||
|
||||
if (this.iterated != null) {
|
||||
_ref = this.iterated;
|
||||
_results = [];
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
view = _ref[_i];
|
||||
_results.push(view.unbind());
|
||||
}
|
||||
return _results;
|
||||
}
|
||||
},
|
||||
routine: function(el, collection) {
|
||||
var data, e, item, itemEl, k, m, n, options, previous, v, view, _i, _j, _k, _len, _len1, _len2, _ref, _ref1, _ref2, _ref3, _ref4, _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);
|
||||
previous = this.iterated.length ? this.iterated[this.iterated.length - 1].els[0] : this.marker;
|
||||
this.marker.parentNode.insertBefore(itemEl, (_ref3 = previous.nextSibling) != null ? _ref3 : null);
|
||||
options = {
|
||||
binders: this.view.options.binders,
|
||||
formatters: this.view.options.binders,
|
||||
config: {}
|
||||
};
|
||||
if (this.view.options.config) {
|
||||
_ref4 = this.view.options.config;
|
||||
for (k in _ref4) {
|
||||
v = _ref4[k];
|
||||
options.config[k] = v;
|
||||
}
|
||||
}
|
||||
options.config.preloadData = true;
|
||||
view = new Rivets.View(itemEl, data, options);
|
||||
view.bind();
|
||||
_results.push(this.iterated.push(view));
|
||||
}
|
||||
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 = {};
|
||||
|
||||
Rivets.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, options) {
|
||||
var view;
|
||||
|
||||
if (models == null) {
|
||||
models = {};
|
||||
}
|
||||
if (options == null) {
|
||||
options = {};
|
||||
}
|
||||
view = new Rivets.View(el, models, options);
|
||||
view.bind();
|
||||
return view;
|
||||
};
|
||||
};
|
||||
|
||||
if (typeof exports === 'object') {
|
||||
Rivets.factory(exports);
|
||||
} else if (typeof define === 'function' && define.amd) {
|
||||
define(['exports'], function(exports) {
|
||||
Rivets.factory(this.rivets = exports);
|
||||
return exports;
|
||||
});
|
||||
} else {
|
||||
Rivets.factory(this.rivets = {});
|
||||
}
|
||||
|
||||
}).call(this);
|
||||
Vendored
+5
File diff suppressed because one or more lines are too long
@@ -0,0 +1,734 @@
|
||||
// Rivets.js
|
||||
// version: 0.5.3
|
||||
// author: Michael Richards
|
||||
// license: MIT
|
||||
(function() {
|
||||
var Rivets,
|
||||
__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(view, el, type, key, keypath, options) {
|
||||
var identifier, regexp, value, _ref;
|
||||
|
||||
this.view = view;
|
||||
this.el = el;
|
||||
this.type = type;
|
||||
this.key = key;
|
||||
this.keypath = keypath;
|
||||
this.options = options != null ? options : {};
|
||||
this.update = __bind(this.update, this);
|
||||
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 = this.view.binders[type])) {
|
||||
_ref = this.view.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 = this.view.binders['*']);
|
||||
if (this.binder instanceof Function) {
|
||||
this.binder = {
|
||||
routine: this.binder
|
||||
};
|
||||
}
|
||||
this.formatters = this.options.formatters || [];
|
||||
this.model = this.view.models[this.key];
|
||||
}
|
||||
|
||||
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] : this.view.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] : this.view.config.adapter.read(this.model, this.keypath));
|
||||
};
|
||||
|
||||
Binding.prototype.publish = function() {
|
||||
var args, formatter, id, value, _i, _len, _ref, _ref1, _ref2;
|
||||
|
||||
value = Rivets.Util.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 = this.view.formatters[id]) != null ? _ref1.publish : void 0) {
|
||||
value = (_ref2 = this.view.formatters[id]).publish.apply(_ref2, [value].concat(__slice.call(args)));
|
||||
}
|
||||
}
|
||||
return this.view.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 {
|
||||
this.view.config.adapter.subscribe(this.model, this.keypath, this.sync);
|
||||
if (this.view.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(this.view.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) {
|
||||
this.view.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(this.view.config.adapter.unsubscribe(model, keypath, this.sync));
|
||||
}
|
||||
return _results;
|
||||
}
|
||||
};
|
||||
|
||||
Binding.prototype.update = function() {
|
||||
this.unbind();
|
||||
this.model = this.view.models[this.key];
|
||||
return this.bind();
|
||||
};
|
||||
|
||||
return Binding;
|
||||
|
||||
})();
|
||||
|
||||
Rivets.View = (function() {
|
||||
function View(els, models, options) {
|
||||
var k, option, v, _base, _i, _len, _ref, _ref1, _ref2, _ref3;
|
||||
|
||||
this.els = els;
|
||||
this.models = models;
|
||||
this.options = options != null ? options : {};
|
||||
this.update = __bind(this.update, this);
|
||||
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];
|
||||
}
|
||||
_ref = ['config', 'binders', 'formatters'];
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
option = _ref[_i];
|
||||
this[option] = {};
|
||||
if (this.options[option]) {
|
||||
_ref1 = this.options[option];
|
||||
for (k in _ref1) {
|
||||
v = _ref1[k];
|
||||
this[option][k] = v;
|
||||
}
|
||||
}
|
||||
_ref2 = Rivets[option];
|
||||
for (k in _ref2) {
|
||||
v = _ref2[k];
|
||||
if ((_ref3 = (_base = this[option])[k]) == null) {
|
||||
_base[k] = v;
|
||||
}
|
||||
}
|
||||
}
|
||||
this.build();
|
||||
}
|
||||
|
||||
View.prototype.bindingRegExp = function() {
|
||||
var prefix;
|
||||
|
||||
prefix = this.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, context, ctx, dependencies, identifier, key, keypath, 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 = _this.binders[type])) {
|
||||
_ref1 = _this.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 = _this.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]) {
|
||||
key = splitPath.shift();
|
||||
} else {
|
||||
key = null;
|
||||
splitPath.shift();
|
||||
}
|
||||
keypath = splitPath.join('.');
|
||||
if (_this.models[key] != null) {
|
||||
if (dependencies = context.shift()) {
|
||||
options.dependencies = dependencies.split(/\s+/);
|
||||
}
|
||||
_this.bindings.push(new Rivets.Binding(_this, node, type, key, keypath, options));
|
||||
}
|
||||
}
|
||||
}
|
||||
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;
|
||||
};
|
||||
|
||||
View.prototype.update = function(models) {
|
||||
var binding, key, model, _results;
|
||||
|
||||
if (models == null) {
|
||||
models = {};
|
||||
}
|
||||
_results = [];
|
||||
for (key in models) {
|
||||
model = models[key];
|
||||
this.models[key] = model;
|
||||
_results.push((function() {
|
||||
var _i, _len, _ref, _results1;
|
||||
|
||||
_ref = this.select(function(b) {
|
||||
return b.key === key;
|
||||
});
|
||||
_results1 = [];
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
binding = _ref[_i];
|
||||
_results1.push(binding.update());
|
||||
}
|
||||
return _results1;
|
||||
}).call(this));
|
||||
}
|
||||
return _results;
|
||||
};
|
||||
|
||||
return View;
|
||||
|
||||
})();
|
||||
|
||||
Rivets.Util = {
|
||||
bindEvent: function(el, event, handler, view) {
|
||||
var fn;
|
||||
|
||||
fn = function(ev) {
|
||||
return handler.call(this, ev, view);
|
||||
};
|
||||
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;
|
||||
|
||||
if (window.jQuery != null) {
|
||||
el = jQuery(el);
|
||||
switch (el[0].type) {
|
||||
case 'checkbox':
|
||||
return el.is(':checked');
|
||||
default:
|
||||
return el.val();
|
||||
}
|
||||
} else {
|
||||
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 = Rivets.Util.bindEvent(el, 'change', this.publish);
|
||||
},
|
||||
unbind: function(el) {
|
||||
return Rivets.Util.unbindEvent(el, 'change', this.currentListener);
|
||||
},
|
||||
routine: function(el, value) {
|
||||
var _ref;
|
||||
|
||||
if (el.type === 'radio') {
|
||||
return el.checked = ((_ref = el.value) != null ? _ref.toString() : void 0) === (value != null ? value.toString() : void 0);
|
||||
} else {
|
||||
return el.checked = !!value;
|
||||
}
|
||||
}
|
||||
},
|
||||
unchecked: {
|
||||
publishes: true,
|
||||
bind: function(el) {
|
||||
return this.currentListener = Rivets.Util.bindEvent(el, 'change', this.publish);
|
||||
},
|
||||
unbind: function(el) {
|
||||
return Rivets.Util.unbindEvent(el, 'change', this.currentListener);
|
||||
},
|
||||
routine: function(el, value) {
|
||||
var _ref;
|
||||
|
||||
if (el.type === 'radio') {
|
||||
return el.checked = ((_ref = el.value) != null ? _ref.toString() : void 0) !== (value != null ? value.toString() : void 0);
|
||||
} 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 = Rivets.Util.bindEvent(el, 'change', this.publish);
|
||||
},
|
||||
unbind: function(el) {
|
||||
return Rivets.Util.unbindEvent(el, 'change', this.currentListener);
|
||||
},
|
||||
routine: function(el, value) {
|
||||
var o, _i, _len, _ref, _ref1, _ref2, _results;
|
||||
|
||||
if (window.jQuery != null) {
|
||||
el = jQuery(el);
|
||||
if ((value != null ? value.toString() : void 0) !== ((_ref = el.val()) != null ? _ref.toString() : void 0)) {
|
||||
return el.val(value != null ? value : '');
|
||||
}
|
||||
} else {
|
||||
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 = (_ref1 = o.value, __indexOf.call(value, _ref1) >= 0));
|
||||
}
|
||||
return _results;
|
||||
}
|
||||
} else if ((value != null ? value.toString() : void 0) !== ((_ref2 = el.value) != null ? _ref2.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) {
|
||||
Rivets.Util.unbindEvent(el, this.args[0], this.currentListener);
|
||||
}
|
||||
return this.currentListener = Rivets.Util.bindEvent(el, this.args[0], value, this.view);
|
||||
}
|
||||
},
|
||||
"each-*": {
|
||||
block: true,
|
||||
bind: function(el) {
|
||||
var attr;
|
||||
|
||||
attr = ['data', this.view.config.prefix, this.type].join('-').replace('--', '-');
|
||||
this.marker = document.createComment(" rivets: " + this.type + " ");
|
||||
this.iterated = [];
|
||||
el.removeAttribute(attr);
|
||||
el.parentNode.insertBefore(this.marker, el);
|
||||
return el.parentNode.removeChild(el);
|
||||
},
|
||||
unbind: function(el) {
|
||||
var view, _i, _len, _ref, _results;
|
||||
|
||||
if (this.iterated != null) {
|
||||
_ref = this.iterated;
|
||||
_results = [];
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
view = _ref[_i];
|
||||
_results.push(view.unbind());
|
||||
}
|
||||
return _results;
|
||||
}
|
||||
},
|
||||
routine: function(el, collection) {
|
||||
var data, i, index, k, key, model, modelName, options, previous, template, v, view, _i, _j, _len, _len1, _ref, _ref1, _ref2, _results;
|
||||
|
||||
modelName = this.args[0];
|
||||
collection = collection || [];
|
||||
if (this.iterated.length > collection.length) {
|
||||
_ref = Array(this.iterated.length - collection.length);
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
i = _ref[_i];
|
||||
view = this.iterated.pop();
|
||||
view.unbind();
|
||||
this.marker.parentNode.removeChild(view.els[0]);
|
||||
}
|
||||
}
|
||||
_results = [];
|
||||
for (index = _j = 0, _len1 = collection.length; _j < _len1; index = ++_j) {
|
||||
model = collection[index];
|
||||
data = {};
|
||||
data[modelName] = model;
|
||||
if (this.iterated[index] == null) {
|
||||
_ref1 = this.view.models;
|
||||
for (key in _ref1) {
|
||||
model = _ref1[key];
|
||||
data[key] = model;
|
||||
}
|
||||
previous = this.iterated.length ? this.iterated[this.iterated.length - 1].els[0] : this.marker;
|
||||
options = {
|
||||
binders: this.view.options.binders,
|
||||
formatters: this.view.options.formatters,
|
||||
config: {}
|
||||
};
|
||||
_ref2 = this.view.options.config;
|
||||
for (k in _ref2) {
|
||||
v = _ref2[k];
|
||||
options.config[k] = v;
|
||||
}
|
||||
options.config.preloadData = true;
|
||||
template = el.cloneNode(true);
|
||||
this.iterated.push(rivets.bind(template, data, options));
|
||||
_results.push(this.marker.parentNode.insertBefore(template, previous.nextSibling));
|
||||
} else if (this.iterated[index].models[modelName] !== model) {
|
||||
_results.push(this.iterated[index].update(data));
|
||||
} else {
|
||||
_results.push(void 0);
|
||||
}
|
||||
}
|
||||
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 = {};
|
||||
|
||||
Rivets.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, options) {
|
||||
var view;
|
||||
|
||||
if (models == null) {
|
||||
models = {};
|
||||
}
|
||||
if (options == null) {
|
||||
options = {};
|
||||
}
|
||||
view = new Rivets.View(el, models, options);
|
||||
view.bind();
|
||||
return view;
|
||||
};
|
||||
};
|
||||
|
||||
if (typeof exports === 'object') {
|
||||
Rivets.factory(exports);
|
||||
} else if (typeof define === 'function' && define.amd) {
|
||||
define(['exports'], function(exports) {
|
||||
Rivets.factory(this.rivets = exports);
|
||||
return exports;
|
||||
});
|
||||
} else {
|
||||
Rivets.factory(this.rivets = {});
|
||||
}
|
||||
|
||||
}).call(this);
|
||||
Vendored
+5
File diff suppressed because one or more lines are too long
@@ -0,0 +1,750 @@
|
||||
// Rivets.js
|
||||
// version: 0.5.4
|
||||
// author: Michael Richards
|
||||
// license: MIT
|
||||
(function() {
|
||||
var Rivets,
|
||||
__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(view, el, type, key, keypath, options) {
|
||||
var identifier, regexp, value, _ref;
|
||||
|
||||
this.view = view;
|
||||
this.el = el;
|
||||
this.type = type;
|
||||
this.key = key;
|
||||
this.keypath = keypath;
|
||||
this.options = options != null ? options : {};
|
||||
this.update = __bind(this.update, this);
|
||||
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.eventHandler = __bind(this.eventHandler, this);
|
||||
this.formattedValue = __bind(this.formattedValue, this);
|
||||
if (!(this.binder = this.view.binders[type])) {
|
||||
_ref = this.view.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 = this.view.binders['*']);
|
||||
if (this.binder instanceof Function) {
|
||||
this.binder = {
|
||||
routine: this.binder
|
||||
};
|
||||
}
|
||||
this.formatters = this.options.formatters || [];
|
||||
this.model = this.key ? this.view.models[this.key] : this.view.models;
|
||||
}
|
||||
|
||||
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] : this.view.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.eventHandler = function(fn) {
|
||||
var binding, handler;
|
||||
|
||||
handler = (binding = this).view.config.handler;
|
||||
return function(ev) {
|
||||
return handler.call(fn, this, ev, binding);
|
||||
};
|
||||
};
|
||||
|
||||
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] : this.view.config.adapter.read(this.model, this.keypath));
|
||||
};
|
||||
|
||||
Binding.prototype.publish = function() {
|
||||
var args, formatter, id, value, _i, _len, _ref, _ref1, _ref2;
|
||||
|
||||
value = Rivets.Util.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 = this.view.formatters[id]) != null ? _ref1.publish : void 0) {
|
||||
value = (_ref2 = this.view.formatters[id]).publish.apply(_ref2, [value].concat(__slice.call(args)));
|
||||
}
|
||||
}
|
||||
return this.view.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 {
|
||||
this.view.config.adapter.subscribe(this.model, this.keypath, this.sync);
|
||||
if (this.view.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(this.view.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) {
|
||||
this.view.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(this.view.config.adapter.unsubscribe(model, keypath, this.sync));
|
||||
}
|
||||
return _results;
|
||||
}
|
||||
};
|
||||
|
||||
Binding.prototype.update = function() {
|
||||
this.unbind();
|
||||
this.model = this.key ? this.view.models[this.key] : this.view.models;
|
||||
return this.bind();
|
||||
};
|
||||
|
||||
return Binding;
|
||||
|
||||
})();
|
||||
|
||||
Rivets.View = (function() {
|
||||
function View(els, models, options) {
|
||||
var k, option, v, _base, _i, _len, _ref, _ref1, _ref2, _ref3;
|
||||
|
||||
this.els = els;
|
||||
this.models = models;
|
||||
this.options = options != null ? options : {};
|
||||
this.update = __bind(this.update, this);
|
||||
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];
|
||||
}
|
||||
_ref = ['config', 'binders', 'formatters'];
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
option = _ref[_i];
|
||||
this[option] = {};
|
||||
if (this.options[option]) {
|
||||
_ref1 = this.options[option];
|
||||
for (k in _ref1) {
|
||||
v = _ref1[k];
|
||||
this[option][k] = v;
|
||||
}
|
||||
}
|
||||
_ref2 = Rivets[option];
|
||||
for (k in _ref2) {
|
||||
v = _ref2[k];
|
||||
if ((_ref3 = (_base = this[option])[k]) == null) {
|
||||
_base[k] = v;
|
||||
}
|
||||
}
|
||||
}
|
||||
this.build();
|
||||
}
|
||||
|
||||
View.prototype.bindingRegExp = function() {
|
||||
var prefix;
|
||||
|
||||
prefix = this.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, context, ctx, dependencies, identifier, key, keypath, 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 = _this.binders[type])) {
|
||||
_ref1 = _this.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 = _this.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]) {
|
||||
key = splitPath.shift();
|
||||
} else {
|
||||
key = null;
|
||||
splitPath.shift();
|
||||
}
|
||||
keypath = splitPath.join('.');
|
||||
if (!key || (_this.models[key] != null)) {
|
||||
if (dependencies = context.shift()) {
|
||||
options.dependencies = dependencies.split(/\s+/);
|
||||
}
|
||||
_this.bindings.push(new Rivets.Binding(_this, node, type, key, keypath, options));
|
||||
}
|
||||
}
|
||||
}
|
||||
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;
|
||||
};
|
||||
|
||||
View.prototype.update = function(models) {
|
||||
var binding, key, model, _results;
|
||||
|
||||
if (models == null) {
|
||||
models = {};
|
||||
}
|
||||
_results = [];
|
||||
for (key in models) {
|
||||
model = models[key];
|
||||
this.models[key] = model;
|
||||
_results.push((function() {
|
||||
var _i, _len, _ref, _results1;
|
||||
|
||||
_ref = this.select(function(b) {
|
||||
return b.key === key;
|
||||
});
|
||||
_results1 = [];
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
binding = _ref[_i];
|
||||
_results1.push(binding.update());
|
||||
}
|
||||
return _results1;
|
||||
}).call(this));
|
||||
}
|
||||
return _results;
|
||||
};
|
||||
|
||||
return View;
|
||||
|
||||
})();
|
||||
|
||||
Rivets.Util = {
|
||||
bindEvent: function(el, event, handler) {
|
||||
if (window.jQuery != null) {
|
||||
el = jQuery(el);
|
||||
if (el.on != null) {
|
||||
return el.on(event, handler);
|
||||
} else {
|
||||
return el.bind(event, handler);
|
||||
}
|
||||
} else if (window.addEventListener != null) {
|
||||
return el.addEventListener(event, handler, false);
|
||||
} else {
|
||||
event = 'on' + event;
|
||||
return el.attachEvent(event, handler);
|
||||
}
|
||||
},
|
||||
unbindEvent: function(el, event, handler) {
|
||||
if (window.jQuery != null) {
|
||||
el = jQuery(el);
|
||||
if (el.off != null) {
|
||||
return el.off(event, handler);
|
||||
} else {
|
||||
return el.unbind(event, handler);
|
||||
}
|
||||
} else if (window.removeEventListener != null) {
|
||||
return el.removeEventListener(event, handler, false);
|
||||
} else {
|
||||
event = 'on' + event;
|
||||
return el.detachEvent(event, handler);
|
||||
}
|
||||
},
|
||||
getInputValue: function(el) {
|
||||
var o, _i, _len, _results;
|
||||
|
||||
if (window.jQuery != null) {
|
||||
el = jQuery(el);
|
||||
switch (el[0].type) {
|
||||
case 'checkbox':
|
||||
return el.is(':checked');
|
||||
default:
|
||||
return el.val();
|
||||
}
|
||||
} else {
|
||||
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 Rivets.Util.bindEvent(el, 'change', this.publish);
|
||||
},
|
||||
unbind: function(el) {
|
||||
return Rivets.Util.unbindEvent(el, 'change', this.publish);
|
||||
},
|
||||
routine: function(el, value) {
|
||||
var _ref;
|
||||
|
||||
if (el.type === 'radio') {
|
||||
return el.checked = ((_ref = el.value) != null ? _ref.toString() : void 0) === (value != null ? value.toString() : void 0);
|
||||
} else {
|
||||
return el.checked = !!value;
|
||||
}
|
||||
}
|
||||
},
|
||||
unchecked: {
|
||||
publishes: true,
|
||||
bind: function(el) {
|
||||
return Rivets.Util.bindEvent(el, 'change', this.publish);
|
||||
},
|
||||
unbind: function(el) {
|
||||
return Rivets.Util.unbindEvent(el, 'change', this.publish);
|
||||
},
|
||||
routine: function(el, value) {
|
||||
var _ref;
|
||||
|
||||
if (el.type === 'radio') {
|
||||
return el.checked = ((_ref = el.value) != null ? _ref.toString() : void 0) !== (value != null ? value.toString() : void 0);
|
||||
} 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 Rivets.Util.bindEvent(el, 'change', this.publish);
|
||||
},
|
||||
unbind: function(el) {
|
||||
return Rivets.Util.unbindEvent(el, 'change', this.publish);
|
||||
},
|
||||
routine: function(el, value) {
|
||||
var o, _i, _len, _ref, _ref1, _ref2, _results;
|
||||
|
||||
if (window.jQuery != null) {
|
||||
el = jQuery(el);
|
||||
if ((value != null ? value.toString() : void 0) !== ((_ref = el.val()) != null ? _ref.toString() : void 0)) {
|
||||
return el.val(value != null ? value : '');
|
||||
}
|
||||
} else {
|
||||
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 = (_ref1 = o.value, __indexOf.call(value, _ref1) >= 0));
|
||||
}
|
||||
return _results;
|
||||
}
|
||||
} else if ((value != null ? value.toString() : void 0) !== ((_ref2 = el.value) != null ? _ref2.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,
|
||||
unbind: function(el) {
|
||||
if (this.handler) {
|
||||
return Rivets.Util.unbindEvent(el, this.args[0], this.handler);
|
||||
}
|
||||
},
|
||||
routine: function(el, value) {
|
||||
if (this.handler) {
|
||||
Rivets.Util.unbindEvent(el, this.args[0], this.handler);
|
||||
}
|
||||
return Rivets.Util.bindEvent(el, this.args[0], this.handler = this.eventHandler(value));
|
||||
}
|
||||
},
|
||||
"each-*": {
|
||||
block: true,
|
||||
bind: function(el) {
|
||||
var attr;
|
||||
|
||||
if (this.marker == null) {
|
||||
attr = ['data', this.view.config.prefix, this.type].join('-').replace('--', '-');
|
||||
this.marker = document.createComment(" rivets: " + this.type + " ");
|
||||
this.iterated = [];
|
||||
el.removeAttribute(attr);
|
||||
el.parentNode.insertBefore(this.marker, el);
|
||||
return el.parentNode.removeChild(el);
|
||||
}
|
||||
},
|
||||
unbind: function(el) {
|
||||
var view, _i, _len, _ref, _results;
|
||||
|
||||
if (this.iterated != null) {
|
||||
_ref = this.iterated;
|
||||
_results = [];
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
view = _ref[_i];
|
||||
_results.push(view.unbind());
|
||||
}
|
||||
return _results;
|
||||
}
|
||||
},
|
||||
routine: function(el, collection) {
|
||||
var data, i, index, k, key, model, modelName, options, previous, template, v, view, _i, _j, _len, _len1, _ref, _ref1, _ref2, _results;
|
||||
|
||||
modelName = this.args[0];
|
||||
collection = collection || [];
|
||||
if (this.iterated.length > collection.length) {
|
||||
_ref = Array(this.iterated.length - collection.length);
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
i = _ref[_i];
|
||||
view = this.iterated.pop();
|
||||
view.unbind();
|
||||
this.marker.parentNode.removeChild(view.els[0]);
|
||||
}
|
||||
}
|
||||
_results = [];
|
||||
for (index = _j = 0, _len1 = collection.length; _j < _len1; index = ++_j) {
|
||||
model = collection[index];
|
||||
data = {};
|
||||
data[modelName] = model;
|
||||
if (this.iterated[index] == null) {
|
||||
_ref1 = this.view.models;
|
||||
for (key in _ref1) {
|
||||
model = _ref1[key];
|
||||
data[key] = model;
|
||||
}
|
||||
previous = this.iterated.length ? this.iterated[this.iterated.length - 1].els[0] : this.marker;
|
||||
options = {
|
||||
binders: this.view.options.binders,
|
||||
formatters: this.view.options.formatters,
|
||||
config: {}
|
||||
};
|
||||
_ref2 = this.view.options.config;
|
||||
for (k in _ref2) {
|
||||
v = _ref2[k];
|
||||
options.config[k] = v;
|
||||
}
|
||||
options.config.preloadData = true;
|
||||
template = el.cloneNode(true);
|
||||
view = new Rivets.View(template, data, options);
|
||||
view.bind();
|
||||
this.iterated.push(view);
|
||||
_results.push(this.marker.parentNode.insertBefore(template, previous.nextSibling));
|
||||
} else if (this.iterated[index].models[modelName] !== model) {
|
||||
_results.push(this.iterated[index].update(data));
|
||||
} else {
|
||||
_results.push(void 0);
|
||||
}
|
||||
}
|
||||
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,
|
||||
handler: function(context, ev, binding) {
|
||||
return this.call(context, ev, binding.view.models);
|
||||
}
|
||||
};
|
||||
|
||||
Rivets.formatters = {};
|
||||
|
||||
Rivets.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, options) {
|
||||
var view;
|
||||
|
||||
if (models == null) {
|
||||
models = {};
|
||||
}
|
||||
if (options == null) {
|
||||
options = {};
|
||||
}
|
||||
view = new Rivets.View(el, models, options);
|
||||
view.bind();
|
||||
return view;
|
||||
};
|
||||
};
|
||||
|
||||
if (typeof exports === 'object') {
|
||||
Rivets.factory(exports);
|
||||
} else if (typeof define === 'function' && define.amd) {
|
||||
define(['exports'], function(exports) {
|
||||
Rivets.factory(this.rivets = exports);
|
||||
return exports;
|
||||
});
|
||||
} else {
|
||||
Rivets.factory(this.rivets = {});
|
||||
}
|
||||
|
||||
}).call(this);
|
||||
Vendored
+5
File diff suppressed because one or more lines are too long
@@ -0,0 +1,752 @@
|
||||
// Rivets.js
|
||||
// version: 0.5.5
|
||||
// author: Michael Richards
|
||||
// license: MIT
|
||||
(function() {
|
||||
var Rivets,
|
||||
__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(view, el, type, key, keypath, options) {
|
||||
var identifier, regexp, value, _ref;
|
||||
|
||||
this.view = view;
|
||||
this.el = el;
|
||||
this.type = type;
|
||||
this.key = key;
|
||||
this.keypath = keypath;
|
||||
this.options = options != null ? options : {};
|
||||
this.update = __bind(this.update, this);
|
||||
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.eventHandler = __bind(this.eventHandler, this);
|
||||
this.formattedValue = __bind(this.formattedValue, this);
|
||||
if (!(this.binder = this.view.binders[type])) {
|
||||
_ref = this.view.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 = this.view.binders['*']);
|
||||
if (this.binder instanceof Function) {
|
||||
this.binder = {
|
||||
routine: this.binder
|
||||
};
|
||||
}
|
||||
this.formatters = this.options.formatters || [];
|
||||
this.model = this.key ? this.view.models[this.key] : this.view.models;
|
||||
}
|
||||
|
||||
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] : this.view.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.eventHandler = function(fn) {
|
||||
var binding, handler;
|
||||
|
||||
handler = (binding = this).view.config.handler;
|
||||
return function(ev) {
|
||||
return handler.call(fn, this, ev, binding);
|
||||
};
|
||||
};
|
||||
|
||||
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] : this.view.config.adapter.read(this.model, this.keypath));
|
||||
};
|
||||
|
||||
Binding.prototype.publish = function() {
|
||||
var args, formatter, id, value, _i, _len, _ref, _ref1, _ref2;
|
||||
|
||||
value = Rivets.Util.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 = this.view.formatters[id]) != null ? _ref1.publish : void 0) {
|
||||
value = (_ref2 = this.view.formatters[id]).publish.apply(_ref2, [value].concat(__slice.call(args)));
|
||||
}
|
||||
}
|
||||
return this.view.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 {
|
||||
this.view.config.adapter.subscribe(this.model, this.keypath, this.sync);
|
||||
if (this.view.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(this.view.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) {
|
||||
this.view.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(this.view.config.adapter.unsubscribe(model, keypath, this.sync));
|
||||
}
|
||||
return _results;
|
||||
}
|
||||
};
|
||||
|
||||
Binding.prototype.update = function() {
|
||||
this.unbind();
|
||||
this.model = this.key ? this.view.models[this.key] : this.view.models;
|
||||
return this.bind();
|
||||
};
|
||||
|
||||
return Binding;
|
||||
|
||||
})();
|
||||
|
||||
Rivets.View = (function() {
|
||||
function View(els, models, options) {
|
||||
var k, option, v, _base, _i, _len, _ref, _ref1, _ref2, _ref3;
|
||||
|
||||
this.els = els;
|
||||
this.models = models;
|
||||
this.options = options != null ? options : {};
|
||||
this.update = __bind(this.update, this);
|
||||
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];
|
||||
}
|
||||
_ref = ['config', 'binders', 'formatters'];
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
option = _ref[_i];
|
||||
this[option] = {};
|
||||
if (this.options[option]) {
|
||||
_ref1 = this.options[option];
|
||||
for (k in _ref1) {
|
||||
v = _ref1[k];
|
||||
this[option][k] = v;
|
||||
}
|
||||
}
|
||||
_ref2 = Rivets[option];
|
||||
for (k in _ref2) {
|
||||
v = _ref2[k];
|
||||
if ((_ref3 = (_base = this[option])[k]) == null) {
|
||||
_base[k] = v;
|
||||
}
|
||||
}
|
||||
}
|
||||
this.build();
|
||||
}
|
||||
|
||||
View.prototype.bindingRegExp = function() {
|
||||
var prefix;
|
||||
|
||||
prefix = this.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, context, ctx, dependencies, identifier, key, keypath, 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 = _this.binders[type])) {
|
||||
_ref1 = _this.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 = _this.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]) {
|
||||
key = splitPath.shift();
|
||||
} else {
|
||||
key = null;
|
||||
splitPath.shift();
|
||||
}
|
||||
keypath = splitPath.join('.');
|
||||
if (!key || (_this.models[key] != null)) {
|
||||
if (dependencies = context.shift()) {
|
||||
options.dependencies = dependencies.split(/\s+/);
|
||||
}
|
||||
_this.bindings.push(new Rivets.Binding(_this, node, type, key, keypath, options));
|
||||
}
|
||||
}
|
||||
}
|
||||
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;
|
||||
};
|
||||
|
||||
View.prototype.update = function(models) {
|
||||
var binding, key, model, _results;
|
||||
|
||||
if (models == null) {
|
||||
models = {};
|
||||
}
|
||||
_results = [];
|
||||
for (key in models) {
|
||||
model = models[key];
|
||||
this.models[key] = model;
|
||||
_results.push((function() {
|
||||
var _i, _len, _ref, _results1;
|
||||
|
||||
_ref = this.select(function(b) {
|
||||
return b.key === key;
|
||||
});
|
||||
_results1 = [];
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
binding = _ref[_i];
|
||||
_results1.push(binding.update());
|
||||
}
|
||||
return _results1;
|
||||
}).call(this));
|
||||
}
|
||||
return _results;
|
||||
};
|
||||
|
||||
return View;
|
||||
|
||||
})();
|
||||
|
||||
Rivets.Util = {
|
||||
bindEvent: function(el, event, handler) {
|
||||
if (window.jQuery != null) {
|
||||
el = jQuery(el);
|
||||
if (el.on != null) {
|
||||
return el.on(event, handler);
|
||||
} else {
|
||||
return el.bind(event, handler);
|
||||
}
|
||||
} else if (window.addEventListener != null) {
|
||||
return el.addEventListener(event, handler, false);
|
||||
} else {
|
||||
event = 'on' + event;
|
||||
return el.attachEvent(event, handler);
|
||||
}
|
||||
},
|
||||
unbindEvent: function(el, event, handler) {
|
||||
if (window.jQuery != null) {
|
||||
el = jQuery(el);
|
||||
if (el.off != null) {
|
||||
return el.off(event, handler);
|
||||
} else {
|
||||
return el.unbind(event, handler);
|
||||
}
|
||||
} else if (window.removeEventListener != null) {
|
||||
return el.removeEventListener(event, handler, false);
|
||||
} else {
|
||||
event = 'on' + event;
|
||||
return el.detachEvent(event, handler);
|
||||
}
|
||||
},
|
||||
getInputValue: function(el) {
|
||||
var o, _i, _len, _results;
|
||||
|
||||
if (window.jQuery != null) {
|
||||
el = jQuery(el);
|
||||
switch (el[0].type) {
|
||||
case 'checkbox':
|
||||
return el.is(':checked');
|
||||
default:
|
||||
return el.val();
|
||||
}
|
||||
} else {
|
||||
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 Rivets.Util.bindEvent(el, 'change', this.publish);
|
||||
},
|
||||
unbind: function(el) {
|
||||
return Rivets.Util.unbindEvent(el, 'change', this.publish);
|
||||
},
|
||||
routine: function(el, value) {
|
||||
var _ref;
|
||||
|
||||
if (el.type === 'radio') {
|
||||
return el.checked = ((_ref = el.value) != null ? _ref.toString() : void 0) === (value != null ? value.toString() : void 0);
|
||||
} else {
|
||||
return el.checked = !!value;
|
||||
}
|
||||
}
|
||||
},
|
||||
unchecked: {
|
||||
publishes: true,
|
||||
bind: function(el) {
|
||||
return Rivets.Util.bindEvent(el, 'change', this.publish);
|
||||
},
|
||||
unbind: function(el) {
|
||||
return Rivets.Util.unbindEvent(el, 'change', this.publish);
|
||||
},
|
||||
routine: function(el, value) {
|
||||
var _ref;
|
||||
|
||||
if (el.type === 'radio') {
|
||||
return el.checked = ((_ref = el.value) != null ? _ref.toString() : void 0) !== (value != null ? value.toString() : void 0);
|
||||
} 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 Rivets.Util.bindEvent(el, 'change', this.publish);
|
||||
},
|
||||
unbind: function(el) {
|
||||
return Rivets.Util.unbindEvent(el, 'change', this.publish);
|
||||
},
|
||||
routine: function(el, value) {
|
||||
var o, _i, _len, _ref, _ref1, _ref2, _results;
|
||||
|
||||
if (window.jQuery != null) {
|
||||
el = jQuery(el);
|
||||
if ((value != null ? value.toString() : void 0) !== ((_ref = el.val()) != null ? _ref.toString() : void 0)) {
|
||||
return el.val(value != null ? value : '');
|
||||
}
|
||||
} else {
|
||||
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 = (_ref1 = o.value, __indexOf.call(value, _ref1) >= 0));
|
||||
}
|
||||
return _results;
|
||||
}
|
||||
} else if ((value != null ? value.toString() : void 0) !== ((_ref2 = el.value) != null ? _ref2.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,
|
||||
unbind: function(el) {
|
||||
if (this.handler) {
|
||||
return Rivets.Util.unbindEvent(el, this.args[0], this.handler);
|
||||
}
|
||||
},
|
||||
routine: function(el, value) {
|
||||
if (this.handler) {
|
||||
Rivets.Util.unbindEvent(el, this.args[0], this.handler);
|
||||
}
|
||||
return Rivets.Util.bindEvent(el, this.args[0], this.handler = this.eventHandler(value));
|
||||
}
|
||||
},
|
||||
"each-*": {
|
||||
block: true,
|
||||
bind: function(el) {
|
||||
var attr;
|
||||
|
||||
if (this.marker == null) {
|
||||
attr = ['data', this.view.config.prefix, this.type].join('-').replace('--', '-');
|
||||
this.marker = document.createComment(" rivets: " + this.type + " ");
|
||||
this.iterated = [];
|
||||
el.removeAttribute(attr);
|
||||
el.parentNode.insertBefore(this.marker, el);
|
||||
return el.parentNode.removeChild(el);
|
||||
}
|
||||
},
|
||||
unbind: function(el) {
|
||||
var view, _i, _len, _ref, _results;
|
||||
|
||||
if (this.iterated != null) {
|
||||
_ref = this.iterated;
|
||||
_results = [];
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
view = _ref[_i];
|
||||
_results.push(view.unbind());
|
||||
}
|
||||
return _results;
|
||||
}
|
||||
},
|
||||
routine: function(el, collection) {
|
||||
var data, i, index, k, key, model, modelName, options, previous, template, v, view, _i, _j, _len, _len1, _ref, _ref1, _ref2, _ref3, _results;
|
||||
|
||||
modelName = this.args[0];
|
||||
collection = collection || [];
|
||||
if (this.iterated.length > collection.length) {
|
||||
_ref = Array(this.iterated.length - collection.length);
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
i = _ref[_i];
|
||||
view = this.iterated.pop();
|
||||
view.unbind();
|
||||
this.marker.parentNode.removeChild(view.els[0]);
|
||||
}
|
||||
}
|
||||
_results = [];
|
||||
for (index = _j = 0, _len1 = collection.length; _j < _len1; index = ++_j) {
|
||||
model = collection[index];
|
||||
data = {};
|
||||
data[modelName] = model;
|
||||
if (this.iterated[index] == null) {
|
||||
_ref1 = this.view.models;
|
||||
for (key in _ref1) {
|
||||
model = _ref1[key];
|
||||
if ((_ref2 = data[key]) == null) {
|
||||
data[key] = model;
|
||||
}
|
||||
}
|
||||
previous = this.iterated.length ? this.iterated[this.iterated.length - 1].els[0] : this.marker;
|
||||
options = {
|
||||
binders: this.view.options.binders,
|
||||
formatters: this.view.options.formatters,
|
||||
config: {}
|
||||
};
|
||||
_ref3 = this.view.options.config;
|
||||
for (k in _ref3) {
|
||||
v = _ref3[k];
|
||||
options.config[k] = v;
|
||||
}
|
||||
options.config.preloadData = true;
|
||||
template = el.cloneNode(true);
|
||||
view = new Rivets.View(template, data, options);
|
||||
view.bind();
|
||||
this.iterated.push(view);
|
||||
_results.push(this.marker.parentNode.insertBefore(template, previous.nextSibling));
|
||||
} else if (this.iterated[index].models[modelName] !== model) {
|
||||
_results.push(this.iterated[index].update(data));
|
||||
} else {
|
||||
_results.push(void 0);
|
||||
}
|
||||
}
|
||||
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,
|
||||
handler: function(context, ev, binding) {
|
||||
return this.call(context, ev, binding.view.models);
|
||||
}
|
||||
};
|
||||
|
||||
Rivets.formatters = {};
|
||||
|
||||
Rivets.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, options) {
|
||||
var view;
|
||||
|
||||
if (models == null) {
|
||||
models = {};
|
||||
}
|
||||
if (options == null) {
|
||||
options = {};
|
||||
}
|
||||
view = new Rivets.View(el, models, options);
|
||||
view.bind();
|
||||
return view;
|
||||
};
|
||||
};
|
||||
|
||||
if (typeof exports === 'object') {
|
||||
Rivets.factory(exports);
|
||||
} else if (typeof define === 'function' && define.amd) {
|
||||
define(['exports'], function(exports) {
|
||||
Rivets.factory(this.rivets = exports);
|
||||
return exports;
|
||||
});
|
||||
} else {
|
||||
Rivets.factory(this.rivets = {});
|
||||
}
|
||||
|
||||
}).call(this);
|
||||
Vendored
+5
File diff suppressed because one or more lines are too long
@@ -0,0 +1,809 @@
|
||||
// Rivets.js
|
||||
// version: 0.5.6
|
||||
// author: Michael Richards
|
||||
// license: MIT
|
||||
(function() {
|
||||
var Rivets,
|
||||
__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(view, el, type, key, keypath, options) {
|
||||
var identifier, regexp, value, _ref;
|
||||
|
||||
this.view = view;
|
||||
this.el = el;
|
||||
this.type = type;
|
||||
this.key = key;
|
||||
this.keypath = keypath;
|
||||
this.options = options != null ? options : {};
|
||||
this.update = __bind(this.update, this);
|
||||
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.eventHandler = __bind(this.eventHandler, this);
|
||||
this.formattedValue = __bind(this.formattedValue, this);
|
||||
if (!(this.binder = this.view.binders[type])) {
|
||||
_ref = this.view.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 = this.view.binders['*']);
|
||||
if (this.binder instanceof Function) {
|
||||
this.binder = {
|
||||
routine: this.binder
|
||||
};
|
||||
}
|
||||
this.formatters = this.options.formatters || [];
|
||||
this.model = this.key ? this.view.models[this.key] : this.view.models;
|
||||
}
|
||||
|
||||
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] : this.view.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.eventHandler = function(fn) {
|
||||
var binding, handler;
|
||||
|
||||
handler = (binding = this).view.config.handler;
|
||||
return function(ev) {
|
||||
return handler.call(fn, this, ev, binding);
|
||||
};
|
||||
};
|
||||
|
||||
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] : this.view.config.adapter.read(this.model, this.keypath));
|
||||
};
|
||||
|
||||
Binding.prototype.publish = function() {
|
||||
var args, formatter, id, value, _i, _len, _ref, _ref1, _ref2;
|
||||
|
||||
value = Rivets.Util.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 = this.view.formatters[id]) != null ? _ref1.publish : void 0) {
|
||||
value = (_ref2 = this.view.formatters[id]).publish.apply(_ref2, [value].concat(__slice.call(args)));
|
||||
}
|
||||
}
|
||||
return this.view.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 {
|
||||
this.view.config.adapter.subscribe(this.model, this.keypath, this.sync);
|
||||
if (this.view.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(this.view.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) {
|
||||
this.view.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(this.view.config.adapter.unsubscribe(model, keypath, this.sync));
|
||||
}
|
||||
return _results;
|
||||
}
|
||||
};
|
||||
|
||||
Binding.prototype.update = function() {
|
||||
this.unbind();
|
||||
this.model = this.key ? this.view.models[this.key] : this.view.models;
|
||||
return this.bind();
|
||||
};
|
||||
|
||||
return Binding;
|
||||
|
||||
})();
|
||||
|
||||
Rivets.View = (function() {
|
||||
function View(els, models, options) {
|
||||
var k, option, v, _base, _i, _len, _ref, _ref1, _ref2, _ref3;
|
||||
|
||||
this.els = els;
|
||||
this.models = models;
|
||||
this.options = options != null ? options : {};
|
||||
this.update = __bind(this.update, this);
|
||||
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];
|
||||
}
|
||||
_ref = ['config', 'binders', 'formatters'];
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
option = _ref[_i];
|
||||
this[option] = {};
|
||||
if (this.options[option]) {
|
||||
_ref1 = this.options[option];
|
||||
for (k in _ref1) {
|
||||
v = _ref1[k];
|
||||
this[option][k] = v;
|
||||
}
|
||||
}
|
||||
_ref2 = Rivets[option];
|
||||
for (k in _ref2) {
|
||||
v = _ref2[k];
|
||||
if ((_ref3 = (_base = this[option])[k]) == null) {
|
||||
_base[k] = v;
|
||||
}
|
||||
}
|
||||
}
|
||||
this.build();
|
||||
}
|
||||
|
||||
View.prototype.bindingRegExp = function() {
|
||||
var prefix;
|
||||
|
||||
prefix = this.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, context, ctx, dependencies, identifier, key, keypath, 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 = _this.binders[type])) {
|
||||
_ref1 = _this.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 = _this.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]) {
|
||||
key = splitPath.shift();
|
||||
} else {
|
||||
key = null;
|
||||
splitPath.shift();
|
||||
}
|
||||
keypath = splitPath.join('.');
|
||||
if (!key || (_this.models[key] != null)) {
|
||||
if (dependencies = context.shift()) {
|
||||
options.dependencies = dependencies.split(/\s+/);
|
||||
}
|
||||
_this.bindings.push(new Rivets.Binding(_this, node, type, key, keypath, options));
|
||||
}
|
||||
}
|
||||
}
|
||||
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;
|
||||
};
|
||||
|
||||
View.prototype.update = function(models) {
|
||||
var binding, key, model, _results;
|
||||
|
||||
if (models == null) {
|
||||
models = {};
|
||||
}
|
||||
_results = [];
|
||||
for (key in models) {
|
||||
model = models[key];
|
||||
this.models[key] = model;
|
||||
_results.push((function() {
|
||||
var _i, _len, _ref, _results1;
|
||||
|
||||
_ref = this.select(function(b) {
|
||||
return b.key === key;
|
||||
});
|
||||
_results1 = [];
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
binding = _ref[_i];
|
||||
_results1.push(binding.update());
|
||||
}
|
||||
return _results1;
|
||||
}).call(this));
|
||||
}
|
||||
return _results;
|
||||
};
|
||||
|
||||
return View;
|
||||
|
||||
})();
|
||||
|
||||
Rivets.Util = {
|
||||
bindEvent: function(el, event, handler) {
|
||||
if (window.jQuery != null) {
|
||||
el = jQuery(el);
|
||||
if (el.on != null) {
|
||||
return el.on(event, handler);
|
||||
} else {
|
||||
return el.bind(event, handler);
|
||||
}
|
||||
} else if (window.addEventListener != null) {
|
||||
return el.addEventListener(event, handler, false);
|
||||
} else {
|
||||
event = 'on' + event;
|
||||
return el.attachEvent(event, handler);
|
||||
}
|
||||
},
|
||||
unbindEvent: function(el, event, handler) {
|
||||
if (window.jQuery != null) {
|
||||
el = jQuery(el);
|
||||
if (el.off != null) {
|
||||
return el.off(event, handler);
|
||||
} else {
|
||||
return el.unbind(event, handler);
|
||||
}
|
||||
} else if (window.removeEventListener != null) {
|
||||
return el.removeEventListener(event, handler, false);
|
||||
} else {
|
||||
event = 'on' + event;
|
||||
return el.detachEvent(event, handler);
|
||||
}
|
||||
},
|
||||
getInputValue: function(el) {
|
||||
var o, _i, _len, _results;
|
||||
|
||||
if (window.jQuery != null) {
|
||||
el = jQuery(el);
|
||||
switch (el[0].type) {
|
||||
case 'checkbox':
|
||||
return el.is(':checked');
|
||||
default:
|
||||
return el.val();
|
||||
}
|
||||
} else {
|
||||
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 Rivets.Util.bindEvent(el, 'change', this.publish);
|
||||
},
|
||||
unbind: function(el) {
|
||||
return Rivets.Util.unbindEvent(el, 'change', this.publish);
|
||||
},
|
||||
routine: function(el, value) {
|
||||
var _ref;
|
||||
|
||||
if (el.type === 'radio') {
|
||||
return el.checked = ((_ref = el.value) != null ? _ref.toString() : void 0) === (value != null ? value.toString() : void 0);
|
||||
} else {
|
||||
return el.checked = !!value;
|
||||
}
|
||||
}
|
||||
},
|
||||
unchecked: {
|
||||
publishes: true,
|
||||
bind: function(el) {
|
||||
return Rivets.Util.bindEvent(el, 'change', this.publish);
|
||||
},
|
||||
unbind: function(el) {
|
||||
return Rivets.Util.unbindEvent(el, 'change', this.publish);
|
||||
},
|
||||
routine: function(el, value) {
|
||||
var _ref;
|
||||
|
||||
if (el.type === 'radio') {
|
||||
return el.checked = ((_ref = el.value) != null ? _ref.toString() : void 0) !== (value != null ? value.toString() : void 0);
|
||||
} 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 Rivets.Util.bindEvent(el, 'change', this.publish);
|
||||
},
|
||||
unbind: function(el) {
|
||||
return Rivets.Util.unbindEvent(el, 'change', this.publish);
|
||||
},
|
||||
routine: function(el, value) {
|
||||
var o, _i, _len, _ref, _ref1, _ref2, _results;
|
||||
|
||||
if (window.jQuery != null) {
|
||||
el = jQuery(el);
|
||||
if ((value != null ? value.toString() : void 0) !== ((_ref = el.val()) != null ? _ref.toString() : void 0)) {
|
||||
return el.val(value != null ? value : '');
|
||||
}
|
||||
} else {
|
||||
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 = (_ref1 = o.value, __indexOf.call(value, _ref1) >= 0));
|
||||
}
|
||||
return _results;
|
||||
}
|
||||
} else if ((value != null ? value.toString() : void 0) !== ((_ref2 = el.value) != null ? _ref2.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 : '';
|
||||
}
|
||||
},
|
||||
"if": {
|
||||
block: true,
|
||||
bind: function(el) {
|
||||
var attr, declaration;
|
||||
|
||||
if (this.marker == null) {
|
||||
attr = ['data', this.view.config.prefix, this.type].join('-').replace('--', '-');
|
||||
declaration = el.getAttribute(attr);
|
||||
this.marker = document.createComment(" rivets: " + this.type + " " + declaration + " ");
|
||||
el.removeAttribute(attr);
|
||||
el.parentNode.insertBefore(this.marker, el);
|
||||
return el.parentNode.removeChild(el);
|
||||
}
|
||||
},
|
||||
unbind: function() {
|
||||
var _ref;
|
||||
|
||||
return (_ref = this.nested) != null ? _ref.unbind() : void 0;
|
||||
},
|
||||
routine: function(el, value) {
|
||||
var key, model, models, options, _ref;
|
||||
|
||||
if (value === (this.nested == null)) {
|
||||
if (value) {
|
||||
models = {};
|
||||
_ref = this.view.models;
|
||||
for (key in _ref) {
|
||||
model = _ref[key];
|
||||
models[key] = model;
|
||||
}
|
||||
options = {
|
||||
binders: this.view.options.binders,
|
||||
formatters: this.view.options.formatters,
|
||||
config: this.view.options.config
|
||||
};
|
||||
(this.nested = new Rivets.View(el, models, options)).bind();
|
||||
return this.marker.parentNode.insertBefore(el, this.marker.nextSibling);
|
||||
} else {
|
||||
el.parentNode.removeChild(el);
|
||||
this.nested.unbind();
|
||||
return delete this.nested;
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
unless: {
|
||||
block: true,
|
||||
bind: function(el) {
|
||||
return rivets.binders["if"].bind.call(this, el);
|
||||
},
|
||||
unbind: function() {
|
||||
return rivets.binders["if"].unbind.call(this);
|
||||
},
|
||||
routine: function(el, value) {
|
||||
return rivets.binders["if"].routine.call(this, el, !value);
|
||||
}
|
||||
},
|
||||
"on-*": {
|
||||
"function": true,
|
||||
unbind: function(el) {
|
||||
if (this.handler) {
|
||||
return Rivets.Util.unbindEvent(el, this.args[0], this.handler);
|
||||
}
|
||||
},
|
||||
routine: function(el, value) {
|
||||
if (this.handler) {
|
||||
Rivets.Util.unbindEvent(el, this.args[0], this.handler);
|
||||
}
|
||||
return Rivets.Util.bindEvent(el, this.args[0], this.handler = this.eventHandler(value));
|
||||
}
|
||||
},
|
||||
"each-*": {
|
||||
block: true,
|
||||
bind: function(el) {
|
||||
var attr;
|
||||
|
||||
if (this.marker == null) {
|
||||
attr = ['data', this.view.config.prefix, this.type].join('-').replace('--', '-');
|
||||
this.marker = document.createComment(" rivets: " + this.type + " ");
|
||||
this.iterated = [];
|
||||
el.removeAttribute(attr);
|
||||
el.parentNode.insertBefore(this.marker, el);
|
||||
return el.parentNode.removeChild(el);
|
||||
}
|
||||
},
|
||||
unbind: function(el) {
|
||||
var view, _i, _len, _ref, _results;
|
||||
|
||||
if (this.iterated != null) {
|
||||
_ref = this.iterated;
|
||||
_results = [];
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
view = _ref[_i];
|
||||
_results.push(view.unbind());
|
||||
}
|
||||
return _results;
|
||||
}
|
||||
},
|
||||
routine: function(el, collection) {
|
||||
var data, i, index, k, key, model, modelName, options, previous, template, v, view, _i, _j, _len, _len1, _ref, _ref1, _ref2, _ref3, _results;
|
||||
|
||||
modelName = this.args[0];
|
||||
collection = collection || [];
|
||||
if (this.iterated.length > collection.length) {
|
||||
_ref = Array(this.iterated.length - collection.length);
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
i = _ref[_i];
|
||||
view = this.iterated.pop();
|
||||
view.unbind();
|
||||
this.marker.parentNode.removeChild(view.els[0]);
|
||||
}
|
||||
}
|
||||
_results = [];
|
||||
for (index = _j = 0, _len1 = collection.length; _j < _len1; index = ++_j) {
|
||||
model = collection[index];
|
||||
data = {};
|
||||
data[modelName] = model;
|
||||
if (this.iterated[index] == null) {
|
||||
_ref1 = this.view.models;
|
||||
for (key in _ref1) {
|
||||
model = _ref1[key];
|
||||
if ((_ref2 = data[key]) == null) {
|
||||
data[key] = model;
|
||||
}
|
||||
}
|
||||
previous = this.iterated.length ? this.iterated[this.iterated.length - 1].els[0] : this.marker;
|
||||
options = {
|
||||
binders: this.view.options.binders,
|
||||
formatters: this.view.options.formatters,
|
||||
config: {}
|
||||
};
|
||||
_ref3 = this.view.options.config;
|
||||
for (k in _ref3) {
|
||||
v = _ref3[k];
|
||||
options.config[k] = v;
|
||||
}
|
||||
options.config.preloadData = true;
|
||||
template = el.cloneNode(true);
|
||||
view = new Rivets.View(template, data, options);
|
||||
view.bind();
|
||||
this.iterated.push(view);
|
||||
_results.push(this.marker.parentNode.insertBefore(template, previous.nextSibling));
|
||||
} else if (this.iterated[index].models[modelName] !== model) {
|
||||
_results.push(this.iterated[index].update(data));
|
||||
} else {
|
||||
_results.push(void 0);
|
||||
}
|
||||
}
|
||||
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,
|
||||
handler: function(context, ev, binding) {
|
||||
return this.call(context, ev, binding.view.models);
|
||||
}
|
||||
};
|
||||
|
||||
Rivets.formatters = {};
|
||||
|
||||
Rivets.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, options) {
|
||||
var view;
|
||||
|
||||
if (models == null) {
|
||||
models = {};
|
||||
}
|
||||
if (options == null) {
|
||||
options = {};
|
||||
}
|
||||
view = new Rivets.View(el, models, options);
|
||||
view.bind();
|
||||
return view;
|
||||
};
|
||||
};
|
||||
|
||||
if (typeof exports === 'object') {
|
||||
Rivets.factory(exports);
|
||||
} else if (typeof define === 'function' && define.amd) {
|
||||
define(['exports'], function(exports) {
|
||||
Rivets.factory(this.rivets = exports);
|
||||
return exports;
|
||||
});
|
||||
} else {
|
||||
Rivets.factory(this.rivets = {});
|
||||
}
|
||||
|
||||
}).call(this);
|
||||
Vendored
+5
File diff suppressed because one or more lines are too long
@@ -0,0 +1,809 @@
|
||||
// Rivets.js
|
||||
// version: 0.5.7
|
||||
// author: Michael Richards
|
||||
// license: MIT
|
||||
(function() {
|
||||
var Rivets,
|
||||
__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(view, el, type, key, keypath, options) {
|
||||
var identifier, regexp, value, _ref;
|
||||
|
||||
this.view = view;
|
||||
this.el = el;
|
||||
this.type = type;
|
||||
this.key = key;
|
||||
this.keypath = keypath;
|
||||
this.options = options != null ? options : {};
|
||||
this.update = __bind(this.update, this);
|
||||
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.eventHandler = __bind(this.eventHandler, this);
|
||||
this.formattedValue = __bind(this.formattedValue, this);
|
||||
if (!(this.binder = this.view.binders[type])) {
|
||||
_ref = this.view.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 = this.view.binders['*']);
|
||||
if (this.binder instanceof Function) {
|
||||
this.binder = {
|
||||
routine: this.binder
|
||||
};
|
||||
}
|
||||
this.formatters = this.options.formatters || [];
|
||||
this.model = this.key ? this.view.models[this.key] : this.view.models;
|
||||
}
|
||||
|
||||
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] : this.view.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.eventHandler = function(fn) {
|
||||
var binding, handler;
|
||||
|
||||
handler = (binding = this).view.config.handler;
|
||||
return function(ev) {
|
||||
return handler.call(fn, this, ev, binding);
|
||||
};
|
||||
};
|
||||
|
||||
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] : this.view.config.adapter.read(this.model, this.keypath));
|
||||
};
|
||||
|
||||
Binding.prototype.publish = function() {
|
||||
var args, formatter, id, value, _i, _len, _ref, _ref1, _ref2;
|
||||
|
||||
value = Rivets.Util.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 = this.view.formatters[id]) != null ? _ref1.publish : void 0) {
|
||||
value = (_ref2 = this.view.formatters[id]).publish.apply(_ref2, [value].concat(__slice.call(args)));
|
||||
}
|
||||
}
|
||||
return this.view.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 {
|
||||
this.view.config.adapter.subscribe(this.model, this.keypath, this.sync);
|
||||
if (this.view.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(this.view.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) {
|
||||
this.view.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(this.view.config.adapter.unsubscribe(model, keypath, this.sync));
|
||||
}
|
||||
return _results;
|
||||
}
|
||||
};
|
||||
|
||||
Binding.prototype.update = function() {
|
||||
this.unbind();
|
||||
this.model = this.key ? this.view.models[this.key] : this.view.models;
|
||||
return this.bind();
|
||||
};
|
||||
|
||||
return Binding;
|
||||
|
||||
})();
|
||||
|
||||
Rivets.View = (function() {
|
||||
function View(els, models, options) {
|
||||
var k, option, v, _base, _i, _len, _ref, _ref1, _ref2, _ref3;
|
||||
|
||||
this.els = els;
|
||||
this.models = models;
|
||||
this.options = options != null ? options : {};
|
||||
this.update = __bind(this.update, this);
|
||||
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];
|
||||
}
|
||||
_ref = ['config', 'binders', 'formatters'];
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
option = _ref[_i];
|
||||
this[option] = {};
|
||||
if (this.options[option]) {
|
||||
_ref1 = this.options[option];
|
||||
for (k in _ref1) {
|
||||
v = _ref1[k];
|
||||
this[option][k] = v;
|
||||
}
|
||||
}
|
||||
_ref2 = Rivets[option];
|
||||
for (k in _ref2) {
|
||||
v = _ref2[k];
|
||||
if ((_ref3 = (_base = this[option])[k]) == null) {
|
||||
_base[k] = v;
|
||||
}
|
||||
}
|
||||
}
|
||||
this.build();
|
||||
}
|
||||
|
||||
View.prototype.bindingRegExp = function() {
|
||||
var prefix;
|
||||
|
||||
prefix = this.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, context, ctx, dependencies, identifier, key, keypath, 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 = _this.binders[type])) {
|
||||
_ref1 = _this.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 = _this.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]) {
|
||||
key = splitPath.shift();
|
||||
} else {
|
||||
key = null;
|
||||
splitPath.shift();
|
||||
}
|
||||
keypath = splitPath.join('.');
|
||||
if (!key || (_this.models[key] != null)) {
|
||||
if (dependencies = context.shift()) {
|
||||
options.dependencies = dependencies.split(/\s+/);
|
||||
}
|
||||
_this.bindings.push(new Rivets.Binding(_this, node, type, key, keypath, options));
|
||||
}
|
||||
}
|
||||
}
|
||||
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;
|
||||
};
|
||||
|
||||
View.prototype.update = function(models) {
|
||||
var binding, key, model, _results;
|
||||
|
||||
if (models == null) {
|
||||
models = {};
|
||||
}
|
||||
_results = [];
|
||||
for (key in models) {
|
||||
model = models[key];
|
||||
this.models[key] = model;
|
||||
_results.push((function() {
|
||||
var _i, _len, _ref, _results1;
|
||||
|
||||
_ref = this.select(function(b) {
|
||||
return b.key === key;
|
||||
});
|
||||
_results1 = [];
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
binding = _ref[_i];
|
||||
_results1.push(binding.update());
|
||||
}
|
||||
return _results1;
|
||||
}).call(this));
|
||||
}
|
||||
return _results;
|
||||
};
|
||||
|
||||
return View;
|
||||
|
||||
})();
|
||||
|
||||
Rivets.Util = {
|
||||
bindEvent: function(el, event, handler) {
|
||||
if (window.jQuery != null) {
|
||||
el = jQuery(el);
|
||||
if (el.on != null) {
|
||||
return el.on(event, handler);
|
||||
} else {
|
||||
return el.bind(event, handler);
|
||||
}
|
||||
} else if (window.addEventListener != null) {
|
||||
return el.addEventListener(event, handler, false);
|
||||
} else {
|
||||
event = 'on' + event;
|
||||
return el.attachEvent(event, handler);
|
||||
}
|
||||
},
|
||||
unbindEvent: function(el, event, handler) {
|
||||
if (window.jQuery != null) {
|
||||
el = jQuery(el);
|
||||
if (el.off != null) {
|
||||
return el.off(event, handler);
|
||||
} else {
|
||||
return el.unbind(event, handler);
|
||||
}
|
||||
} else if (window.removeEventListener != null) {
|
||||
return el.removeEventListener(event, handler, false);
|
||||
} else {
|
||||
event = 'on' + event;
|
||||
return el.detachEvent(event, handler);
|
||||
}
|
||||
},
|
||||
getInputValue: function(el) {
|
||||
var o, _i, _len, _results;
|
||||
|
||||
if (window.jQuery != null) {
|
||||
el = jQuery(el);
|
||||
switch (el[0].type) {
|
||||
case 'checkbox':
|
||||
return el.is(':checked');
|
||||
default:
|
||||
return el.val();
|
||||
}
|
||||
} else {
|
||||
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 Rivets.Util.bindEvent(el, 'change', this.publish);
|
||||
},
|
||||
unbind: function(el) {
|
||||
return Rivets.Util.unbindEvent(el, 'change', this.publish);
|
||||
},
|
||||
routine: function(el, value) {
|
||||
var _ref;
|
||||
|
||||
if (el.type === 'radio') {
|
||||
return el.checked = ((_ref = el.value) != null ? _ref.toString() : void 0) === (value != null ? value.toString() : void 0);
|
||||
} else {
|
||||
return el.checked = !!value;
|
||||
}
|
||||
}
|
||||
},
|
||||
unchecked: {
|
||||
publishes: true,
|
||||
bind: function(el) {
|
||||
return Rivets.Util.bindEvent(el, 'change', this.publish);
|
||||
},
|
||||
unbind: function(el) {
|
||||
return Rivets.Util.unbindEvent(el, 'change', this.publish);
|
||||
},
|
||||
routine: function(el, value) {
|
||||
var _ref;
|
||||
|
||||
if (el.type === 'radio') {
|
||||
return el.checked = ((_ref = el.value) != null ? _ref.toString() : void 0) !== (value != null ? value.toString() : void 0);
|
||||
} 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 Rivets.Util.bindEvent(el, 'change', this.publish);
|
||||
},
|
||||
unbind: function(el) {
|
||||
return Rivets.Util.unbindEvent(el, 'change', this.publish);
|
||||
},
|
||||
routine: function(el, value) {
|
||||
var o, _i, _len, _ref, _ref1, _ref2, _results;
|
||||
|
||||
if (window.jQuery != null) {
|
||||
el = jQuery(el);
|
||||
if ((value != null ? value.toString() : void 0) !== ((_ref = el.val()) != null ? _ref.toString() : void 0)) {
|
||||
return el.val(value != null ? value : '');
|
||||
}
|
||||
} else {
|
||||
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 = (_ref1 = o.value, __indexOf.call(value, _ref1) >= 0));
|
||||
}
|
||||
return _results;
|
||||
}
|
||||
} else if ((value != null ? value.toString() : void 0) !== ((_ref2 = el.value) != null ? _ref2.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 : '';
|
||||
}
|
||||
},
|
||||
"if": {
|
||||
block: true,
|
||||
bind: function(el) {
|
||||
var attr, declaration;
|
||||
|
||||
if (this.marker == null) {
|
||||
attr = ['data', this.view.config.prefix, this.type].join('-').replace('--', '-');
|
||||
declaration = el.getAttribute(attr);
|
||||
this.marker = document.createComment(" rivets: " + this.type + " " + declaration + " ");
|
||||
el.removeAttribute(attr);
|
||||
el.parentNode.insertBefore(this.marker, el);
|
||||
return el.parentNode.removeChild(el);
|
||||
}
|
||||
},
|
||||
unbind: function() {
|
||||
var _ref;
|
||||
|
||||
return (_ref = this.nested) != null ? _ref.unbind() : void 0;
|
||||
},
|
||||
routine: function(el, value) {
|
||||
var key, model, models, options, _ref;
|
||||
|
||||
if (value === (this.nested == null)) {
|
||||
if (value) {
|
||||
models = {};
|
||||
_ref = this.view.models;
|
||||
for (key in _ref) {
|
||||
model = _ref[key];
|
||||
models[key] = model;
|
||||
}
|
||||
options = {
|
||||
binders: this.view.options.binders,
|
||||
formatters: this.view.options.formatters,
|
||||
config: this.view.options.config
|
||||
};
|
||||
(this.nested = new Rivets.View(el, models, options)).bind();
|
||||
return this.marker.parentNode.insertBefore(el, this.marker.nextSibling);
|
||||
} else {
|
||||
el.parentNode.removeChild(el);
|
||||
this.nested.unbind();
|
||||
return delete this.nested;
|
||||
}
|
||||
}
|
||||
}
|
||||
},
|
||||
unless: {
|
||||
block: true,
|
||||
bind: function(el) {
|
||||
return Rivets.binders["if"].bind.call(this, el);
|
||||
},
|
||||
unbind: function() {
|
||||
return Rivets.binders["if"].unbind.call(this);
|
||||
},
|
||||
routine: function(el, value) {
|
||||
return Rivets.binders["if"].routine.call(this, el, !value);
|
||||
}
|
||||
},
|
||||
"on-*": {
|
||||
"function": true,
|
||||
unbind: function(el) {
|
||||
if (this.handler) {
|
||||
return Rivets.Util.unbindEvent(el, this.args[0], this.handler);
|
||||
}
|
||||
},
|
||||
routine: function(el, value) {
|
||||
if (this.handler) {
|
||||
Rivets.Util.unbindEvent(el, this.args[0], this.handler);
|
||||
}
|
||||
return Rivets.Util.bindEvent(el, this.args[0], this.handler = this.eventHandler(value));
|
||||
}
|
||||
},
|
||||
"each-*": {
|
||||
block: true,
|
||||
bind: function(el) {
|
||||
var attr;
|
||||
|
||||
if (this.marker == null) {
|
||||
attr = ['data', this.view.config.prefix, this.type].join('-').replace('--', '-');
|
||||
this.marker = document.createComment(" rivets: " + this.type + " ");
|
||||
this.iterated = [];
|
||||
el.removeAttribute(attr);
|
||||
el.parentNode.insertBefore(this.marker, el);
|
||||
return el.parentNode.removeChild(el);
|
||||
}
|
||||
},
|
||||
unbind: function(el) {
|
||||
var view, _i, _len, _ref, _results;
|
||||
|
||||
if (this.iterated != null) {
|
||||
_ref = this.iterated;
|
||||
_results = [];
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
view = _ref[_i];
|
||||
_results.push(view.unbind());
|
||||
}
|
||||
return _results;
|
||||
}
|
||||
},
|
||||
routine: function(el, collection) {
|
||||
var data, i, index, k, key, model, modelName, options, previous, template, v, view, _i, _j, _len, _len1, _ref, _ref1, _ref2, _ref3, _results;
|
||||
|
||||
modelName = this.args[0];
|
||||
collection = collection || [];
|
||||
if (this.iterated.length > collection.length) {
|
||||
_ref = Array(this.iterated.length - collection.length);
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
i = _ref[_i];
|
||||
view = this.iterated.pop();
|
||||
view.unbind();
|
||||
this.marker.parentNode.removeChild(view.els[0]);
|
||||
}
|
||||
}
|
||||
_results = [];
|
||||
for (index = _j = 0, _len1 = collection.length; _j < _len1; index = ++_j) {
|
||||
model = collection[index];
|
||||
data = {};
|
||||
data[modelName] = model;
|
||||
if (this.iterated[index] == null) {
|
||||
_ref1 = this.view.models;
|
||||
for (key in _ref1) {
|
||||
model = _ref1[key];
|
||||
if ((_ref2 = data[key]) == null) {
|
||||
data[key] = model;
|
||||
}
|
||||
}
|
||||
previous = this.iterated.length ? this.iterated[this.iterated.length - 1].els[0] : this.marker;
|
||||
options = {
|
||||
binders: this.view.options.binders,
|
||||
formatters: this.view.options.formatters,
|
||||
config: {}
|
||||
};
|
||||
_ref3 = this.view.options.config;
|
||||
for (k in _ref3) {
|
||||
v = _ref3[k];
|
||||
options.config[k] = v;
|
||||
}
|
||||
options.config.preloadData = true;
|
||||
template = el.cloneNode(true);
|
||||
view = new Rivets.View(template, data, options);
|
||||
view.bind();
|
||||
this.iterated.push(view);
|
||||
_results.push(this.marker.parentNode.insertBefore(template, previous.nextSibling));
|
||||
} else if (this.iterated[index].models[modelName] !== model) {
|
||||
_results.push(this.iterated[index].update(data));
|
||||
} else {
|
||||
_results.push(void 0);
|
||||
}
|
||||
}
|
||||
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,
|
||||
handler: function(context, ev, binding) {
|
||||
return this.call(context, ev, binding.view.models);
|
||||
}
|
||||
};
|
||||
|
||||
Rivets.formatters = {};
|
||||
|
||||
Rivets.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, options) {
|
||||
var view;
|
||||
|
||||
if (models == null) {
|
||||
models = {};
|
||||
}
|
||||
if (options == null) {
|
||||
options = {};
|
||||
}
|
||||
view = new Rivets.View(el, models, options);
|
||||
view.bind();
|
||||
return view;
|
||||
};
|
||||
};
|
||||
|
||||
if (typeof exports === 'object') {
|
||||
Rivets.factory(exports);
|
||||
} else if (typeof define === 'function' && define.amd) {
|
||||
define(['exports'], function(exports) {
|
||||
Rivets.factory(this.rivets = exports);
|
||||
return exports;
|
||||
});
|
||||
} else {
|
||||
Rivets.factory(this.rivets = {});
|
||||
}
|
||||
|
||||
}).call(this);
|
||||
Vendored
+5
File diff suppressed because one or more lines are too long
@@ -0,0 +1,846 @@
|
||||
// Rivets.js
|
||||
// version: 0.5.8
|
||||
// author: Michael Richards
|
||||
// license: MIT
|
||||
(function() {
|
||||
var Rivets,
|
||||
__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(view, el, type, key, keypath, options) {
|
||||
var identifier, regexp, value, _ref;
|
||||
|
||||
this.view = view;
|
||||
this.el = el;
|
||||
this.type = type;
|
||||
this.key = key;
|
||||
this.keypath = keypath;
|
||||
this.options = options != null ? options : {};
|
||||
this.update = __bind(this.update, this);
|
||||
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.eventHandler = __bind(this.eventHandler, this);
|
||||
this.formattedValue = __bind(this.formattedValue, this);
|
||||
if (!(this.binder = this.view.binders[type])) {
|
||||
_ref = this.view.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 = this.view.binders['*']);
|
||||
if (this.binder instanceof Function) {
|
||||
this.binder = {
|
||||
routine: this.binder
|
||||
};
|
||||
}
|
||||
this.formatters = this.options.formatters || [];
|
||||
this.model = this.key ? this.view.models[this.key] : this.view.models;
|
||||
}
|
||||
|
||||
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] : this.view.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.eventHandler = function(fn) {
|
||||
var binding, handler;
|
||||
|
||||
handler = (binding = this).view.config.handler;
|
||||
return function(ev) {
|
||||
return handler.call(fn, this, ev, binding);
|
||||
};
|
||||
};
|
||||
|
||||
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] : this.view.config.adapter.read(this.model, this.keypath));
|
||||
};
|
||||
|
||||
Binding.prototype.publish = function() {
|
||||
var args, formatter, id, value, _i, _len, _ref, _ref1, _ref2;
|
||||
|
||||
value = Rivets.Util.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 = this.view.formatters[id]) != null ? _ref1.publish : void 0) {
|
||||
value = (_ref2 = this.view.formatters[id]).publish.apply(_ref2, [value].concat(__slice.call(args)));
|
||||
}
|
||||
}
|
||||
return this.view.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 {
|
||||
this.view.config.adapter.subscribe(this.model, this.keypath, this.sync);
|
||||
if (this.view.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(this.view.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) {
|
||||
this.view.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(this.view.config.adapter.unsubscribe(model, keypath, this.sync));
|
||||
}
|
||||
return _results;
|
||||
}
|
||||
};
|
||||
|
||||
Binding.prototype.update = function(models) {
|
||||
var _ref;
|
||||
|
||||
if (models == null) {
|
||||
models = {};
|
||||
}
|
||||
if (this.key) {
|
||||
if (models[this.key]) {
|
||||
if (!this.options.bypass) {
|
||||
this.view.config.adapter.unsubscribe(this.model, this.keypath, this.sync);
|
||||
}
|
||||
this.model = models[this.key];
|
||||
if (this.options.bypass) {
|
||||
this.sync();
|
||||
} else {
|
||||
this.view.config.adapter.subscribe(this.model, this.keypath, this.sync);
|
||||
if (this.view.config.preloadData) {
|
||||
this.sync();
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
this.sync();
|
||||
}
|
||||
return (_ref = this.binder.update) != null ? _ref.call(this, models) : void 0;
|
||||
};
|
||||
|
||||
return Binding;
|
||||
|
||||
})();
|
||||
|
||||
Rivets.View = (function() {
|
||||
function View(els, models, options) {
|
||||
var k, option, v, _base, _i, _len, _ref, _ref1, _ref2, _ref3;
|
||||
|
||||
this.els = els;
|
||||
this.models = models;
|
||||
this.options = options != null ? options : {};
|
||||
this.update = __bind(this.update, this);
|
||||
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];
|
||||
}
|
||||
_ref = ['config', 'binders', 'formatters'];
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
option = _ref[_i];
|
||||
this[option] = {};
|
||||
if (this.options[option]) {
|
||||
_ref1 = this.options[option];
|
||||
for (k in _ref1) {
|
||||
v = _ref1[k];
|
||||
this[option][k] = v;
|
||||
}
|
||||
}
|
||||
_ref2 = Rivets[option];
|
||||
for (k in _ref2) {
|
||||
v = _ref2[k];
|
||||
if ((_ref3 = (_base = this[option])[k]) == null) {
|
||||
_base[k] = v;
|
||||
}
|
||||
}
|
||||
}
|
||||
this.build();
|
||||
}
|
||||
|
||||
View.prototype.bindingRegExp = function() {
|
||||
var prefix;
|
||||
|
||||
prefix = this.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, context, ctx, dependencies, identifier, key, keypath, 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 = _this.binders[type])) {
|
||||
_ref1 = _this.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 = _this.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]) {
|
||||
key = splitPath.shift();
|
||||
} else {
|
||||
key = null;
|
||||
splitPath.shift();
|
||||
}
|
||||
keypath = splitPath.join('.');
|
||||
if (!key || (_this.models[key] != null)) {
|
||||
if (dependencies = context.shift()) {
|
||||
options.dependencies = dependencies.split(/\s+/);
|
||||
}
|
||||
_this.bindings.push(new Rivets.Binding(_this, node, type, key, keypath, options));
|
||||
}
|
||||
}
|
||||
}
|
||||
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;
|
||||
};
|
||||
|
||||
View.prototype.update = function(models) {
|
||||
var binding, key, model, _i, _len, _ref, _results;
|
||||
|
||||
if (models == null) {
|
||||
models = {};
|
||||
}
|
||||
for (key in models) {
|
||||
model = models[key];
|
||||
this.models[key] = model;
|
||||
}
|
||||
_ref = this.bindings;
|
||||
_results = [];
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
binding = _ref[_i];
|
||||
_results.push(binding.update(models));
|
||||
}
|
||||
return _results;
|
||||
};
|
||||
|
||||
return View;
|
||||
|
||||
})();
|
||||
|
||||
Rivets.Util = {
|
||||
bindEvent: function(el, event, handler) {
|
||||
if (window.jQuery != null) {
|
||||
el = jQuery(el);
|
||||
if (el.on != null) {
|
||||
return el.on(event, handler);
|
||||
} else {
|
||||
return el.bind(event, handler);
|
||||
}
|
||||
} else if (window.addEventListener != null) {
|
||||
return el.addEventListener(event, handler, false);
|
||||
} else {
|
||||
event = 'on' + event;
|
||||
return el.attachEvent(event, handler);
|
||||
}
|
||||
},
|
||||
unbindEvent: function(el, event, handler) {
|
||||
if (window.jQuery != null) {
|
||||
el = jQuery(el);
|
||||
if (el.off != null) {
|
||||
return el.off(event, handler);
|
||||
} else {
|
||||
return el.unbind(event, handler);
|
||||
}
|
||||
} else if (window.removeEventListener != null) {
|
||||
return el.removeEventListener(event, handler, false);
|
||||
} else {
|
||||
event = 'on' + event;
|
||||
return el.detachEvent(event, handler);
|
||||
}
|
||||
},
|
||||
getInputValue: function(el) {
|
||||
var o, _i, _len, _results;
|
||||
|
||||
if (window.jQuery != null) {
|
||||
el = jQuery(el);
|
||||
switch (el[0].type) {
|
||||
case 'checkbox':
|
||||
return el.is(':checked');
|
||||
default:
|
||||
return el.val();
|
||||
}
|
||||
} else {
|
||||
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 Rivets.Util.bindEvent(el, 'change', this.publish);
|
||||
},
|
||||
unbind: function(el) {
|
||||
return Rivets.Util.unbindEvent(el, 'change', this.publish);
|
||||
},
|
||||
routine: function(el, value) {
|
||||
var _ref;
|
||||
|
||||
if (el.type === 'radio') {
|
||||
return el.checked = ((_ref = el.value) != null ? _ref.toString() : void 0) === (value != null ? value.toString() : void 0);
|
||||
} else {
|
||||
return el.checked = !!value;
|
||||
}
|
||||
}
|
||||
},
|
||||
unchecked: {
|
||||
publishes: true,
|
||||
bind: function(el) {
|
||||
return Rivets.Util.bindEvent(el, 'change', this.publish);
|
||||
},
|
||||
unbind: function(el) {
|
||||
return Rivets.Util.unbindEvent(el, 'change', this.publish);
|
||||
},
|
||||
routine: function(el, value) {
|
||||
var _ref;
|
||||
|
||||
if (el.type === 'radio') {
|
||||
return el.checked = ((_ref = el.value) != null ? _ref.toString() : void 0) !== (value != null ? value.toString() : void 0);
|
||||
} 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 Rivets.Util.bindEvent(el, 'change', this.publish);
|
||||
},
|
||||
unbind: function(el) {
|
||||
return Rivets.Util.unbindEvent(el, 'change', this.publish);
|
||||
},
|
||||
routine: function(el, value) {
|
||||
var o, _i, _len, _ref, _ref1, _ref2, _results;
|
||||
|
||||
if (window.jQuery != null) {
|
||||
el = jQuery(el);
|
||||
if ((value != null ? value.toString() : void 0) !== ((_ref = el.val()) != null ? _ref.toString() : void 0)) {
|
||||
return el.val(value != null ? value : '');
|
||||
}
|
||||
} else {
|
||||
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 = (_ref1 = o.value, __indexOf.call(value, _ref1) >= 0));
|
||||
}
|
||||
return _results;
|
||||
}
|
||||
} else if ((value != null ? value.toString() : void 0) !== ((_ref2 = el.value) != null ? _ref2.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 : '';
|
||||
}
|
||||
},
|
||||
"if": {
|
||||
block: true,
|
||||
bind: function(el) {
|
||||
var attr, declaration;
|
||||
|
||||
if (this.marker == null) {
|
||||
attr = ['data', this.view.config.prefix, this.type].join('-').replace('--', '-');
|
||||
declaration = el.getAttribute(attr);
|
||||
this.marker = document.createComment(" rivets: " + this.type + " " + declaration + " ");
|
||||
el.removeAttribute(attr);
|
||||
el.parentNode.insertBefore(this.marker, el);
|
||||
return el.parentNode.removeChild(el);
|
||||
}
|
||||
},
|
||||
unbind: function() {
|
||||
var _ref;
|
||||
|
||||
return (_ref = this.nested) != null ? _ref.unbind() : void 0;
|
||||
},
|
||||
routine: function(el, value) {
|
||||
var key, model, models, options, _ref;
|
||||
|
||||
if (value === (this.nested == null)) {
|
||||
if (value) {
|
||||
models = {};
|
||||
_ref = this.view.models;
|
||||
for (key in _ref) {
|
||||
model = _ref[key];
|
||||
models[key] = model;
|
||||
}
|
||||
options = {
|
||||
binders: this.view.options.binders,
|
||||
formatters: this.view.options.formatters,
|
||||
config: this.view.options.config
|
||||
};
|
||||
(this.nested = new Rivets.View(el, models, options)).bind();
|
||||
return this.marker.parentNode.insertBefore(el, this.marker.nextSibling);
|
||||
} else {
|
||||
el.parentNode.removeChild(el);
|
||||
this.nested.unbind();
|
||||
return delete this.nested;
|
||||
}
|
||||
}
|
||||
},
|
||||
update: function(models) {
|
||||
return this.nested.update(models);
|
||||
}
|
||||
},
|
||||
unless: {
|
||||
block: true,
|
||||
bind: function(el) {
|
||||
return Rivets.binders["if"].bind.call(this, el);
|
||||
},
|
||||
unbind: function() {
|
||||
return Rivets.binders["if"].unbind.call(this);
|
||||
},
|
||||
routine: function(el, value) {
|
||||
return Rivets.binders["if"].routine.call(this, el, !value);
|
||||
},
|
||||
update: function(models) {
|
||||
return Rivets.binders["if"].update.call(this, models);
|
||||
}
|
||||
},
|
||||
"on-*": {
|
||||
"function": true,
|
||||
unbind: function(el) {
|
||||
if (this.handler) {
|
||||
return Rivets.Util.unbindEvent(el, this.args[0], this.handler);
|
||||
}
|
||||
},
|
||||
routine: function(el, value) {
|
||||
if (this.handler) {
|
||||
Rivets.Util.unbindEvent(el, this.args[0], this.handler);
|
||||
}
|
||||
return Rivets.Util.bindEvent(el, this.args[0], this.handler = this.eventHandler(value));
|
||||
}
|
||||
},
|
||||
"each-*": {
|
||||
block: true,
|
||||
bind: function(el) {
|
||||
var attr;
|
||||
|
||||
if (this.marker == null) {
|
||||
attr = ['data', this.view.config.prefix, this.type].join('-').replace('--', '-');
|
||||
this.marker = document.createComment(" rivets: " + this.type + " ");
|
||||
this.iterated = [];
|
||||
el.removeAttribute(attr);
|
||||
el.parentNode.insertBefore(this.marker, el);
|
||||
return el.parentNode.removeChild(el);
|
||||
}
|
||||
},
|
||||
unbind: function(el) {
|
||||
var view, _i, _len, _ref, _results;
|
||||
|
||||
if (this.iterated != null) {
|
||||
_ref = this.iterated;
|
||||
_results = [];
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
view = _ref[_i];
|
||||
_results.push(view.unbind());
|
||||
}
|
||||
return _results;
|
||||
}
|
||||
},
|
||||
routine: function(el, collection) {
|
||||
var data, i, index, k, key, model, modelName, options, previous, template, v, view, _i, _j, _len, _len1, _ref, _ref1, _ref2, _ref3, _results;
|
||||
|
||||
modelName = this.args[0];
|
||||
collection = collection || [];
|
||||
if (this.iterated.length > collection.length) {
|
||||
_ref = Array(this.iterated.length - collection.length);
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
i = _ref[_i];
|
||||
view = this.iterated.pop();
|
||||
view.unbind();
|
||||
this.marker.parentNode.removeChild(view.els[0]);
|
||||
}
|
||||
}
|
||||
_results = [];
|
||||
for (index = _j = 0, _len1 = collection.length; _j < _len1; index = ++_j) {
|
||||
model = collection[index];
|
||||
data = {};
|
||||
data[modelName] = model;
|
||||
if (this.iterated[index] == null) {
|
||||
_ref1 = this.view.models;
|
||||
for (key in _ref1) {
|
||||
model = _ref1[key];
|
||||
if ((_ref2 = data[key]) == null) {
|
||||
data[key] = model;
|
||||
}
|
||||
}
|
||||
previous = this.iterated.length ? this.iterated[this.iterated.length - 1].els[0] : this.marker;
|
||||
options = {
|
||||
binders: this.view.options.binders,
|
||||
formatters: this.view.options.formatters,
|
||||
config: {}
|
||||
};
|
||||
_ref3 = this.view.options.config;
|
||||
for (k in _ref3) {
|
||||
v = _ref3[k];
|
||||
options.config[k] = v;
|
||||
}
|
||||
options.config.preloadData = true;
|
||||
template = el.cloneNode(true);
|
||||
view = new Rivets.View(template, data, options);
|
||||
view.bind();
|
||||
this.iterated.push(view);
|
||||
_results.push(this.marker.parentNode.insertBefore(template, previous.nextSibling));
|
||||
} else if (this.iterated[index].models[modelName] !== model) {
|
||||
_results.push(this.iterated[index].update(data));
|
||||
} else {
|
||||
_results.push(void 0);
|
||||
}
|
||||
}
|
||||
return _results;
|
||||
},
|
||||
update: function(models) {
|
||||
var data, key, model, view, _i, _len, _ref, _results;
|
||||
|
||||
data = {};
|
||||
for (key in models) {
|
||||
model = models[key];
|
||||
if (key !== this.args[0]) {
|
||||
data[key] = model;
|
||||
}
|
||||
}
|
||||
_ref = this.iterated;
|
||||
_results = [];
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
view = _ref[_i];
|
||||
_results.push(view.update(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,
|
||||
handler: function(context, ev, binding) {
|
||||
return this.call(context, ev, binding.view.models);
|
||||
}
|
||||
};
|
||||
|
||||
Rivets.formatters = {};
|
||||
|
||||
Rivets.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, options) {
|
||||
var view;
|
||||
|
||||
if (models == null) {
|
||||
models = {};
|
||||
}
|
||||
if (options == null) {
|
||||
options = {};
|
||||
}
|
||||
view = new Rivets.View(el, models, options);
|
||||
view.bind();
|
||||
return view;
|
||||
};
|
||||
};
|
||||
|
||||
if (typeof exports === 'object') {
|
||||
Rivets.factory(exports);
|
||||
} else if (typeof define === 'function' && define.amd) {
|
||||
define(['exports'], function(exports) {
|
||||
Rivets.factory(this.rivets = exports);
|
||||
return exports;
|
||||
});
|
||||
} else {
|
||||
Rivets.factory(this.rivets = {});
|
||||
}
|
||||
|
||||
}).call(this);
|
||||
Vendored
+5
File diff suppressed because one or more lines are too long
@@ -0,0 +1,939 @@
|
||||
// Rivets.js
|
||||
// version: 0.5.9
|
||||
// author: Michael Richards
|
||||
// license: MIT
|
||||
(function() {
|
||||
var Rivets,
|
||||
__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(view, el, type, key, keypath, options) {
|
||||
var identifier, regexp, value, _ref;
|
||||
|
||||
this.view = view;
|
||||
this.el = el;
|
||||
this.type = type;
|
||||
this.key = key;
|
||||
this.keypath = keypath;
|
||||
this.options = options != null ? options : {};
|
||||
this.update = __bind(this.update, this);
|
||||
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.eventHandler = __bind(this.eventHandler, this);
|
||||
this.formattedValue = __bind(this.formattedValue, this);
|
||||
if (!(this.binder = Rivets.internalBinders[this.type] || this.view.binders[type])) {
|
||||
_ref = this.view.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 = this.view.binders['*']);
|
||||
if (this.binder instanceof Function) {
|
||||
this.binder = {
|
||||
routine: this.binder
|
||||
};
|
||||
}
|
||||
this.formatters = this.options.formatters || [];
|
||||
this.model = this.key ? this.view.models[this.key] : this.view.models;
|
||||
}
|
||||
|
||||
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] : this.view.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.eventHandler = function(fn) {
|
||||
var binding, handler;
|
||||
|
||||
handler = (binding = this).view.config.handler;
|
||||
return function(ev) {
|
||||
return handler.call(fn, this, ev, binding);
|
||||
};
|
||||
};
|
||||
|
||||
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] : this.view.config.adapter.read(this.model, this.keypath));
|
||||
};
|
||||
|
||||
Binding.prototype.publish = function() {
|
||||
var args, formatter, id, value, _i, _len, _ref, _ref1, _ref2;
|
||||
|
||||
value = Rivets.Util.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 = this.view.formatters[id]) != null ? _ref1.publish : void 0) {
|
||||
value = (_ref2 = this.view.formatters[id]).publish.apply(_ref2, [value].concat(__slice.call(args)));
|
||||
}
|
||||
}
|
||||
return this.view.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 {
|
||||
this.view.config.adapter.subscribe(this.model, this.keypath, this.sync);
|
||||
if (this.view.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(this.view.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) {
|
||||
this.view.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(this.view.config.adapter.unsubscribe(model, keypath, this.sync));
|
||||
}
|
||||
return _results;
|
||||
}
|
||||
};
|
||||
|
||||
Binding.prototype.update = function(models) {
|
||||
var _ref;
|
||||
|
||||
if (models == null) {
|
||||
models = {};
|
||||
}
|
||||
if (this.key) {
|
||||
if (models[this.key]) {
|
||||
if (!this.options.bypass) {
|
||||
this.view.config.adapter.unsubscribe(this.model, this.keypath, this.sync);
|
||||
}
|
||||
this.model = models[this.key];
|
||||
if (this.options.bypass) {
|
||||
this.sync();
|
||||
} else {
|
||||
this.view.config.adapter.subscribe(this.model, this.keypath, this.sync);
|
||||
if (this.view.config.preloadData) {
|
||||
this.sync();
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
this.sync();
|
||||
}
|
||||
return (_ref = this.binder.update) != null ? _ref.call(this, models) : void 0;
|
||||
};
|
||||
|
||||
return Binding;
|
||||
|
||||
})();
|
||||
|
||||
Rivets.View = (function() {
|
||||
function View(els, models, options) {
|
||||
var k, option, v, _base, _i, _len, _ref, _ref1, _ref2, _ref3;
|
||||
|
||||
this.els = els;
|
||||
this.models = models;
|
||||
this.options = options != null ? options : {};
|
||||
this.update = __bind(this.update, this);
|
||||
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];
|
||||
}
|
||||
_ref = ['config', 'binders', 'formatters'];
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
option = _ref[_i];
|
||||
this[option] = {};
|
||||
if (this.options[option]) {
|
||||
_ref1 = this.options[option];
|
||||
for (k in _ref1) {
|
||||
v = _ref1[k];
|
||||
this[option][k] = v;
|
||||
}
|
||||
}
|
||||
_ref2 = Rivets[option];
|
||||
for (k in _ref2) {
|
||||
v = _ref2[k];
|
||||
if ((_ref3 = (_base = this[option])[k]) == null) {
|
||||
_base[k] = v;
|
||||
}
|
||||
}
|
||||
}
|
||||
this.build();
|
||||
}
|
||||
|
||||
View.prototype.bindingRegExp = function() {
|
||||
var prefix;
|
||||
|
||||
prefix = this.config.prefix;
|
||||
if (prefix) {
|
||||
return new RegExp("^data-" + prefix + "-");
|
||||
} else {
|
||||
return /^data-/;
|
||||
}
|
||||
};
|
||||
|
||||
View.prototype.build = function() {
|
||||
var bindingRegExp, buildBinding, el, parse, skipNodes, _i, _len, _ref,
|
||||
_this = this;
|
||||
|
||||
this.bindings = [];
|
||||
skipNodes = [];
|
||||
bindingRegExp = this.bindingRegExp();
|
||||
buildBinding = function(node, type, declaration) {
|
||||
var context, ctx, dependencies, key, keypath, options, path, pipe, pipes, splitPath;
|
||||
|
||||
options = {};
|
||||
pipes = (function() {
|
||||
var _i, _len, _ref, _results;
|
||||
|
||||
_ref = declaration.split('|');
|
||||
_results = [];
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
pipe = _ref[_i];
|
||||
_results.push(pipe.trim());
|
||||
}
|
||||
return _results;
|
||||
})();
|
||||
context = (function() {
|
||||
var _i, _len, _ref, _results;
|
||||
|
||||
_ref = pipes.shift().split('<');
|
||||
_results = [];
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
ctx = _ref[_i];
|
||||
_results.push(ctx.trim());
|
||||
}
|
||||
return _results;
|
||||
})();
|
||||
path = context.shift();
|
||||
splitPath = path.split(/\.|:/);
|
||||
options.formatters = pipes;
|
||||
options.bypass = path.indexOf(':') !== -1;
|
||||
if (splitPath[0]) {
|
||||
key = splitPath.shift();
|
||||
} else {
|
||||
key = null;
|
||||
splitPath.shift();
|
||||
}
|
||||
keypath = splitPath.join('.');
|
||||
if (dependencies = context.shift()) {
|
||||
options.dependencies = dependencies.split(/\s+/);
|
||||
}
|
||||
return _this.bindings.push(new Rivets.Binding(_this, node, type, key, keypath, options));
|
||||
};
|
||||
parse = function(node) {
|
||||
var attribute, attributes, binder, childNode, delimiters, identifier, n, parser, regexp, restTokens, startToken, text, token, tokens, type, value, _i, _j, _k, _l, _len, _len1, _len2, _len3, _len4, _m, _ref, _ref1, _ref2, _ref3, _ref4, _results;
|
||||
|
||||
if (__indexOf.call(skipNodes, node) < 0) {
|
||||
if (node.nodeType === Node.TEXT_NODE) {
|
||||
parser = Rivets.TextTemplateParser;
|
||||
if (delimiters = _this.config.templateDelimiters) {
|
||||
if ((tokens = parser.parse(node.data, delimiters)).length) {
|
||||
if (!(tokens.length === 1 && tokens[0].type === parser.types.text)) {
|
||||
startToken = tokens[0], restTokens = 2 <= tokens.length ? __slice.call(tokens, 1) : [];
|
||||
node.data = startToken.value;
|
||||
switch (startToken.type) {
|
||||
case 0:
|
||||
node.data = startToken.value;
|
||||
break;
|
||||
case 1:
|
||||
buildBinding(node, 'textNode', startToken.value);
|
||||
}
|
||||
for (_i = 0, _len = restTokens.length; _i < _len; _i++) {
|
||||
token = restTokens[_i];
|
||||
node.parentNode.appendChild((text = document.createTextNode(token.value)));
|
||||
if (token.type === 1) {
|
||||
buildBinding(text, 'textNode', token.value);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else if (node.attributes != null) {
|
||||
_ref = node.attributes;
|
||||
for (_j = 0, _len1 = _ref.length; _j < _len1; _j++) {
|
||||
attribute = _ref[_j];
|
||||
if (bindingRegExp.test(attribute.name)) {
|
||||
type = attribute.name.replace(bindingRegExp, '');
|
||||
if (!(binder = _this.binders[type])) {
|
||||
_ref1 = _this.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 = _this.binders['*']);
|
||||
if (binder.block) {
|
||||
_ref2 = node.childNodes;
|
||||
for (_k = 0, _len2 = _ref2.length; _k < _len2; _k++) {
|
||||
n = _ref2[_k];
|
||||
skipNodes.push(n);
|
||||
}
|
||||
attributes = [attribute];
|
||||
}
|
||||
}
|
||||
}
|
||||
_ref3 = attributes || node.attributes;
|
||||
for (_l = 0, _len3 = _ref3.length; _l < _len3; _l++) {
|
||||
attribute = _ref3[_l];
|
||||
if (bindingRegExp.test(attribute.name)) {
|
||||
type = attribute.name.replace(bindingRegExp, '');
|
||||
buildBinding(node, type, attribute.value);
|
||||
}
|
||||
}
|
||||
}
|
||||
_ref4 = node.childNodes;
|
||||
_results = [];
|
||||
for (_m = 0, _len4 = _ref4.length; _m < _len4; _m++) {
|
||||
childNode = _ref4[_m];
|
||||
_results.push(parse(childNode));
|
||||
}
|
||||
return _results;
|
||||
}
|
||||
};
|
||||
_ref = this.els;
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
el = _ref[_i];
|
||||
parse(el);
|
||||
}
|
||||
};
|
||||
|
||||
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;
|
||||
};
|
||||
|
||||
View.prototype.update = function(models) {
|
||||
var binding, key, model, _i, _len, _ref, _results;
|
||||
|
||||
if (models == null) {
|
||||
models = {};
|
||||
}
|
||||
for (key in models) {
|
||||
model = models[key];
|
||||
this.models[key] = model;
|
||||
}
|
||||
_ref = this.bindings;
|
||||
_results = [];
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
binding = _ref[_i];
|
||||
_results.push(binding.update(models));
|
||||
}
|
||||
return _results;
|
||||
};
|
||||
|
||||
return View;
|
||||
|
||||
})();
|
||||
|
||||
Rivets.TextTemplateParser = (function() {
|
||||
function TextTemplateParser() {}
|
||||
|
||||
TextTemplateParser.types = {
|
||||
text: 0,
|
||||
binding: 1
|
||||
};
|
||||
|
||||
TextTemplateParser.parse = function(template, delimiters) {
|
||||
var index, lastIndex, lastToken, length, substring, tokens, value;
|
||||
|
||||
tokens = [];
|
||||
length = template.length;
|
||||
index = 0;
|
||||
lastIndex = 0;
|
||||
while (lastIndex < length) {
|
||||
index = template.indexOf(delimiters[0], lastIndex);
|
||||
if (index < 0) {
|
||||
tokens.push({
|
||||
type: this.types.text,
|
||||
value: template.slice(lastIndex)
|
||||
});
|
||||
break;
|
||||
} else {
|
||||
if (index > 0 && lastIndex < index) {
|
||||
tokens.push({
|
||||
type: this.types.text,
|
||||
value: template.slice(lastIndex, index)
|
||||
});
|
||||
}
|
||||
lastIndex = index + 2;
|
||||
index = template.indexOf(delimiters[1], lastIndex);
|
||||
if (index < 0) {
|
||||
substring = template.slice(lastIndex - 2);
|
||||
lastToken = tokens[tokens.length - 1];
|
||||
if ((lastToken != null ? lastToken.type : void 0) === this.types.text) {
|
||||
lastToken.value += substring;
|
||||
} else {
|
||||
tokens.push({
|
||||
type: this.types.text,
|
||||
value: substring
|
||||
});
|
||||
}
|
||||
break;
|
||||
}
|
||||
value = template.slice(lastIndex, index).trim();
|
||||
tokens.push({
|
||||
type: this.types.binding,
|
||||
value: value
|
||||
});
|
||||
lastIndex = index + 2;
|
||||
}
|
||||
}
|
||||
return tokens;
|
||||
};
|
||||
|
||||
return TextTemplateParser;
|
||||
|
||||
})();
|
||||
|
||||
Rivets.Util = {
|
||||
bindEvent: function(el, event, handler) {
|
||||
if (window.jQuery != null) {
|
||||
el = jQuery(el);
|
||||
if (el.on != null) {
|
||||
return el.on(event, handler);
|
||||
} else {
|
||||
return el.bind(event, handler);
|
||||
}
|
||||
} else if (window.addEventListener != null) {
|
||||
return el.addEventListener(event, handler, false);
|
||||
} else {
|
||||
event = 'on' + event;
|
||||
return el.attachEvent(event, handler);
|
||||
}
|
||||
},
|
||||
unbindEvent: function(el, event, handler) {
|
||||
if (window.jQuery != null) {
|
||||
el = jQuery(el);
|
||||
if (el.off != null) {
|
||||
return el.off(event, handler);
|
||||
} else {
|
||||
return el.unbind(event, handler);
|
||||
}
|
||||
} else if (window.removeEventListener != null) {
|
||||
return el.removeEventListener(event, handler, false);
|
||||
} else {
|
||||
event = 'on' + event;
|
||||
return el.detachEvent(event, handler);
|
||||
}
|
||||
},
|
||||
getInputValue: function(el) {
|
||||
var o, _i, _len, _results;
|
||||
|
||||
if (window.jQuery != null) {
|
||||
el = jQuery(el);
|
||||
switch (el[0].type) {
|
||||
case 'checkbox':
|
||||
return el.is(':checked');
|
||||
default:
|
||||
return el.val();
|
||||
}
|
||||
} else {
|
||||
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 Rivets.Util.bindEvent(el, 'change', this.publish);
|
||||
},
|
||||
unbind: function(el) {
|
||||
return Rivets.Util.unbindEvent(el, 'change', this.publish);
|
||||
},
|
||||
routine: function(el, value) {
|
||||
var _ref;
|
||||
|
||||
if (el.type === 'radio') {
|
||||
return el.checked = ((_ref = el.value) != null ? _ref.toString() : void 0) === (value != null ? value.toString() : void 0);
|
||||
} else {
|
||||
return el.checked = !!value;
|
||||
}
|
||||
}
|
||||
},
|
||||
unchecked: {
|
||||
publishes: true,
|
||||
bind: function(el) {
|
||||
return Rivets.Util.bindEvent(el, 'change', this.publish);
|
||||
},
|
||||
unbind: function(el) {
|
||||
return Rivets.Util.unbindEvent(el, 'change', this.publish);
|
||||
},
|
||||
routine: function(el, value) {
|
||||
var _ref;
|
||||
|
||||
if (el.type === 'radio') {
|
||||
return el.checked = ((_ref = el.value) != null ? _ref.toString() : void 0) !== (value != null ? value.toString() : void 0);
|
||||
} 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 Rivets.Util.bindEvent(el, 'change', this.publish);
|
||||
},
|
||||
unbind: function(el) {
|
||||
return Rivets.Util.unbindEvent(el, 'change', this.publish);
|
||||
},
|
||||
routine: function(el, value) {
|
||||
var o, _i, _len, _ref, _ref1, _ref2, _results;
|
||||
|
||||
if (window.jQuery != null) {
|
||||
el = jQuery(el);
|
||||
if ((value != null ? value.toString() : void 0) !== ((_ref = el.val()) != null ? _ref.toString() : void 0)) {
|
||||
return el.val(value != null ? value : '');
|
||||
}
|
||||
} else {
|
||||
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 = (_ref1 = o.value, __indexOf.call(value, _ref1) >= 0));
|
||||
}
|
||||
return _results;
|
||||
}
|
||||
} else if ((value != null ? value.toString() : void 0) !== ((_ref2 = el.value) != null ? _ref2.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 : '';
|
||||
}
|
||||
},
|
||||
"if": {
|
||||
block: true,
|
||||
bind: function(el) {
|
||||
var attr, declaration;
|
||||
|
||||
if (this.marker == null) {
|
||||
attr = ['data', this.view.config.prefix, this.type].join('-').replace('--', '-');
|
||||
declaration = el.getAttribute(attr);
|
||||
this.marker = document.createComment(" rivets: " + this.type + " " + declaration + " ");
|
||||
el.removeAttribute(attr);
|
||||
el.parentNode.insertBefore(this.marker, el);
|
||||
return el.parentNode.removeChild(el);
|
||||
}
|
||||
},
|
||||
unbind: function() {
|
||||
var _ref;
|
||||
|
||||
return (_ref = this.nested) != null ? _ref.unbind() : void 0;
|
||||
},
|
||||
routine: function(el, value) {
|
||||
var key, model, models, options, _ref;
|
||||
|
||||
if (!!value === (this.nested == null)) {
|
||||
if (value) {
|
||||
models = {};
|
||||
_ref = this.view.models;
|
||||
for (key in _ref) {
|
||||
model = _ref[key];
|
||||
models[key] = model;
|
||||
}
|
||||
options = {
|
||||
binders: this.view.options.binders,
|
||||
formatters: this.view.options.formatters,
|
||||
config: this.view.options.config
|
||||
};
|
||||
(this.nested = new Rivets.View(el, models, options)).bind();
|
||||
return this.marker.parentNode.insertBefore(el, this.marker.nextSibling);
|
||||
} else {
|
||||
el.parentNode.removeChild(el);
|
||||
this.nested.unbind();
|
||||
return delete this.nested;
|
||||
}
|
||||
}
|
||||
},
|
||||
update: function(models) {
|
||||
return this.nested.update(models);
|
||||
}
|
||||
},
|
||||
unless: {
|
||||
block: true,
|
||||
bind: function(el) {
|
||||
return Rivets.binders["if"].bind.call(this, el);
|
||||
},
|
||||
unbind: function() {
|
||||
return Rivets.binders["if"].unbind.call(this);
|
||||
},
|
||||
routine: function(el, value) {
|
||||
return Rivets.binders["if"].routine.call(this, el, !value);
|
||||
},
|
||||
update: function(models) {
|
||||
return Rivets.binders["if"].update.call(this, models);
|
||||
}
|
||||
},
|
||||
"on-*": {
|
||||
"function": true,
|
||||
unbind: function(el) {
|
||||
if (this.handler) {
|
||||
return Rivets.Util.unbindEvent(el, this.args[0], this.handler);
|
||||
}
|
||||
},
|
||||
routine: function(el, value) {
|
||||
if (this.handler) {
|
||||
Rivets.Util.unbindEvent(el, this.args[0], this.handler);
|
||||
}
|
||||
return Rivets.Util.bindEvent(el, this.args[0], this.handler = this.eventHandler(value));
|
||||
}
|
||||
},
|
||||
"each-*": {
|
||||
block: true,
|
||||
bind: function(el) {
|
||||
var attr;
|
||||
|
||||
if (this.marker == null) {
|
||||
attr = ['data', this.view.config.prefix, this.type].join('-').replace('--', '-');
|
||||
this.marker = document.createComment(" rivets: " + this.type + " ");
|
||||
this.iterated = [];
|
||||
el.removeAttribute(attr);
|
||||
el.parentNode.insertBefore(this.marker, el);
|
||||
return el.parentNode.removeChild(el);
|
||||
}
|
||||
},
|
||||
unbind: function(el) {
|
||||
var view, _i, _len, _ref, _results;
|
||||
|
||||
if (this.iterated != null) {
|
||||
_ref = this.iterated;
|
||||
_results = [];
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
view = _ref[_i];
|
||||
_results.push(view.unbind());
|
||||
}
|
||||
return _results;
|
||||
}
|
||||
},
|
||||
routine: function(el, collection) {
|
||||
var data, i, index, k, key, model, modelName, options, previous, template, v, view, _i, _j, _len, _len1, _ref, _ref1, _ref2, _ref3, _results;
|
||||
|
||||
modelName = this.args[0];
|
||||
collection = collection || [];
|
||||
if (this.iterated.length > collection.length) {
|
||||
_ref = Array(this.iterated.length - collection.length);
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
i = _ref[_i];
|
||||
view = this.iterated.pop();
|
||||
view.unbind();
|
||||
this.marker.parentNode.removeChild(view.els[0]);
|
||||
}
|
||||
}
|
||||
_results = [];
|
||||
for (index = _j = 0, _len1 = collection.length; _j < _len1; index = ++_j) {
|
||||
model = collection[index];
|
||||
data = {};
|
||||
data[modelName] = model;
|
||||
if (this.iterated[index] == null) {
|
||||
_ref1 = this.view.models;
|
||||
for (key in _ref1) {
|
||||
model = _ref1[key];
|
||||
if ((_ref2 = data[key]) == null) {
|
||||
data[key] = model;
|
||||
}
|
||||
}
|
||||
previous = this.iterated.length ? this.iterated[this.iterated.length - 1].els[0] : this.marker;
|
||||
options = {
|
||||
binders: this.view.options.binders,
|
||||
formatters: this.view.options.formatters,
|
||||
config: {}
|
||||
};
|
||||
_ref3 = this.view.options.config;
|
||||
for (k in _ref3) {
|
||||
v = _ref3[k];
|
||||
options.config[k] = v;
|
||||
}
|
||||
options.config.preloadData = true;
|
||||
template = el.cloneNode(true);
|
||||
view = new Rivets.View(template, data, options);
|
||||
view.bind();
|
||||
this.iterated.push(view);
|
||||
_results.push(this.marker.parentNode.insertBefore(template, previous.nextSibling));
|
||||
} else if (this.iterated[index].models[modelName] !== model) {
|
||||
_results.push(this.iterated[index].update(data));
|
||||
} else {
|
||||
_results.push(void 0);
|
||||
}
|
||||
}
|
||||
return _results;
|
||||
},
|
||||
update: function(models) {
|
||||
var data, key, model, view, _i, _len, _ref, _results;
|
||||
|
||||
data = {};
|
||||
for (key in models) {
|
||||
model = models[key];
|
||||
if (key !== this.args[0]) {
|
||||
data[key] = model;
|
||||
}
|
||||
}
|
||||
_ref = this.iterated;
|
||||
_results = [];
|
||||
for (_i = 0, _len = _ref.length; _i < _len; _i++) {
|
||||
view = _ref[_i];
|
||||
_results.push(view.update(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.internalBinders = {
|
||||
textNode: function(node, value) {
|
||||
return node.data = value != null ? value : '';
|
||||
}
|
||||
};
|
||||
|
||||
Rivets.config = {
|
||||
preloadData: true,
|
||||
handler: function(context, ev, binding) {
|
||||
return this.call(context, ev, binding.view.models);
|
||||
}
|
||||
};
|
||||
|
||||
Rivets.formatters = {};
|
||||
|
||||
Rivets.factory = function(exports) {
|
||||
exports._ = Rivets;
|
||||
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, options) {
|
||||
var view;
|
||||
|
||||
if (models == null) {
|
||||
models = {};
|
||||
}
|
||||
if (options == null) {
|
||||
options = {};
|
||||
}
|
||||
view = new Rivets.View(el, models, options);
|
||||
view.bind();
|
||||
return view;
|
||||
};
|
||||
};
|
||||
|
||||
if (typeof exports === 'object') {
|
||||
Rivets.factory(exports);
|
||||
} else if (typeof define === 'function' && define.amd) {
|
||||
define(['exports'], function(exports) {
|
||||
Rivets.factory(this.rivets = exports);
|
||||
return exports;
|
||||
});
|
||||
} else {
|
||||
Rivets.factory(this.rivets = {});
|
||||
}
|
||||
|
||||
}).call(this);
|
||||
Vendored
+5
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
Vendored
+5
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
Vendored
+5
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
Vendored
+5
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
Vendored
+5
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
Vendored
+5
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
Vendored
+5
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
Vendored
+5
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
Vendored
+5
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
Vendored
+5
File diff suppressed because one or more lines are too long
File diff suppressed because it is too large
Load Diff
Vendored
+5
File diff suppressed because one or more lines are too long
Reference in New Issue
Block a user