diff --git a/ajax/libs/rivets/0.5.0/rivets.js b/ajax/libs/rivets/0.5.0/rivets.js new file mode 100644 index 000000000..2b06bda50 --- /dev/null +++ b/ajax/libs/rivets/0.5.0/rivets.js @@ -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); diff --git a/ajax/libs/rivets/0.5.0/rivets.min.js b/ajax/libs/rivets/0.5.0/rivets.min.js new file mode 100644 index 000000000..29a43aab2 --- /dev/null +++ b/ajax/libs/rivets/0.5.0/rivets.min.js @@ -0,0 +1,5 @@ +// rivets.js +// version: 0.5.0 +// author: Michael Richards +// license: MIT +(function(){var t,i,e,n,s,r=function(t,i){return function(){return t.apply(i,arguments)}},h=[].slice,o=[].indexOf||function(t){for(var i=0,e=this.length;e>i;i++)if(i in this&&this[i]===t)return i;return-1};t={},String.prototype.trim||(String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g,"")}),t.Binding=function(){function t(t,i,e,n,s,h){var o,u,l,a;if(this.view=t,this.el=i,this.type=e,this.key=n,this.keypath=s,this.options=null!=h?h:{},this.update=r(this.update,this),this.unbind=r(this.unbind,this),this.bind=r(this.bind,this),this.publish=r(this.publish,this),this.sync=r(this.sync,this),this.set=r(this.set,this),this.formattedValue=r(this.formattedValue,this),!(this.binder=this.view.binders[e])){a=this.view.binders;for(o in a)l=a[o],"*"!==o&&-1!==o.indexOf("*")&&(u=RegExp("^"+o.replace("*",".+")+"$"),u.test(e)&&(this.binder=l,this.args=RegExp("^"+o.replace("*","(.+)")+"$").exec(e),this.args.shift()))}this.binder||(this.binder=this.view.binders["*"]),this.binder instanceof Function&&(this.binder={routine:this.binder}),this.formatters=this.options.formatters||[],this.model=this.view.models[this.key]}return t.prototype.formattedValue=function(t){var i,e,n,s,r,o,u,l,a;for(o=this.formatters,s=0,r=o.length;r>s;s++)e=o[s],i=e.split(/\s+/),n=i.shift(),e=this.model[n]instanceof Function?this.model[n]:(null!=(u=this.options)?null!=(l=u.bindingOptions)?null!=(a=l.formatters)?a[n]:void 0:void 0:void 0)instanceof Function?this.options.bindingOptions.formatters[n]:this.view.formatters[n],(null!=e?e.read:void 0)instanceof Function?t=e.read.apply(e,[t].concat(h.call(i))):e instanceof Function&&(t=e.apply(null,[t].concat(h.call(i))));return t},t.prototype.set=function(t){var i;return t=t instanceof Function&&!this.binder["function"]?this.formattedValue(t.call(this.model)):this.formattedValue(t),null!=(i=this.binder.routine)?i.call(this,this.el,t):void 0},t.prototype.sync=function(){return this.set(this.options.bypass?this.model[this.keypath]:this.view.config.adapter.read(this.model,this.keypath))},t.prototype.publish=function(){var t,i,e,s,r,o,u,l,a;for(s=n(this.el),u=this.formatters.slice(0).reverse(),r=0,o=u.length;o>r;r++)i=u[r],t=i.split(/\s+/),e=t.shift(),(null!=(l=this.view.formatters[e])?l.publish:void 0)&&(s=(a=this.view.formatters[e]).publish.apply(a,[s].concat(h.call(t))));return this.view.config.adapter.publish(this.model,this.keypath,s)},t.prototype.bind=function(){var t,i,e,n,s,r,h,o,u;if(null!=(r=this.binder.bind)&&r.call(this,this.el),this.options.bypass?this.sync():(this.view.config.adapter.subscribe(this.model,this.keypath,this.sync),this.view.config.preloadData&&this.sync()),null!=(h=this.options.dependencies)?h.length:void 0){for(o=this.options.dependencies,u=[],n=0,s=o.length;s>n;n++)t=o[n],/^\./.test(t)?(e=this.model,i=t.substr(1)):(t=t.split("."),e=this.view.models[t.shift()],i=t.join(".")),u.push(this.view.config.adapter.subscribe(e,i,this.sync));return u}},t.prototype.unbind=function(){var t,i,e,n,s,r,h,o,u;if(null!=(r=this.binder.unbind)&&r.call(this,this.el),this.options.bypass||this.view.config.adapter.unsubscribe(this.model,this.keypath,this.sync),null!=(h=this.options.dependencies)?h.length:void 0){for(o=this.options.dependencies,u=[],n=0,s=o.length;s>n;n++)t=o[n],/^\./.test(t)?(e=this.model,i=t.substr(1)):(t=t.split("."),e=this.view.models[t.shift()],i=t.join(".")),u.push(this.view.config.adapter.unsubscribe(e,i,this.sync));return u}},t.prototype.update=function(){return this.unbind(),this.model=this.view.models[this.key],this.bind()},t}(),t.View=function(){function i(i,e,n){var s,h,o,u,l,a,d,c,f,p;for(this.els=i,this.models=e,this.options=null!=n?n:{},this.update=r(this.update,this),this.publish=r(this.publish,this),this.sync=r(this.sync,this),this.unbind=r(this.unbind,this),this.bind=r(this.bind,this),this.select=r(this.select,this),this.build=r(this.build,this),this.bindingRegExp=r(this.bindingRegExp,this),this.els.jquery||this.els instanceof Array||(this.els=[this.els]),d=["config","binders","formatters"],l=0,a=d.length;a>l;l++){if(h=d[l],this[h]={},this.options[h]){c=this.options[h];for(s in c)o=c[s],this[h][s]=o}f=t[h];for(s in f)o=f[s],null==(p=(u=this[h])[s])&&(u[s]=o)}this.build()}return i.prototype.bindingRegExp=function(){var t;return t=this.config.prefix,t?RegExp("^data-"+t+"-"):/^data-/},i.prototype.build=function(){var i,e,n,s,r,h,u,l,a,d,c,f=this;for(this.bindings=[],r=[],i=this.bindingRegExp(),s=function(e){var n,s,h,u,l,a,d,c,p,b,v,g,y,m,w,x,k,E,j,L,N,Q,R,V,O,B,F,T;if(0>o.call(r,e)){for(O=e.attributes,j=0,Q=O.length;Q>j;j++)if(n=O[j],i.test(n.name)){if(k=n.name.replace(i,""),!(h=f.binders[k])){B=f.binders;for(d in B)E=B[d],"*"!==d&&-1!==d.indexOf("*")&&(w=RegExp("^"+d.replace("*",".+")+"$"),w.test(k)&&(h=E))}if(h||(h=f.binders["*"]),h.block){for(F=e.getElementsByTagName("*"),L=0,R=F.length;R>L;L++)b=F[L],r.push(b);s=[n]}}for(T=s||e.attributes,N=0,V=T.length;V>N;N++)n=T[N],i.test(n.name)&&(v={},k=n.name.replace(i,""),m=function(){var t,i,e,s;for(e=n.value.split("|"),s=[],t=0,i=e.length;i>t;t++)y=e[t],s.push(y.trim());return s}(),u=function(){var t,i,e,n;for(e=m.shift().split("<"),n=[],t=0,i=e.length;i>t;t++)l=e[t],n.push(l.trim());return n}(),g=u.shift(),x=g.split(/\.|:/),v.formatters=m,v.bypass=-1!==g.indexOf(":"),x[0]?c=x.shift():(c=null,x.shift()),p=x.join("."),null!=f.models[c]&&((a=u.shift())&&(v.dependencies=a.split(/\s+/)),f.bindings.push(new t.Binding(f,e,k,c,p,v))));s&&(s=null)}},d=this.els,h=0,l=d.length;l>h;h++)for(e=d[h],s(e),c=e.getElementsByTagName("*"),u=0,a=c.length;a>u;u++)n=c[u],null!=n.attributes&&s(n)},i.prototype.select=function(t){var i,e,n,s,r;for(s=this.bindings,r=[],e=0,n=s.length;n>e;e++)i=s[e],t(i)&&r.push(i);return r},i.prototype.bind=function(){var t,i,e,n,s;for(n=this.bindings,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.bind());return s},i.prototype.unbind=function(){var t,i,e,n,s;for(n=this.bindings,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.unbind());return s},i.prototype.sync=function(){var t,i,e,n,s;for(n=this.bindings,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.sync());return s},i.prototype.publish=function(){var t,i,e,n,s;for(n=this.select(function(t){return t.binder.publishes}),s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.publish());return s},i.prototype.update=function(t){var i,e,n,s;null==t&&(t={}),s=[];for(e in t)n=t[e],this.models[e]=n,s.push(function(){var t,n,s,r;for(s=this.select(function(t){return t.key===e}),r=[],t=0,n=s.length;n>t;t++)i=s[t],r.push(i.update());return r}.call(this));return s},i}(),i=function(t,i,e,n){var s;return s=function(t){return e.call(n,t)},null!=window.jQuery?(t=jQuery(t),null!=t.on?t.on(i,s):t.bind(i,s)):null!=window.addEventListener?t.addEventListener(i,s,!1):(i="on"+i,t.attachEvent(i,s)),s},s=function(t,i,e){return null!=window.jQuery?(t=jQuery(t),null!=t.off?t.off(i,e):t.unbind(i,e)):window.removeEventListener?t.removeEventListener(i,e,!1):(i="on"+i,t.detachEvent(i,e))},n=function(t){var i,e,n,s;if(null!=window.jQuery)switch(t=jQuery(t),t[0].type){case"checkbox":return t.is(":checked");default:return t.val()}else switch(t.type){case"checkbox":return t.checked;case"select-multiple":for(s=[],e=0,n=t.length;n>e;e++)i=t[e],i.selected&&s.push(i.value);return s;default:return t.value}},t.binders={enabled:function(t,i){return t.disabled=!i},disabled:function(t,i){return t.disabled=!!i},checked:{publishes:!0,bind:function(t){return this.currentListener=i(t,"change",this.publish)},unbind:function(t){return s(t,"change",this.currentListener)},routine:function(t,i){var e;return t.checked="radio"===t.type?(null!=(e=t.value)?""+e:void 0)===(null!=i?""+i:void 0):!!i}},unchecked:{publishes:!0,bind:function(t){return this.currentListener=i(t,"change",this.publish)},unbind:function(t){return s(t,"change",this.currentListener)},routine:function(t,i){var e;return t.checked="radio"===t.type?(null!=(e=t.value)?""+e:void 0)!==(null!=i?""+i:void 0):!i}},show:function(t,i){return t.style.display=i?"":"none"},hide:function(t,i){return t.style.display=i?"none":""},html:function(t,i){return t.innerHTML=null!=i?i:""},value:{publishes:!0,bind:function(t){return this.currentListener=i(t,"change",this.publish)},unbind:function(t){return s(t,"change",this.currentListener)},routine:function(t,i){var e,n,s,r,h,u,l;if(null!=window.jQuery){if(t=jQuery(t),(null!=i?""+i:void 0)!==(null!=(r=t.val())?""+r:void 0))return t.val(null!=i?i:"")}else if("select-multiple"===t.type){if(null!=i){for(l=[],n=0,s=t.length;s>n;n++)e=t[n],l.push(e.selected=(h=e.value,o.call(i,h)>=0));return l}}else if((null!=i?""+i:void 0)!==(null!=(u=t.value)?""+u:void 0))return t.value=null!=i?i:""}},text:function(t,i){return null!=t.innerText?t.innerText=null!=i?i:"":t.textContent=null!=i?i:""},"on-*":{"function":!0,routine:function(t,e){return this.currentListener&&s(t,this.args[0],this.currentListener),this.currentListener=i(t,this.args[0],e,this.model)}},"each-*":{block:!0,bind:function(t){return t.removeAttribute(["data",this.view.config.prefix,this.type].join("-").replace("--","-"))},unbind:function(){var t,i,e,n,s;if(null!=this.iterated){for(n=this.iterated,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.unbind());return s}},routine:function(i,e){var n,s,r,h,o,u,l,a,d,c,f,p,b,v,g,y,m,w,x;if(null!=this.iterated)for(g=this.iterated,d=0,p=g.length;p>d;d++)for(a=g[d],a.unbind(),y=a.els,c=0,b=y.length;b>c;c++)s=y[c],s.parentNode.removeChild(s);else this.marker=document.createComment(" rivets: "+this.type+" "),i.parentNode.insertBefore(this.marker,i),i.parentNode.removeChild(i);if(this.iterated=[],e){for(x=[],f=0,v=e.length;v>f;f++){r=e[f],n={},m=this.view.models;for(u in m)o=m[u],n[u]=o;n[this.args[0]]=r,h=i.cloneNode(!0),l=this.iterated.length?this.iterated[this.iterated.length-1].els[0]:this.marker,this.marker.parentNode.insertBefore(h,null!=(w=l.nextSibling)?w:null),a=new t.View(h,n,this.view.options),a.bind(),x.push(this.iterated.push(a))}return x}}},"class-*":function(t,i){var e;return e=" "+t.className+" ",!i==(-1!==e.indexOf(" "+this.args[0]+" "))?t.className=i?""+t.className+" "+this.args[0]:e.replace(" "+this.args[0]+" "," ").trim():void 0},"*":function(t,i){return i?t.setAttribute(this.type,i):t.removeAttribute(this.type)}},t.config={preloadData:!0},t.formatters={},e=function(i){return i.binders=t.binders,i.formatters=t.formatters,i.config=t.config,i.configure=function(i){var e,n;null==i&&(i={});for(e in i)n=i[e],t.config[e]=n},i.bind=function(i,e,n){var s;return null==e&&(e={}),null==n&&(n={}),s=new t.View(i,e,n),s.bind(),s}},"object"==typeof exports?e(exports):"function"==typeof define&&define.amd?define(["exports"],function(t){return e(this.rivets=t),t}):e(this.rivets={})}).call(this); \ No newline at end of file diff --git a/ajax/libs/rivets/0.5.1/rivets.js b/ajax/libs/rivets/0.5.1/rivets.js new file mode 100644 index 000000000..5d63a5cae --- /dev/null +++ b/ajax/libs/rivets/0.5.1/rivets.js @@ -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); diff --git a/ajax/libs/rivets/0.5.1/rivets.min.js b/ajax/libs/rivets/0.5.1/rivets.min.js new file mode 100644 index 000000000..07c322a1b --- /dev/null +++ b/ajax/libs/rivets/0.5.1/rivets.min.js @@ -0,0 +1,5 @@ +// Rivets.js +// version: 0.5.1 +// author: Michael Richards +// license: MIT +(function(){var t,i=function(t,i){return function(){return t.apply(i,arguments)}},e=[].slice,n=[].indexOf||function(t){for(var i=0,e=this.length;e>i;i++)if(i in this&&this[i]===t)return i;return-1};t={},String.prototype.trim||(String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g,"")}),t.Binding=function(){function n(t,e,n,s,r,h){var u,o,l,a;if(this.view=t,this.el=e,this.type=n,this.key=s,this.keypath=r,this.options=null!=h?h:{},this.update=i(this.update,this),this.unbind=i(this.unbind,this),this.bind=i(this.bind,this),this.publish=i(this.publish,this),this.sync=i(this.sync,this),this.set=i(this.set,this),this.formattedValue=i(this.formattedValue,this),!(this.binder=this.view.binders[n])){a=this.view.binders;for(u in a)l=a[u],"*"!==u&&-1!==u.indexOf("*")&&(o=RegExp("^"+u.replace("*",".+")+"$"),o.test(n)&&(this.binder=l,this.args=RegExp("^"+u.replace("*","(.+)")+"$").exec(n),this.args.shift()))}this.binder||(this.binder=this.view.binders["*"]),this.binder instanceof Function&&(this.binder={routine:this.binder}),this.formatters=this.options.formatters||[],this.model=this.view.models[this.key]}return n.prototype.formattedValue=function(t){var i,n,s,r,h,u;for(u=this.formatters,r=0,h=u.length;h>r;r++)n=u[r],i=n.split(/\s+/),s=i.shift(),n=this.model[s]instanceof Function?this.model[s]:this.view.formatters[s],(null!=n?n.read:void 0)instanceof Function?t=n.read.apply(n,[t].concat(e.call(i))):n instanceof Function&&(t=n.apply(null,[t].concat(e.call(i))));return t},n.prototype.set=function(t){var i;return t=t instanceof Function&&!this.binder["function"]?this.formattedValue(t.call(this.model)):this.formattedValue(t),null!=(i=this.binder.routine)?i.call(this,this.el,t):void 0},n.prototype.sync=function(){return this.set(this.options.bypass?this.model[this.keypath]:this.view.config.adapter.read(this.model,this.keypath))},n.prototype.publish=function(){var i,n,s,r,h,u,o,l,a;for(r=t.Util.getInputValue(this.el),o=this.formatters.slice(0).reverse(),h=0,u=o.length;u>h;h++)n=o[h],i=n.split(/\s+/),s=i.shift(),(null!=(l=this.view.formatters[s])?l.publish:void 0)&&(r=(a=this.view.formatters[s]).publish.apply(a,[r].concat(e.call(i))));return this.view.config.adapter.publish(this.model,this.keypath,r)},n.prototype.bind=function(){var t,i,e,n,s,r,h,u,o;if(null!=(r=this.binder.bind)&&r.call(this,this.el),this.options.bypass?this.sync():(this.view.config.adapter.subscribe(this.model,this.keypath,this.sync),this.view.config.preloadData&&this.sync()),null!=(h=this.options.dependencies)?h.length:void 0){for(u=this.options.dependencies,o=[],n=0,s=u.length;s>n;n++)t=u[n],/^\./.test(t)?(e=this.model,i=t.substr(1)):(t=t.split("."),e=this.view.models[t.shift()],i=t.join(".")),o.push(this.view.config.adapter.subscribe(e,i,this.sync));return o}},n.prototype.unbind=function(){var t,i,e,n,s,r,h,u,o;if(null!=(r=this.binder.unbind)&&r.call(this,this.el),this.options.bypass||this.view.config.adapter.unsubscribe(this.model,this.keypath,this.sync),null!=(h=this.options.dependencies)?h.length:void 0){for(u=this.options.dependencies,o=[],n=0,s=u.length;s>n;n++)t=u[n],/^\./.test(t)?(e=this.model,i=t.substr(1)):(t=t.split("."),e=this.view.models[t.shift()],i=t.join(".")),o.push(this.view.config.adapter.unsubscribe(e,i,this.sync));return o}},n.prototype.update=function(){return this.unbind(),this.model=this.view.models[this.key],this.bind()},n}(),t.View=function(){function e(e,n,s){var r,h,u,o,l,a,d,c,f,p;for(this.els=e,this.models=n,this.options=null!=s?s:{},this.update=i(this.update,this),this.publish=i(this.publish,this),this.sync=i(this.sync,this),this.unbind=i(this.unbind,this),this.bind=i(this.bind,this),this.select=i(this.select,this),this.build=i(this.build,this),this.bindingRegExp=i(this.bindingRegExp,this),this.els.jquery||this.els instanceof Array||(this.els=[this.els]),d=["config","binders","formatters"],l=0,a=d.length;a>l;l++){if(h=d[l],this[h]={},this.options[h]){c=this.options[h];for(r in c)u=c[r],this[h][r]=u}f=t[h];for(r in f)u=f[r],null==(p=(o=this[h])[r])&&(o[r]=u)}this.build()}return e.prototype.bindingRegExp=function(){var t;return t=this.config.prefix,t?RegExp("^data-"+t+"-"):/^data-/},e.prototype.build=function(){var i,e,s,r,h,u,o,l,a,d,c,f=this;for(this.bindings=[],h=[],i=this.bindingRegExp(),r=function(e){var s,r,u,o,l,a,d,c,p,b,v,g,y,m,w,x,E,k,j,L,N,U,V,Q,R,B,F,O;if(0>n.call(h,e)){for(R=e.attributes,j=0,U=R.length;U>j;j++)if(s=R[j],i.test(s.name)){if(E=s.name.replace(i,""),!(u=f.binders[E])){B=f.binders;for(d in B)k=B[d],"*"!==d&&-1!==d.indexOf("*")&&(w=RegExp("^"+d.replace("*",".+")+"$"),w.test(E)&&(u=k))}if(u||(u=f.binders["*"]),u.block){for(F=e.getElementsByTagName("*"),L=0,V=F.length;V>L;L++)b=F[L],h.push(b);r=[s]}}for(O=r||e.attributes,N=0,Q=O.length;Q>N;N++)s=O[N],i.test(s.name)&&(v={},E=s.name.replace(i,""),m=function(){var t,i,e,n;for(e=s.value.split("|"),n=[],t=0,i=e.length;i>t;t++)y=e[t],n.push(y.trim());return n}(),o=function(){var t,i,e,n;for(e=m.shift().split("<"),n=[],t=0,i=e.length;i>t;t++)l=e[t],n.push(l.trim());return n}(),g=o.shift(),x=g.split(/\.|:/),v.formatters=m,v.bypass=-1!==g.indexOf(":"),x[0]?c=x.shift():(c=null,x.shift()),p=x.join("."),null!=f.models[c]&&((a=o.shift())&&(v.dependencies=a.split(/\s+/)),f.bindings.push(new t.Binding(f,e,E,c,p,v))));r&&(r=null)}},d=this.els,u=0,l=d.length;l>u;u++)for(e=d[u],r(e),c=e.getElementsByTagName("*"),o=0,a=c.length;a>o;o++)s=c[o],null!=s.attributes&&r(s)},e.prototype.select=function(t){var i,e,n,s,r;for(s=this.bindings,r=[],e=0,n=s.length;n>e;e++)i=s[e],t(i)&&r.push(i);return r},e.prototype.bind=function(){var t,i,e,n,s;for(n=this.bindings,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.bind());return s},e.prototype.unbind=function(){var t,i,e,n,s;for(n=this.bindings,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.unbind());return s},e.prototype.sync=function(){var t,i,e,n,s;for(n=this.bindings,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.sync());return s},e.prototype.publish=function(){var t,i,e,n,s;for(n=this.select(function(t){return t.binder.publishes}),s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.publish());return s},e.prototype.update=function(t){var i,e,n,s;null==t&&(t={}),s=[];for(e in t)n=t[e],this.models[e]=n,s.push(function(){var t,n,s,r;for(s=this.select(function(t){return t.key===e}),r=[],t=0,n=s.length;n>t;t++)i=s[t],r.push(i.update());return r}.call(this));return s},e}(),t.Util={bindEvent:function(t,i,e,n){var s;return s=function(t){return e.call(n,t)},null!=window.jQuery?(t=jQuery(t),null!=t.on?t.on(i,s):t.bind(i,s)):null!=window.addEventListener?t.addEventListener(i,s,!1):(i="on"+i,t.attachEvent(i,s)),s},unbindEvent:function(t,i,e){return null!=window.jQuery?(t=jQuery(t),null!=t.off?t.off(i,e):t.unbind(i,e)):window.removeEventListener?t.removeEventListener(i,e,!1):(i="on"+i,t.detachEvent(i,e))},getInputValue:function(t){var i,e,n,s;if(null!=window.jQuery)switch(t=jQuery(t),t[0].type){case"checkbox":return t.is(":checked");default:return t.val()}else switch(t.type){case"checkbox":return t.checked;case"select-multiple":for(s=[],e=0,n=t.length;n>e;e++)i=t[e],i.selected&&s.push(i.value);return s;default:return t.value}}},t.binders={enabled:function(t,i){return t.disabled=!i},disabled:function(t,i){return t.disabled=!!i},checked:{publishes:!0,bind:function(i){return this.currentListener=t.Util.bindEvent(i,"change",this.publish)},unbind:function(i){return t.Util.unbindEvent(i,"change",this.currentListener)},routine:function(t,i){var e;return t.checked="radio"===t.type?(null!=(e=t.value)?""+e:void 0)===(null!=i?""+i:void 0):!!i}},unchecked:{publishes:!0,bind:function(i){return this.currentListener=t.Util.bindEvent(i,"change",this.publish)},unbind:function(i){return t.Util.unbindEvent(i,"change",this.currentListener)},routine:function(t,i){var e;return t.checked="radio"===t.type?(null!=(e=t.value)?""+e:void 0)!==(null!=i?""+i:void 0):!i}},show:function(t,i){return t.style.display=i?"":"none"},hide:function(t,i){return t.style.display=i?"none":""},html:function(t,i){return t.innerHTML=null!=i?i:""},value:{publishes:!0,bind:function(i){return this.currentListener=t.Util.bindEvent(i,"change",this.publish)},unbind:function(i){return t.Util.unbindEvent(i,"change",this.currentListener)},routine:function(t,i){var e,s,r,h,u,o,l;if(null!=window.jQuery){if(t=jQuery(t),(null!=i?""+i:void 0)!==(null!=(h=t.val())?""+h:void 0))return t.val(null!=i?i:"")}else if("select-multiple"===t.type){if(null!=i){for(l=[],s=0,r=t.length;r>s;s++)e=t[s],l.push(e.selected=(u=e.value,n.call(i,u)>=0));return l}}else if((null!=i?""+i:void 0)!==(null!=(o=t.value)?""+o:void 0))return t.value=null!=i?i:""}},text:function(t,i){return null!=t.innerText?t.innerText=null!=i?i:"":t.textContent=null!=i?i:""},"on-*":{"function":!0,routine:function(i,e){return this.currentListener&&t.Util.unbindEvent(i,this.args[0],this.currentListener),this.currentListener=t.Util.bindEvent(i,this.args[0],e,this.model)}},"each-*":{block:!0,bind:function(t){return t.removeAttribute(["data",this.view.config.prefix,this.type].join("-").replace("--","-"))},unbind:function(){var t,i,e,n,s;if(null!=this.iterated){for(n=this.iterated,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.unbind());return s}},routine:function(i,e){var n,s,r,h,u,o,l,a,d,c,f,p,b,v,g,y,m,w,x;if(null!=this.iterated)for(g=this.iterated,d=0,p=g.length;p>d;d++)for(a=g[d],a.unbind(),y=a.els,c=0,b=y.length;b>c;c++)s=y[c],s.parentNode.removeChild(s);else this.marker=document.createComment(" rivets: "+this.type+" "),i.parentNode.insertBefore(this.marker,i),i.parentNode.removeChild(i);if(this.iterated=[],e){for(x=[],f=0,v=e.length;v>f;f++){r=e[f],n={},m=this.view.models;for(o in m)u=m[o],n[o]=u;n[this.args[0]]=r,h=i.cloneNode(!0),l=this.iterated.length?this.iterated[this.iterated.length-1].els[0]:this.marker,this.marker.parentNode.insertBefore(h,null!=(w=l.nextSibling)?w:null),a=new t.View(h,n,this.view.options),a.bind(),x.push(this.iterated.push(a))}return x}}},"class-*":function(t,i){var e;return e=" "+t.className+" ",!i==(-1!==e.indexOf(" "+this.args[0]+" "))?t.className=i?""+t.className+" "+this.args[0]:e.replace(" "+this.args[0]+" "," ").trim():void 0},"*":function(t,i){return i?t.setAttribute(this.type,i):t.removeAttribute(this.type)}},t.config={preloadData:!0},t.formatters={},t.factory=function(i){return i.binders=t.binders,i.formatters=t.formatters,i.config=t.config,i.configure=function(i){var e,n;null==i&&(i={});for(e in i)n=i[e],t.config[e]=n},i.bind=function(i,e,n){var s;return null==e&&(e={}),null==n&&(n={}),s=new t.View(i,e,n),s.bind(),s}},"object"==typeof exports?t.factory(exports):"function"==typeof define&&define.amd?define(["exports"],function(i){return t.factory(this.rivets=i),i}):t.factory(this.rivets={})}).call(this); \ No newline at end of file diff --git a/ajax/libs/rivets/0.5.10/rivets.js b/ajax/libs/rivets/0.5.10/rivets.js new file mode 100644 index 000000000..235934808 --- /dev/null +++ b/ajax/libs/rivets/0.5.10/rivets.js @@ -0,0 +1,1033 @@ +// Rivets.js +// version: 0.5.10 +// author: Michael Richards +// license: MIT +(function() { + var Rivets, + __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, + __slice = [].slice, + __hasProp = {}.hasOwnProperty, + __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, + __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.ComponentBinding = (function(_super) { + __extends(ComponentBinding, _super); + + function ComponentBinding(view, el, type) { + var attribute, _i, _len, _ref, _ref1; + + this.view = view; + this.el = el; + this.type = type; + this.unbind = __bind(this.unbind, this); + this.bind = __bind(this.bind, this); + this.update = __bind(this.update, this); + this.locals = __bind(this.locals, this); + this.component = Rivets.components[this.type]; + this.attributes = {}; + this.inflections = {}; + _ref = this.el.attributes || []; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + attribute = _ref[_i]; + if (_ref1 = attribute.name, __indexOf.call(this.component.attributes, _ref1) >= 0) { + this.attributes[attribute.name] = attribute.value; + } else { + this.inflections[attribute.name] = attribute.value; + } + } + } + + ComponentBinding.prototype.sync = function() {}; + + ComponentBinding.prototype.locals = function(models) { + var inverse, key, model, result, _ref, _ref1; + + if (models == null) { + models = this.view.models; + } + result = {}; + _ref = this.inflections; + for (key in _ref) { + inverse = _ref[key]; + result[key] = models[inverse]; + } + for (key in models) { + model = models[key]; + if ((_ref1 = result[key]) == null) { + result[key] = model; + } + } + return result; + }; + + ComponentBinding.prototype.update = function(models) { + var _ref; + + return (_ref = this.componentView) != null ? _ref.update(this.locals(models)) : void 0; + }; + + ComponentBinding.prototype.bind = function() { + var el, _ref; + + if (this.componentView != null) { + return (_ref = this.componentView) != null ? _ref.bind() : void 0; + } else { + el = this.component.build.call(this.attributes); + (this.componentView = new Rivets.View(el, this.locals(), this.view.options)).bind(); + return this.el.parentNode.replaceChild(el, this.el); + } + }; + + ComponentBinding.prototype.unbind = function() { + var _ref; + + return (_ref = this.componentView) != null ? _ref.unbind() : void 0; + }; + + return ComponentBinding; + + })(Rivets.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.componentRegExp = __bind(this.componentRegExp, 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.componentRegExp = function() { + var _ref, _ref1; + + return new RegExp("^" + ((_ref = (_ref1 = this.config.prefix) != null ? _ref1.toUpperCase() : void 0) != null ? _ref : 'RV') + "-"); + }; + + View.prototype.build = function() { + var bindingRegExp, buildBinding, componentRegExp, el, parse, skipNodes, _i, _len, _ref, + _this = this; + + this.bindings = []; + skipNodes = []; + bindingRegExp = this.bindingRegExp(); + componentRegExp = this.componentRegExp(); + 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 (componentRegExp.test(node.tagName)) { + type = node.tagName.replace(componentRegExp, '').toLowerCase(); + _this.bindings.push(new Rivets.ComponentBinding(_this, node, type)); + } 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.components = {}; + + 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.components = Rivets.components; + 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); diff --git a/ajax/libs/rivets/0.5.10/rivets.min.js b/ajax/libs/rivets/0.5.10/rivets.min.js new file mode 100644 index 000000000..e49fa6b00 --- /dev/null +++ b/ajax/libs/rivets/0.5.10/rivets.min.js @@ -0,0 +1,5 @@ +// Rivets.js +// version: 0.5.10 +// author: Michael Richards +// license: MIT +(function(){var t,i=function(t,i){return function(){return t.apply(i,arguments)}},e=[].slice,n={}.hasOwnProperty,s=function(t,i){function e(){this.constructor=t}for(var s in i)n.call(i,s)&&(t[s]=i[s]);return e.prototype=i.prototype,t.prototype=new e,t.__super__=i.prototype,t},r=[].indexOf||function(t){for(var i=0,e=this.length;e>i;i++)if(i in this&&this[i]===t)return i;return-1};t={},String.prototype.trim||(String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g,"")}),t.Binding=function(){function n(e,n,s,r,o,h){var l,u,a,d;if(this.view=e,this.el=n,this.type=s,this.key=r,this.keypath=o,this.options=null!=h?h:{},this.update=i(this.update,this),this.unbind=i(this.unbind,this),this.bind=i(this.bind,this),this.publish=i(this.publish,this),this.sync=i(this.sync,this),this.set=i(this.set,this),this.eventHandler=i(this.eventHandler,this),this.formattedValue=i(this.formattedValue,this),!(this.binder=t.internalBinders[this.type]||this.view.binders[s])){d=this.view.binders;for(l in d)a=d[l],"*"!==l&&-1!==l.indexOf("*")&&(u=RegExp("^"+l.replace("*",".+")+"$"),u.test(s)&&(this.binder=a,this.args=RegExp("^"+l.replace("*","(.+)")+"$").exec(s),this.args.shift()))}this.binder||(this.binder=this.view.binders["*"]),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}return n.prototype.formattedValue=function(t){var i,n,s,r,o,h;for(h=this.formatters,r=0,o=h.length;o>r;r++)n=h[r],i=n.split(/\s+/),s=i.shift(),n=this.model[s]instanceof Function?this.model[s]:this.view.formatters[s],(null!=n?n.read:void 0)instanceof Function?t=n.read.apply(n,[t].concat(e.call(i))):n instanceof Function&&(t=n.apply(null,[t].concat(e.call(i))));return t},n.prototype.eventHandler=function(t){var i,e;return e=(i=this).view.config.handler,function(n){return e.call(t,this,n,i)}},n.prototype.set=function(t){var i;return t=t instanceof Function&&!this.binder["function"]?this.formattedValue(t.call(this.model)):this.formattedValue(t),null!=(i=this.binder.routine)?i.call(this,this.el,t):void 0},n.prototype.sync=function(){return this.set(this.options.bypass?this.model[this.keypath]:this.view.config.adapter.read(this.model,this.keypath))},n.prototype.publish=function(){var i,n,s,r,o,h,l,u,a;for(r=t.Util.getInputValue(this.el),l=this.formatters.slice(0).reverse(),o=0,h=l.length;h>o;o++)n=l[o],i=n.split(/\s+/),s=i.shift(),(null!=(u=this.view.formatters[s])?u.publish:void 0)&&(r=(a=this.view.formatters[s]).publish.apply(a,[r].concat(e.call(i))));return this.view.config.adapter.publish(this.model,this.keypath,r)},n.prototype.bind=function(){var t,i,e,n,s,r,o,h,l;if(null!=(r=this.binder.bind)&&r.call(this,this.el),this.options.bypass?this.sync():(this.view.config.adapter.subscribe(this.model,this.keypath,this.sync),this.view.config.preloadData&&this.sync()),null!=(o=this.options.dependencies)?o.length:void 0){for(h=this.options.dependencies,l=[],n=0,s=h.length;s>n;n++)t=h[n],/^\./.test(t)?(e=this.model,i=t.substr(1)):(t=t.split("."),e=this.view.models[t.shift()],i=t.join(".")),l.push(this.view.config.adapter.subscribe(e,i,this.sync));return l}},n.prototype.unbind=function(){var t,i,e,n,s,r,o,h,l;if(null!=(r=this.binder.unbind)&&r.call(this,this.el),this.options.bypass||this.view.config.adapter.unsubscribe(this.model,this.keypath,this.sync),null!=(o=this.options.dependencies)?o.length:void 0){for(h=this.options.dependencies,l=[],n=0,s=h.length;s>n;n++)t=h[n],/^\./.test(t)?(e=this.model,i=t.substr(1)):(t=t.split("."),e=this.view.models[t.shift()],i=t.join(".")),l.push(this.view.config.adapter.unsubscribe(e,i,this.sync));return l}},n.prototype.update=function(t){var i;return null==t&&(t={}),this.key?t[this.key]&&(this.options.bypass||this.view.config.adapter.unsubscribe(this.model,this.keypath,this.sync),this.model=t[this.key],this.options.bypass?this.sync():(this.view.config.adapter.subscribe(this.model,this.keypath,this.sync),this.view.config.preloadData&&this.sync())):this.sync(),null!=(i=this.binder.update)?i.call(this,t):void 0},n}(),t.ComponentBinding=function(e){function n(e,n,s){var o,h,l,u,a;for(this.view=e,this.el=n,this.type=s,this.unbind=i(this.unbind,this),this.bind=i(this.bind,this),this.update=i(this.update,this),this.locals=i(this.locals,this),this.component=t.components[this.type],this.attributes={},this.inflections={},u=this.el.attributes||[],h=0,l=u.length;l>h;h++)o=u[h],a=o.name,r.call(this.component.attributes,a)>=0?this.attributes[o.name]=o.value:this.inflections[o.name]=o.value}return s(n,e),n.prototype.sync=function(){},n.prototype.locals=function(t){var i,e,n,s,r,o;null==t&&(t=this.view.models),s={},r=this.inflections;for(e in r)i=r[e],s[e]=t[i];for(e in t)n=t[e],null==(o=s[e])&&(s[e]=n);return s},n.prototype.update=function(t){var i;return null!=(i=this.componentView)?i.update(this.locals(t)):void 0},n.prototype.bind=function(){var i,e;return null!=this.componentView?null!=(e=this.componentView)?e.bind():void 0:(i=this.component.build.call(this.attributes),(this.componentView=new t.View(i,this.locals(),this.view.options)).bind(),this.el.parentNode.replaceChild(i,this.el))},n.prototype.unbind=function(){var t;return null!=(t=this.componentView)?t.unbind():void 0},n}(t.Binding),t.View=function(){function n(e,n,s){var r,o,h,l,u,a,d,p,c,f;for(this.els=e,this.models=n,this.options=null!=s?s:{},this.update=i(this.update,this),this.publish=i(this.publish,this),this.sync=i(this.sync,this),this.unbind=i(this.unbind,this),this.bind=i(this.bind,this),this.select=i(this.select,this),this.build=i(this.build,this),this.componentRegExp=i(this.componentRegExp,this),this.bindingRegExp=i(this.bindingRegExp,this),this.els.jquery||this.els instanceof Array||(this.els=[this.els]),d=["config","binders","formatters"],u=0,a=d.length;a>u;u++){if(o=d[u],this[o]={},this.options[o]){p=this.options[o];for(r in p)h=p[r],this[o][r]=h}c=t[o];for(r in c)h=c[r],null==(f=(l=this[o])[r])&&(l[r]=h)}this.build()}return n.prototype.bindingRegExp=function(){var t;return t=this.config.prefix,t?RegExp("^data-"+t+"-"):/^data-/},n.prototype.componentRegExp=function(){var t,i;return RegExp("^"+(null!=(t=null!=(i=this.config.prefix)?i.toUpperCase():void 0)?t:"RV")+"-")},n.prototype.build=function(){var i,n,s,o,h,l,u,a,d,p=this;for(this.bindings=[],l=[],i=this.bindingRegExp(),s=this.componentRegExp(),n=function(i,e,n){var s,r,o,h,l,u,a,d,c,f;return u={},c=function(){var t,i,e,s;for(e=n.split("|"),s=[],t=0,i=e.length;i>t;t++)d=e[t],s.push(d.trim());return s}(),s=function(){var t,i,e,n;for(e=c.shift().split("<"),n=[],t=0,i=e.length;i>t;t++)r=e[t],n.push(r.trim());return n}(),a=s.shift(),f=a.split(/\.|:/),u.formatters=c,u.bypass=-1!==a.indexOf(":"),f[0]?h=f.shift():(h=null,f.shift()),l=f.join("."),(o=s.shift())&&(u.dependencies=o.split(/\s+/)),p.bindings.push(new t.Binding(p,i,e,h,l,u))},h=function(o){var u,a,d,c,f,b,v,g,y,m,w,x,k,E,N,V,j,R,C,U,B,T,O,Q,A,D,L,_,F,H,S,$;if(0>r.call(l,o)){if(o.nodeType===Node.TEXT_NODE){if(g=t.TextTemplateParser,(f=p.config.templateDelimiters)&&(E=g.parse(o.data,f)).length&&(1!==E.length||E[0].type!==g.types.text)){switch(w=E[0],m=E.length>=2?e.call(E,1):[],o.data=w.value,w.type){case 0:o.data=w.value;break;case 1:n(o,"textNode",w.value)}for(j=0,B=m.length;B>j;j++)k=m[j],o.parentNode.appendChild(x=document.createTextNode(k.value)),1===k.type&&n(x,"textNode",k.value)}}else if(s.test(o.tagName))N=o.tagName.replace(s,"").toLowerCase(),p.bindings.push(new t.ComponentBinding(p,o,N));else if(null!=o.attributes){for(L=o.attributes,R=0,T=L.length;T>R;R++)if(u=L[R],i.test(u.name)){if(N=u.name.replace(i,""),!(d=p.binders[N])){_=p.binders;for(b in _)V=_[b],"*"!==b&&-1!==b.indexOf("*")&&(y=RegExp("^"+b.replace("*",".+")+"$"),y.test(N)&&(d=V))}if(d||(d=p.binders["*"]),d.block){for(F=o.childNodes,C=0,O=F.length;O>C;C++)v=F[C],l.push(v);a=[u]}}for(H=a||o.attributes,U=0,Q=H.length;Q>U;U++)u=H[U],i.test(u.name)&&(N=u.name.replace(i,""),n(o,N,u.value))}for(S=o.childNodes,$=[],D=0,A=S.length;A>D;D++)c=S[D],$.push(h(c));return $}},d=this.els,u=0,a=d.length;a>u;u++)o=d[u],h(o)},n.prototype.select=function(t){var i,e,n,s,r;for(s=this.bindings,r=[],e=0,n=s.length;n>e;e++)i=s[e],t(i)&&r.push(i);return r},n.prototype.bind=function(){var t,i,e,n,s;for(n=this.bindings,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.bind());return s},n.prototype.unbind=function(){var t,i,e,n,s;for(n=this.bindings,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.unbind());return s},n.prototype.sync=function(){var t,i,e,n,s;for(n=this.bindings,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.sync());return s},n.prototype.publish=function(){var t,i,e,n,s;for(n=this.select(function(t){return t.binder.publishes}),s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.publish());return s},n.prototype.update=function(t){var i,e,n,s,r,o,h;null==t&&(t={});for(e in t)n=t[e],this.models[e]=n;for(o=this.bindings,h=[],s=0,r=o.length;r>s;s++)i=o[s],h.push(i.update(t));return h},n}(),t.TextTemplateParser=function(){function t(){}return t.types={text:0,binding:1},t.parse=function(t,i){var e,n,s,r,o,h,l;for(h=[],r=t.length,e=0,n=0;r>n;){if(e=t.indexOf(i[0],n),0>e){h.push({type:this.types.text,value:t.slice(n)});break}if(e>0&&e>n&&h.push({type:this.types.text,value:t.slice(n,e)}),n=e+2,e=t.indexOf(i[1],n),0>e){o=t.slice(n-2),s=h[h.length-1],(null!=s?s.type:void 0)===this.types.text?s.value+=o:h.push({type:this.types.text,value:o});break}l=t.slice(n,e).trim(),h.push({type:this.types.binding,value:l}),n=e+2}return h},t}(),t.Util={bindEvent:function(t,i,e){return null!=window.jQuery?(t=jQuery(t),null!=t.on?t.on(i,e):t.bind(i,e)):null!=window.addEventListener?t.addEventListener(i,e,!1):(i="on"+i,t.attachEvent(i,e))},unbindEvent:function(t,i,e){return null!=window.jQuery?(t=jQuery(t),null!=t.off?t.off(i,e):t.unbind(i,e)):null!=window.removeEventListener?t.removeEventListener(i,e,!1):(i="on"+i,t.detachEvent(i,e))},getInputValue:function(t){var i,e,n,s;if(null!=window.jQuery)switch(t=jQuery(t),t[0].type){case"checkbox":return t.is(":checked");default:return t.val()}else switch(t.type){case"checkbox":return t.checked;case"select-multiple":for(s=[],e=0,n=t.length;n>e;e++)i=t[e],i.selected&&s.push(i.value);return s;default:return t.value}}},t.binders={enabled:function(t,i){return t.disabled=!i},disabled:function(t,i){return t.disabled=!!i},checked:{publishes:!0,bind:function(i){return t.Util.bindEvent(i,"change",this.publish)},unbind:function(i){return t.Util.unbindEvent(i,"change",this.publish)},routine:function(t,i){var e;return t.checked="radio"===t.type?(null!=(e=t.value)?""+e:void 0)===(null!=i?""+i:void 0):!!i}},unchecked:{publishes:!0,bind:function(i){return t.Util.bindEvent(i,"change",this.publish)},unbind:function(i){return t.Util.unbindEvent(i,"change",this.publish)},routine:function(t,i){var e;return t.checked="radio"===t.type?(null!=(e=t.value)?""+e:void 0)!==(null!=i?""+i:void 0):!i}},show:function(t,i){return t.style.display=i?"":"none"},hide:function(t,i){return t.style.display=i?"none":""},html:function(t,i){return t.innerHTML=null!=i?i:""},value:{publishes:!0,bind:function(i){return t.Util.bindEvent(i,"change",this.publish)},unbind:function(i){return t.Util.unbindEvent(i,"change",this.publish)},routine:function(t,i){var e,n,s,o,h,l,u;if(null!=window.jQuery){if(t=jQuery(t),(null!=i?""+i:void 0)!==(null!=(o=t.val())?""+o:void 0))return t.val(null!=i?i:"")}else if("select-multiple"===t.type){if(null!=i){for(u=[],n=0,s=t.length;s>n;n++)e=t[n],u.push(e.selected=(h=e.value,r.call(i,h)>=0));return u}}else if((null!=i?""+i:void 0)!==(null!=(l=t.value)?""+l:void 0))return t.value=null!=i?i:""}},text:function(t,i){return null!=t.innerText?t.innerText=null!=i?i:"":t.textContent=null!=i?i:""},"if":{block:!0,bind:function(t){var i,e;return null==this.marker?(i=["data",this.view.config.prefix,this.type].join("-").replace("--","-"),e=t.getAttribute(i),this.marker=document.createComment(" rivets: "+this.type+" "+e+" "),t.removeAttribute(i),t.parentNode.insertBefore(this.marker,t),t.parentNode.removeChild(t)):void 0},unbind:function(){var t;return null!=(t=this.nested)?t.unbind():void 0},routine:function(i,e){var n,s,r,o,h;if(!!e==(null==this.nested)){if(e){r={},h=this.view.models;for(n in h)s=h[n],r[n]=s;return o={binders:this.view.options.binders,formatters:this.view.options.formatters,config:this.view.options.config},(this.nested=new t.View(i,r,o)).bind(),this.marker.parentNode.insertBefore(i,this.marker.nextSibling)}return i.parentNode.removeChild(i),this.nested.unbind(),delete this.nested}},update:function(t){return this.nested.update(t)}},unless:{block:!0,bind:function(i){return t.binders["if"].bind.call(this,i)},unbind:function(){return t.binders["if"].unbind.call(this)},routine:function(i,e){return t.binders["if"].routine.call(this,i,!e)},update:function(i){return t.binders["if"].update.call(this,i)}},"on-*":{"function":!0,unbind:function(i){return this.handler?t.Util.unbindEvent(i,this.args[0],this.handler):void 0},routine:function(i,e){return this.handler&&t.Util.unbindEvent(i,this.args[0],this.handler),t.Util.bindEvent(i,this.args[0],this.handler=this.eventHandler(e))}},"each-*":{block:!0,bind:function(t){var i;return null==this.marker?(i=["data",this.view.config.prefix,this.type].join("-").replace("--","-"),this.marker=document.createComment(" rivets: "+this.type+" "),this.iterated=[],t.removeAttribute(i),t.parentNode.insertBefore(this.marker,t),t.parentNode.removeChild(t)):void 0},unbind:function(){var t,i,e,n,s;if(null!=this.iterated){for(n=this.iterated,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.unbind());return s}},routine:function(i,e){var n,s,r,o,h,l,u,a,d,p,c,f,b,v,g,y,m,w,x,k,E;if(u=this.args[0],e=e||[],this.iterated.length>e.length)for(m=Array(this.iterated.length-e.length),b=0,g=m.length;g>b;b++)s=m[b],f=this.iterated.pop(),f.unbind(),this.marker.parentNode.removeChild(f.els[0]);for(E=[],r=v=0,y=e.length;y>v;r=++v)if(l=e[r],n={},n[u]=l,null==this.iterated[r]){w=this.view.models;for(h in w)l=w[h],null==(x=n[h])&&(n[h]=l);d=this.iterated.length?this.iterated[this.iterated.length-1].els[0]:this.marker,a={binders:this.view.options.binders,formatters:this.view.options.formatters,config:{}},k=this.view.options.config;for(o in k)c=k[o],a.config[o]=c;a.config.preloadData=!0,p=i.cloneNode(!0),f=new t.View(p,n,a),f.bind(),this.iterated.push(f),E.push(this.marker.parentNode.insertBefore(p,d.nextSibling))}else this.iterated[r].models[u]!==l?E.push(this.iterated[r].update(n)):E.push(void 0);return E},update:function(t){var i,e,n,s,r,o,h,l;i={};for(e in t)n=t[e],e!==this.args[0]&&(i[e]=n);for(h=this.iterated,l=[],r=0,o=h.length;o>r;r++)s=h[r],l.push(s.update(i));return l}},"class-*":function(t,i){var e;return e=" "+t.className+" ",!i==(-1!==e.indexOf(" "+this.args[0]+" "))?t.className=i?""+t.className+" "+this.args[0]:e.replace(" "+this.args[0]+" "," ").trim():void 0},"*":function(t,i){return i?t.setAttribute(this.type,i):t.removeAttribute(this.type)}},t.internalBinders={textNode:function(t,i){return t.data=null!=i?i:""}},t.components={},t.config={preloadData:!0,handler:function(t,i,e){return this.call(t,i,e.view.models)}},t.formatters={},t.factory=function(i){return i._=t,i.binders=t.binders,i.components=t.components,i.formatters=t.formatters,i.config=t.config,i.configure=function(i){var e,n;null==i&&(i={});for(e in i)n=i[e],t.config[e]=n},i.bind=function(i,e,n){var s;return null==e&&(e={}),null==n&&(n={}),s=new t.View(i,e,n),s.bind(),s}},"object"==typeof exports?t.factory(exports):"function"==typeof define&&define.amd?define(["exports"],function(i){return t.factory(this.rivets=i),i}):t.factory(this.rivets={})}).call(this); \ No newline at end of file diff --git a/ajax/libs/rivets/0.5.11/rivets.js b/ajax/libs/rivets/0.5.11/rivets.js new file mode 100644 index 000000000..1bcff9283 --- /dev/null +++ b/ajax/libs/rivets/0.5.11/rivets.js @@ -0,0 +1,1037 @@ +// Rivets.js +// version: 0.5.11 +// author: Michael Richards +// license: MIT +(function() { + var Rivets, + __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, + __slice = [].slice, + __hasProp = {}.hasOwnProperty, + __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, + __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.ComponentBinding = (function(_super) { + __extends(ComponentBinding, _super); + + function ComponentBinding(view, el, type) { + var attribute, _i, _len, _ref, _ref1; + + this.view = view; + this.el = el; + this.type = type; + this.unbind = __bind(this.unbind, this); + this.bind = __bind(this.bind, this); + this.update = __bind(this.update, this); + this.locals = __bind(this.locals, this); + this.component = Rivets.components[this.type]; + this.attributes = {}; + this.inflections = {}; + _ref = this.el.attributes || []; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + attribute = _ref[_i]; + if (_ref1 = attribute.name, __indexOf.call(this.component.attributes, _ref1) >= 0) { + this.attributes[attribute.name] = attribute.value; + } else { + this.inflections[attribute.name] = attribute.value; + } + } + } + + ComponentBinding.prototype.sync = function() {}; + + ComponentBinding.prototype.locals = function(models) { + var inverse, key, model, path, result, _i, _len, _ref, _ref1, _ref2; + + if (models == null) { + models = this.view.models; + } + result = {}; + _ref = this.inflections; + for (key in _ref) { + inverse = _ref[key]; + _ref1 = inverse.split('.'); + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + path = _ref1[_i]; + result[key] = (result[key] || models)[path]; + } + } + for (key in models) { + model = models[key]; + if ((_ref2 = result[key]) == null) { + result[key] = model; + } + } + return result; + }; + + ComponentBinding.prototype.update = function(models) { + var _ref; + + return (_ref = this.componentView) != null ? _ref.update(this.locals(models)) : void 0; + }; + + ComponentBinding.prototype.bind = function() { + var el, _ref; + + if (this.componentView != null) { + return (_ref = this.componentView) != null ? _ref.bind() : void 0; + } else { + el = this.component.build.call(this.attributes); + (this.componentView = new Rivets.View(el, this.locals(), this.view.options)).bind(); + return this.el.parentNode.replaceChild(el, this.el); + } + }; + + ComponentBinding.prototype.unbind = function() { + var _ref; + + return (_ref = this.componentView) != null ? _ref.unbind() : void 0; + }; + + return ComponentBinding; + + })(Rivets.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.componentRegExp = __bind(this.componentRegExp, 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.componentRegExp = function() { + var _ref, _ref1; + + return new RegExp("^" + ((_ref = (_ref1 = this.config.prefix) != null ? _ref1.toUpperCase() : void 0) != null ? _ref : 'RV') + "-"); + }; + + View.prototype.build = function() { + var bindingRegExp, buildBinding, componentRegExp, el, parse, skipNodes, _i, _len, _ref, + _this = this; + + this.bindings = []; + skipNodes = []; + bindingRegExp = this.bindingRegExp(); + componentRegExp = this.componentRegExp(); + 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 (componentRegExp.test(node.tagName)) { + type = node.tagName.replace(componentRegExp, '').toLowerCase(); + _this.bindings.push(new Rivets.ComponentBinding(_this, node, type)); + } 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.components = {}; + + 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.components = Rivets.components; + 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); diff --git a/ajax/libs/rivets/0.5.11/rivets.min.js b/ajax/libs/rivets/0.5.11/rivets.min.js new file mode 100644 index 000000000..1d52c76e2 --- /dev/null +++ b/ajax/libs/rivets/0.5.11/rivets.min.js @@ -0,0 +1,5 @@ +// Rivets.js +// version: 0.5.11 +// author: Michael Richards +// license: MIT +(function(){var t,i=function(t,i){return function(){return t.apply(i,arguments)}},e=[].slice,n={}.hasOwnProperty,s=function(t,i){function e(){this.constructor=t}for(var s in i)n.call(i,s)&&(t[s]=i[s]);return e.prototype=i.prototype,t.prototype=new e,t.__super__=i.prototype,t},r=[].indexOf||function(t){for(var i=0,e=this.length;e>i;i++)if(i in this&&this[i]===t)return i;return-1};t={},String.prototype.trim||(String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g,"")}),t.Binding=function(){function n(e,n,s,r,o,h){var l,u,a,d;if(this.view=e,this.el=n,this.type=s,this.key=r,this.keypath=o,this.options=null!=h?h:{},this.update=i(this.update,this),this.unbind=i(this.unbind,this),this.bind=i(this.bind,this),this.publish=i(this.publish,this),this.sync=i(this.sync,this),this.set=i(this.set,this),this.eventHandler=i(this.eventHandler,this),this.formattedValue=i(this.formattedValue,this),!(this.binder=t.internalBinders[this.type]||this.view.binders[s])){d=this.view.binders;for(l in d)a=d[l],"*"!==l&&-1!==l.indexOf("*")&&(u=RegExp("^"+l.replace("*",".+")+"$"),u.test(s)&&(this.binder=a,this.args=RegExp("^"+l.replace("*","(.+)")+"$").exec(s),this.args.shift()))}this.binder||(this.binder=this.view.binders["*"]),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}return n.prototype.formattedValue=function(t){var i,n,s,r,o,h;for(h=this.formatters,r=0,o=h.length;o>r;r++)n=h[r],i=n.split(/\s+/),s=i.shift(),n=this.model[s]instanceof Function?this.model[s]:this.view.formatters[s],(null!=n?n.read:void 0)instanceof Function?t=n.read.apply(n,[t].concat(e.call(i))):n instanceof Function&&(t=n.apply(null,[t].concat(e.call(i))));return t},n.prototype.eventHandler=function(t){var i,e;return e=(i=this).view.config.handler,function(n){return e.call(t,this,n,i)}},n.prototype.set=function(t){var i;return t=t instanceof Function&&!this.binder["function"]?this.formattedValue(t.call(this.model)):this.formattedValue(t),null!=(i=this.binder.routine)?i.call(this,this.el,t):void 0},n.prototype.sync=function(){return this.set(this.options.bypass?this.model[this.keypath]:this.view.config.adapter.read(this.model,this.keypath))},n.prototype.publish=function(){var i,n,s,r,o,h,l,u,a;for(r=t.Util.getInputValue(this.el),l=this.formatters.slice(0).reverse(),o=0,h=l.length;h>o;o++)n=l[o],i=n.split(/\s+/),s=i.shift(),(null!=(u=this.view.formatters[s])?u.publish:void 0)&&(r=(a=this.view.formatters[s]).publish.apply(a,[r].concat(e.call(i))));return this.view.config.adapter.publish(this.model,this.keypath,r)},n.prototype.bind=function(){var t,i,e,n,s,r,o,h,l;if(null!=(r=this.binder.bind)&&r.call(this,this.el),this.options.bypass?this.sync():(this.view.config.adapter.subscribe(this.model,this.keypath,this.sync),this.view.config.preloadData&&this.sync()),null!=(o=this.options.dependencies)?o.length:void 0){for(h=this.options.dependencies,l=[],n=0,s=h.length;s>n;n++)t=h[n],/^\./.test(t)?(e=this.model,i=t.substr(1)):(t=t.split("."),e=this.view.models[t.shift()],i=t.join(".")),l.push(this.view.config.adapter.subscribe(e,i,this.sync));return l}},n.prototype.unbind=function(){var t,i,e,n,s,r,o,h,l;if(null!=(r=this.binder.unbind)&&r.call(this,this.el),this.options.bypass||this.view.config.adapter.unsubscribe(this.model,this.keypath,this.sync),null!=(o=this.options.dependencies)?o.length:void 0){for(h=this.options.dependencies,l=[],n=0,s=h.length;s>n;n++)t=h[n],/^\./.test(t)?(e=this.model,i=t.substr(1)):(t=t.split("."),e=this.view.models[t.shift()],i=t.join(".")),l.push(this.view.config.adapter.unsubscribe(e,i,this.sync));return l}},n.prototype.update=function(t){var i;return null==t&&(t={}),this.key?t[this.key]&&(this.options.bypass||this.view.config.adapter.unsubscribe(this.model,this.keypath,this.sync),this.model=t[this.key],this.options.bypass?this.sync():(this.view.config.adapter.subscribe(this.model,this.keypath,this.sync),this.view.config.preloadData&&this.sync())):this.sync(),null!=(i=this.binder.update)?i.call(this,t):void 0},n}(),t.ComponentBinding=function(e){function n(e,n,s){var o,h,l,u,a;for(this.view=e,this.el=n,this.type=s,this.unbind=i(this.unbind,this),this.bind=i(this.bind,this),this.update=i(this.update,this),this.locals=i(this.locals,this),this.component=t.components[this.type],this.attributes={},this.inflections={},u=this.el.attributes||[],h=0,l=u.length;l>h;h++)o=u[h],a=o.name,r.call(this.component.attributes,a)>=0?this.attributes[o.name]=o.value:this.inflections[o.name]=o.value}return s(n,e),n.prototype.sync=function(){},n.prototype.locals=function(t){var i,e,n,s,r,o,h,l,u,a;null==t&&(t=this.view.models),r={},l=this.inflections;for(e in l)for(i=l[e],u=i.split("."),o=0,h=u.length;h>o;o++)s=u[o],r[e]=(r[e]||t)[s];for(e in t)n=t[e],null==(a=r[e])&&(r[e]=n);return r},n.prototype.update=function(t){var i;return null!=(i=this.componentView)?i.update(this.locals(t)):void 0},n.prototype.bind=function(){var i,e;return null!=this.componentView?null!=(e=this.componentView)?e.bind():void 0:(i=this.component.build.call(this.attributes),(this.componentView=new t.View(i,this.locals(),this.view.options)).bind(),this.el.parentNode.replaceChild(i,this.el))},n.prototype.unbind=function(){var t;return null!=(t=this.componentView)?t.unbind():void 0},n}(t.Binding),t.View=function(){function n(e,n,s){var r,o,h,l,u,a,d,p,c,f;for(this.els=e,this.models=n,this.options=null!=s?s:{},this.update=i(this.update,this),this.publish=i(this.publish,this),this.sync=i(this.sync,this),this.unbind=i(this.unbind,this),this.bind=i(this.bind,this),this.select=i(this.select,this),this.build=i(this.build,this),this.componentRegExp=i(this.componentRegExp,this),this.bindingRegExp=i(this.bindingRegExp,this),this.els.jquery||this.els instanceof Array||(this.els=[this.els]),d=["config","binders","formatters"],u=0,a=d.length;a>u;u++){if(o=d[u],this[o]={},this.options[o]){p=this.options[o];for(r in p)h=p[r],this[o][r]=h}c=t[o];for(r in c)h=c[r],null==(f=(l=this[o])[r])&&(l[r]=h)}this.build()}return n.prototype.bindingRegExp=function(){var t;return t=this.config.prefix,t?RegExp("^data-"+t+"-"):/^data-/},n.prototype.componentRegExp=function(){var t,i;return RegExp("^"+(null!=(t=null!=(i=this.config.prefix)?i.toUpperCase():void 0)?t:"RV")+"-")},n.prototype.build=function(){var i,n,s,o,h,l,u,a,d,p=this;for(this.bindings=[],l=[],i=this.bindingRegExp(),s=this.componentRegExp(),n=function(i,e,n){var s,r,o,h,l,u,a,d,c,f;return u={},c=function(){var t,i,e,s;for(e=n.split("|"),s=[],t=0,i=e.length;i>t;t++)d=e[t],s.push(d.trim());return s}(),s=function(){var t,i,e,n;for(e=c.shift().split("<"),n=[],t=0,i=e.length;i>t;t++)r=e[t],n.push(r.trim());return n}(),a=s.shift(),f=a.split(/\.|:/),u.formatters=c,u.bypass=-1!==a.indexOf(":"),f[0]?h=f.shift():(h=null,f.shift()),l=f.join("."),(o=s.shift())&&(u.dependencies=o.split(/\s+/)),p.bindings.push(new t.Binding(p,i,e,h,l,u))},h=function(o){var u,a,d,c,f,b,v,g,y,m,w,x,k,E,N,V,j,R,C,U,B,T,O,Q,A,D,L,_,F,H,S,$;if(0>r.call(l,o)){if(o.nodeType===Node.TEXT_NODE){if(g=t.TextTemplateParser,(f=p.config.templateDelimiters)&&(E=g.parse(o.data,f)).length&&(1!==E.length||E[0].type!==g.types.text)){switch(w=E[0],m=E.length>=2?e.call(E,1):[],o.data=w.value,w.type){case 0:o.data=w.value;break;case 1:n(o,"textNode",w.value)}for(j=0,B=m.length;B>j;j++)k=m[j],o.parentNode.appendChild(x=document.createTextNode(k.value)),1===k.type&&n(x,"textNode",k.value)}}else if(s.test(o.tagName))N=o.tagName.replace(s,"").toLowerCase(),p.bindings.push(new t.ComponentBinding(p,o,N));else if(null!=o.attributes){for(L=o.attributes,R=0,T=L.length;T>R;R++)if(u=L[R],i.test(u.name)){if(N=u.name.replace(i,""),!(d=p.binders[N])){_=p.binders;for(b in _)V=_[b],"*"!==b&&-1!==b.indexOf("*")&&(y=RegExp("^"+b.replace("*",".+")+"$"),y.test(N)&&(d=V))}if(d||(d=p.binders["*"]),d.block){for(F=o.childNodes,C=0,O=F.length;O>C;C++)v=F[C],l.push(v);a=[u]}}for(H=a||o.attributes,U=0,Q=H.length;Q>U;U++)u=H[U],i.test(u.name)&&(N=u.name.replace(i,""),n(o,N,u.value))}for(S=o.childNodes,$=[],D=0,A=S.length;A>D;D++)c=S[D],$.push(h(c));return $}},d=this.els,u=0,a=d.length;a>u;u++)o=d[u],h(o)},n.prototype.select=function(t){var i,e,n,s,r;for(s=this.bindings,r=[],e=0,n=s.length;n>e;e++)i=s[e],t(i)&&r.push(i);return r},n.prototype.bind=function(){var t,i,e,n,s;for(n=this.bindings,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.bind());return s},n.prototype.unbind=function(){var t,i,e,n,s;for(n=this.bindings,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.unbind());return s},n.prototype.sync=function(){var t,i,e,n,s;for(n=this.bindings,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.sync());return s},n.prototype.publish=function(){var t,i,e,n,s;for(n=this.select(function(t){return t.binder.publishes}),s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.publish());return s},n.prototype.update=function(t){var i,e,n,s,r,o,h;null==t&&(t={});for(e in t)n=t[e],this.models[e]=n;for(o=this.bindings,h=[],s=0,r=o.length;r>s;s++)i=o[s],h.push(i.update(t));return h},n}(),t.TextTemplateParser=function(){function t(){}return t.types={text:0,binding:1},t.parse=function(t,i){var e,n,s,r,o,h,l;for(h=[],r=t.length,e=0,n=0;r>n;){if(e=t.indexOf(i[0],n),0>e){h.push({type:this.types.text,value:t.slice(n)});break}if(e>0&&e>n&&h.push({type:this.types.text,value:t.slice(n,e)}),n=e+2,e=t.indexOf(i[1],n),0>e){o=t.slice(n-2),s=h[h.length-1],(null!=s?s.type:void 0)===this.types.text?s.value+=o:h.push({type:this.types.text,value:o});break}l=t.slice(n,e).trim(),h.push({type:this.types.binding,value:l}),n=e+2}return h},t}(),t.Util={bindEvent:function(t,i,e){return null!=window.jQuery?(t=jQuery(t),null!=t.on?t.on(i,e):t.bind(i,e)):null!=window.addEventListener?t.addEventListener(i,e,!1):(i="on"+i,t.attachEvent(i,e))},unbindEvent:function(t,i,e){return null!=window.jQuery?(t=jQuery(t),null!=t.off?t.off(i,e):t.unbind(i,e)):null!=window.removeEventListener?t.removeEventListener(i,e,!1):(i="on"+i,t.detachEvent(i,e))},getInputValue:function(t){var i,e,n,s;if(null!=window.jQuery)switch(t=jQuery(t),t[0].type){case"checkbox":return t.is(":checked");default:return t.val()}else switch(t.type){case"checkbox":return t.checked;case"select-multiple":for(s=[],e=0,n=t.length;n>e;e++)i=t[e],i.selected&&s.push(i.value);return s;default:return t.value}}},t.binders={enabled:function(t,i){return t.disabled=!i},disabled:function(t,i){return t.disabled=!!i},checked:{publishes:!0,bind:function(i){return t.Util.bindEvent(i,"change",this.publish)},unbind:function(i){return t.Util.unbindEvent(i,"change",this.publish)},routine:function(t,i){var e;return t.checked="radio"===t.type?(null!=(e=t.value)?""+e:void 0)===(null!=i?""+i:void 0):!!i}},unchecked:{publishes:!0,bind:function(i){return t.Util.bindEvent(i,"change",this.publish)},unbind:function(i){return t.Util.unbindEvent(i,"change",this.publish)},routine:function(t,i){var e;return t.checked="radio"===t.type?(null!=(e=t.value)?""+e:void 0)!==(null!=i?""+i:void 0):!i}},show:function(t,i){return t.style.display=i?"":"none"},hide:function(t,i){return t.style.display=i?"none":""},html:function(t,i){return t.innerHTML=null!=i?i:""},value:{publishes:!0,bind:function(i){return t.Util.bindEvent(i,"change",this.publish)},unbind:function(i){return t.Util.unbindEvent(i,"change",this.publish)},routine:function(t,i){var e,n,s,o,h,l,u;if(null!=window.jQuery){if(t=jQuery(t),(null!=i?""+i:void 0)!==(null!=(o=t.val())?""+o:void 0))return t.val(null!=i?i:"")}else if("select-multiple"===t.type){if(null!=i){for(u=[],n=0,s=t.length;s>n;n++)e=t[n],u.push(e.selected=(h=e.value,r.call(i,h)>=0));return u}}else if((null!=i?""+i:void 0)!==(null!=(l=t.value)?""+l:void 0))return t.value=null!=i?i:""}},text:function(t,i){return null!=t.innerText?t.innerText=null!=i?i:"":t.textContent=null!=i?i:""},"if":{block:!0,bind:function(t){var i,e;return null==this.marker?(i=["data",this.view.config.prefix,this.type].join("-").replace("--","-"),e=t.getAttribute(i),this.marker=document.createComment(" rivets: "+this.type+" "+e+" "),t.removeAttribute(i),t.parentNode.insertBefore(this.marker,t),t.parentNode.removeChild(t)):void 0},unbind:function(){var t;return null!=(t=this.nested)?t.unbind():void 0},routine:function(i,e){var n,s,r,o,h;if(!!e==(null==this.nested)){if(e){r={},h=this.view.models;for(n in h)s=h[n],r[n]=s;return o={binders:this.view.options.binders,formatters:this.view.options.formatters,config:this.view.options.config},(this.nested=new t.View(i,r,o)).bind(),this.marker.parentNode.insertBefore(i,this.marker.nextSibling)}return i.parentNode.removeChild(i),this.nested.unbind(),delete this.nested}},update:function(t){return this.nested.update(t)}},unless:{block:!0,bind:function(i){return t.binders["if"].bind.call(this,i)},unbind:function(){return t.binders["if"].unbind.call(this)},routine:function(i,e){return t.binders["if"].routine.call(this,i,!e)},update:function(i){return t.binders["if"].update.call(this,i)}},"on-*":{"function":!0,unbind:function(i){return this.handler?t.Util.unbindEvent(i,this.args[0],this.handler):void 0},routine:function(i,e){return this.handler&&t.Util.unbindEvent(i,this.args[0],this.handler),t.Util.bindEvent(i,this.args[0],this.handler=this.eventHandler(e))}},"each-*":{block:!0,bind:function(t){var i;return null==this.marker?(i=["data",this.view.config.prefix,this.type].join("-").replace("--","-"),this.marker=document.createComment(" rivets: "+this.type+" "),this.iterated=[],t.removeAttribute(i),t.parentNode.insertBefore(this.marker,t),t.parentNode.removeChild(t)):void 0},unbind:function(){var t,i,e,n,s;if(null!=this.iterated){for(n=this.iterated,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.unbind());return s}},routine:function(i,e){var n,s,r,o,h,l,u,a,d,p,c,f,b,v,g,y,m,w,x,k,E;if(u=this.args[0],e=e||[],this.iterated.length>e.length)for(m=Array(this.iterated.length-e.length),b=0,g=m.length;g>b;b++)s=m[b],f=this.iterated.pop(),f.unbind(),this.marker.parentNode.removeChild(f.els[0]);for(E=[],r=v=0,y=e.length;y>v;r=++v)if(l=e[r],n={},n[u]=l,null==this.iterated[r]){w=this.view.models;for(h in w)l=w[h],null==(x=n[h])&&(n[h]=l);d=this.iterated.length?this.iterated[this.iterated.length-1].els[0]:this.marker,a={binders:this.view.options.binders,formatters:this.view.options.formatters,config:{}},k=this.view.options.config;for(o in k)c=k[o],a.config[o]=c;a.config.preloadData=!0,p=i.cloneNode(!0),f=new t.View(p,n,a),f.bind(),this.iterated.push(f),E.push(this.marker.parentNode.insertBefore(p,d.nextSibling))}else this.iterated[r].models[u]!==l?E.push(this.iterated[r].update(n)):E.push(void 0);return E},update:function(t){var i,e,n,s,r,o,h,l;i={};for(e in t)n=t[e],e!==this.args[0]&&(i[e]=n);for(h=this.iterated,l=[],r=0,o=h.length;o>r;r++)s=h[r],l.push(s.update(i));return l}},"class-*":function(t,i){var e;return e=" "+t.className+" ",!i==(-1!==e.indexOf(" "+this.args[0]+" "))?t.className=i?""+t.className+" "+this.args[0]:e.replace(" "+this.args[0]+" "," ").trim():void 0},"*":function(t,i){return i?t.setAttribute(this.type,i):t.removeAttribute(this.type)}},t.internalBinders={textNode:function(t,i){return t.data=null!=i?i:""}},t.components={},t.config={preloadData:!0,handler:function(t,i,e){return this.call(t,i,e.view.models)}},t.formatters={},t.factory=function(i){return i._=t,i.binders=t.binders,i.components=t.components,i.formatters=t.formatters,i.config=t.config,i.configure=function(i){var e,n;null==i&&(i={});for(e in i)n=i[e],t.config[e]=n},i.bind=function(i,e,n){var s;return null==e&&(e={}),null==n&&(n={}),s=new t.View(i,e,n),s.bind(),s}},"object"==typeof exports?t.factory(exports):"function"==typeof define&&define.amd?define(["exports"],function(i){return t.factory(this.rivets=i),i}):t.factory(this.rivets={})}).call(this); \ No newline at end of file diff --git a/ajax/libs/rivets/0.5.12/rivets.js b/ajax/libs/rivets/0.5.12/rivets.js new file mode 100644 index 000000000..b731abf5a --- /dev/null +++ b/ajax/libs/rivets/0.5.12/rivets.js @@ -0,0 +1,1018 @@ +// Rivets.js +// version: 0.5.12 +// author: Michael Richards +// license: MIT +(function() { + var Rivets, + __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, + __slice = [].slice, + __hasProp = {}.hasOwnProperty, + __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, + __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.ComponentBinding = (function(_super) { + __extends(ComponentBinding, _super); + + function ComponentBinding(view, el, type) { + var attribute, _i, _len, _ref, _ref1; + this.view = view; + this.el = el; + this.type = type; + this.unbind = __bind(this.unbind, this); + this.bind = __bind(this.bind, this); + this.update = __bind(this.update, this); + this.locals = __bind(this.locals, this); + this.component = Rivets.components[this.type]; + this.attributes = {}; + this.inflections = {}; + _ref = this.el.attributes || []; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + attribute = _ref[_i]; + if (_ref1 = attribute.name, __indexOf.call(this.component.attributes, _ref1) >= 0) { + this.attributes[attribute.name] = attribute.value; + } else { + this.inflections[attribute.name] = attribute.value; + } + } + } + + ComponentBinding.prototype.sync = function() {}; + + ComponentBinding.prototype.locals = function(models) { + var inverse, key, model, path, result, _i, _len, _ref, _ref1; + if (models == null) { + models = this.view.models; + } + result = {}; + _ref = this.inflections; + for (key in _ref) { + inverse = _ref[key]; + _ref1 = inverse.split('.'); + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + path = _ref1[_i]; + result[key] = (result[key] || models)[path]; + } + } + for (key in models) { + model = models[key]; + if (result[key] == null) { + result[key] = model; + } + } + return result; + }; + + ComponentBinding.prototype.update = function(models) { + var _ref; + return (_ref = this.componentView) != null ? _ref.update(this.locals(models)) : void 0; + }; + + ComponentBinding.prototype.bind = function() { + var el, _ref; + if (this.componentView != null) { + return (_ref = this.componentView) != null ? _ref.bind() : void 0; + } else { + el = this.component.build.call(this.attributes); + (this.componentView = new Rivets.View(el, this.locals(), this.view.options)).bind(); + return this.el.parentNode.replaceChild(el, this.el); + } + }; + + ComponentBinding.prototype.unbind = function() { + var _ref; + return (_ref = this.componentView) != null ? _ref.unbind() : void 0; + }; + + return ComponentBinding; + + })(Rivets.Binding); + + Rivets.TextBinding = (function(_super) { + __extends(TextBinding, _super); + + function TextBinding(view, el, type, key, keypath, options) { + this.view = view; + this.el = el; + this.type = type; + this.key = key; + this.keypath = keypath; + this.options = options != null ? options : {}; + this.sync = __bind(this.sync, this); + this.formatters = this.options.formatters || []; + this.model = this.key ? this.view.models[this.key] : this.view.models; + } + + TextBinding.prototype.binder = { + routine: function(node, value) { + return node.data = value != null ? value : ''; + } + }; + + TextBinding.prototype.sync = function() { + return TextBinding.__super__.sync.apply(this, arguments); + }; + + return TextBinding; + + })(Rivets.Binding); + + Rivets.View = (function() { + function View(els, models, options) { + var k, option, v, _base, _i, _len, _ref, _ref1, _ref2; + 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.componentRegExp = __bind(this.componentRegExp, 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 ((_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.componentRegExp = function() { + var _ref, _ref1; + return new RegExp("^" + ((_ref = (_ref1 = this.config.prefix) != null ? _ref1.toUpperCase() : void 0) != null ? _ref : 'RV') + "-"); + }; + + View.prototype.build = function() { + var bindingRegExp, buildBinding, componentRegExp, el, parse, skipNodes, _i, _len, _ref, + _this = this; + this.bindings = []; + skipNodes = []; + bindingRegExp = this.bindingRegExp(); + componentRegExp = this.componentRegExp(); + buildBinding = function(binding, 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; + if (startToken.type === 0) { + node.data = startToken.value; + } else { + buildBinding('TextBinding', node, null, startToken.value); + } + for (_i = 0, _len = restTokens.length; _i < _len; _i++) { + token = restTokens[_i]; + text = document.createTextNode(token.value); + node.parentNode.appendChild(text); + if (token.type === 1) { + buildBinding('TextBinding', text, null, token.value); + } + } + } + } + } + } else if (componentRegExp.test(node.tagName)) { + type = node.tagName.replace(componentRegExp, '').toLowerCase(); + _this.bindings.push(new Rivets.ComponentBinding(_this, node, type)); + } 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('Binding', 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) { + var _ref; + return (_ref = this.nested) != null ? _ref.update(models) : void 0; + } + }, + 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, _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 (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: {} + }; + _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; + }, + 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.components = {}; + + 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.components = Rivets.components; + 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); diff --git a/ajax/libs/rivets/0.5.12/rivets.min.js b/ajax/libs/rivets/0.5.12/rivets.min.js new file mode 100644 index 000000000..e61d2d815 --- /dev/null +++ b/ajax/libs/rivets/0.5.12/rivets.min.js @@ -0,0 +1,5 @@ +// Rivets.js +// version: 0.5.12 +// author: Michael Richards +// license: MIT +!function(){var a,b=function(a,b){return function(){return a.apply(b,arguments)}},c=[].slice,d={}.hasOwnProperty,e=function(a,b){function c(){this.constructor=a}for(var e in b)d.call(b,e)&&(a[e]=b[e]);return c.prototype=b.prototype,a.prototype=new c,a.__super__=b.prototype,a},f=[].indexOf||function(a){for(var b=0,c=this.length;c>b;b++)if(b in this&&this[b]===a)return b;return-1};a={},String.prototype.trim||(String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g,"")}),a.Binding=function(){function d(a,c,d,e,f,g){var h,i,j,k;if(this.view=a,this.el=c,this.type=d,this.key=e,this.keypath=f,this.options=null!=g?g:{},this.update=b(this.update,this),this.unbind=b(this.unbind,this),this.bind=b(this.bind,this),this.publish=b(this.publish,this),this.sync=b(this.sync,this),this.set=b(this.set,this),this.eventHandler=b(this.eventHandler,this),this.formattedValue=b(this.formattedValue,this),!(this.binder=this.view.binders[d])){k=this.view.binders;for(h in k)j=k[h],"*"!==h&&-1!==h.indexOf("*")&&(i=new RegExp("^"+h.replace("*",".+")+"$"),i.test(d)&&(this.binder=j,this.args=new RegExp("^"+h.replace("*","(.+)")+"$").exec(d),this.args.shift()))}this.binder||(this.binder=this.view.binders["*"]),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}return d.prototype.formattedValue=function(a){var b,d,e,f,g,h;for(h=this.formatters,f=0,g=h.length;g>f;f++)d=h[f],b=d.split(/\s+/),e=b.shift(),d=this.model[e]instanceof Function?this.model[e]:this.view.formatters[e],(null!=d?d.read:void 0)instanceof Function?a=d.read.apply(d,[a].concat(c.call(b))):d instanceof Function&&(a=d.apply(null,[a].concat(c.call(b))));return a},d.prototype.eventHandler=function(a){var b,c;return c=(b=this).view.config.handler,function(d){return c.call(a,this,d,b)}},d.prototype.set=function(a){var b;return a=a instanceof Function&&!this.binder["function"]?this.formattedValue(a.call(this.model)):this.formattedValue(a),null!=(b=this.binder.routine)?b.call(this,this.el,a):void 0},d.prototype.sync=function(){return this.set(this.options.bypass?this.model[this.keypath]:this.view.config.adapter.read(this.model,this.keypath))},d.prototype.publish=function(){var b,d,e,f,g,h,i,j,k;for(f=a.Util.getInputValue(this.el),i=this.formatters.slice(0).reverse(),g=0,h=i.length;h>g;g++)d=i[g],b=d.split(/\s+/),e=b.shift(),(null!=(j=this.view.formatters[e])?j.publish:void 0)&&(f=(k=this.view.formatters[e]).publish.apply(k,[f].concat(c.call(b))));return this.view.config.adapter.publish(this.model,this.keypath,f)},d.prototype.bind=function(){var a,b,c,d,e,f,g,h,i;if(null!=(f=this.binder.bind)&&f.call(this,this.el),this.options.bypass?this.sync():(this.view.config.adapter.subscribe(this.model,this.keypath,this.sync),this.view.config.preloadData&&this.sync()),null!=(g=this.options.dependencies)?g.length:void 0){for(h=this.options.dependencies,i=[],d=0,e=h.length;e>d;d++)a=h[d],/^\./.test(a)?(c=this.model,b=a.substr(1)):(a=a.split("."),c=this.view.models[a.shift()],b=a.join(".")),i.push(this.view.config.adapter.subscribe(c,b,this.sync));return i}},d.prototype.unbind=function(){var a,b,c,d,e,f,g,h,i;if(null!=(f=this.binder.unbind)&&f.call(this,this.el),this.options.bypass||this.view.config.adapter.unsubscribe(this.model,this.keypath,this.sync),null!=(g=this.options.dependencies)?g.length:void 0){for(h=this.options.dependencies,i=[],d=0,e=h.length;e>d;d++)a=h[d],/^\./.test(a)?(c=this.model,b=a.substr(1)):(a=a.split("."),c=this.view.models[a.shift()],b=a.join(".")),i.push(this.view.config.adapter.unsubscribe(c,b,this.sync));return i}},d.prototype.update=function(a){var b;return null==a&&(a={}),this.key?a[this.key]&&(this.options.bypass||this.view.config.adapter.unsubscribe(this.model,this.keypath,this.sync),this.model=a[this.key],this.options.bypass?this.sync():(this.view.config.adapter.subscribe(this.model,this.keypath,this.sync),this.view.config.preloadData&&this.sync())):this.sync(),null!=(b=this.binder.update)?b.call(this,a):void 0},d}(),a.ComponentBinding=function(c){function d(c,d,e){var g,h,i,j,k;for(this.view=c,this.el=d,this.type=e,this.unbind=b(this.unbind,this),this.bind=b(this.bind,this),this.update=b(this.update,this),this.locals=b(this.locals,this),this.component=a.components[this.type],this.attributes={},this.inflections={},j=this.el.attributes||[],h=0,i=j.length;i>h;h++)g=j[h],k=g.name,f.call(this.component.attributes,k)>=0?this.attributes[g.name]=g.value:this.inflections[g.name]=g.value}return e(d,c),d.prototype.sync=function(){},d.prototype.locals=function(a){var b,c,d,e,f,g,h,i,j;null==a&&(a=this.view.models),f={},i=this.inflections;for(c in i)for(b=i[c],j=b.split("."),g=0,h=j.length;h>g;g++)e=j[g],f[c]=(f[c]||a)[e];for(c in a)d=a[c],null==f[c]&&(f[c]=d);return f},d.prototype.update=function(a){var b;return null!=(b=this.componentView)?b.update(this.locals(a)):void 0},d.prototype.bind=function(){var b,c;return null!=this.componentView?null!=(c=this.componentView)?c.bind():void 0:(b=this.component.build.call(this.attributes),(this.componentView=new a.View(b,this.locals(),this.view.options)).bind(),this.el.parentNode.replaceChild(b,this.el))},d.prototype.unbind=function(){var a;return null!=(a=this.componentView)?a.unbind():void 0},d}(a.Binding),a.TextBinding=function(a){function c(a,c,d,e,f,g){this.view=a,this.el=c,this.type=d,this.key=e,this.keypath=f,this.options=null!=g?g:{},this.sync=b(this.sync,this),this.formatters=this.options.formatters||[],this.model=this.key?this.view.models[this.key]:this.view.models}return e(c,a),c.prototype.binder={routine:function(a,b){return a.data=null!=b?b:""}},c.prototype.sync=function(){return c.__super__.sync.apply(this,arguments)},c}(a.Binding),a.View=function(){function d(c,d,e){var f,g,h,i,j,k,l,m,n;for(this.els=c,this.models=d,this.options=null!=e?e:{},this.update=b(this.update,this),this.publish=b(this.publish,this),this.sync=b(this.sync,this),this.unbind=b(this.unbind,this),this.bind=b(this.bind,this),this.select=b(this.select,this),this.build=b(this.build,this),this.componentRegExp=b(this.componentRegExp,this),this.bindingRegExp=b(this.bindingRegExp,this),this.els.jquery||this.els instanceof Array||(this.els=[this.els]),l=["config","binders","formatters"],j=0,k=l.length;k>j;j++){if(g=l[j],this[g]={},this.options[g]){m=this.options[g];for(f in m)h=m[f],this[g][f]=h}n=a[g];for(f in n)h=n[f],null==(i=this[g])[f]&&(i[f]=h)}this.build()}return d.prototype.bindingRegExp=function(){var a;return a=this.config.prefix,a?new RegExp("^data-"+a+"-"):/^data-/},d.prototype.componentRegExp=function(){var a,b;return new RegExp("^"+(null!=(a=null!=(b=this.config.prefix)?b.toUpperCase():void 0)?a:"RV")+"-")},d.prototype.build=function(){var b,d,e,g,h,i,j,k,l,m=this;for(this.bindings=[],i=[],b=this.bindingRegExp(),e=this.componentRegExp(),d=function(b,c,d,e){var f,g,h,i,j,k,l,n,o,p;return k={},o=function(){var a,b,c,d;for(c=e.split("|"),d=[],a=0,b=c.length;b>a;a++)n=c[a],d.push(n.trim());return d}(),f=function(){var a,b,c,d;for(c=o.shift().split("<"),d=[],a=0,b=c.length;b>a;a++)g=c[a],d.push(g.trim());return d}(),l=f.shift(),p=l.split(/\.|:/),k.formatters=o,k.bypass=-1!==l.indexOf(":"),p[0]?i=p.shift():(i=null,p.shift()),j=p.join("."),(h=f.shift())&&(k.dependencies=h.split(/\s+/)),m.bindings.push(new a[b](m,c,d,i,j,k))},h=function(g){var j,k,l,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P;if(f.call(i,g)<0){if(g.nodeType===Node.TEXT_NODE){if(r=a.TextTemplateParser,(o=m.config.templateDelimiters)&&(x=r.parse(g.data,o)).length&&(1!==x.length||x[0].type!==r.types.text))for(u=x[0],t=2<=x.length?c.call(x,1):[],g.data=u.value,0===u.type?g.data=u.value:d("TextBinding",g,null,u.value),A=0,E=t.length;E>A;A++)w=t[A],v=document.createTextNode(w.value),g.parentNode.appendChild(v),1===w.type&&d("TextBinding",v,null,w.value)}else if(e.test(g.tagName))y=g.tagName.replace(e,"").toLowerCase(),m.bindings.push(new a.ComponentBinding(m,g,y));else if(null!=g.attributes){for(K=g.attributes,B=0,F=K.length;F>B;B++)if(j=K[B],b.test(j.name)){if(y=j.name.replace(b,""),!(l=m.binders[y])){L=m.binders;for(p in L)z=L[p],"*"!==p&&-1!==p.indexOf("*")&&(s=new RegExp("^"+p.replace("*",".+")+"$"),s.test(y)&&(l=z))}if(l||(l=m.binders["*"]),l.block){for(M=g.childNodes,C=0,G=M.length;G>C;C++)q=M[C],i.push(q);k=[j]}}for(N=k||g.attributes,D=0,H=N.length;H>D;D++)j=N[D],b.test(j.name)&&(y=j.name.replace(b,""),d("Binding",g,y,j.value))}for(O=g.childNodes,P=[],J=0,I=O.length;I>J;J++)n=O[J],P.push(h(n));return P}},l=this.els,j=0,k=l.length;k>j;j++)g=l[j],h(g)},d.prototype.select=function(a){var b,c,d,e,f;for(e=this.bindings,f=[],c=0,d=e.length;d>c;c++)b=e[c],a(b)&&f.push(b);return f},d.prototype.bind=function(){var a,b,c,d,e;for(d=this.bindings,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.bind());return e},d.prototype.unbind=function(){var a,b,c,d,e;for(d=this.bindings,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.unbind());return e},d.prototype.sync=function(){var a,b,c,d,e;for(d=this.bindings,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.sync());return e},d.prototype.publish=function(){var a,b,c,d,e;for(d=this.select(function(a){return a.binder.publishes}),e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.publish());return e},d.prototype.update=function(a){var b,c,d,e,f,g,h;null==a&&(a={});for(c in a)d=a[c],this.models[c]=d;for(g=this.bindings,h=[],e=0,f=g.length;f>e;e++)b=g[e],h.push(b.update(a));return h},d}(),a.TextTemplateParser=function(){function a(){}return a.types={text:0,binding:1},a.parse=function(a,b){var c,d,e,f,g,h,i;for(h=[],f=a.length,c=0,d=0;f>d;){if(c=a.indexOf(b[0],d),0>c){h.push({type:this.types.text,value:a.slice(d)});break}if(c>0&&c>d&&h.push({type:this.types.text,value:a.slice(d,c)}),d=c+2,c=a.indexOf(b[1],d),0>c){g=a.slice(d-2),e=h[h.length-1],(null!=e?e.type:void 0)===this.types.text?e.value+=g:h.push({type:this.types.text,value:g});break}i=a.slice(d,c).trim(),h.push({type:this.types.binding,value:i}),d=c+2}return h},a}(),a.Util={bindEvent:function(a,b,c){return null!=window.jQuery?(a=jQuery(a),null!=a.on?a.on(b,c):a.bind(b,c)):null!=window.addEventListener?a.addEventListener(b,c,!1):(b="on"+b,a.attachEvent(b,c))},unbindEvent:function(a,b,c){return null!=window.jQuery?(a=jQuery(a),null!=a.off?a.off(b,c):a.unbind(b,c)):null!=window.removeEventListener?a.removeEventListener(b,c,!1):(b="on"+b,a.detachEvent(b,c))},getInputValue:function(a){var b,c,d,e;if(null!=window.jQuery)switch(a=jQuery(a),a[0].type){case"checkbox":return a.is(":checked");default:return a.val()}else switch(a.type){case"checkbox":return a.checked;case"select-multiple":for(e=[],c=0,d=a.length;d>c;c++)b=a[c],b.selected&&e.push(b.value);return e;default:return a.value}}},a.binders={enabled:function(a,b){return a.disabled=!b},disabled:function(a,b){return a.disabled=!!b},checked:{publishes:!0,bind:function(b){return a.Util.bindEvent(b,"change",this.publish)},unbind:function(b){return a.Util.unbindEvent(b,"change",this.publish)},routine:function(a,b){var c;return a.checked="radio"===a.type?(null!=(c=a.value)?c.toString():void 0)===(null!=b?b.toString():void 0):!!b}},unchecked:{publishes:!0,bind:function(b){return a.Util.bindEvent(b,"change",this.publish)},unbind:function(b){return a.Util.unbindEvent(b,"change",this.publish)},routine:function(a,b){var c;return a.checked="radio"===a.type?(null!=(c=a.value)?c.toString():void 0)!==(null!=b?b.toString():void 0):!b}},show:function(a,b){return a.style.display=b?"":"none"},hide:function(a,b){return a.style.display=b?"none":""},html:function(a,b){return a.innerHTML=null!=b?b:""},value:{publishes:!0,bind:function(b){return a.Util.bindEvent(b,"change",this.publish)},unbind:function(b){return a.Util.unbindEvent(b,"change",this.publish)},routine:function(a,b){var c,d,e,g,h,i,j;if(null!=window.jQuery){if(a=jQuery(a),(null!=b?b.toString():void 0)!==(null!=(g=a.val())?g.toString():void 0))return a.val(null!=b?b:"")}else if("select-multiple"===a.type){if(null!=b){for(j=[],d=0,e=a.length;e>d;d++)c=a[d],j.push(c.selected=(h=c.value,f.call(b,h)>=0));return j}}else if((null!=b?b.toString():void 0)!==(null!=(i=a.value)?i.toString():void 0))return a.value=null!=b?b:""}},text:function(a,b){return null!=a.innerText?a.innerText=null!=b?b:"":a.textContent=null!=b?b:""},"if":{block:!0,bind:function(a){var b,c;return null==this.marker?(b=["data",this.view.config.prefix,this.type].join("-").replace("--","-"),c=a.getAttribute(b),this.marker=document.createComment(" rivets: "+this.type+" "+c+" "),a.removeAttribute(b),a.parentNode.insertBefore(this.marker,a),a.parentNode.removeChild(a)):void 0},unbind:function(){var a;return null!=(a=this.nested)?a.unbind():void 0},routine:function(b,c){var d,e,f,g,h;if(!!c==(null==this.nested)){if(c){f={},h=this.view.models;for(d in h)e=h[d],f[d]=e;return g={binders:this.view.options.binders,formatters:this.view.options.formatters,config:this.view.options.config},(this.nested=new a.View(b,f,g)).bind(),this.marker.parentNode.insertBefore(b,this.marker.nextSibling)}return b.parentNode.removeChild(b),this.nested.unbind(),delete this.nested}},update:function(a){var b;return null!=(b=this.nested)?b.update(a):void 0}},unless:{block:!0,bind:function(b){return a.binders["if"].bind.call(this,b)},unbind:function(){return a.binders["if"].unbind.call(this)},routine:function(b,c){return a.binders["if"].routine.call(this,b,!c)},update:function(b){return a.binders["if"].update.call(this,b)}},"on-*":{"function":!0,unbind:function(b){return this.handler?a.Util.unbindEvent(b,this.args[0],this.handler):void 0},routine:function(b,c){return this.handler&&a.Util.unbindEvent(b,this.args[0],this.handler),a.Util.bindEvent(b,this.args[0],this.handler=this.eventHandler(c))}},"each-*":{block:!0,bind:function(a){var b;return null==this.marker?(b=["data",this.view.config.prefix,this.type].join("-").replace("--","-"),this.marker=document.createComment(" rivets: "+this.type+" "),this.iterated=[],a.removeAttribute(b),a.parentNode.insertBefore(this.marker,a),a.parentNode.removeChild(a)):void 0},unbind:function(){var a,b,c,d,e;if(null!=this.iterated){for(d=this.iterated,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.unbind());return e}},routine:function(b,c){var d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w;if(j=this.args[0],c=c||[],this.iterated.length>c.length)for(t=Array(this.iterated.length-c.length),p=0,r=t.length;r>p;p++)e=t[p],o=this.iterated.pop(),o.unbind(),this.marker.parentNode.removeChild(o.els[0]);for(w=[],f=q=0,s=c.length;s>q;f=++q)if(i=c[f],d={},d[j]=i,null==this.iterated[f]){u=this.view.models;for(h in u)i=u[h],null==d[h]&&(d[h]=i);l=this.iterated.length?this.iterated[this.iterated.length-1].els[0]:this.marker,k={binders:this.view.options.binders,formatters:this.view.options.formatters,config:{}},v=this.view.options.config;for(g in v)n=v[g],k.config[g]=n;k.config.preloadData=!0,m=b.cloneNode(!0),o=new a.View(m,d,k),o.bind(),this.iterated.push(o),w.push(this.marker.parentNode.insertBefore(m,l.nextSibling))}else this.iterated[f].models[j]!==i?w.push(this.iterated[f].update(d)):w.push(void 0);return w},update:function(a){var b,c,d,e,f,g,h,i;b={};for(c in a)d=a[c],c!==this.args[0]&&(b[c]=d);for(h=this.iterated,i=[],f=0,g=h.length;g>f;f++)e=h[f],i.push(e.update(b));return i}},"class-*":function(a,b){var c;return c=" "+a.className+" ",!b==(-1!==c.indexOf(" "+this.args[0]+" "))?a.className=b?""+a.className+" "+this.args[0]:c.replace(" "+this.args[0]+" "," ").trim():void 0},"*":function(a,b){return b?a.setAttribute(this.type,b):a.removeAttribute(this.type)}},a.components={},a.config={preloadData:!0,handler:function(a,b,c){return this.call(a,b,c.view.models)}},a.formatters={},a.factory=function(b){return b._=a,b.binders=a.binders,b.components=a.components,b.formatters=a.formatters,b.config=a.config,b.configure=function(b){var c,d;null==b&&(b={});for(c in b)d=b[c],a.config[c]=d},b.bind=function(b,c,d){var e;return null==c&&(c={}),null==d&&(d={}),e=new a.View(b,c,d),e.bind(),e}},"object"==typeof exports?a.factory(exports):"function"==typeof define&&define.amd?define(["exports"],function(b){return a.factory(this.rivets=b),b}):a.factory(this.rivets={})}.call(this); \ No newline at end of file diff --git a/ajax/libs/rivets/0.5.13/rivets.js b/ajax/libs/rivets/0.5.13/rivets.js new file mode 100644 index 000000000..21c0e7206 --- /dev/null +++ b/ajax/libs/rivets/0.5.13/rivets.js @@ -0,0 +1,1020 @@ +// Rivets.js +// version: 0.5.13 +// author: Michael Richards +// license: MIT +(function() { + var Rivets, jQuery, + __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, + __slice = [].slice, + __hasProp = {}.hasOwnProperty, + __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, + __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 = {}; + + jQuery = window.jQuery || window.Zepto; + + 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.ComponentBinding = (function(_super) { + __extends(ComponentBinding, _super); + + function ComponentBinding(view, el, type) { + var attribute, _i, _len, _ref, _ref1; + this.view = view; + this.el = el; + this.type = type; + this.unbind = __bind(this.unbind, this); + this.bind = __bind(this.bind, this); + this.update = __bind(this.update, this); + this.locals = __bind(this.locals, this); + this.component = Rivets.components[this.type]; + this.attributes = {}; + this.inflections = {}; + _ref = this.el.attributes || []; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + attribute = _ref[_i]; + if (_ref1 = attribute.name, __indexOf.call(this.component.attributes, _ref1) >= 0) { + this.attributes[attribute.name] = attribute.value; + } else { + this.inflections[attribute.name] = attribute.value; + } + } + } + + ComponentBinding.prototype.sync = function() {}; + + ComponentBinding.prototype.locals = function(models) { + var inverse, key, model, path, result, _i, _len, _ref, _ref1; + if (models == null) { + models = this.view.models; + } + result = {}; + _ref = this.inflections; + for (key in _ref) { + inverse = _ref[key]; + _ref1 = inverse.split('.'); + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + path = _ref1[_i]; + result[key] = (result[key] || models)[path]; + } + } + for (key in models) { + model = models[key]; + if (result[key] == null) { + result[key] = model; + } + } + return result; + }; + + ComponentBinding.prototype.update = function(models) { + var _ref; + return (_ref = this.componentView) != null ? _ref.update(this.locals(models)) : void 0; + }; + + ComponentBinding.prototype.bind = function() { + var el, _ref; + if (this.componentView != null) { + return (_ref = this.componentView) != null ? _ref.bind() : void 0; + } else { + el = this.component.build.call(this.attributes); + (this.componentView = new Rivets.View(el, this.locals(), this.view.options)).bind(); + return this.el.parentNode.replaceChild(el, this.el); + } + }; + + ComponentBinding.prototype.unbind = function() { + var _ref; + return (_ref = this.componentView) != null ? _ref.unbind() : void 0; + }; + + return ComponentBinding; + + })(Rivets.Binding); + + Rivets.TextBinding = (function(_super) { + __extends(TextBinding, _super); + + function TextBinding(view, el, type, key, keypath, options) { + this.view = view; + this.el = el; + this.type = type; + this.key = key; + this.keypath = keypath; + this.options = options != null ? options : {}; + this.sync = __bind(this.sync, this); + this.formatters = this.options.formatters || []; + this.model = this.key ? this.view.models[this.key] : this.view.models; + } + + TextBinding.prototype.binder = { + routine: function(node, value) { + return node.data = value != null ? value : ''; + } + }; + + TextBinding.prototype.sync = function() { + return TextBinding.__super__.sync.apply(this, arguments); + }; + + return TextBinding; + + })(Rivets.Binding); + + Rivets.View = (function() { + function View(els, models, options) { + var k, option, v, _base, _i, _len, _ref, _ref1, _ref2; + 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.componentRegExp = __bind(this.componentRegExp, this); + this.bindingRegExp = __bind(this.bindingRegExp, this); + if (typeof this.els.length === 'undefined') { + 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 ((_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.componentRegExp = function() { + var _ref, _ref1; + return new RegExp("^" + ((_ref = (_ref1 = this.config.prefix) != null ? _ref1.toUpperCase() : void 0) != null ? _ref : 'RV') + "-"); + }; + + View.prototype.build = function() { + var bindingRegExp, buildBinding, componentRegExp, el, parse, skipNodes, _i, _len, _ref, + _this = this; + this.bindings = []; + skipNodes = []; + bindingRegExp = this.bindingRegExp(); + componentRegExp = this.componentRegExp(); + buildBinding = function(binding, 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; + if (startToken.type === 0) { + node.data = startToken.value; + } else { + buildBinding('TextBinding', node, null, startToken.value); + } + for (_i = 0, _len = restTokens.length; _i < _len; _i++) { + token = restTokens[_i]; + text = document.createTextNode(token.value); + node.parentNode.appendChild(text); + if (token.type === 1) { + buildBinding('TextBinding', text, null, token.value); + } + } + } + } + } + } else if (componentRegExp.test(node.tagName)) { + type = node.tagName.replace(componentRegExp, '').toLowerCase(); + _this.bindings.push(new Rivets.ComponentBinding(_this, node, type)); + } 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('Binding', 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) { + var _ref; + return (_ref = this.nested) != null ? _ref.update(models) : void 0; + } + }, + 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, _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 (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: {} + }; + _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; + }, + 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.components = {}; + + 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.components = Rivets.components; + 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); diff --git a/ajax/libs/rivets/0.5.13/rivets.min.js b/ajax/libs/rivets/0.5.13/rivets.min.js new file mode 100644 index 000000000..c6fd7c893 --- /dev/null +++ b/ajax/libs/rivets/0.5.13/rivets.min.js @@ -0,0 +1,5 @@ +// Rivets.js +// version: 0.5.13 +// author: Michael Richards +// license: MIT +!function(){var a,b,c=function(a,b){return function(){return a.apply(b,arguments)}},d=[].slice,e={}.hasOwnProperty,f=function(a,b){function c(){this.constructor=a}for(var d in b)e.call(b,d)&&(a[d]=b[d]);return c.prototype=b.prototype,a.prototype=new c,a.__super__=b.prototype,a},g=[].indexOf||function(a){for(var b=0,c=this.length;c>b;b++)if(b in this&&this[b]===a)return b;return-1};a={},b=window.jQuery||window.Zepto,String.prototype.trim||(String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g,"")}),a.Binding=function(){function b(a,b,d,e,f,g){var h,i,j,k;if(this.view=a,this.el=b,this.type=d,this.key=e,this.keypath=f,this.options=null!=g?g:{},this.update=c(this.update,this),this.unbind=c(this.unbind,this),this.bind=c(this.bind,this),this.publish=c(this.publish,this),this.sync=c(this.sync,this),this.set=c(this.set,this),this.eventHandler=c(this.eventHandler,this),this.formattedValue=c(this.formattedValue,this),!(this.binder=this.view.binders[d])){k=this.view.binders;for(h in k)j=k[h],"*"!==h&&-1!==h.indexOf("*")&&(i=new RegExp("^"+h.replace("*",".+")+"$"),i.test(d)&&(this.binder=j,this.args=new RegExp("^"+h.replace("*","(.+)")+"$").exec(d),this.args.shift()))}this.binder||(this.binder=this.view.binders["*"]),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}return b.prototype.formattedValue=function(a){var b,c,e,f,g,h;for(h=this.formatters,f=0,g=h.length;g>f;f++)c=h[f],b=c.split(/\s+/),e=b.shift(),c=this.model[e]instanceof Function?this.model[e]:this.view.formatters[e],(null!=c?c.read:void 0)instanceof Function?a=c.read.apply(c,[a].concat(d.call(b))):c instanceof Function&&(a=c.apply(null,[a].concat(d.call(b))));return a},b.prototype.eventHandler=function(a){var b,c;return c=(b=this).view.config.handler,function(d){return c.call(a,this,d,b)}},b.prototype.set=function(a){var b;return a=a instanceof Function&&!this.binder["function"]?this.formattedValue(a.call(this.model)):this.formattedValue(a),null!=(b=this.binder.routine)?b.call(this,this.el,a):void 0},b.prototype.sync=function(){return this.set(this.options.bypass?this.model[this.keypath]:this.view.config.adapter.read(this.model,this.keypath))},b.prototype.publish=function(){var b,c,e,f,g,h,i,j,k;for(f=a.Util.getInputValue(this.el),i=this.formatters.slice(0).reverse(),g=0,h=i.length;h>g;g++)c=i[g],b=c.split(/\s+/),e=b.shift(),(null!=(j=this.view.formatters[e])?j.publish:void 0)&&(f=(k=this.view.formatters[e]).publish.apply(k,[f].concat(d.call(b))));return this.view.config.adapter.publish(this.model,this.keypath,f)},b.prototype.bind=function(){var a,b,c,d,e,f,g,h,i;if(null!=(f=this.binder.bind)&&f.call(this,this.el),this.options.bypass?this.sync():(this.view.config.adapter.subscribe(this.model,this.keypath,this.sync),this.view.config.preloadData&&this.sync()),null!=(g=this.options.dependencies)?g.length:void 0){for(h=this.options.dependencies,i=[],d=0,e=h.length;e>d;d++)a=h[d],/^\./.test(a)?(c=this.model,b=a.substr(1)):(a=a.split("."),c=this.view.models[a.shift()],b=a.join(".")),i.push(this.view.config.adapter.subscribe(c,b,this.sync));return i}},b.prototype.unbind=function(){var a,b,c,d,e,f,g,h,i;if(null!=(f=this.binder.unbind)&&f.call(this,this.el),this.options.bypass||this.view.config.adapter.unsubscribe(this.model,this.keypath,this.sync),null!=(g=this.options.dependencies)?g.length:void 0){for(h=this.options.dependencies,i=[],d=0,e=h.length;e>d;d++)a=h[d],/^\./.test(a)?(c=this.model,b=a.substr(1)):(a=a.split("."),c=this.view.models[a.shift()],b=a.join(".")),i.push(this.view.config.adapter.unsubscribe(c,b,this.sync));return i}},b.prototype.update=function(a){var b;return null==a&&(a={}),this.key?a[this.key]&&(this.options.bypass||this.view.config.adapter.unsubscribe(this.model,this.keypath,this.sync),this.model=a[this.key],this.options.bypass?this.sync():(this.view.config.adapter.subscribe(this.model,this.keypath,this.sync),this.view.config.preloadData&&this.sync())):this.sync(),null!=(b=this.binder.update)?b.call(this,a):void 0},b}(),a.ComponentBinding=function(b){function d(b,d,e){var f,h,i,j,k;for(this.view=b,this.el=d,this.type=e,this.unbind=c(this.unbind,this),this.bind=c(this.bind,this),this.update=c(this.update,this),this.locals=c(this.locals,this),this.component=a.components[this.type],this.attributes={},this.inflections={},j=this.el.attributes||[],h=0,i=j.length;i>h;h++)f=j[h],k=f.name,g.call(this.component.attributes,k)>=0?this.attributes[f.name]=f.value:this.inflections[f.name]=f.value}return f(d,b),d.prototype.sync=function(){},d.prototype.locals=function(a){var b,c,d,e,f,g,h,i,j;null==a&&(a=this.view.models),f={},i=this.inflections;for(c in i)for(b=i[c],j=b.split("."),g=0,h=j.length;h>g;g++)e=j[g],f[c]=(f[c]||a)[e];for(c in a)d=a[c],null==f[c]&&(f[c]=d);return f},d.prototype.update=function(a){var b;return null!=(b=this.componentView)?b.update(this.locals(a)):void 0},d.prototype.bind=function(){var b,c;return null!=this.componentView?null!=(c=this.componentView)?c.bind():void 0:(b=this.component.build.call(this.attributes),(this.componentView=new a.View(b,this.locals(),this.view.options)).bind(),this.el.parentNode.replaceChild(b,this.el))},d.prototype.unbind=function(){var a;return null!=(a=this.componentView)?a.unbind():void 0},d}(a.Binding),a.TextBinding=function(a){function b(a,b,d,e,f,g){this.view=a,this.el=b,this.type=d,this.key=e,this.keypath=f,this.options=null!=g?g:{},this.sync=c(this.sync,this),this.formatters=this.options.formatters||[],this.model=this.key?this.view.models[this.key]:this.view.models}return f(b,a),b.prototype.binder={routine:function(a,b){return a.data=null!=b?b:""}},b.prototype.sync=function(){return b.__super__.sync.apply(this,arguments)},b}(a.Binding),a.View=function(){function b(b,d,e){var f,g,h,i,j,k,l,m,n;for(this.els=b,this.models=d,this.options=null!=e?e:{},this.update=c(this.update,this),this.publish=c(this.publish,this),this.sync=c(this.sync,this),this.unbind=c(this.unbind,this),this.bind=c(this.bind,this),this.select=c(this.select,this),this.build=c(this.build,this),this.componentRegExp=c(this.componentRegExp,this),this.bindingRegExp=c(this.bindingRegExp,this),"undefined"==typeof this.els.length&&(this.els=[this.els]),l=["config","binders","formatters"],j=0,k=l.length;k>j;j++){if(g=l[j],this[g]={},this.options[g]){m=this.options[g];for(f in m)h=m[f],this[g][f]=h}n=a[g];for(f in n)h=n[f],null==(i=this[g])[f]&&(i[f]=h)}this.build()}return b.prototype.bindingRegExp=function(){var a;return a=this.config.prefix,a?new RegExp("^data-"+a+"-"):/^data-/},b.prototype.componentRegExp=function(){var a,b;return new RegExp("^"+(null!=(a=null!=(b=this.config.prefix)?b.toUpperCase():void 0)?a:"RV")+"-")},b.prototype.build=function(){var b,c,e,f,h,i,j,k,l,m=this;for(this.bindings=[],i=[],b=this.bindingRegExp(),e=this.componentRegExp(),c=function(b,c,d,e){var f,g,h,i,j,k,l,n,o,p;return k={},o=function(){var a,b,c,d;for(c=e.split("|"),d=[],a=0,b=c.length;b>a;a++)n=c[a],d.push(n.trim());return d}(),f=function(){var a,b,c,d;for(c=o.shift().split("<"),d=[],a=0,b=c.length;b>a;a++)g=c[a],d.push(g.trim());return d}(),l=f.shift(),p=l.split(/\.|:/),k.formatters=o,k.bypass=-1!==l.indexOf(":"),p[0]?i=p.shift():(i=null,p.shift()),j=p.join("."),(h=f.shift())&&(k.dependencies=h.split(/\s+/)),m.bindings.push(new a[b](m,c,d,i,j,k))},h=function(f){var j,k,l,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P;if(g.call(i,f)<0){if(f.nodeType===Node.TEXT_NODE){if(r=a.TextTemplateParser,(o=m.config.templateDelimiters)&&(x=r.parse(f.data,o)).length&&(1!==x.length||x[0].type!==r.types.text))for(u=x[0],t=2<=x.length?d.call(x,1):[],f.data=u.value,0===u.type?f.data=u.value:c("TextBinding",f,null,u.value),A=0,E=t.length;E>A;A++)w=t[A],v=document.createTextNode(w.value),f.parentNode.appendChild(v),1===w.type&&c("TextBinding",v,null,w.value)}else if(e.test(f.tagName))y=f.tagName.replace(e,"").toLowerCase(),m.bindings.push(new a.ComponentBinding(m,f,y));else if(null!=f.attributes){for(K=f.attributes,B=0,F=K.length;F>B;B++)if(j=K[B],b.test(j.name)){if(y=j.name.replace(b,""),!(l=m.binders[y])){L=m.binders;for(p in L)z=L[p],"*"!==p&&-1!==p.indexOf("*")&&(s=new RegExp("^"+p.replace("*",".+")+"$"),s.test(y)&&(l=z))}if(l||(l=m.binders["*"]),l.block){for(M=f.childNodes,C=0,G=M.length;G>C;C++)q=M[C],i.push(q);k=[j]}}for(N=k||f.attributes,D=0,H=N.length;H>D;D++)j=N[D],b.test(j.name)&&(y=j.name.replace(b,""),c("Binding",f,y,j.value))}for(O=f.childNodes,P=[],J=0,I=O.length;I>J;J++)n=O[J],P.push(h(n));return P}},l=this.els,j=0,k=l.length;k>j;j++)f=l[j],h(f)},b.prototype.select=function(a){var b,c,d,e,f;for(e=this.bindings,f=[],c=0,d=e.length;d>c;c++)b=e[c],a(b)&&f.push(b);return f},b.prototype.bind=function(){var a,b,c,d,e;for(d=this.bindings,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.bind());return e},b.prototype.unbind=function(){var a,b,c,d,e;for(d=this.bindings,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.unbind());return e},b.prototype.sync=function(){var a,b,c,d,e;for(d=this.bindings,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.sync());return e},b.prototype.publish=function(){var a,b,c,d,e;for(d=this.select(function(a){return a.binder.publishes}),e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.publish());return e},b.prototype.update=function(a){var b,c,d,e,f,g,h;null==a&&(a={});for(c in a)d=a[c],this.models[c]=d;for(g=this.bindings,h=[],e=0,f=g.length;f>e;e++)b=g[e],h.push(b.update(a));return h},b}(),a.TextTemplateParser=function(){function a(){}return a.types={text:0,binding:1},a.parse=function(a,b){var c,d,e,f,g,h,i;for(h=[],f=a.length,c=0,d=0;f>d;){if(c=a.indexOf(b[0],d),0>c){h.push({type:this.types.text,value:a.slice(d)});break}if(c>0&&c>d&&h.push({type:this.types.text,value:a.slice(d,c)}),d=c+2,c=a.indexOf(b[1],d),0>c){g=a.slice(d-2),e=h[h.length-1],(null!=e?e.type:void 0)===this.types.text?e.value+=g:h.push({type:this.types.text,value:g});break}i=a.slice(d,c).trim(),h.push({type:this.types.binding,value:i}),d=c+2}return h},a}(),a.Util={bindEvent:function(a,c,d){return null!=window.jQuery?(a=b(a),null!=a.on?a.on(c,d):a.bind(c,d)):null!=window.addEventListener?a.addEventListener(c,d,!1):(c="on"+c,a.attachEvent(c,d))},unbindEvent:function(a,c,d){return null!=window.jQuery?(a=b(a),null!=a.off?a.off(c,d):a.unbind(c,d)):null!=window.removeEventListener?a.removeEventListener(c,d,!1):(c="on"+c,a.detachEvent(c,d))},getInputValue:function(a){var c,d,e,f;if(null!=window.jQuery)switch(a=b(a),a[0].type){case"checkbox":return a.is(":checked");default:return a.val()}else switch(a.type){case"checkbox":return a.checked;case"select-multiple":for(f=[],d=0,e=a.length;e>d;d++)c=a[d],c.selected&&f.push(c.value);return f;default:return a.value}}},a.binders={enabled:function(a,b){return a.disabled=!b},disabled:function(a,b){return a.disabled=!!b},checked:{publishes:!0,bind:function(b){return a.Util.bindEvent(b,"change",this.publish)},unbind:function(b){return a.Util.unbindEvent(b,"change",this.publish)},routine:function(a,b){var c;return a.checked="radio"===a.type?(null!=(c=a.value)?c.toString():void 0)===(null!=b?b.toString():void 0):!!b}},unchecked:{publishes:!0,bind:function(b){return a.Util.bindEvent(b,"change",this.publish)},unbind:function(b){return a.Util.unbindEvent(b,"change",this.publish)},routine:function(a,b){var c;return a.checked="radio"===a.type?(null!=(c=a.value)?c.toString():void 0)!==(null!=b?b.toString():void 0):!b}},show:function(a,b){return a.style.display=b?"":"none"},hide:function(a,b){return a.style.display=b?"none":""},html:function(a,b){return a.innerHTML=null!=b?b:""},value:{publishes:!0,bind:function(b){return a.Util.bindEvent(b,"change",this.publish)},unbind:function(b){return a.Util.unbindEvent(b,"change",this.publish)},routine:function(a,c){var d,e,f,h,i,j,k;if(null!=window.jQuery){if(a=b(a),(null!=c?c.toString():void 0)!==(null!=(h=a.val())?h.toString():void 0))return a.val(null!=c?c:"")}else if("select-multiple"===a.type){if(null!=c){for(k=[],e=0,f=a.length;f>e;e++)d=a[e],k.push(d.selected=(i=d.value,g.call(c,i)>=0));return k}}else if((null!=c?c.toString():void 0)!==(null!=(j=a.value)?j.toString():void 0))return a.value=null!=c?c:""}},text:function(a,b){return null!=a.innerText?a.innerText=null!=b?b:"":a.textContent=null!=b?b:""},"if":{block:!0,bind:function(a){var b,c;return null==this.marker?(b=["data",this.view.config.prefix,this.type].join("-").replace("--","-"),c=a.getAttribute(b),this.marker=document.createComment(" rivets: "+this.type+" "+c+" "),a.removeAttribute(b),a.parentNode.insertBefore(this.marker,a),a.parentNode.removeChild(a)):void 0},unbind:function(){var a;return null!=(a=this.nested)?a.unbind():void 0},routine:function(b,c){var d,e,f,g,h;if(!!c==(null==this.nested)){if(c){f={},h=this.view.models;for(d in h)e=h[d],f[d]=e;return g={binders:this.view.options.binders,formatters:this.view.options.formatters,config:this.view.options.config},(this.nested=new a.View(b,f,g)).bind(),this.marker.parentNode.insertBefore(b,this.marker.nextSibling)}return b.parentNode.removeChild(b),this.nested.unbind(),delete this.nested}},update:function(a){var b;return null!=(b=this.nested)?b.update(a):void 0}},unless:{block:!0,bind:function(b){return a.binders["if"].bind.call(this,b)},unbind:function(){return a.binders["if"].unbind.call(this)},routine:function(b,c){return a.binders["if"].routine.call(this,b,!c)},update:function(b){return a.binders["if"].update.call(this,b)}},"on-*":{"function":!0,unbind:function(b){return this.handler?a.Util.unbindEvent(b,this.args[0],this.handler):void 0},routine:function(b,c){return this.handler&&a.Util.unbindEvent(b,this.args[0],this.handler),a.Util.bindEvent(b,this.args[0],this.handler=this.eventHandler(c))}},"each-*":{block:!0,bind:function(a){var b;return null==this.marker?(b=["data",this.view.config.prefix,this.type].join("-").replace("--","-"),this.marker=document.createComment(" rivets: "+this.type+" "),this.iterated=[],a.removeAttribute(b),a.parentNode.insertBefore(this.marker,a),a.parentNode.removeChild(a)):void 0},unbind:function(){var a,b,c,d,e;if(null!=this.iterated){for(d=this.iterated,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.unbind());return e}},routine:function(b,c){var d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w;if(j=this.args[0],c=c||[],this.iterated.length>c.length)for(t=Array(this.iterated.length-c.length),p=0,r=t.length;r>p;p++)e=t[p],o=this.iterated.pop(),o.unbind(),this.marker.parentNode.removeChild(o.els[0]);for(w=[],f=q=0,s=c.length;s>q;f=++q)if(i=c[f],d={},d[j]=i,null==this.iterated[f]){u=this.view.models;for(h in u)i=u[h],null==d[h]&&(d[h]=i);l=this.iterated.length?this.iterated[this.iterated.length-1].els[0]:this.marker,k={binders:this.view.options.binders,formatters:this.view.options.formatters,config:{}},v=this.view.options.config;for(g in v)n=v[g],k.config[g]=n;k.config.preloadData=!0,m=b.cloneNode(!0),o=new a.View(m,d,k),o.bind(),this.iterated.push(o),w.push(this.marker.parentNode.insertBefore(m,l.nextSibling))}else this.iterated[f].models[j]!==i?w.push(this.iterated[f].update(d)):w.push(void 0);return w},update:function(a){var b,c,d,e,f,g,h,i;b={};for(c in a)d=a[c],c!==this.args[0]&&(b[c]=d);for(h=this.iterated,i=[],f=0,g=h.length;g>f;f++)e=h[f],i.push(e.update(b));return i}},"class-*":function(a,b){var c;return c=" "+a.className+" ",!b==(-1!==c.indexOf(" "+this.args[0]+" "))?a.className=b?""+a.className+" "+this.args[0]:c.replace(" "+this.args[0]+" "," ").trim():void 0},"*":function(a,b){return b?a.setAttribute(this.type,b):a.removeAttribute(this.type)}},a.components={},a.config={preloadData:!0,handler:function(a,b,c){return this.call(a,b,c.view.models)}},a.formatters={},a.factory=function(b){return b._=a,b.binders=a.binders,b.components=a.components,b.formatters=a.formatters,b.config=a.config,b.configure=function(b){var c,d;null==b&&(b={});for(c in b)d=b[c],a.config[c]=d},b.bind=function(b,c,d){var e;return null==c&&(c={}),null==d&&(d={}),e=new a.View(b,c,d),e.bind(),e}},"object"==typeof exports?a.factory(exports):"function"==typeof define&&define.amd?define(["exports"],function(b){return a.factory(this.rivets=b),b}):a.factory(this.rivets={})}.call(this); \ No newline at end of file diff --git a/ajax/libs/rivets/0.5.2/rivets.js b/ajax/libs/rivets/0.5.2/rivets.js new file mode 100644 index 000000000..769ee9e15 --- /dev/null +++ b/ajax/libs/rivets/0.5.2/rivets.js @@ -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); diff --git a/ajax/libs/rivets/0.5.2/rivets.min.js b/ajax/libs/rivets/0.5.2/rivets.min.js new file mode 100644 index 000000000..daf720908 --- /dev/null +++ b/ajax/libs/rivets/0.5.2/rivets.min.js @@ -0,0 +1,5 @@ +// Rivets.js +// version: 0.5.2 +// author: Michael Richards +// license: MIT +(function(){var t,i=function(t,i){return function(){return t.apply(i,arguments)}},e=[].slice,n=[].indexOf||function(t){for(var i=0,e=this.length;e>i;i++)if(i in this&&this[i]===t)return i;return-1};t={},String.prototype.trim||(String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g,"")}),t.Binding=function(){function n(t,e,n,s,r,o){var h,u,l,a;if(this.view=t,this.el=e,this.type=n,this.key=s,this.keypath=r,this.options=null!=o?o:{},this.update=i(this.update,this),this.unbind=i(this.unbind,this),this.bind=i(this.bind,this),this.publish=i(this.publish,this),this.sync=i(this.sync,this),this.set=i(this.set,this),this.formattedValue=i(this.formattedValue,this),!(this.binder=this.view.binders[n])){a=this.view.binders;for(h in a)l=a[h],"*"!==h&&-1!==h.indexOf("*")&&(u=RegExp("^"+h.replace("*",".+")+"$"),u.test(n)&&(this.binder=l,this.args=RegExp("^"+h.replace("*","(.+)")+"$").exec(n),this.args.shift()))}this.binder||(this.binder=this.view.binders["*"]),this.binder instanceof Function&&(this.binder={routine:this.binder}),this.formatters=this.options.formatters||[],this.model=this.view.models[this.key]}return n.prototype.formattedValue=function(t){var i,n,s,r,o,h;for(h=this.formatters,r=0,o=h.length;o>r;r++)n=h[r],i=n.split(/\s+/),s=i.shift(),n=this.model[s]instanceof Function?this.model[s]:this.view.formatters[s],(null!=n?n.read:void 0)instanceof Function?t=n.read.apply(n,[t].concat(e.call(i))):n instanceof Function&&(t=n.apply(null,[t].concat(e.call(i))));return t},n.prototype.set=function(t){var i;return t=t instanceof Function&&!this.binder["function"]?this.formattedValue(t.call(this.model)):this.formattedValue(t),null!=(i=this.binder.routine)?i.call(this,this.el,t):void 0},n.prototype.sync=function(){return this.set(this.options.bypass?this.model[this.keypath]:this.view.config.adapter.read(this.model,this.keypath))},n.prototype.publish=function(){var i,n,s,r,o,h,u,l,a;for(r=t.Util.getInputValue(this.el),u=this.formatters.slice(0).reverse(),o=0,h=u.length;h>o;o++)n=u[o],i=n.split(/\s+/),s=i.shift(),(null!=(l=this.view.formatters[s])?l.publish:void 0)&&(r=(a=this.view.formatters[s]).publish.apply(a,[r].concat(e.call(i))));return this.view.config.adapter.publish(this.model,this.keypath,r)},n.prototype.bind=function(){var t,i,e,n,s,r,o,h,u;if(null!=(r=this.binder.bind)&&r.call(this,this.el),this.options.bypass?this.sync():(this.view.config.adapter.subscribe(this.model,this.keypath,this.sync),this.view.config.preloadData&&this.sync()),null!=(o=this.options.dependencies)?o.length:void 0){for(h=this.options.dependencies,u=[],n=0,s=h.length;s>n;n++)t=h[n],/^\./.test(t)?(e=this.model,i=t.substr(1)):(t=t.split("."),e=this.view.models[t.shift()],i=t.join(".")),u.push(this.view.config.adapter.subscribe(e,i,this.sync));return u}},n.prototype.unbind=function(){var t,i,e,n,s,r,o,h,u;if(null!=(r=this.binder.unbind)&&r.call(this,this.el),this.options.bypass||this.view.config.adapter.unsubscribe(this.model,this.keypath,this.sync),null!=(o=this.options.dependencies)?o.length:void 0){for(h=this.options.dependencies,u=[],n=0,s=h.length;s>n;n++)t=h[n],/^\./.test(t)?(e=this.model,i=t.substr(1)):(t=t.split("."),e=this.view.models[t.shift()],i=t.join(".")),u.push(this.view.config.adapter.unsubscribe(e,i,this.sync));return u}},n.prototype.update=function(){return this.unbind(),this.model=this.view.models[this.key],this.bind()},n}(),t.View=function(){function e(e,n,s){var r,o,h,u,l,a,d,c,f,p;for(this.els=e,this.models=n,this.options=null!=s?s:{},this.update=i(this.update,this),this.publish=i(this.publish,this),this.sync=i(this.sync,this),this.unbind=i(this.unbind,this),this.bind=i(this.bind,this),this.select=i(this.select,this),this.build=i(this.build,this),this.bindingRegExp=i(this.bindingRegExp,this),this.els.jquery||this.els instanceof Array||(this.els=[this.els]),d=["config","binders","formatters"],l=0,a=d.length;a>l;l++){if(o=d[l],this[o]={},this.options[o]){c=this.options[o];for(r in c)h=c[r],this[o][r]=h}f=t[o];for(r in f)h=f[r],null==(p=(u=this[o])[r])&&(u[r]=h)}this.build()}return e.prototype.bindingRegExp=function(){var t;return t=this.config.prefix,t?RegExp("^data-"+t+"-"):/^data-/},e.prototype.build=function(){var i,e,s,r,o,h,u,l,a,d,c,f=this;for(this.bindings=[],o=[],i=this.bindingRegExp(),r=function(e){var s,r,h,u,l,a,d,c,p,b,v,g,y,m,w,x,E,k,j,L,N,U,V,Q,R,B,F,O;if(0>n.call(o,e)){for(R=e.attributes,j=0,U=R.length;U>j;j++)if(s=R[j],i.test(s.name)){if(E=s.name.replace(i,""),!(h=f.binders[E])){B=f.binders;for(d in B)k=B[d],"*"!==d&&-1!==d.indexOf("*")&&(w=RegExp("^"+d.replace("*",".+")+"$"),w.test(E)&&(h=k))}if(h||(h=f.binders["*"]),h.block){for(F=e.getElementsByTagName("*"),L=0,V=F.length;V>L;L++)b=F[L],o.push(b);r=[s]}}for(O=r||e.attributes,N=0,Q=O.length;Q>N;N++)s=O[N],i.test(s.name)&&(v={},E=s.name.replace(i,""),m=function(){var t,i,e,n;for(e=s.value.split("|"),n=[],t=0,i=e.length;i>t;t++)y=e[t],n.push(y.trim());return n}(),u=function(){var t,i,e,n;for(e=m.shift().split("<"),n=[],t=0,i=e.length;i>t;t++)l=e[t],n.push(l.trim());return n}(),g=u.shift(),x=g.split(/\.|:/),v.formatters=m,v.bypass=-1!==g.indexOf(":"),x[0]?c=x.shift():(c=null,x.shift()),p=x.join("."),null!=f.models[c]&&((a=u.shift())&&(v.dependencies=a.split(/\s+/)),f.bindings.push(new t.Binding(f,e,E,c,p,v))));r&&(r=null)}},d=this.els,h=0,l=d.length;l>h;h++)for(e=d[h],r(e),c=e.getElementsByTagName("*"),u=0,a=c.length;a>u;u++)s=c[u],null!=s.attributes&&r(s)},e.prototype.select=function(t){var i,e,n,s,r;for(s=this.bindings,r=[],e=0,n=s.length;n>e;e++)i=s[e],t(i)&&r.push(i);return r},e.prototype.bind=function(){var t,i,e,n,s;for(n=this.bindings,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.bind());return s},e.prototype.unbind=function(){var t,i,e,n,s;for(n=this.bindings,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.unbind());return s},e.prototype.sync=function(){var t,i,e,n,s;for(n=this.bindings,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.sync());return s},e.prototype.publish=function(){var t,i,e,n,s;for(n=this.select(function(t){return t.binder.publishes}),s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.publish());return s},e.prototype.update=function(t){var i,e,n,s;null==t&&(t={}),s=[];for(e in t)n=t[e],this.models[e]=n,s.push(function(){var t,n,s,r;for(s=this.select(function(t){return t.key===e}),r=[],t=0,n=s.length;n>t;t++)i=s[t],r.push(i.update());return r}.call(this));return s},e}(),t.Util={bindEvent:function(t,i,e,n){var s;return s=function(t){return e.call(this,t,n)},null!=window.jQuery?(t=jQuery(t),null!=t.on?t.on(i,s):t.bind(i,s)):null!=window.addEventListener?t.addEventListener(i,s,!1):(i="on"+i,t.attachEvent(i,s)),s},unbindEvent:function(t,i,e){return null!=window.jQuery?(t=jQuery(t),null!=t.off?t.off(i,e):t.unbind(i,e)):window.removeEventListener?t.removeEventListener(i,e,!1):(i="on"+i,t.detachEvent(i,e))},getInputValue:function(t){var i,e,n,s;if(null!=window.jQuery)switch(t=jQuery(t),t[0].type){case"checkbox":return t.is(":checked");default:return t.val()}else switch(t.type){case"checkbox":return t.checked;case"select-multiple":for(s=[],e=0,n=t.length;n>e;e++)i=t[e],i.selected&&s.push(i.value);return s;default:return t.value}}},t.binders={enabled:function(t,i){return t.disabled=!i},disabled:function(t,i){return t.disabled=!!i},checked:{publishes:!0,bind:function(i){return this.currentListener=t.Util.bindEvent(i,"change",this.publish)},unbind:function(i){return t.Util.unbindEvent(i,"change",this.currentListener)},routine:function(t,i){var e;return t.checked="radio"===t.type?(null!=(e=t.value)?""+e:void 0)===(null!=i?""+i:void 0):!!i}},unchecked:{publishes:!0,bind:function(i){return this.currentListener=t.Util.bindEvent(i,"change",this.publish)},unbind:function(i){return t.Util.unbindEvent(i,"change",this.currentListener)},routine:function(t,i){var e;return t.checked="radio"===t.type?(null!=(e=t.value)?""+e:void 0)!==(null!=i?""+i:void 0):!i}},show:function(t,i){return t.style.display=i?"":"none"},hide:function(t,i){return t.style.display=i?"none":""},html:function(t,i){return t.innerHTML=null!=i?i:""},value:{publishes:!0,bind:function(i){return this.currentListener=t.Util.bindEvent(i,"change",this.publish)},unbind:function(i){return t.Util.unbindEvent(i,"change",this.currentListener)},routine:function(t,i){var e,s,r,o,h,u,l;if(null!=window.jQuery){if(t=jQuery(t),(null!=i?""+i:void 0)!==(null!=(o=t.val())?""+o:void 0))return t.val(null!=i?i:"")}else if("select-multiple"===t.type){if(null!=i){for(l=[],s=0,r=t.length;r>s;s++)e=t[s],l.push(e.selected=(h=e.value,n.call(i,h)>=0));return l}}else if((null!=i?""+i:void 0)!==(null!=(u=t.value)?""+u:void 0))return t.value=null!=i?i:""}},text:function(t,i){return null!=t.innerText?t.innerText=null!=i?i:"":t.textContent=null!=i?i:""},"on-*":{"function":!0,routine:function(i,e){return this.currentListener&&t.Util.unbindEvent(i,this.args[0],this.currentListener),this.currentListener=t.Util.bindEvent(i,this.args[0],e,this.view)}},"each-*":{block:!0,bind:function(t){return t.removeAttribute(["data",this.view.config.prefix,this.type].join("-").replace("--","-"))},unbind:function(){var t,i,e,n,s;if(null!=this.iterated){for(n=this.iterated,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.unbind());return s}},routine:function(i,e){var n,s,r,o,h,u,l,a,d,c,f,p,b,v,g,y,m,w,x,E,k,j,L;if(null!=this.iterated)for(w=this.iterated,p=0,g=w.length;g>p;p++)for(f=w[p],f.unbind(),x=f.els,b=0,y=x.length;y>b;b++)s=x[b],s.parentNode.removeChild(s);else this.marker=document.createComment(" rivets: "+this.type+" "),i.parentNode.insertBefore(this.marker,i),i.parentNode.removeChild(i);if(this.iterated=[],e){for(L=[],v=0,m=e.length;m>v;v++){r=e[v],n={},E=this.view.models;for(l in E)u=E[l],n[l]=u;if(n[this.args[0]]=r,o=i.cloneNode(!0),d=this.iterated.length?this.iterated[this.iterated.length-1].els[0]:this.marker,this.marker.parentNode.insertBefore(o,null!=(k=d.nextSibling)?k:null),a={binders:this.view.options.binders,formatters:this.view.options.binders,config:{}},this.view.options.config){j=this.view.options.config;for(h in j)c=j[h],a.config[h]=c}a.config.preloadData=!0,f=new t.View(o,n,a),f.bind(),L.push(this.iterated.push(f))}return L}}},"class-*":function(t,i){var e;return e=" "+t.className+" ",!i==(-1!==e.indexOf(" "+this.args[0]+" "))?t.className=i?""+t.className+" "+this.args[0]:e.replace(" "+this.args[0]+" "," ").trim():void 0},"*":function(t,i){return i?t.setAttribute(this.type,i):t.removeAttribute(this.type)}},t.config={preloadData:!0},t.formatters={},t.factory=function(i){return i.binders=t.binders,i.formatters=t.formatters,i.config=t.config,i.configure=function(i){var e,n;null==i&&(i={});for(e in i)n=i[e],t.config[e]=n},i.bind=function(i,e,n){var s;return null==e&&(e={}),null==n&&(n={}),s=new t.View(i,e,n),s.bind(),s}},"object"==typeof exports?t.factory(exports):"function"==typeof define&&define.amd?define(["exports"],function(i){return t.factory(this.rivets=i),i}):t.factory(this.rivets={})}).call(this); \ No newline at end of file diff --git a/ajax/libs/rivets/0.5.3/rivets.js b/ajax/libs/rivets/0.5.3/rivets.js new file mode 100644 index 000000000..2647e142d --- /dev/null +++ b/ajax/libs/rivets/0.5.3/rivets.js @@ -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); diff --git a/ajax/libs/rivets/0.5.3/rivets.min.js b/ajax/libs/rivets/0.5.3/rivets.min.js new file mode 100644 index 000000000..d2f85a15e --- /dev/null +++ b/ajax/libs/rivets/0.5.3/rivets.min.js @@ -0,0 +1,5 @@ +// Rivets.js +// version: 0.5.3 +// author: Michael Richards +// license: MIT +(function(){var t,i=function(t,i){return function(){return t.apply(i,arguments)}},e=[].slice,n=[].indexOf||function(t){for(var i=0,e=this.length;e>i;i++)if(i in this&&this[i]===t)return i;return-1};t={},String.prototype.trim||(String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g,"")}),t.Binding=function(){function n(t,e,n,s,r,h){var o,u,l,a;if(this.view=t,this.el=e,this.type=n,this.key=s,this.keypath=r,this.options=null!=h?h:{},this.update=i(this.update,this),this.unbind=i(this.unbind,this),this.bind=i(this.bind,this),this.publish=i(this.publish,this),this.sync=i(this.sync,this),this.set=i(this.set,this),this.formattedValue=i(this.formattedValue,this),!(this.binder=this.view.binders[n])){a=this.view.binders;for(o in a)l=a[o],"*"!==o&&-1!==o.indexOf("*")&&(u=RegExp("^"+o.replace("*",".+")+"$"),u.test(n)&&(this.binder=l,this.args=RegExp("^"+o.replace("*","(.+)")+"$").exec(n),this.args.shift()))}this.binder||(this.binder=this.view.binders["*"]),this.binder instanceof Function&&(this.binder={routine:this.binder}),this.formatters=this.options.formatters||[],this.model=this.view.models[this.key]}return n.prototype.formattedValue=function(t){var i,n,s,r,h,o;for(o=this.formatters,r=0,h=o.length;h>r;r++)n=o[r],i=n.split(/\s+/),s=i.shift(),n=this.model[s]instanceof Function?this.model[s]:this.view.formatters[s],(null!=n?n.read:void 0)instanceof Function?t=n.read.apply(n,[t].concat(e.call(i))):n instanceof Function&&(t=n.apply(null,[t].concat(e.call(i))));return t},n.prototype.set=function(t){var i;return t=t instanceof Function&&!this.binder["function"]?this.formattedValue(t.call(this.model)):this.formattedValue(t),null!=(i=this.binder.routine)?i.call(this,this.el,t):void 0},n.prototype.sync=function(){return this.set(this.options.bypass?this.model[this.keypath]:this.view.config.adapter.read(this.model,this.keypath))},n.prototype.publish=function(){var i,n,s,r,h,o,u,l,a;for(r=t.Util.getInputValue(this.el),u=this.formatters.slice(0).reverse(),h=0,o=u.length;o>h;h++)n=u[h],i=n.split(/\s+/),s=i.shift(),(null!=(l=this.view.formatters[s])?l.publish:void 0)&&(r=(a=this.view.formatters[s]).publish.apply(a,[r].concat(e.call(i))));return this.view.config.adapter.publish(this.model,this.keypath,r)},n.prototype.bind=function(){var t,i,e,n,s,r,h,o,u;if(null!=(r=this.binder.bind)&&r.call(this,this.el),this.options.bypass?this.sync():(this.view.config.adapter.subscribe(this.model,this.keypath,this.sync),this.view.config.preloadData&&this.sync()),null!=(h=this.options.dependencies)?h.length:void 0){for(o=this.options.dependencies,u=[],n=0,s=o.length;s>n;n++)t=o[n],/^\./.test(t)?(e=this.model,i=t.substr(1)):(t=t.split("."),e=this.view.models[t.shift()],i=t.join(".")),u.push(this.view.config.adapter.subscribe(e,i,this.sync));return u}},n.prototype.unbind=function(){var t,i,e,n,s,r,h,o,u;if(null!=(r=this.binder.unbind)&&r.call(this,this.el),this.options.bypass||this.view.config.adapter.unsubscribe(this.model,this.keypath,this.sync),null!=(h=this.options.dependencies)?h.length:void 0){for(o=this.options.dependencies,u=[],n=0,s=o.length;s>n;n++)t=o[n],/^\./.test(t)?(e=this.model,i=t.substr(1)):(t=t.split("."),e=this.view.models[t.shift()],i=t.join(".")),u.push(this.view.config.adapter.unsubscribe(e,i,this.sync));return u}},n.prototype.update=function(){return this.unbind(),this.model=this.view.models[this.key],this.bind()},n}(),t.View=function(){function e(e,n,s){var r,h,o,u,l,a,d,c,f,p;for(this.els=e,this.models=n,this.options=null!=s?s:{},this.update=i(this.update,this),this.publish=i(this.publish,this),this.sync=i(this.sync,this),this.unbind=i(this.unbind,this),this.bind=i(this.bind,this),this.select=i(this.select,this),this.build=i(this.build,this),this.bindingRegExp=i(this.bindingRegExp,this),this.els.jquery||this.els instanceof Array||(this.els=[this.els]),d=["config","binders","formatters"],l=0,a=d.length;a>l;l++){if(h=d[l],this[h]={},this.options[h]){c=this.options[h];for(r in c)o=c[r],this[h][r]=o}f=t[h];for(r in f)o=f[r],null==(p=(u=this[h])[r])&&(u[r]=o)}this.build()}return e.prototype.bindingRegExp=function(){var t;return t=this.config.prefix,t?RegExp("^data-"+t+"-"):/^data-/},e.prototype.build=function(){var i,e,s,r,h,o,u,l,a,d,c,f=this;for(this.bindings=[],h=[],i=this.bindingRegExp(),r=function(e){var s,r,o,u,l,a,d,c,p,b,v,g,y,m,w,x,E,k,j,L,N,U,V,Q,R,B,A,F;if(0>n.call(h,e)){for(R=e.attributes,j=0,U=R.length;U>j;j++)if(s=R[j],i.test(s.name)){if(E=s.name.replace(i,""),!(o=f.binders[E])){B=f.binders;for(d in B)k=B[d],"*"!==d&&-1!==d.indexOf("*")&&(w=RegExp("^"+d.replace("*",".+")+"$"),w.test(E)&&(o=k))}if(o||(o=f.binders["*"]),o.block){for(A=e.getElementsByTagName("*"),L=0,V=A.length;V>L;L++)b=A[L],h.push(b);r=[s]}}for(F=r||e.attributes,N=0,Q=F.length;Q>N;N++)s=F[N],i.test(s.name)&&(v={},E=s.name.replace(i,""),m=function(){var t,i,e,n;for(e=s.value.split("|"),n=[],t=0,i=e.length;i>t;t++)y=e[t],n.push(y.trim());return n}(),u=function(){var t,i,e,n;for(e=m.shift().split("<"),n=[],t=0,i=e.length;i>t;t++)l=e[t],n.push(l.trim());return n}(),g=u.shift(),x=g.split(/\.|:/),v.formatters=m,v.bypass=-1!==g.indexOf(":"),x[0]?c=x.shift():(c=null,x.shift()),p=x.join("."),null!=f.models[c]&&((a=u.shift())&&(v.dependencies=a.split(/\s+/)),f.bindings.push(new t.Binding(f,e,E,c,p,v))));r&&(r=null)}},d=this.els,o=0,l=d.length;l>o;o++)for(e=d[o],r(e),c=e.getElementsByTagName("*"),u=0,a=c.length;a>u;u++)s=c[u],null!=s.attributes&&r(s)},e.prototype.select=function(t){var i,e,n,s,r;for(s=this.bindings,r=[],e=0,n=s.length;n>e;e++)i=s[e],t(i)&&r.push(i);return r},e.prototype.bind=function(){var t,i,e,n,s;for(n=this.bindings,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.bind());return s},e.prototype.unbind=function(){var t,i,e,n,s;for(n=this.bindings,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.unbind());return s},e.prototype.sync=function(){var t,i,e,n,s;for(n=this.bindings,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.sync());return s},e.prototype.publish=function(){var t,i,e,n,s;for(n=this.select(function(t){return t.binder.publishes}),s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.publish());return s},e.prototype.update=function(t){var i,e,n,s;null==t&&(t={}),s=[];for(e in t)n=t[e],this.models[e]=n,s.push(function(){var t,n,s,r;for(s=this.select(function(t){return t.key===e}),r=[],t=0,n=s.length;n>t;t++)i=s[t],r.push(i.update());return r}.call(this));return s},e}(),t.Util={bindEvent:function(t,i,e,n){var s;return s=function(t){return e.call(this,t,n)},null!=window.jQuery?(t=jQuery(t),null!=t.on?t.on(i,s):t.bind(i,s)):null!=window.addEventListener?t.addEventListener(i,s,!1):(i="on"+i,t.attachEvent(i,s)),s},unbindEvent:function(t,i,e){return null!=window.jQuery?(t=jQuery(t),null!=t.off?t.off(i,e):t.unbind(i,e)):window.removeEventListener?t.removeEventListener(i,e,!1):(i="on"+i,t.detachEvent(i,e))},getInputValue:function(t){var i,e,n,s;if(null!=window.jQuery)switch(t=jQuery(t),t[0].type){case"checkbox":return t.is(":checked");default:return t.val()}else switch(t.type){case"checkbox":return t.checked;case"select-multiple":for(s=[],e=0,n=t.length;n>e;e++)i=t[e],i.selected&&s.push(i.value);return s;default:return t.value}}},t.binders={enabled:function(t,i){return t.disabled=!i},disabled:function(t,i){return t.disabled=!!i},checked:{publishes:!0,bind:function(i){return this.currentListener=t.Util.bindEvent(i,"change",this.publish)},unbind:function(i){return t.Util.unbindEvent(i,"change",this.currentListener)},routine:function(t,i){var e;return t.checked="radio"===t.type?(null!=(e=t.value)?""+e:void 0)===(null!=i?""+i:void 0):!!i}},unchecked:{publishes:!0,bind:function(i){return this.currentListener=t.Util.bindEvent(i,"change",this.publish)},unbind:function(i){return t.Util.unbindEvent(i,"change",this.currentListener)},routine:function(t,i){var e;return t.checked="radio"===t.type?(null!=(e=t.value)?""+e:void 0)!==(null!=i?""+i:void 0):!i}},show:function(t,i){return t.style.display=i?"":"none"},hide:function(t,i){return t.style.display=i?"none":""},html:function(t,i){return t.innerHTML=null!=i?i:""},value:{publishes:!0,bind:function(i){return this.currentListener=t.Util.bindEvent(i,"change",this.publish)},unbind:function(i){return t.Util.unbindEvent(i,"change",this.currentListener)},routine:function(t,i){var e,s,r,h,o,u,l;if(null!=window.jQuery){if(t=jQuery(t),(null!=i?""+i:void 0)!==(null!=(h=t.val())?""+h:void 0))return t.val(null!=i?i:"")}else if("select-multiple"===t.type){if(null!=i){for(l=[],s=0,r=t.length;r>s;s++)e=t[s],l.push(e.selected=(o=e.value,n.call(i,o)>=0));return l}}else if((null!=i?""+i:void 0)!==(null!=(u=t.value)?""+u:void 0))return t.value=null!=i?i:""}},text:function(t,i){return null!=t.innerText?t.innerText=null!=i?i:"":t.textContent=null!=i?i:""},"on-*":{"function":!0,routine:function(i,e){return this.currentListener&&t.Util.unbindEvent(i,this.args[0],this.currentListener),this.currentListener=t.Util.bindEvent(i,this.args[0],e,this.view)}},"each-*":{block:!0,bind:function(t){var i;return i=["data",this.view.config.prefix,this.type].join("-").replace("--","-"),this.marker=document.createComment(" rivets: "+this.type+" "),this.iterated=[],t.removeAttribute(i),t.parentNode.insertBefore(this.marker,t),t.parentNode.removeChild(t)},unbind:function(){var t,i,e,n,s;if(null!=this.iterated){for(n=this.iterated,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.unbind());return s}},routine:function(t,i){var e,n,s,r,h,o,u,l,a,d,c,f,p,b,v,g,y,m,w,x;if(u=this.args[0],i=i||[],this.iterated.length>i.length)for(y=Array(this.iterated.length-i.length),p=0,v=y.length;v>p;p++)n=y[p],f=this.iterated.pop(),f.unbind(),this.marker.parentNode.removeChild(f.els[0]);for(x=[],s=b=0,g=i.length;g>b;s=++b)if(o=i[s],e={},e[u]=o,null==this.iterated[s]){m=this.view.models;for(h in m)o=m[h],e[h]=o;a=this.iterated.length?this.iterated[this.iterated.length-1].els[0]:this.marker,l={binders:this.view.options.binders,formatters:this.view.options.formatters,config:{}},w=this.view.options.config;for(r in w)c=w[r],l.config[r]=c;l.config.preloadData=!0,d=t.cloneNode(!0),this.iterated.push(rivets.bind(d,e,l)),x.push(this.marker.parentNode.insertBefore(d,a.nextSibling))}else this.iterated[s].models[u]!==o?x.push(this.iterated[s].update(e)):x.push(void 0);return x}},"class-*":function(t,i){var e;return e=" "+t.className+" ",!i==(-1!==e.indexOf(" "+this.args[0]+" "))?t.className=i?""+t.className+" "+this.args[0]:e.replace(" "+this.args[0]+" "," ").trim():void 0},"*":function(t,i){return i?t.setAttribute(this.type,i):t.removeAttribute(this.type)}},t.config={preloadData:!0},t.formatters={},t.factory=function(i){return i.binders=t.binders,i.formatters=t.formatters,i.config=t.config,i.configure=function(i){var e,n;null==i&&(i={});for(e in i)n=i[e],t.config[e]=n},i.bind=function(i,e,n){var s;return null==e&&(e={}),null==n&&(n={}),s=new t.View(i,e,n),s.bind(),s}},"object"==typeof exports?t.factory(exports):"function"==typeof define&&define.amd?define(["exports"],function(i){return t.factory(this.rivets=i),i}):t.factory(this.rivets={})}).call(this); \ No newline at end of file diff --git a/ajax/libs/rivets/0.5.4/rivets.js b/ajax/libs/rivets/0.5.4/rivets.js new file mode 100644 index 000000000..64cb3aa88 --- /dev/null +++ b/ajax/libs/rivets/0.5.4/rivets.js @@ -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); diff --git a/ajax/libs/rivets/0.5.4/rivets.min.js b/ajax/libs/rivets/0.5.4/rivets.min.js new file mode 100644 index 000000000..70535048a --- /dev/null +++ b/ajax/libs/rivets/0.5.4/rivets.min.js @@ -0,0 +1,5 @@ +// Rivets.js +// version: 0.5.4 +// author: Michael Richards +// license: MIT +(function(){var t,i=function(t,i){return function(){return t.apply(i,arguments)}},e=[].slice,n=[].indexOf||function(t){for(var i=0,e=this.length;e>i;i++)if(i in this&&this[i]===t)return i;return-1};t={},String.prototype.trim||(String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g,"")}),t.Binding=function(){function n(t,e,n,s,r,h){var o,l,u,a;if(this.view=t,this.el=e,this.type=n,this.key=s,this.keypath=r,this.options=null!=h?h:{},this.update=i(this.update,this),this.unbind=i(this.unbind,this),this.bind=i(this.bind,this),this.publish=i(this.publish,this),this.sync=i(this.sync,this),this.set=i(this.set,this),this.eventHandler=i(this.eventHandler,this),this.formattedValue=i(this.formattedValue,this),!(this.binder=this.view.binders[n])){a=this.view.binders;for(o in a)u=a[o],"*"!==o&&-1!==o.indexOf("*")&&(l=RegExp("^"+o.replace("*",".+")+"$"),l.test(n)&&(this.binder=u,this.args=RegExp("^"+o.replace("*","(.+)")+"$").exec(n),this.args.shift()))}this.binder||(this.binder=this.view.binders["*"]),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}return n.prototype.formattedValue=function(t){var i,n,s,r,h,o;for(o=this.formatters,r=0,h=o.length;h>r;r++)n=o[r],i=n.split(/\s+/),s=i.shift(),n=this.model[s]instanceof Function?this.model[s]:this.view.formatters[s],(null!=n?n.read:void 0)instanceof Function?t=n.read.apply(n,[t].concat(e.call(i))):n instanceof Function&&(t=n.apply(null,[t].concat(e.call(i))));return t},n.prototype.eventHandler=function(t){var i,e;return e=(i=this).view.config.handler,function(n){return e.call(t,this,n,i)}},n.prototype.set=function(t){var i;return t=t instanceof Function&&!this.binder["function"]?this.formattedValue(t.call(this.model)):this.formattedValue(t),null!=(i=this.binder.routine)?i.call(this,this.el,t):void 0},n.prototype.sync=function(){return this.set(this.options.bypass?this.model[this.keypath]:this.view.config.adapter.read(this.model,this.keypath))},n.prototype.publish=function(){var i,n,s,r,h,o,l,u,a;for(r=t.Util.getInputValue(this.el),l=this.formatters.slice(0).reverse(),h=0,o=l.length;o>h;h++)n=l[h],i=n.split(/\s+/),s=i.shift(),(null!=(u=this.view.formatters[s])?u.publish:void 0)&&(r=(a=this.view.formatters[s]).publish.apply(a,[r].concat(e.call(i))));return this.view.config.adapter.publish(this.model,this.keypath,r)},n.prototype.bind=function(){var t,i,e,n,s,r,h,o,l;if(null!=(r=this.binder.bind)&&r.call(this,this.el),this.options.bypass?this.sync():(this.view.config.adapter.subscribe(this.model,this.keypath,this.sync),this.view.config.preloadData&&this.sync()),null!=(h=this.options.dependencies)?h.length:void 0){for(o=this.options.dependencies,l=[],n=0,s=o.length;s>n;n++)t=o[n],/^\./.test(t)?(e=this.model,i=t.substr(1)):(t=t.split("."),e=this.view.models[t.shift()],i=t.join(".")),l.push(this.view.config.adapter.subscribe(e,i,this.sync));return l}},n.prototype.unbind=function(){var t,i,e,n,s,r,h,o,l;if(null!=(r=this.binder.unbind)&&r.call(this,this.el),this.options.bypass||this.view.config.adapter.unsubscribe(this.model,this.keypath,this.sync),null!=(h=this.options.dependencies)?h.length:void 0){for(o=this.options.dependencies,l=[],n=0,s=o.length;s>n;n++)t=o[n],/^\./.test(t)?(e=this.model,i=t.substr(1)):(t=t.split("."),e=this.view.models[t.shift()],i=t.join(".")),l.push(this.view.config.adapter.unsubscribe(e,i,this.sync));return l}},n.prototype.update=function(){return this.unbind(),this.model=this.key?this.view.models[this.key]:this.view.models,this.bind()},n}(),t.View=function(){function e(e,n,s){var r,h,o,l,u,a,d,c,f,p;for(this.els=e,this.models=n,this.options=null!=s?s:{},this.update=i(this.update,this),this.publish=i(this.publish,this),this.sync=i(this.sync,this),this.unbind=i(this.unbind,this),this.bind=i(this.bind,this),this.select=i(this.select,this),this.build=i(this.build,this),this.bindingRegExp=i(this.bindingRegExp,this),this.els.jquery||this.els instanceof Array||(this.els=[this.els]),d=["config","binders","formatters"],u=0,a=d.length;a>u;u++){if(h=d[u],this[h]={},this.options[h]){c=this.options[h];for(r in c)o=c[r],this[h][r]=o}f=t[h];for(r in f)o=f[r],null==(p=(l=this[h])[r])&&(l[r]=o)}this.build()}return e.prototype.bindingRegExp=function(){var t;return t=this.config.prefix,t?RegExp("^data-"+t+"-"):/^data-/},e.prototype.build=function(){var i,e,s,r,h,o,l,u,a,d,c,f=this;for(this.bindings=[],h=[],i=this.bindingRegExp(),r=function(e){var s,r,o,l,u,a,d,c,p,b,v,g,y,m,w,k,E,x,j,U,N,V,Q,R,B,A,F,H;if(0>n.call(h,e)){for(B=e.attributes,j=0,V=B.length;V>j;j++)if(s=B[j],i.test(s.name)){if(E=s.name.replace(i,""),!(o=f.binders[E])){A=f.binders;for(d in A)x=A[d],"*"!==d&&-1!==d.indexOf("*")&&(w=RegExp("^"+d.replace("*",".+")+"$"),w.test(E)&&(o=x))}if(o||(o=f.binders["*"]),o.block){for(F=e.getElementsByTagName("*"),U=0,Q=F.length;Q>U;U++)b=F[U],h.push(b);r=[s]}}for(H=r||e.attributes,N=0,R=H.length;R>N;N++)s=H[N],i.test(s.name)&&(v={},E=s.name.replace(i,""),m=function(){var t,i,e,n;for(e=s.value.split("|"),n=[],t=0,i=e.length;i>t;t++)y=e[t],n.push(y.trim());return n}(),l=function(){var t,i,e,n;for(e=m.shift().split("<"),n=[],t=0,i=e.length;i>t;t++)u=e[t],n.push(u.trim());return n}(),g=l.shift(),k=g.split(/\.|:/),v.formatters=m,v.bypass=-1!==g.indexOf(":"),k[0]?c=k.shift():(c=null,k.shift()),p=k.join("."),c&&null==f.models[c]||((a=l.shift())&&(v.dependencies=a.split(/\s+/)),f.bindings.push(new t.Binding(f,e,E,c,p,v))));r&&(r=null)}},d=this.els,o=0,u=d.length;u>o;o++)for(e=d[o],r(e),c=e.getElementsByTagName("*"),l=0,a=c.length;a>l;l++)s=c[l],null!=s.attributes&&r(s)},e.prototype.select=function(t){var i,e,n,s,r;for(s=this.bindings,r=[],e=0,n=s.length;n>e;e++)i=s[e],t(i)&&r.push(i);return r},e.prototype.bind=function(){var t,i,e,n,s;for(n=this.bindings,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.bind());return s},e.prototype.unbind=function(){var t,i,e,n,s;for(n=this.bindings,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.unbind());return s},e.prototype.sync=function(){var t,i,e,n,s;for(n=this.bindings,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.sync());return s},e.prototype.publish=function(){var t,i,e,n,s;for(n=this.select(function(t){return t.binder.publishes}),s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.publish());return s},e.prototype.update=function(t){var i,e,n,s;null==t&&(t={}),s=[];for(e in t)n=t[e],this.models[e]=n,s.push(function(){var t,n,s,r;for(s=this.select(function(t){return t.key===e}),r=[],t=0,n=s.length;n>t;t++)i=s[t],r.push(i.update());return r}.call(this));return s},e}(),t.Util={bindEvent:function(t,i,e){return null!=window.jQuery?(t=jQuery(t),null!=t.on?t.on(i,e):t.bind(i,e)):null!=window.addEventListener?t.addEventListener(i,e,!1):(i="on"+i,t.attachEvent(i,e))},unbindEvent:function(t,i,e){return null!=window.jQuery?(t=jQuery(t),null!=t.off?t.off(i,e):t.unbind(i,e)):null!=window.removeEventListener?t.removeEventListener(i,e,!1):(i="on"+i,t.detachEvent(i,e))},getInputValue:function(t){var i,e,n,s;if(null!=window.jQuery)switch(t=jQuery(t),t[0].type){case"checkbox":return t.is(":checked");default:return t.val()}else switch(t.type){case"checkbox":return t.checked;case"select-multiple":for(s=[],e=0,n=t.length;n>e;e++)i=t[e],i.selected&&s.push(i.value);return s;default:return t.value}}},t.binders={enabled:function(t,i){return t.disabled=!i},disabled:function(t,i){return t.disabled=!!i},checked:{publishes:!0,bind:function(i){return t.Util.bindEvent(i,"change",this.publish)},unbind:function(i){return t.Util.unbindEvent(i,"change",this.publish)},routine:function(t,i){var e;return t.checked="radio"===t.type?(null!=(e=t.value)?""+e:void 0)===(null!=i?""+i:void 0):!!i}},unchecked:{publishes:!0,bind:function(i){return t.Util.bindEvent(i,"change",this.publish)},unbind:function(i){return t.Util.unbindEvent(i,"change",this.publish)},routine:function(t,i){var e;return t.checked="radio"===t.type?(null!=(e=t.value)?""+e:void 0)!==(null!=i?""+i:void 0):!i}},show:function(t,i){return t.style.display=i?"":"none"},hide:function(t,i){return t.style.display=i?"none":""},html:function(t,i){return t.innerHTML=null!=i?i:""},value:{publishes:!0,bind:function(i){return t.Util.bindEvent(i,"change",this.publish)},unbind:function(i){return t.Util.unbindEvent(i,"change",this.publish)},routine:function(t,i){var e,s,r,h,o,l,u;if(null!=window.jQuery){if(t=jQuery(t),(null!=i?""+i:void 0)!==(null!=(h=t.val())?""+h:void 0))return t.val(null!=i?i:"")}else if("select-multiple"===t.type){if(null!=i){for(u=[],s=0,r=t.length;r>s;s++)e=t[s],u.push(e.selected=(o=e.value,n.call(i,o)>=0));return u}}else if((null!=i?""+i:void 0)!==(null!=(l=t.value)?""+l:void 0))return t.value=null!=i?i:""}},text:function(t,i){return null!=t.innerText?t.innerText=null!=i?i:"":t.textContent=null!=i?i:""},"on-*":{"function":!0,unbind:function(i){return this.handler?t.Util.unbindEvent(i,this.args[0],this.handler):void 0},routine:function(i,e){return this.handler&&t.Util.unbindEvent(i,this.args[0],this.handler),t.Util.bindEvent(i,this.args[0],this.handler=this.eventHandler(e))}},"each-*":{block:!0,bind:function(t){var i;return null==this.marker?(i=["data",this.view.config.prefix,this.type].join("-").replace("--","-"),this.marker=document.createComment(" rivets: "+this.type+" "),this.iterated=[],t.removeAttribute(i),t.parentNode.insertBefore(this.marker,t),t.parentNode.removeChild(t)):void 0},unbind:function(){var t,i,e,n,s;if(null!=this.iterated){for(n=this.iterated,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.unbind());return s}},routine:function(i,e){var n,s,r,h,o,l,u,a,d,c,f,p,b,v,g,y,m,w,k,E;if(u=this.args[0],e=e||[],this.iterated.length>e.length)for(m=Array(this.iterated.length-e.length),b=0,g=m.length;g>b;b++)s=m[b],p=this.iterated.pop(),p.unbind(),this.marker.parentNode.removeChild(p.els[0]);for(E=[],r=v=0,y=e.length;y>v;r=++v)if(l=e[r],n={},n[u]=l,null==this.iterated[r]){w=this.view.models;for(o in w)l=w[o],n[o]=l;d=this.iterated.length?this.iterated[this.iterated.length-1].els[0]:this.marker,a={binders:this.view.options.binders,formatters:this.view.options.formatters,config:{}},k=this.view.options.config;for(h in k)f=k[h],a.config[h]=f;a.config.preloadData=!0,c=i.cloneNode(!0),p=new t.View(c,n,a),p.bind(),this.iterated.push(p),E.push(this.marker.parentNode.insertBefore(c,d.nextSibling))}else this.iterated[r].models[u]!==l?E.push(this.iterated[r].update(n)):E.push(void 0);return E}},"class-*":function(t,i){var e;return e=" "+t.className+" ",!i==(-1!==e.indexOf(" "+this.args[0]+" "))?t.className=i?""+t.className+" "+this.args[0]:e.replace(" "+this.args[0]+" "," ").trim():void 0},"*":function(t,i){return i?t.setAttribute(this.type,i):t.removeAttribute(this.type)}},t.config={preloadData:!0,handler:function(t,i,e){return this.call(t,i,e.view.models)}},t.formatters={},t.factory=function(i){return i.binders=t.binders,i.formatters=t.formatters,i.config=t.config,i.configure=function(i){var e,n;null==i&&(i={});for(e in i)n=i[e],t.config[e]=n},i.bind=function(i,e,n){var s;return null==e&&(e={}),null==n&&(n={}),s=new t.View(i,e,n),s.bind(),s}},"object"==typeof exports?t.factory(exports):"function"==typeof define&&define.amd?define(["exports"],function(i){return t.factory(this.rivets=i),i}):t.factory(this.rivets={})}).call(this); \ No newline at end of file diff --git a/ajax/libs/rivets/0.5.5/rivets.js b/ajax/libs/rivets/0.5.5/rivets.js new file mode 100644 index 000000000..35aa8f3b0 --- /dev/null +++ b/ajax/libs/rivets/0.5.5/rivets.js @@ -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); diff --git a/ajax/libs/rivets/0.5.5/rivets.min.js b/ajax/libs/rivets/0.5.5/rivets.min.js new file mode 100644 index 000000000..bb952cd09 --- /dev/null +++ b/ajax/libs/rivets/0.5.5/rivets.min.js @@ -0,0 +1,5 @@ +// Rivets.js +// version: 0.5.5 +// author: Michael Richards +// license: MIT +(function(){var t,i=function(t,i){return function(){return t.apply(i,arguments)}},e=[].slice,n=[].indexOf||function(t){for(var i=0,e=this.length;e>i;i++)if(i in this&&this[i]===t)return i;return-1};t={},String.prototype.trim||(String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g,"")}),t.Binding=function(){function n(t,e,n,s,r,h){var o,l,u,a;if(this.view=t,this.el=e,this.type=n,this.key=s,this.keypath=r,this.options=null!=h?h:{},this.update=i(this.update,this),this.unbind=i(this.unbind,this),this.bind=i(this.bind,this),this.publish=i(this.publish,this),this.sync=i(this.sync,this),this.set=i(this.set,this),this.eventHandler=i(this.eventHandler,this),this.formattedValue=i(this.formattedValue,this),!(this.binder=this.view.binders[n])){a=this.view.binders;for(o in a)u=a[o],"*"!==o&&-1!==o.indexOf("*")&&(l=RegExp("^"+o.replace("*",".+")+"$"),l.test(n)&&(this.binder=u,this.args=RegExp("^"+o.replace("*","(.+)")+"$").exec(n),this.args.shift()))}this.binder||(this.binder=this.view.binders["*"]),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}return n.prototype.formattedValue=function(t){var i,n,s,r,h,o;for(o=this.formatters,r=0,h=o.length;h>r;r++)n=o[r],i=n.split(/\s+/),s=i.shift(),n=this.model[s]instanceof Function?this.model[s]:this.view.formatters[s],(null!=n?n.read:void 0)instanceof Function?t=n.read.apply(n,[t].concat(e.call(i))):n instanceof Function&&(t=n.apply(null,[t].concat(e.call(i))));return t},n.prototype.eventHandler=function(t){var i,e;return e=(i=this).view.config.handler,function(n){return e.call(t,this,n,i)}},n.prototype.set=function(t){var i;return t=t instanceof Function&&!this.binder["function"]?this.formattedValue(t.call(this.model)):this.formattedValue(t),null!=(i=this.binder.routine)?i.call(this,this.el,t):void 0},n.prototype.sync=function(){return this.set(this.options.bypass?this.model[this.keypath]:this.view.config.adapter.read(this.model,this.keypath))},n.prototype.publish=function(){var i,n,s,r,h,o,l,u,a;for(r=t.Util.getInputValue(this.el),l=this.formatters.slice(0).reverse(),h=0,o=l.length;o>h;h++)n=l[h],i=n.split(/\s+/),s=i.shift(),(null!=(u=this.view.formatters[s])?u.publish:void 0)&&(r=(a=this.view.formatters[s]).publish.apply(a,[r].concat(e.call(i))));return this.view.config.adapter.publish(this.model,this.keypath,r)},n.prototype.bind=function(){var t,i,e,n,s,r,h,o,l;if(null!=(r=this.binder.bind)&&r.call(this,this.el),this.options.bypass?this.sync():(this.view.config.adapter.subscribe(this.model,this.keypath,this.sync),this.view.config.preloadData&&this.sync()),null!=(h=this.options.dependencies)?h.length:void 0){for(o=this.options.dependencies,l=[],n=0,s=o.length;s>n;n++)t=o[n],/^\./.test(t)?(e=this.model,i=t.substr(1)):(t=t.split("."),e=this.view.models[t.shift()],i=t.join(".")),l.push(this.view.config.adapter.subscribe(e,i,this.sync));return l}},n.prototype.unbind=function(){var t,i,e,n,s,r,h,o,l;if(null!=(r=this.binder.unbind)&&r.call(this,this.el),this.options.bypass||this.view.config.adapter.unsubscribe(this.model,this.keypath,this.sync),null!=(h=this.options.dependencies)?h.length:void 0){for(o=this.options.dependencies,l=[],n=0,s=o.length;s>n;n++)t=o[n],/^\./.test(t)?(e=this.model,i=t.substr(1)):(t=t.split("."),e=this.view.models[t.shift()],i=t.join(".")),l.push(this.view.config.adapter.unsubscribe(e,i,this.sync));return l}},n.prototype.update=function(){return this.unbind(),this.model=this.key?this.view.models[this.key]:this.view.models,this.bind()},n}(),t.View=function(){function e(e,n,s){var r,h,o,l,u,a,d,c,f,p;for(this.els=e,this.models=n,this.options=null!=s?s:{},this.update=i(this.update,this),this.publish=i(this.publish,this),this.sync=i(this.sync,this),this.unbind=i(this.unbind,this),this.bind=i(this.bind,this),this.select=i(this.select,this),this.build=i(this.build,this),this.bindingRegExp=i(this.bindingRegExp,this),this.els.jquery||this.els instanceof Array||(this.els=[this.els]),d=["config","binders","formatters"],u=0,a=d.length;a>u;u++){if(h=d[u],this[h]={},this.options[h]){c=this.options[h];for(r in c)o=c[r],this[h][r]=o}f=t[h];for(r in f)o=f[r],null==(p=(l=this[h])[r])&&(l[r]=o)}this.build()}return e.prototype.bindingRegExp=function(){var t;return t=this.config.prefix,t?RegExp("^data-"+t+"-"):/^data-/},e.prototype.build=function(){var i,e,s,r,h,o,l,u,a,d,c,f=this;for(this.bindings=[],h=[],i=this.bindingRegExp(),r=function(e){var s,r,o,l,u,a,d,c,p,b,v,g,y,m,w,k,E,x,j,U,N,V,Q,R,B,A,F,H;if(0>n.call(h,e)){for(B=e.attributes,j=0,V=B.length;V>j;j++)if(s=B[j],i.test(s.name)){if(E=s.name.replace(i,""),!(o=f.binders[E])){A=f.binders;for(d in A)x=A[d],"*"!==d&&-1!==d.indexOf("*")&&(w=RegExp("^"+d.replace("*",".+")+"$"),w.test(E)&&(o=x))}if(o||(o=f.binders["*"]),o.block){for(F=e.getElementsByTagName("*"),U=0,Q=F.length;Q>U;U++)b=F[U],h.push(b);r=[s]}}for(H=r||e.attributes,N=0,R=H.length;R>N;N++)s=H[N],i.test(s.name)&&(v={},E=s.name.replace(i,""),m=function(){var t,i,e,n;for(e=s.value.split("|"),n=[],t=0,i=e.length;i>t;t++)y=e[t],n.push(y.trim());return n}(),l=function(){var t,i,e,n;for(e=m.shift().split("<"),n=[],t=0,i=e.length;i>t;t++)u=e[t],n.push(u.trim());return n}(),g=l.shift(),k=g.split(/\.|:/),v.formatters=m,v.bypass=-1!==g.indexOf(":"),k[0]?c=k.shift():(c=null,k.shift()),p=k.join("."),c&&null==f.models[c]||((a=l.shift())&&(v.dependencies=a.split(/\s+/)),f.bindings.push(new t.Binding(f,e,E,c,p,v))));r&&(r=null)}},d=this.els,o=0,u=d.length;u>o;o++)for(e=d[o],r(e),c=e.getElementsByTagName("*"),l=0,a=c.length;a>l;l++)s=c[l],null!=s.attributes&&r(s)},e.prototype.select=function(t){var i,e,n,s,r;for(s=this.bindings,r=[],e=0,n=s.length;n>e;e++)i=s[e],t(i)&&r.push(i);return r},e.prototype.bind=function(){var t,i,e,n,s;for(n=this.bindings,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.bind());return s},e.prototype.unbind=function(){var t,i,e,n,s;for(n=this.bindings,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.unbind());return s},e.prototype.sync=function(){var t,i,e,n,s;for(n=this.bindings,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.sync());return s},e.prototype.publish=function(){var t,i,e,n,s;for(n=this.select(function(t){return t.binder.publishes}),s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.publish());return s},e.prototype.update=function(t){var i,e,n,s;null==t&&(t={}),s=[];for(e in t)n=t[e],this.models[e]=n,s.push(function(){var t,n,s,r;for(s=this.select(function(t){return t.key===e}),r=[],t=0,n=s.length;n>t;t++)i=s[t],r.push(i.update());return r}.call(this));return s},e}(),t.Util={bindEvent:function(t,i,e){return null!=window.jQuery?(t=jQuery(t),null!=t.on?t.on(i,e):t.bind(i,e)):null!=window.addEventListener?t.addEventListener(i,e,!1):(i="on"+i,t.attachEvent(i,e))},unbindEvent:function(t,i,e){return null!=window.jQuery?(t=jQuery(t),null!=t.off?t.off(i,e):t.unbind(i,e)):null!=window.removeEventListener?t.removeEventListener(i,e,!1):(i="on"+i,t.detachEvent(i,e))},getInputValue:function(t){var i,e,n,s;if(null!=window.jQuery)switch(t=jQuery(t),t[0].type){case"checkbox":return t.is(":checked");default:return t.val()}else switch(t.type){case"checkbox":return t.checked;case"select-multiple":for(s=[],e=0,n=t.length;n>e;e++)i=t[e],i.selected&&s.push(i.value);return s;default:return t.value}}},t.binders={enabled:function(t,i){return t.disabled=!i},disabled:function(t,i){return t.disabled=!!i},checked:{publishes:!0,bind:function(i){return t.Util.bindEvent(i,"change",this.publish)},unbind:function(i){return t.Util.unbindEvent(i,"change",this.publish)},routine:function(t,i){var e;return t.checked="radio"===t.type?(null!=(e=t.value)?""+e:void 0)===(null!=i?""+i:void 0):!!i}},unchecked:{publishes:!0,bind:function(i){return t.Util.bindEvent(i,"change",this.publish)},unbind:function(i){return t.Util.unbindEvent(i,"change",this.publish)},routine:function(t,i){var e;return t.checked="radio"===t.type?(null!=(e=t.value)?""+e:void 0)!==(null!=i?""+i:void 0):!i}},show:function(t,i){return t.style.display=i?"":"none"},hide:function(t,i){return t.style.display=i?"none":""},html:function(t,i){return t.innerHTML=null!=i?i:""},value:{publishes:!0,bind:function(i){return t.Util.bindEvent(i,"change",this.publish)},unbind:function(i){return t.Util.unbindEvent(i,"change",this.publish)},routine:function(t,i){var e,s,r,h,o,l,u;if(null!=window.jQuery){if(t=jQuery(t),(null!=i?""+i:void 0)!==(null!=(h=t.val())?""+h:void 0))return t.val(null!=i?i:"")}else if("select-multiple"===t.type){if(null!=i){for(u=[],s=0,r=t.length;r>s;s++)e=t[s],u.push(e.selected=(o=e.value,n.call(i,o)>=0));return u}}else if((null!=i?""+i:void 0)!==(null!=(l=t.value)?""+l:void 0))return t.value=null!=i?i:""}},text:function(t,i){return null!=t.innerText?t.innerText=null!=i?i:"":t.textContent=null!=i?i:""},"on-*":{"function":!0,unbind:function(i){return this.handler?t.Util.unbindEvent(i,this.args[0],this.handler):void 0},routine:function(i,e){return this.handler&&t.Util.unbindEvent(i,this.args[0],this.handler),t.Util.bindEvent(i,this.args[0],this.handler=this.eventHandler(e))}},"each-*":{block:!0,bind:function(t){var i;return null==this.marker?(i=["data",this.view.config.prefix,this.type].join("-").replace("--","-"),this.marker=document.createComment(" rivets: "+this.type+" "),this.iterated=[],t.removeAttribute(i),t.parentNode.insertBefore(this.marker,t),t.parentNode.removeChild(t)):void 0},unbind:function(){var t,i,e,n,s;if(null!=this.iterated){for(n=this.iterated,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.unbind());return s}},routine:function(i,e){var n,s,r,h,o,l,u,a,d,c,f,p,b,v,g,y,m,w,k,E,x;if(u=this.args[0],e=e||[],this.iterated.length>e.length)for(m=Array(this.iterated.length-e.length),b=0,g=m.length;g>b;b++)s=m[b],p=this.iterated.pop(),p.unbind(),this.marker.parentNode.removeChild(p.els[0]);for(x=[],r=v=0,y=e.length;y>v;r=++v)if(l=e[r],n={},n[u]=l,null==this.iterated[r]){w=this.view.models;for(o in w)l=w[o],null==(k=n[o])&&(n[o]=l);d=this.iterated.length?this.iterated[this.iterated.length-1].els[0]:this.marker,a={binders:this.view.options.binders,formatters:this.view.options.formatters,config:{}},E=this.view.options.config;for(h in E)f=E[h],a.config[h]=f;a.config.preloadData=!0,c=i.cloneNode(!0),p=new t.View(c,n,a),p.bind(),this.iterated.push(p),x.push(this.marker.parentNode.insertBefore(c,d.nextSibling))}else this.iterated[r].models[u]!==l?x.push(this.iterated[r].update(n)):x.push(void 0);return x}},"class-*":function(t,i){var e;return e=" "+t.className+" ",!i==(-1!==e.indexOf(" "+this.args[0]+" "))?t.className=i?""+t.className+" "+this.args[0]:e.replace(" "+this.args[0]+" "," ").trim():void 0},"*":function(t,i){return i?t.setAttribute(this.type,i):t.removeAttribute(this.type)}},t.config={preloadData:!0,handler:function(t,i,e){return this.call(t,i,e.view.models)}},t.formatters={},t.factory=function(i){return i.binders=t.binders,i.formatters=t.formatters,i.config=t.config,i.configure=function(i){var e,n;null==i&&(i={});for(e in i)n=i[e],t.config[e]=n},i.bind=function(i,e,n){var s;return null==e&&(e={}),null==n&&(n={}),s=new t.View(i,e,n),s.bind(),s}},"object"==typeof exports?t.factory(exports):"function"==typeof define&&define.amd?define(["exports"],function(i){return t.factory(this.rivets=i),i}):t.factory(this.rivets={})}).call(this); \ No newline at end of file diff --git a/ajax/libs/rivets/0.5.6/rivets.js b/ajax/libs/rivets/0.5.6/rivets.js new file mode 100644 index 000000000..0b7089241 --- /dev/null +++ b/ajax/libs/rivets/0.5.6/rivets.js @@ -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); diff --git a/ajax/libs/rivets/0.5.6/rivets.min.js b/ajax/libs/rivets/0.5.6/rivets.min.js new file mode 100644 index 000000000..5201879bd --- /dev/null +++ b/ajax/libs/rivets/0.5.6/rivets.min.js @@ -0,0 +1,5 @@ +// Rivets.js +// version: 0.5.6 +// author: Michael Richards +// license: MIT +(function(){var t,i=function(t,i){return function(){return t.apply(i,arguments)}},e=[].slice,n=[].indexOf||function(t){for(var i=0,e=this.length;e>i;i++)if(i in this&&this[i]===t)return i;return-1};t={},String.prototype.trim||(String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g,"")}),t.Binding=function(){function n(t,e,n,s,r,h){var o,l,u,d;if(this.view=t,this.el=e,this.type=n,this.key=s,this.keypath=r,this.options=null!=h?h:{},this.update=i(this.update,this),this.unbind=i(this.unbind,this),this.bind=i(this.bind,this),this.publish=i(this.publish,this),this.sync=i(this.sync,this),this.set=i(this.set,this),this.eventHandler=i(this.eventHandler,this),this.formattedValue=i(this.formattedValue,this),!(this.binder=this.view.binders[n])){d=this.view.binders;for(o in d)u=d[o],"*"!==o&&-1!==o.indexOf("*")&&(l=RegExp("^"+o.replace("*",".+")+"$"),l.test(n)&&(this.binder=u,this.args=RegExp("^"+o.replace("*","(.+)")+"$").exec(n),this.args.shift()))}this.binder||(this.binder=this.view.binders["*"]),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}return n.prototype.formattedValue=function(t){var i,n,s,r,h,o;for(o=this.formatters,r=0,h=o.length;h>r;r++)n=o[r],i=n.split(/\s+/),s=i.shift(),n=this.model[s]instanceof Function?this.model[s]:this.view.formatters[s],(null!=n?n.read:void 0)instanceof Function?t=n.read.apply(n,[t].concat(e.call(i))):n instanceof Function&&(t=n.apply(null,[t].concat(e.call(i))));return t},n.prototype.eventHandler=function(t){var i,e;return e=(i=this).view.config.handler,function(n){return e.call(t,this,n,i)}},n.prototype.set=function(t){var i;return t=t instanceof Function&&!this.binder["function"]?this.formattedValue(t.call(this.model)):this.formattedValue(t),null!=(i=this.binder.routine)?i.call(this,this.el,t):void 0},n.prototype.sync=function(){return this.set(this.options.bypass?this.model[this.keypath]:this.view.config.adapter.read(this.model,this.keypath))},n.prototype.publish=function(){var i,n,s,r,h,o,l,u,d;for(r=t.Util.getInputValue(this.el),l=this.formatters.slice(0).reverse(),h=0,o=l.length;o>h;h++)n=l[h],i=n.split(/\s+/),s=i.shift(),(null!=(u=this.view.formatters[s])?u.publish:void 0)&&(r=(d=this.view.formatters[s]).publish.apply(d,[r].concat(e.call(i))));return this.view.config.adapter.publish(this.model,this.keypath,r)},n.prototype.bind=function(){var t,i,e,n,s,r,h,o,l;if(null!=(r=this.binder.bind)&&r.call(this,this.el),this.options.bypass?this.sync():(this.view.config.adapter.subscribe(this.model,this.keypath,this.sync),this.view.config.preloadData&&this.sync()),null!=(h=this.options.dependencies)?h.length:void 0){for(o=this.options.dependencies,l=[],n=0,s=o.length;s>n;n++)t=o[n],/^\./.test(t)?(e=this.model,i=t.substr(1)):(t=t.split("."),e=this.view.models[t.shift()],i=t.join(".")),l.push(this.view.config.adapter.subscribe(e,i,this.sync));return l}},n.prototype.unbind=function(){var t,i,e,n,s,r,h,o,l;if(null!=(r=this.binder.unbind)&&r.call(this,this.el),this.options.bypass||this.view.config.adapter.unsubscribe(this.model,this.keypath,this.sync),null!=(h=this.options.dependencies)?h.length:void 0){for(o=this.options.dependencies,l=[],n=0,s=o.length;s>n;n++)t=o[n],/^\./.test(t)?(e=this.model,i=t.substr(1)):(t=t.split("."),e=this.view.models[t.shift()],i=t.join(".")),l.push(this.view.config.adapter.unsubscribe(e,i,this.sync));return l}},n.prototype.update=function(){return this.unbind(),this.model=this.key?this.view.models[this.key]:this.view.models,this.bind()},n}(),t.View=function(){function e(e,n,s){var r,h,o,l,u,d,a,c,f,p;for(this.els=e,this.models=n,this.options=null!=s?s:{},this.update=i(this.update,this),this.publish=i(this.publish,this),this.sync=i(this.sync,this),this.unbind=i(this.unbind,this),this.bind=i(this.bind,this),this.select=i(this.select,this),this.build=i(this.build,this),this.bindingRegExp=i(this.bindingRegExp,this),this.els.jquery||this.els instanceof Array||(this.els=[this.els]),a=["config","binders","formatters"],u=0,d=a.length;d>u;u++){if(h=a[u],this[h]={},this.options[h]){c=this.options[h];for(r in c)o=c[r],this[h][r]=o}f=t[h];for(r in f)o=f[r],null==(p=(l=this[h])[r])&&(l[r]=o)}this.build()}return e.prototype.bindingRegExp=function(){var t;return t=this.config.prefix,t?RegExp("^data-"+t+"-"):/^data-/},e.prototype.build=function(){var i,e,s,r,h,o,l,u,d,a,c,f=this;for(this.bindings=[],h=[],i=this.bindingRegExp(),r=function(e){var s,r,o,l,u,d,a,c,p,b,v,g,m,y,w,k,x,E,j,N,U,V,B,Q,R,A,C,F;if(0>n.call(h,e)){for(R=e.attributes,j=0,V=R.length;V>j;j++)if(s=R[j],i.test(s.name)){if(x=s.name.replace(i,""),!(o=f.binders[x])){A=f.binders;for(a in A)E=A[a],"*"!==a&&-1!==a.indexOf("*")&&(w=RegExp("^"+a.replace("*",".+")+"$"),w.test(x)&&(o=E))}if(o||(o=f.binders["*"]),o.block){for(C=e.getElementsByTagName("*"),N=0,B=C.length;B>N;N++)b=C[N],h.push(b);r=[s]}}for(F=r||e.attributes,U=0,Q=F.length;Q>U;U++)s=F[U],i.test(s.name)&&(v={},x=s.name.replace(i,""),y=function(){var t,i,e,n;for(e=s.value.split("|"),n=[],t=0,i=e.length;i>t;t++)m=e[t],n.push(m.trim());return n}(),l=function(){var t,i,e,n;for(e=y.shift().split("<"),n=[],t=0,i=e.length;i>t;t++)u=e[t],n.push(u.trim());return n}(),g=l.shift(),k=g.split(/\.|:/),v.formatters=y,v.bypass=-1!==g.indexOf(":"),k[0]?c=k.shift():(c=null,k.shift()),p=k.join("."),c&&null==f.models[c]||((d=l.shift())&&(v.dependencies=d.split(/\s+/)),f.bindings.push(new t.Binding(f,e,x,c,p,v))));r&&(r=null)}},a=this.els,o=0,u=a.length;u>o;o++)for(e=a[o],r(e),c=e.getElementsByTagName("*"),l=0,d=c.length;d>l;l++)s=c[l],null!=s.attributes&&r(s)},e.prototype.select=function(t){var i,e,n,s,r;for(s=this.bindings,r=[],e=0,n=s.length;n>e;e++)i=s[e],t(i)&&r.push(i);return r},e.prototype.bind=function(){var t,i,e,n,s;for(n=this.bindings,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.bind());return s},e.prototype.unbind=function(){var t,i,e,n,s;for(n=this.bindings,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.unbind());return s},e.prototype.sync=function(){var t,i,e,n,s;for(n=this.bindings,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.sync());return s},e.prototype.publish=function(){var t,i,e,n,s;for(n=this.select(function(t){return t.binder.publishes}),s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.publish());return s},e.prototype.update=function(t){var i,e,n,s;null==t&&(t={}),s=[];for(e in t)n=t[e],this.models[e]=n,s.push(function(){var t,n,s,r;for(s=this.select(function(t){return t.key===e}),r=[],t=0,n=s.length;n>t;t++)i=s[t],r.push(i.update());return r}.call(this));return s},e}(),t.Util={bindEvent:function(t,i,e){return null!=window.jQuery?(t=jQuery(t),null!=t.on?t.on(i,e):t.bind(i,e)):null!=window.addEventListener?t.addEventListener(i,e,!1):(i="on"+i,t.attachEvent(i,e))},unbindEvent:function(t,i,e){return null!=window.jQuery?(t=jQuery(t),null!=t.off?t.off(i,e):t.unbind(i,e)):null!=window.removeEventListener?t.removeEventListener(i,e,!1):(i="on"+i,t.detachEvent(i,e))},getInputValue:function(t){var i,e,n,s;if(null!=window.jQuery)switch(t=jQuery(t),t[0].type){case"checkbox":return t.is(":checked");default:return t.val()}else switch(t.type){case"checkbox":return t.checked;case"select-multiple":for(s=[],e=0,n=t.length;n>e;e++)i=t[e],i.selected&&s.push(i.value);return s;default:return t.value}}},t.binders={enabled:function(t,i){return t.disabled=!i},disabled:function(t,i){return t.disabled=!!i},checked:{publishes:!0,bind:function(i){return t.Util.bindEvent(i,"change",this.publish)},unbind:function(i){return t.Util.unbindEvent(i,"change",this.publish)},routine:function(t,i){var e;return t.checked="radio"===t.type?(null!=(e=t.value)?""+e:void 0)===(null!=i?""+i:void 0):!!i}},unchecked:{publishes:!0,bind:function(i){return t.Util.bindEvent(i,"change",this.publish)},unbind:function(i){return t.Util.unbindEvent(i,"change",this.publish)},routine:function(t,i){var e;return t.checked="radio"===t.type?(null!=(e=t.value)?""+e:void 0)!==(null!=i?""+i:void 0):!i}},show:function(t,i){return t.style.display=i?"":"none"},hide:function(t,i){return t.style.display=i?"none":""},html:function(t,i){return t.innerHTML=null!=i?i:""},value:{publishes:!0,bind:function(i){return t.Util.bindEvent(i,"change",this.publish)},unbind:function(i){return t.Util.unbindEvent(i,"change",this.publish)},routine:function(t,i){var e,s,r,h,o,l,u;if(null!=window.jQuery){if(t=jQuery(t),(null!=i?""+i:void 0)!==(null!=(h=t.val())?""+h:void 0))return t.val(null!=i?i:"")}else if("select-multiple"===t.type){if(null!=i){for(u=[],s=0,r=t.length;r>s;s++)e=t[s],u.push(e.selected=(o=e.value,n.call(i,o)>=0));return u}}else if((null!=i?""+i:void 0)!==(null!=(l=t.value)?""+l:void 0))return t.value=null!=i?i:""}},text:function(t,i){return null!=t.innerText?t.innerText=null!=i?i:"":t.textContent=null!=i?i:""},"if":{block:!0,bind:function(t){var i,e;return null==this.marker?(i=["data",this.view.config.prefix,this.type].join("-").replace("--","-"),e=t.getAttribute(i),this.marker=document.createComment(" rivets: "+this.type+" "+e+" "),t.removeAttribute(i),t.parentNode.insertBefore(this.marker,t),t.parentNode.removeChild(t)):void 0},unbind:function(){var t;return null!=(t=this.nested)?t.unbind():void 0},routine:function(i,e){var n,s,r,h,o;if(e===(null==this.nested)){if(e){r={},o=this.view.models;for(n in o)s=o[n],r[n]=s;return h={binders:this.view.options.binders,formatters:this.view.options.formatters,config:this.view.options.config},(this.nested=new t.View(i,r,h)).bind(),this.marker.parentNode.insertBefore(i,this.marker.nextSibling)}return i.parentNode.removeChild(i),this.nested.unbind(),delete this.nested}}},unless:{block:!0,bind:function(t){return rivets.binders["if"].bind.call(this,t)},unbind:function(){return rivets.binders["if"].unbind.call(this)},routine:function(t,i){return rivets.binders["if"].routine.call(this,t,!i)}},"on-*":{"function":!0,unbind:function(i){return this.handler?t.Util.unbindEvent(i,this.args[0],this.handler):void 0},routine:function(i,e){return this.handler&&t.Util.unbindEvent(i,this.args[0],this.handler),t.Util.bindEvent(i,this.args[0],this.handler=this.eventHandler(e))}},"each-*":{block:!0,bind:function(t){var i;return null==this.marker?(i=["data",this.view.config.prefix,this.type].join("-").replace("--","-"),this.marker=document.createComment(" rivets: "+this.type+" "),this.iterated=[],t.removeAttribute(i),t.parentNode.insertBefore(this.marker,t),t.parentNode.removeChild(t)):void 0},unbind:function(){var t,i,e,n,s;if(null!=this.iterated){for(n=this.iterated,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.unbind());return s}},routine:function(i,e){var n,s,r,h,o,l,u,d,a,c,f,p,b,v,g,m,y,w,k,x,E;if(u=this.args[0],e=e||[],this.iterated.length>e.length)for(y=Array(this.iterated.length-e.length),b=0,g=y.length;g>b;b++)s=y[b],p=this.iterated.pop(),p.unbind(),this.marker.parentNode.removeChild(p.els[0]);for(E=[],r=v=0,m=e.length;m>v;r=++v)if(l=e[r],n={},n[u]=l,null==this.iterated[r]){w=this.view.models;for(o in w)l=w[o],null==(k=n[o])&&(n[o]=l);a=this.iterated.length?this.iterated[this.iterated.length-1].els[0]:this.marker,d={binders:this.view.options.binders,formatters:this.view.options.formatters,config:{}},x=this.view.options.config;for(h in x)f=x[h],d.config[h]=f;d.config.preloadData=!0,c=i.cloneNode(!0),p=new t.View(c,n,d),p.bind(),this.iterated.push(p),E.push(this.marker.parentNode.insertBefore(c,a.nextSibling))}else this.iterated[r].models[u]!==l?E.push(this.iterated[r].update(n)):E.push(void 0);return E}},"class-*":function(t,i){var e;return e=" "+t.className+" ",!i==(-1!==e.indexOf(" "+this.args[0]+" "))?t.className=i?""+t.className+" "+this.args[0]:e.replace(" "+this.args[0]+" "," ").trim():void 0},"*":function(t,i){return i?t.setAttribute(this.type,i):t.removeAttribute(this.type)}},t.config={preloadData:!0,handler:function(t,i,e){return this.call(t,i,e.view.models)}},t.formatters={},t.factory=function(i){return i.binders=t.binders,i.formatters=t.formatters,i.config=t.config,i.configure=function(i){var e,n;null==i&&(i={});for(e in i)n=i[e],t.config[e]=n},i.bind=function(i,e,n){var s;return null==e&&(e={}),null==n&&(n={}),s=new t.View(i,e,n),s.bind(),s}},"object"==typeof exports?t.factory(exports):"function"==typeof define&&define.amd?define(["exports"],function(i){return t.factory(this.rivets=i),i}):t.factory(this.rivets={})}).call(this); \ No newline at end of file diff --git a/ajax/libs/rivets/0.5.7/rivets.js b/ajax/libs/rivets/0.5.7/rivets.js new file mode 100644 index 000000000..042c3092d --- /dev/null +++ b/ajax/libs/rivets/0.5.7/rivets.js @@ -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); diff --git a/ajax/libs/rivets/0.5.7/rivets.min.js b/ajax/libs/rivets/0.5.7/rivets.min.js new file mode 100644 index 000000000..c9c63e48e --- /dev/null +++ b/ajax/libs/rivets/0.5.7/rivets.min.js @@ -0,0 +1,5 @@ +// Rivets.js +// version: 0.5.7 +// author: Michael Richards +// license: MIT +(function(){var t,i=function(t,i){return function(){return t.apply(i,arguments)}},e=[].slice,n=[].indexOf||function(t){for(var i=0,e=this.length;e>i;i++)if(i in this&&this[i]===t)return i;return-1};t={},String.prototype.trim||(String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g,"")}),t.Binding=function(){function n(t,e,n,s,r,h){var o,l,u,d;if(this.view=t,this.el=e,this.type=n,this.key=s,this.keypath=r,this.options=null!=h?h:{},this.update=i(this.update,this),this.unbind=i(this.unbind,this),this.bind=i(this.bind,this),this.publish=i(this.publish,this),this.sync=i(this.sync,this),this.set=i(this.set,this),this.eventHandler=i(this.eventHandler,this),this.formattedValue=i(this.formattedValue,this),!(this.binder=this.view.binders[n])){d=this.view.binders;for(o in d)u=d[o],"*"!==o&&-1!==o.indexOf("*")&&(l=RegExp("^"+o.replace("*",".+")+"$"),l.test(n)&&(this.binder=u,this.args=RegExp("^"+o.replace("*","(.+)")+"$").exec(n),this.args.shift()))}this.binder||(this.binder=this.view.binders["*"]),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}return n.prototype.formattedValue=function(t){var i,n,s,r,h,o;for(o=this.formatters,r=0,h=o.length;h>r;r++)n=o[r],i=n.split(/\s+/),s=i.shift(),n=this.model[s]instanceof Function?this.model[s]:this.view.formatters[s],(null!=n?n.read:void 0)instanceof Function?t=n.read.apply(n,[t].concat(e.call(i))):n instanceof Function&&(t=n.apply(null,[t].concat(e.call(i))));return t},n.prototype.eventHandler=function(t){var i,e;return e=(i=this).view.config.handler,function(n){return e.call(t,this,n,i)}},n.prototype.set=function(t){var i;return t=t instanceof Function&&!this.binder["function"]?this.formattedValue(t.call(this.model)):this.formattedValue(t),null!=(i=this.binder.routine)?i.call(this,this.el,t):void 0},n.prototype.sync=function(){return this.set(this.options.bypass?this.model[this.keypath]:this.view.config.adapter.read(this.model,this.keypath))},n.prototype.publish=function(){var i,n,s,r,h,o,l,u,d;for(r=t.Util.getInputValue(this.el),l=this.formatters.slice(0).reverse(),h=0,o=l.length;o>h;h++)n=l[h],i=n.split(/\s+/),s=i.shift(),(null!=(u=this.view.formatters[s])?u.publish:void 0)&&(r=(d=this.view.formatters[s]).publish.apply(d,[r].concat(e.call(i))));return this.view.config.adapter.publish(this.model,this.keypath,r)},n.prototype.bind=function(){var t,i,e,n,s,r,h,o,l;if(null!=(r=this.binder.bind)&&r.call(this,this.el),this.options.bypass?this.sync():(this.view.config.adapter.subscribe(this.model,this.keypath,this.sync),this.view.config.preloadData&&this.sync()),null!=(h=this.options.dependencies)?h.length:void 0){for(o=this.options.dependencies,l=[],n=0,s=o.length;s>n;n++)t=o[n],/^\./.test(t)?(e=this.model,i=t.substr(1)):(t=t.split("."),e=this.view.models[t.shift()],i=t.join(".")),l.push(this.view.config.adapter.subscribe(e,i,this.sync));return l}},n.prototype.unbind=function(){var t,i,e,n,s,r,h,o,l;if(null!=(r=this.binder.unbind)&&r.call(this,this.el),this.options.bypass||this.view.config.adapter.unsubscribe(this.model,this.keypath,this.sync),null!=(h=this.options.dependencies)?h.length:void 0){for(o=this.options.dependencies,l=[],n=0,s=o.length;s>n;n++)t=o[n],/^\./.test(t)?(e=this.model,i=t.substr(1)):(t=t.split("."),e=this.view.models[t.shift()],i=t.join(".")),l.push(this.view.config.adapter.unsubscribe(e,i,this.sync));return l}},n.prototype.update=function(){return this.unbind(),this.model=this.key?this.view.models[this.key]:this.view.models,this.bind()},n}(),t.View=function(){function e(e,n,s){var r,h,o,l,u,d,a,c,f,p;for(this.els=e,this.models=n,this.options=null!=s?s:{},this.update=i(this.update,this),this.publish=i(this.publish,this),this.sync=i(this.sync,this),this.unbind=i(this.unbind,this),this.bind=i(this.bind,this),this.select=i(this.select,this),this.build=i(this.build,this),this.bindingRegExp=i(this.bindingRegExp,this),this.els.jquery||this.els instanceof Array||(this.els=[this.els]),a=["config","binders","formatters"],u=0,d=a.length;d>u;u++){if(h=a[u],this[h]={},this.options[h]){c=this.options[h];for(r in c)o=c[r],this[h][r]=o}f=t[h];for(r in f)o=f[r],null==(p=(l=this[h])[r])&&(l[r]=o)}this.build()}return e.prototype.bindingRegExp=function(){var t;return t=this.config.prefix,t?RegExp("^data-"+t+"-"):/^data-/},e.prototype.build=function(){var i,e,s,r,h,o,l,u,d,a,c,f=this;for(this.bindings=[],h=[],i=this.bindingRegExp(),r=function(e){var s,r,o,l,u,d,a,c,p,b,v,g,m,y,w,k,x,E,j,N,U,V,B,Q,R,A,C,F;if(0>n.call(h,e)){for(R=e.attributes,j=0,V=R.length;V>j;j++)if(s=R[j],i.test(s.name)){if(x=s.name.replace(i,""),!(o=f.binders[x])){A=f.binders;for(a in A)E=A[a],"*"!==a&&-1!==a.indexOf("*")&&(w=RegExp("^"+a.replace("*",".+")+"$"),w.test(x)&&(o=E))}if(o||(o=f.binders["*"]),o.block){for(C=e.getElementsByTagName("*"),N=0,B=C.length;B>N;N++)b=C[N],h.push(b);r=[s]}}for(F=r||e.attributes,U=0,Q=F.length;Q>U;U++)s=F[U],i.test(s.name)&&(v={},x=s.name.replace(i,""),y=function(){var t,i,e,n;for(e=s.value.split("|"),n=[],t=0,i=e.length;i>t;t++)m=e[t],n.push(m.trim());return n}(),l=function(){var t,i,e,n;for(e=y.shift().split("<"),n=[],t=0,i=e.length;i>t;t++)u=e[t],n.push(u.trim());return n}(),g=l.shift(),k=g.split(/\.|:/),v.formatters=y,v.bypass=-1!==g.indexOf(":"),k[0]?c=k.shift():(c=null,k.shift()),p=k.join("."),c&&null==f.models[c]||((d=l.shift())&&(v.dependencies=d.split(/\s+/)),f.bindings.push(new t.Binding(f,e,x,c,p,v))));r&&(r=null)}},a=this.els,o=0,u=a.length;u>o;o++)for(e=a[o],r(e),c=e.getElementsByTagName("*"),l=0,d=c.length;d>l;l++)s=c[l],null!=s.attributes&&r(s)},e.prototype.select=function(t){var i,e,n,s,r;for(s=this.bindings,r=[],e=0,n=s.length;n>e;e++)i=s[e],t(i)&&r.push(i);return r},e.prototype.bind=function(){var t,i,e,n,s;for(n=this.bindings,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.bind());return s},e.prototype.unbind=function(){var t,i,e,n,s;for(n=this.bindings,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.unbind());return s},e.prototype.sync=function(){var t,i,e,n,s;for(n=this.bindings,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.sync());return s},e.prototype.publish=function(){var t,i,e,n,s;for(n=this.select(function(t){return t.binder.publishes}),s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.publish());return s},e.prototype.update=function(t){var i,e,n,s;null==t&&(t={}),s=[];for(e in t)n=t[e],this.models[e]=n,s.push(function(){var t,n,s,r;for(s=this.select(function(t){return t.key===e}),r=[],t=0,n=s.length;n>t;t++)i=s[t],r.push(i.update());return r}.call(this));return s},e}(),t.Util={bindEvent:function(t,i,e){return null!=window.jQuery?(t=jQuery(t),null!=t.on?t.on(i,e):t.bind(i,e)):null!=window.addEventListener?t.addEventListener(i,e,!1):(i="on"+i,t.attachEvent(i,e))},unbindEvent:function(t,i,e){return null!=window.jQuery?(t=jQuery(t),null!=t.off?t.off(i,e):t.unbind(i,e)):null!=window.removeEventListener?t.removeEventListener(i,e,!1):(i="on"+i,t.detachEvent(i,e))},getInputValue:function(t){var i,e,n,s;if(null!=window.jQuery)switch(t=jQuery(t),t[0].type){case"checkbox":return t.is(":checked");default:return t.val()}else switch(t.type){case"checkbox":return t.checked;case"select-multiple":for(s=[],e=0,n=t.length;n>e;e++)i=t[e],i.selected&&s.push(i.value);return s;default:return t.value}}},t.binders={enabled:function(t,i){return t.disabled=!i},disabled:function(t,i){return t.disabled=!!i},checked:{publishes:!0,bind:function(i){return t.Util.bindEvent(i,"change",this.publish)},unbind:function(i){return t.Util.unbindEvent(i,"change",this.publish)},routine:function(t,i){var e;return t.checked="radio"===t.type?(null!=(e=t.value)?""+e:void 0)===(null!=i?""+i:void 0):!!i}},unchecked:{publishes:!0,bind:function(i){return t.Util.bindEvent(i,"change",this.publish)},unbind:function(i){return t.Util.unbindEvent(i,"change",this.publish)},routine:function(t,i){var e;return t.checked="radio"===t.type?(null!=(e=t.value)?""+e:void 0)!==(null!=i?""+i:void 0):!i}},show:function(t,i){return t.style.display=i?"":"none"},hide:function(t,i){return t.style.display=i?"none":""},html:function(t,i){return t.innerHTML=null!=i?i:""},value:{publishes:!0,bind:function(i){return t.Util.bindEvent(i,"change",this.publish)},unbind:function(i){return t.Util.unbindEvent(i,"change",this.publish)},routine:function(t,i){var e,s,r,h,o,l,u;if(null!=window.jQuery){if(t=jQuery(t),(null!=i?""+i:void 0)!==(null!=(h=t.val())?""+h:void 0))return t.val(null!=i?i:"")}else if("select-multiple"===t.type){if(null!=i){for(u=[],s=0,r=t.length;r>s;s++)e=t[s],u.push(e.selected=(o=e.value,n.call(i,o)>=0));return u}}else if((null!=i?""+i:void 0)!==(null!=(l=t.value)?""+l:void 0))return t.value=null!=i?i:""}},text:function(t,i){return null!=t.innerText?t.innerText=null!=i?i:"":t.textContent=null!=i?i:""},"if":{block:!0,bind:function(t){var i,e;return null==this.marker?(i=["data",this.view.config.prefix,this.type].join("-").replace("--","-"),e=t.getAttribute(i),this.marker=document.createComment(" rivets: "+this.type+" "+e+" "),t.removeAttribute(i),t.parentNode.insertBefore(this.marker,t),t.parentNode.removeChild(t)):void 0},unbind:function(){var t;return null!=(t=this.nested)?t.unbind():void 0},routine:function(i,e){var n,s,r,h,o;if(e===(null==this.nested)){if(e){r={},o=this.view.models;for(n in o)s=o[n],r[n]=s;return h={binders:this.view.options.binders,formatters:this.view.options.formatters,config:this.view.options.config},(this.nested=new t.View(i,r,h)).bind(),this.marker.parentNode.insertBefore(i,this.marker.nextSibling)}return i.parentNode.removeChild(i),this.nested.unbind(),delete this.nested}}},unless:{block:!0,bind:function(i){return t.binders["if"].bind.call(this,i)},unbind:function(){return t.binders["if"].unbind.call(this)},routine:function(i,e){return t.binders["if"].routine.call(this,i,!e)}},"on-*":{"function":!0,unbind:function(i){return this.handler?t.Util.unbindEvent(i,this.args[0],this.handler):void 0},routine:function(i,e){return this.handler&&t.Util.unbindEvent(i,this.args[0],this.handler),t.Util.bindEvent(i,this.args[0],this.handler=this.eventHandler(e))}},"each-*":{block:!0,bind:function(t){var i;return null==this.marker?(i=["data",this.view.config.prefix,this.type].join("-").replace("--","-"),this.marker=document.createComment(" rivets: "+this.type+" "),this.iterated=[],t.removeAttribute(i),t.parentNode.insertBefore(this.marker,t),t.parentNode.removeChild(t)):void 0},unbind:function(){var t,i,e,n,s;if(null!=this.iterated){for(n=this.iterated,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.unbind());return s}},routine:function(i,e){var n,s,r,h,o,l,u,d,a,c,f,p,b,v,g,m,y,w,k,x,E;if(u=this.args[0],e=e||[],this.iterated.length>e.length)for(y=Array(this.iterated.length-e.length),b=0,g=y.length;g>b;b++)s=y[b],p=this.iterated.pop(),p.unbind(),this.marker.parentNode.removeChild(p.els[0]);for(E=[],r=v=0,m=e.length;m>v;r=++v)if(l=e[r],n={},n[u]=l,null==this.iterated[r]){w=this.view.models;for(o in w)l=w[o],null==(k=n[o])&&(n[o]=l);a=this.iterated.length?this.iterated[this.iterated.length-1].els[0]:this.marker,d={binders:this.view.options.binders,formatters:this.view.options.formatters,config:{}},x=this.view.options.config;for(h in x)f=x[h],d.config[h]=f;d.config.preloadData=!0,c=i.cloneNode(!0),p=new t.View(c,n,d),p.bind(),this.iterated.push(p),E.push(this.marker.parentNode.insertBefore(c,a.nextSibling))}else this.iterated[r].models[u]!==l?E.push(this.iterated[r].update(n)):E.push(void 0);return E}},"class-*":function(t,i){var e;return e=" "+t.className+" ",!i==(-1!==e.indexOf(" "+this.args[0]+" "))?t.className=i?""+t.className+" "+this.args[0]:e.replace(" "+this.args[0]+" "," ").trim():void 0},"*":function(t,i){return i?t.setAttribute(this.type,i):t.removeAttribute(this.type)}},t.config={preloadData:!0,handler:function(t,i,e){return this.call(t,i,e.view.models)}},t.formatters={},t.factory=function(i){return i.binders=t.binders,i.formatters=t.formatters,i.config=t.config,i.configure=function(i){var e,n;null==i&&(i={});for(e in i)n=i[e],t.config[e]=n},i.bind=function(i,e,n){var s;return null==e&&(e={}),null==n&&(n={}),s=new t.View(i,e,n),s.bind(),s}},"object"==typeof exports?t.factory(exports):"function"==typeof define&&define.amd?define(["exports"],function(i){return t.factory(this.rivets=i),i}):t.factory(this.rivets={})}).call(this); \ No newline at end of file diff --git a/ajax/libs/rivets/0.5.8/rivets.js b/ajax/libs/rivets/0.5.8/rivets.js new file mode 100644 index 000000000..80b5e6ec3 --- /dev/null +++ b/ajax/libs/rivets/0.5.8/rivets.js @@ -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); diff --git a/ajax/libs/rivets/0.5.8/rivets.min.js b/ajax/libs/rivets/0.5.8/rivets.min.js new file mode 100644 index 000000000..57b6802cf --- /dev/null +++ b/ajax/libs/rivets/0.5.8/rivets.min.js @@ -0,0 +1,5 @@ +// Rivets.js +// version: 0.5.8 +// author: Michael Richards +// license: MIT +(function(){var t,i=function(t,i){return function(){return t.apply(i,arguments)}},e=[].slice,n=[].indexOf||function(t){for(var i=0,e=this.length;e>i;i++)if(i in this&&this[i]===t)return i;return-1};t={},String.prototype.trim||(String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g,"")}),t.Binding=function(){function n(t,e,n,s,r,h){var o,u,l,a;if(this.view=t,this.el=e,this.type=n,this.key=s,this.keypath=r,this.options=null!=h?h:{},this.update=i(this.update,this),this.unbind=i(this.unbind,this),this.bind=i(this.bind,this),this.publish=i(this.publish,this),this.sync=i(this.sync,this),this.set=i(this.set,this),this.eventHandler=i(this.eventHandler,this),this.formattedValue=i(this.formattedValue,this),!(this.binder=this.view.binders[n])){a=this.view.binders;for(o in a)l=a[o],"*"!==o&&-1!==o.indexOf("*")&&(u=RegExp("^"+o.replace("*",".+")+"$"),u.test(n)&&(this.binder=l,this.args=RegExp("^"+o.replace("*","(.+)")+"$").exec(n),this.args.shift()))}this.binder||(this.binder=this.view.binders["*"]),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}return n.prototype.formattedValue=function(t){var i,n,s,r,h,o;for(o=this.formatters,r=0,h=o.length;h>r;r++)n=o[r],i=n.split(/\s+/),s=i.shift(),n=this.model[s]instanceof Function?this.model[s]:this.view.formatters[s],(null!=n?n.read:void 0)instanceof Function?t=n.read.apply(n,[t].concat(e.call(i))):n instanceof Function&&(t=n.apply(null,[t].concat(e.call(i))));return t},n.prototype.eventHandler=function(t){var i,e;return e=(i=this).view.config.handler,function(n){return e.call(t,this,n,i)}},n.prototype.set=function(t){var i;return t=t instanceof Function&&!this.binder["function"]?this.formattedValue(t.call(this.model)):this.formattedValue(t),null!=(i=this.binder.routine)?i.call(this,this.el,t):void 0},n.prototype.sync=function(){return this.set(this.options.bypass?this.model[this.keypath]:this.view.config.adapter.read(this.model,this.keypath))},n.prototype.publish=function(){var i,n,s,r,h,o,u,l,a;for(r=t.Util.getInputValue(this.el),u=this.formatters.slice(0).reverse(),h=0,o=u.length;o>h;h++)n=u[h],i=n.split(/\s+/),s=i.shift(),(null!=(l=this.view.formatters[s])?l.publish:void 0)&&(r=(a=this.view.formatters[s]).publish.apply(a,[r].concat(e.call(i))));return this.view.config.adapter.publish(this.model,this.keypath,r)},n.prototype.bind=function(){var t,i,e,n,s,r,h,o,u;if(null!=(r=this.binder.bind)&&r.call(this,this.el),this.options.bypass?this.sync():(this.view.config.adapter.subscribe(this.model,this.keypath,this.sync),this.view.config.preloadData&&this.sync()),null!=(h=this.options.dependencies)?h.length:void 0){for(o=this.options.dependencies,u=[],n=0,s=o.length;s>n;n++)t=o[n],/^\./.test(t)?(e=this.model,i=t.substr(1)):(t=t.split("."),e=this.view.models[t.shift()],i=t.join(".")),u.push(this.view.config.adapter.subscribe(e,i,this.sync));return u}},n.prototype.unbind=function(){var t,i,e,n,s,r,h,o,u;if(null!=(r=this.binder.unbind)&&r.call(this,this.el),this.options.bypass||this.view.config.adapter.unsubscribe(this.model,this.keypath,this.sync),null!=(h=this.options.dependencies)?h.length:void 0){for(o=this.options.dependencies,u=[],n=0,s=o.length;s>n;n++)t=o[n],/^\./.test(t)?(e=this.model,i=t.substr(1)):(t=t.split("."),e=this.view.models[t.shift()],i=t.join(".")),u.push(this.view.config.adapter.unsubscribe(e,i,this.sync));return u}},n.prototype.update=function(t){var i;return null==t&&(t={}),this.key?t[this.key]&&(this.options.bypass||this.view.config.adapter.unsubscribe(this.model,this.keypath,this.sync),this.model=t[this.key],this.options.bypass?this.sync():(this.view.config.adapter.subscribe(this.model,this.keypath,this.sync),this.view.config.preloadData&&this.sync())):this.sync(),null!=(i=this.binder.update)?i.call(this,t):void 0},n}(),t.View=function(){function e(e,n,s){var r,h,o,u,l,a,d,c,f,p;for(this.els=e,this.models=n,this.options=null!=s?s:{},this.update=i(this.update,this),this.publish=i(this.publish,this),this.sync=i(this.sync,this),this.unbind=i(this.unbind,this),this.bind=i(this.bind,this),this.select=i(this.select,this),this.build=i(this.build,this),this.bindingRegExp=i(this.bindingRegExp,this),this.els.jquery||this.els instanceof Array||(this.els=[this.els]),d=["config","binders","formatters"],l=0,a=d.length;a>l;l++){if(h=d[l],this[h]={},this.options[h]){c=this.options[h];for(r in c)o=c[r],this[h][r]=o}f=t[h];for(r in f)o=f[r],null==(p=(u=this[h])[r])&&(u[r]=o)}this.build()}return e.prototype.bindingRegExp=function(){var t;return t=this.config.prefix,t?RegExp("^data-"+t+"-"):/^data-/},e.prototype.build=function(){var i,e,s,r,h,o,u,l,a,d,c,f=this;for(this.bindings=[],h=[],i=this.bindingRegExp(),r=function(e){var s,r,o,u,l,a,d,c,p,b,v,g,y,m,w,k,x,E,j,N,U,V,B,Q,R,A,C,F;if(0>n.call(h,e)){for(R=e.attributes,j=0,V=R.length;V>j;j++)if(s=R[j],i.test(s.name)){if(x=s.name.replace(i,""),!(o=f.binders[x])){A=f.binders;for(d in A)E=A[d],"*"!==d&&-1!==d.indexOf("*")&&(w=RegExp("^"+d.replace("*",".+")+"$"),w.test(x)&&(o=E))}if(o||(o=f.binders["*"]),o.block){for(C=e.getElementsByTagName("*"),N=0,B=C.length;B>N;N++)b=C[N],h.push(b);r=[s]}}for(F=r||e.attributes,U=0,Q=F.length;Q>U;U++)s=F[U],i.test(s.name)&&(v={},x=s.name.replace(i,""),m=function(){var t,i,e,n;for(e=s.value.split("|"),n=[],t=0,i=e.length;i>t;t++)y=e[t],n.push(y.trim());return n}(),u=function(){var t,i,e,n;for(e=m.shift().split("<"),n=[],t=0,i=e.length;i>t;t++)l=e[t],n.push(l.trim());return n}(),g=u.shift(),k=g.split(/\.|:/),v.formatters=m,v.bypass=-1!==g.indexOf(":"),k[0]?c=k.shift():(c=null,k.shift()),p=k.join("."),c&&null==f.models[c]||((a=u.shift())&&(v.dependencies=a.split(/\s+/)),f.bindings.push(new t.Binding(f,e,x,c,p,v))));r&&(r=null)}},d=this.els,o=0,l=d.length;l>o;o++)for(e=d[o],r(e),c=e.getElementsByTagName("*"),u=0,a=c.length;a>u;u++)s=c[u],null!=s.attributes&&r(s)},e.prototype.select=function(t){var i,e,n,s,r;for(s=this.bindings,r=[],e=0,n=s.length;n>e;e++)i=s[e],t(i)&&r.push(i);return r},e.prototype.bind=function(){var t,i,e,n,s;for(n=this.bindings,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.bind());return s},e.prototype.unbind=function(){var t,i,e,n,s;for(n=this.bindings,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.unbind());return s},e.prototype.sync=function(){var t,i,e,n,s;for(n=this.bindings,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.sync());return s},e.prototype.publish=function(){var t,i,e,n,s;for(n=this.select(function(t){return t.binder.publishes}),s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.publish());return s},e.prototype.update=function(t){var i,e,n,s,r,h,o;null==t&&(t={});for(e in t)n=t[e],this.models[e]=n;for(h=this.bindings,o=[],s=0,r=h.length;r>s;s++)i=h[s],o.push(i.update(t));return o},e}(),t.Util={bindEvent:function(t,i,e){return null!=window.jQuery?(t=jQuery(t),null!=t.on?t.on(i,e):t.bind(i,e)):null!=window.addEventListener?t.addEventListener(i,e,!1):(i="on"+i,t.attachEvent(i,e))},unbindEvent:function(t,i,e){return null!=window.jQuery?(t=jQuery(t),null!=t.off?t.off(i,e):t.unbind(i,e)):null!=window.removeEventListener?t.removeEventListener(i,e,!1):(i="on"+i,t.detachEvent(i,e))},getInputValue:function(t){var i,e,n,s;if(null!=window.jQuery)switch(t=jQuery(t),t[0].type){case"checkbox":return t.is(":checked");default:return t.val()}else switch(t.type){case"checkbox":return t.checked;case"select-multiple":for(s=[],e=0,n=t.length;n>e;e++)i=t[e],i.selected&&s.push(i.value);return s;default:return t.value}}},t.binders={enabled:function(t,i){return t.disabled=!i},disabled:function(t,i){return t.disabled=!!i},checked:{publishes:!0,bind:function(i){return t.Util.bindEvent(i,"change",this.publish)},unbind:function(i){return t.Util.unbindEvent(i,"change",this.publish)},routine:function(t,i){var e;return t.checked="radio"===t.type?(null!=(e=t.value)?""+e:void 0)===(null!=i?""+i:void 0):!!i}},unchecked:{publishes:!0,bind:function(i){return t.Util.bindEvent(i,"change",this.publish)},unbind:function(i){return t.Util.unbindEvent(i,"change",this.publish)},routine:function(t,i){var e;return t.checked="radio"===t.type?(null!=(e=t.value)?""+e:void 0)!==(null!=i?""+i:void 0):!i}},show:function(t,i){return t.style.display=i?"":"none"},hide:function(t,i){return t.style.display=i?"none":""},html:function(t,i){return t.innerHTML=null!=i?i:""},value:{publishes:!0,bind:function(i){return t.Util.bindEvent(i,"change",this.publish)},unbind:function(i){return t.Util.unbindEvent(i,"change",this.publish)},routine:function(t,i){var e,s,r,h,o,u,l;if(null!=window.jQuery){if(t=jQuery(t),(null!=i?""+i:void 0)!==(null!=(h=t.val())?""+h:void 0))return t.val(null!=i?i:"")}else if("select-multiple"===t.type){if(null!=i){for(l=[],s=0,r=t.length;r>s;s++)e=t[s],l.push(e.selected=(o=e.value,n.call(i,o)>=0));return l}}else if((null!=i?""+i:void 0)!==(null!=(u=t.value)?""+u:void 0))return t.value=null!=i?i:""}},text:function(t,i){return null!=t.innerText?t.innerText=null!=i?i:"":t.textContent=null!=i?i:""},"if":{block:!0,bind:function(t){var i,e;return null==this.marker?(i=["data",this.view.config.prefix,this.type].join("-").replace("--","-"),e=t.getAttribute(i),this.marker=document.createComment(" rivets: "+this.type+" "+e+" "),t.removeAttribute(i),t.parentNode.insertBefore(this.marker,t),t.parentNode.removeChild(t)):void 0},unbind:function(){var t;return null!=(t=this.nested)?t.unbind():void 0},routine:function(i,e){var n,s,r,h,o;if(e===(null==this.nested)){if(e){r={},o=this.view.models;for(n in o)s=o[n],r[n]=s;return h={binders:this.view.options.binders,formatters:this.view.options.formatters,config:this.view.options.config},(this.nested=new t.View(i,r,h)).bind(),this.marker.parentNode.insertBefore(i,this.marker.nextSibling)}return i.parentNode.removeChild(i),this.nested.unbind(),delete this.nested}},update:function(t){return this.nested.update(t)}},unless:{block:!0,bind:function(i){return t.binders["if"].bind.call(this,i)},unbind:function(){return t.binders["if"].unbind.call(this)},routine:function(i,e){return t.binders["if"].routine.call(this,i,!e)},update:function(i){return t.binders["if"].update.call(this,i)}},"on-*":{"function":!0,unbind:function(i){return this.handler?t.Util.unbindEvent(i,this.args[0],this.handler):void 0},routine:function(i,e){return this.handler&&t.Util.unbindEvent(i,this.args[0],this.handler),t.Util.bindEvent(i,this.args[0],this.handler=this.eventHandler(e))}},"each-*":{block:!0,bind:function(t){var i;return null==this.marker?(i=["data",this.view.config.prefix,this.type].join("-").replace("--","-"),this.marker=document.createComment(" rivets: "+this.type+" "),this.iterated=[],t.removeAttribute(i),t.parentNode.insertBefore(this.marker,t),t.parentNode.removeChild(t)):void 0},unbind:function(){var t,i,e,n,s;if(null!=this.iterated){for(n=this.iterated,s=[],i=0,e=n.length;e>i;i++)t=n[i],s.push(t.unbind());return s}},routine:function(i,e){var n,s,r,h,o,u,l,a,d,c,f,p,b,v,g,y,m,w,k,x,E;if(l=this.args[0],e=e||[],this.iterated.length>e.length)for(m=Array(this.iterated.length-e.length),b=0,g=m.length;g>b;b++)s=m[b],p=this.iterated.pop(),p.unbind(),this.marker.parentNode.removeChild(p.els[0]);for(E=[],r=v=0,y=e.length;y>v;r=++v)if(u=e[r],n={},n[l]=u,null==this.iterated[r]){w=this.view.models;for(o in w)u=w[o],null==(k=n[o])&&(n[o]=u);d=this.iterated.length?this.iterated[this.iterated.length-1].els[0]:this.marker,a={binders:this.view.options.binders,formatters:this.view.options.formatters,config:{}},x=this.view.options.config;for(h in x)f=x[h],a.config[h]=f;a.config.preloadData=!0,c=i.cloneNode(!0),p=new t.View(c,n,a),p.bind(),this.iterated.push(p),E.push(this.marker.parentNode.insertBefore(c,d.nextSibling))}else this.iterated[r].models[l]!==u?E.push(this.iterated[r].update(n)):E.push(void 0);return E},update:function(t){var i,e,n,s,r,h,o,u;i={};for(e in t)n=t[e],e!==this.args[0]&&(i[e]=n);for(o=this.iterated,u=[],r=0,h=o.length;h>r;r++)s=o[r],u.push(s.update(i));return u}},"class-*":function(t,i){var e;return e=" "+t.className+" ",!i==(-1!==e.indexOf(" "+this.args[0]+" "))?t.className=i?""+t.className+" "+this.args[0]:e.replace(" "+this.args[0]+" "," ").trim():void 0},"*":function(t,i){return i?t.setAttribute(this.type,i):t.removeAttribute(this.type)}},t.config={preloadData:!0,handler:function(t,i,e){return this.call(t,i,e.view.models)}},t.formatters={},t.factory=function(i){return i.binders=t.binders,i.formatters=t.formatters,i.config=t.config,i.configure=function(i){var e,n;null==i&&(i={});for(e in i)n=i[e],t.config[e]=n},i.bind=function(i,e,n){var s;return null==e&&(e={}),null==n&&(n={}),s=new t.View(i,e,n),s.bind(),s}},"object"==typeof exports?t.factory(exports):"function"==typeof define&&define.amd?define(["exports"],function(i){return t.factory(this.rivets=i),i}):t.factory(this.rivets={})}).call(this); \ No newline at end of file diff --git a/ajax/libs/rivets/0.5.9/rivets.js b/ajax/libs/rivets/0.5.9/rivets.js new file mode 100644 index 000000000..dbe2ee027 --- /dev/null +++ b/ajax/libs/rivets/0.5.9/rivets.js @@ -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); diff --git a/ajax/libs/rivets/0.5.9/rivets.min.js b/ajax/libs/rivets/0.5.9/rivets.min.js new file mode 100644 index 000000000..d2dc67cb6 --- /dev/null +++ b/ajax/libs/rivets/0.5.9/rivets.min.js @@ -0,0 +1,5 @@ +// Rivets.js +// version: 0.5.9 +// author: Michael Richards +// license: MIT +(function(){var t,e=function(t,e){return function(){return t.apply(e,arguments)}},i=[].slice,n=[].indexOf||function(t){for(var e=0,i=this.length;i>e;e++)if(e in this&&this[e]===t)return e;return-1};t={},String.prototype.trim||(String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g,"")}),t.Binding=function(){function n(i,n,s,r,h,o){var u,l,a,d;if(this.view=i,this.el=n,this.type=s,this.key=r,this.keypath=h,this.options=null!=o?o:{},this.update=e(this.update,this),this.unbind=e(this.unbind,this),this.bind=e(this.bind,this),this.publish=e(this.publish,this),this.sync=e(this.sync,this),this.set=e(this.set,this),this.eventHandler=e(this.eventHandler,this),this.formattedValue=e(this.formattedValue,this),!(this.binder=t.internalBinders[this.type]||this.view.binders[s])){d=this.view.binders;for(u in d)a=d[u],"*"!==u&&-1!==u.indexOf("*")&&(l=RegExp("^"+u.replace("*",".+")+"$"),l.test(s)&&(this.binder=a,this.args=RegExp("^"+u.replace("*","(.+)")+"$").exec(s),this.args.shift()))}this.binder||(this.binder=this.view.binders["*"]),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}return n.prototype.formattedValue=function(t){var e,n,s,r,h,o;for(o=this.formatters,r=0,h=o.length;h>r;r++)n=o[r],e=n.split(/\s+/),s=e.shift(),n=this.model[s]instanceof Function?this.model[s]:this.view.formatters[s],(null!=n?n.read:void 0)instanceof Function?t=n.read.apply(n,[t].concat(i.call(e))):n instanceof Function&&(t=n.apply(null,[t].concat(i.call(e))));return t},n.prototype.eventHandler=function(t){var e,i;return i=(e=this).view.config.handler,function(n){return i.call(t,this,n,e)}},n.prototype.set=function(t){var e;return t=t instanceof Function&&!this.binder["function"]?this.formattedValue(t.call(this.model)):this.formattedValue(t),null!=(e=this.binder.routine)?e.call(this,this.el,t):void 0},n.prototype.sync=function(){return this.set(this.options.bypass?this.model[this.keypath]:this.view.config.adapter.read(this.model,this.keypath))},n.prototype.publish=function(){var e,n,s,r,h,o,u,l,a;for(r=t.Util.getInputValue(this.el),u=this.formatters.slice(0).reverse(),h=0,o=u.length;o>h;h++)n=u[h],e=n.split(/\s+/),s=e.shift(),(null!=(l=this.view.formatters[s])?l.publish:void 0)&&(r=(a=this.view.formatters[s]).publish.apply(a,[r].concat(i.call(e))));return this.view.config.adapter.publish(this.model,this.keypath,r)},n.prototype.bind=function(){var t,e,i,n,s,r,h,o,u;if(null!=(r=this.binder.bind)&&r.call(this,this.el),this.options.bypass?this.sync():(this.view.config.adapter.subscribe(this.model,this.keypath,this.sync),this.view.config.preloadData&&this.sync()),null!=(h=this.options.dependencies)?h.length:void 0){for(o=this.options.dependencies,u=[],n=0,s=o.length;s>n;n++)t=o[n],/^\./.test(t)?(i=this.model,e=t.substr(1)):(t=t.split("."),i=this.view.models[t.shift()],e=t.join(".")),u.push(this.view.config.adapter.subscribe(i,e,this.sync));return u}},n.prototype.unbind=function(){var t,e,i,n,s,r,h,o,u;if(null!=(r=this.binder.unbind)&&r.call(this,this.el),this.options.bypass||this.view.config.adapter.unsubscribe(this.model,this.keypath,this.sync),null!=(h=this.options.dependencies)?h.length:void 0){for(o=this.options.dependencies,u=[],n=0,s=o.length;s>n;n++)t=o[n],/^\./.test(t)?(i=this.model,e=t.substr(1)):(t=t.split("."),i=this.view.models[t.shift()],e=t.join(".")),u.push(this.view.config.adapter.unsubscribe(i,e,this.sync));return u}},n.prototype.update=function(t){var e;return null==t&&(t={}),this.key?t[this.key]&&(this.options.bypass||this.view.config.adapter.unsubscribe(this.model,this.keypath,this.sync),this.model=t[this.key],this.options.bypass?this.sync():(this.view.config.adapter.subscribe(this.model,this.keypath,this.sync),this.view.config.preloadData&&this.sync())):this.sync(),null!=(e=this.binder.update)?e.call(this,t):void 0},n}(),t.View=function(){function s(i,n,s){var r,h,o,u,l,a,d,c,p,f;for(this.els=i,this.models=n,this.options=null!=s?s:{},this.update=e(this.update,this),this.publish=e(this.publish,this),this.sync=e(this.sync,this),this.unbind=e(this.unbind,this),this.bind=e(this.bind,this),this.select=e(this.select,this),this.build=e(this.build,this),this.bindingRegExp=e(this.bindingRegExp,this),this.els.jquery||this.els instanceof Array||(this.els=[this.els]),d=["config","binders","formatters"],l=0,a=d.length;a>l;l++){if(h=d[l],this[h]={},this.options[h]){c=this.options[h];for(r in c)o=c[r],this[h][r]=o}p=t[h];for(r in p)o=p[r],null==(f=(u=this[h])[r])&&(u[r]=o)}this.build()}return s.prototype.bindingRegExp=function(){var t;return t=this.config.prefix,t?RegExp("^data-"+t+"-"):/^data-/},s.prototype.build=function(){var e,s,r,h,o,u,l,a,d=this;for(this.bindings=[],o=[],e=this.bindingRegExp(),s=function(e,i,n){var s,r,h,o,u,l,a,c,p,f;return l={},p=function(){var t,e,i,s;for(i=n.split("|"),s=[],t=0,e=i.length;e>t;t++)c=i[t],s.push(c.trim());return s}(),s=function(){var t,e,i,n;for(i=p.shift().split("<"),n=[],t=0,e=i.length;e>t;t++)r=i[t],n.push(r.trim());return n}(),a=s.shift(),f=a.split(/\.|:/),l.formatters=p,l.bypass=-1!==a.indexOf(":"),f[0]?o=f.shift():(o=null,f.shift()),u=f.join("."),(h=s.shift())&&(l.dependencies=h.split(/\s+/)),d.bindings.push(new t.Binding(d,e,i,o,u,l))},h=function(r){var u,l,a,c,p,f,b,v,g,y,m,w,x,k,E,N,j,T,U,V,B,C,O,Q,R,A,D,F,H,L,S,$;if(0>n.call(o,r)){if(r.nodeType===Node.TEXT_NODE){if(v=t.TextTemplateParser,(p=d.config.templateDelimiters)&&(k=v.parse(r.data,p)).length&&(1!==k.length||k[0].type!==v.types.text)){switch(m=k[0],y=k.length>=2?i.call(k,1):[],r.data=m.value,m.type){case 0:r.data=m.value;break;case 1:s(r,"textNode",m.value)}for(j=0,B=y.length;B>j;j++)x=y[j],r.parentNode.appendChild(w=document.createTextNode(x.value)),1===x.type&&s(w,"textNode",x.value)}}else if(null!=r.attributes){for(D=r.attributes,T=0,C=D.length;C>T;T++)if(u=D[T],e.test(u.name)){if(E=u.name.replace(e,""),!(a=d.binders[E])){F=d.binders;for(f in F)N=F[f],"*"!==f&&-1!==f.indexOf("*")&&(g=RegExp("^"+f.replace("*",".+")+"$"),g.test(E)&&(a=N))}if(a||(a=d.binders["*"]),a.block){for(H=r.childNodes,U=0,O=H.length;O>U;U++)b=H[U],o.push(b);l=[u]}}for(L=l||r.attributes,V=0,Q=L.length;Q>V;V++)u=L[V],e.test(u.name)&&(E=u.name.replace(e,""),s(r,E,u.value))}for(S=r.childNodes,$=[],A=0,R=S.length;R>A;A++)c=S[A],$.push(h(c));return $}},a=this.els,u=0,l=a.length;l>u;u++)r=a[u],h(r)},s.prototype.select=function(t){var e,i,n,s,r;for(s=this.bindings,r=[],i=0,n=s.length;n>i;i++)e=s[i],t(e)&&r.push(e);return r},s.prototype.bind=function(){var t,e,i,n,s;for(n=this.bindings,s=[],e=0,i=n.length;i>e;e++)t=n[e],s.push(t.bind());return s},s.prototype.unbind=function(){var t,e,i,n,s;for(n=this.bindings,s=[],e=0,i=n.length;i>e;e++)t=n[e],s.push(t.unbind());return s},s.prototype.sync=function(){var t,e,i,n,s;for(n=this.bindings,s=[],e=0,i=n.length;i>e;e++)t=n[e],s.push(t.sync());return s},s.prototype.publish=function(){var t,e,i,n,s;for(n=this.select(function(t){return t.binder.publishes}),s=[],e=0,i=n.length;i>e;e++)t=n[e],s.push(t.publish());return s},s.prototype.update=function(t){var e,i,n,s,r,h,o;null==t&&(t={});for(i in t)n=t[i],this.models[i]=n;for(h=this.bindings,o=[],s=0,r=h.length;r>s;s++)e=h[s],o.push(e.update(t));return o},s}(),t.TextTemplateParser=function(){function t(){}return t.types={text:0,binding:1},t.parse=function(t,e){var i,n,s,r,h,o,u;for(o=[],r=t.length,i=0,n=0;r>n;){if(i=t.indexOf(e[0],n),0>i){o.push({type:this.types.text,value:t.slice(n)});break}if(i>0&&i>n&&o.push({type:this.types.text,value:t.slice(n,i)}),n=i+2,i=t.indexOf(e[1],n),0>i){h=t.slice(n-2),s=o[o.length-1],(null!=s?s.type:void 0)===this.types.text?s.value+=h:o.push({type:this.types.text,value:h});break}u=t.slice(n,i).trim(),o.push({type:this.types.binding,value:u}),n=i+2}return o},t}(),t.Util={bindEvent:function(t,e,i){return null!=window.jQuery?(t=jQuery(t),null!=t.on?t.on(e,i):t.bind(e,i)):null!=window.addEventListener?t.addEventListener(e,i,!1):(e="on"+e,t.attachEvent(e,i))},unbindEvent:function(t,e,i){return null!=window.jQuery?(t=jQuery(t),null!=t.off?t.off(e,i):t.unbind(e,i)):null!=window.removeEventListener?t.removeEventListener(e,i,!1):(e="on"+e,t.detachEvent(e,i))},getInputValue:function(t){var e,i,n,s;if(null!=window.jQuery)switch(t=jQuery(t),t[0].type){case"checkbox":return t.is(":checked");default:return t.val()}else switch(t.type){case"checkbox":return t.checked;case"select-multiple":for(s=[],i=0,n=t.length;n>i;i++)e=t[i],e.selected&&s.push(e.value);return s;default:return t.value}}},t.binders={enabled:function(t,e){return t.disabled=!e},disabled:function(t,e){return t.disabled=!!e},checked:{publishes:!0,bind:function(e){return t.Util.bindEvent(e,"change",this.publish)},unbind:function(e){return t.Util.unbindEvent(e,"change",this.publish)},routine:function(t,e){var i;return t.checked="radio"===t.type?(null!=(i=t.value)?""+i:void 0)===(null!=e?""+e:void 0):!!e}},unchecked:{publishes:!0,bind:function(e){return t.Util.bindEvent(e,"change",this.publish)},unbind:function(e){return t.Util.unbindEvent(e,"change",this.publish)},routine:function(t,e){var i;return t.checked="radio"===t.type?(null!=(i=t.value)?""+i:void 0)!==(null!=e?""+e:void 0):!e}},show:function(t,e){return t.style.display=e?"":"none"},hide:function(t,e){return t.style.display=e?"none":""},html:function(t,e){return t.innerHTML=null!=e?e:""},value:{publishes:!0,bind:function(e){return t.Util.bindEvent(e,"change",this.publish)},unbind:function(e){return t.Util.unbindEvent(e,"change",this.publish)},routine:function(t,e){var i,s,r,h,o,u,l;if(null!=window.jQuery){if(t=jQuery(t),(null!=e?""+e:void 0)!==(null!=(h=t.val())?""+h:void 0))return t.val(null!=e?e:"")}else if("select-multiple"===t.type){if(null!=e){for(l=[],s=0,r=t.length;r>s;s++)i=t[s],l.push(i.selected=(o=i.value,n.call(e,o)>=0));return l}}else if((null!=e?""+e:void 0)!==(null!=(u=t.value)?""+u:void 0))return t.value=null!=e?e:""}},text:function(t,e){return null!=t.innerText?t.innerText=null!=e?e:"":t.textContent=null!=e?e:""},"if":{block:!0,bind:function(t){var e,i;return null==this.marker?(e=["data",this.view.config.prefix,this.type].join("-").replace("--","-"),i=t.getAttribute(e),this.marker=document.createComment(" rivets: "+this.type+" "+i+" "),t.removeAttribute(e),t.parentNode.insertBefore(this.marker,t),t.parentNode.removeChild(t)):void 0},unbind:function(){var t;return null!=(t=this.nested)?t.unbind():void 0},routine:function(e,i){var n,s,r,h,o;if(!!i==(null==this.nested)){if(i){r={},o=this.view.models;for(n in o)s=o[n],r[n]=s;return h={binders:this.view.options.binders,formatters:this.view.options.formatters,config:this.view.options.config},(this.nested=new t.View(e,r,h)).bind(),this.marker.parentNode.insertBefore(e,this.marker.nextSibling)}return e.parentNode.removeChild(e),this.nested.unbind(),delete this.nested}},update:function(t){return this.nested.update(t)}},unless:{block:!0,bind:function(e){return t.binders["if"].bind.call(this,e)},unbind:function(){return t.binders["if"].unbind.call(this)},routine:function(e,i){return t.binders["if"].routine.call(this,e,!i)},update:function(e){return t.binders["if"].update.call(this,e)}},"on-*":{"function":!0,unbind:function(e){return this.handler?t.Util.unbindEvent(e,this.args[0],this.handler):void 0},routine:function(e,i){return this.handler&&t.Util.unbindEvent(e,this.args[0],this.handler),t.Util.bindEvent(e,this.args[0],this.handler=this.eventHandler(i))}},"each-*":{block:!0,bind:function(t){var e;return null==this.marker?(e=["data",this.view.config.prefix,this.type].join("-").replace("--","-"),this.marker=document.createComment(" rivets: "+this.type+" "),this.iterated=[],t.removeAttribute(e),t.parentNode.insertBefore(this.marker,t),t.parentNode.removeChild(t)):void 0},unbind:function(){var t,e,i,n,s;if(null!=this.iterated){for(n=this.iterated,s=[],e=0,i=n.length;i>e;e++)t=n[e],s.push(t.unbind());return s}},routine:function(e,i){var n,s,r,h,o,u,l,a,d,c,p,f,b,v,g,y,m,w,x,k,E;if(l=this.args[0],i=i||[],this.iterated.length>i.length)for(m=Array(this.iterated.length-i.length),b=0,g=m.length;g>b;b++)s=m[b],f=this.iterated.pop(),f.unbind(),this.marker.parentNode.removeChild(f.els[0]);for(E=[],r=v=0,y=i.length;y>v;r=++v)if(u=i[r],n={},n[l]=u,null==this.iterated[r]){w=this.view.models;for(o in w)u=w[o],null==(x=n[o])&&(n[o]=u);d=this.iterated.length?this.iterated[this.iterated.length-1].els[0]:this.marker,a={binders:this.view.options.binders,formatters:this.view.options.formatters,config:{}},k=this.view.options.config;for(h in k)p=k[h],a.config[h]=p;a.config.preloadData=!0,c=e.cloneNode(!0),f=new t.View(c,n,a),f.bind(),this.iterated.push(f),E.push(this.marker.parentNode.insertBefore(c,d.nextSibling))}else this.iterated[r].models[l]!==u?E.push(this.iterated[r].update(n)):E.push(void 0);return E},update:function(t){var e,i,n,s,r,h,o,u;e={};for(i in t)n=t[i],i!==this.args[0]&&(e[i]=n);for(o=this.iterated,u=[],r=0,h=o.length;h>r;r++)s=o[r],u.push(s.update(e));return u}},"class-*":function(t,e){var i;return i=" "+t.className+" ",!e==(-1!==i.indexOf(" "+this.args[0]+" "))?t.className=e?""+t.className+" "+this.args[0]:i.replace(" "+this.args[0]+" "," ").trim():void 0},"*":function(t,e){return e?t.setAttribute(this.type,e):t.removeAttribute(this.type)}},t.internalBinders={textNode:function(t,e){return t.data=null!=e?e:""}},t.config={preloadData:!0,handler:function(t,e,i){return this.call(t,e,i.view.models)}},t.formatters={},t.factory=function(e){return e._=t,e.binders=t.binders,e.formatters=t.formatters,e.config=t.config,e.configure=function(e){var i,n;null==e&&(e={});for(i in e)n=e[i],t.config[i]=n},e.bind=function(e,i,n){var s;return null==i&&(i={}),null==n&&(n={}),s=new t.View(e,i,n),s.bind(),s}},"object"==typeof exports?t.factory(exports):"function"==typeof define&&define.amd?define(["exports"],function(e){return t.factory(this.rivets=e),e}):t.factory(this.rivets={})}).call(this); \ No newline at end of file diff --git a/ajax/libs/rivets/0.6.0/rivets.js b/ajax/libs/rivets/0.6.0/rivets.js new file mode 100644 index 000000000..47d380c1d --- /dev/null +++ b/ajax/libs/rivets/0.6.0/rivets.js @@ -0,0 +1,1239 @@ +// Rivets.js +// version: 0.6.0 +// author: Michael Richards +// license: MIT +(function() { + var KeypathObserver, Rivets, + __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, + __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; }, + __slice = [].slice, + __hasProp = {}.hasOwnProperty, + __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; + + Rivets = { + binders: {}, + components: {}, + formatters: {}, + adapters: {}, + config: { + prefix: 'rv', + templateDelimiters: ['{', '}'], + rootInterface: '.', + preloadData: true, + handler: function(context, ev, binding) { + return this.call(context, ev, binding.view.models); + } + } + }; + + 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.View = (function() { + function View(els, models, options) { + var k, option, v, _base, _i, _len, _ref, _ref1, _ref2; + 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.componentRegExp = __bind(this.componentRegExp, this); + this.bindingRegExp = __bind(this.bindingRegExp, this); + if (!(this.els.jquery || this.els instanceof Array)) { + this.els = [this.els]; + } + _ref = ['config', 'binders', 'formatters', 'adapters']; + 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 ((_base = this[option])[k] == null) { + _base[k] = v; + } + } + } + this.build(); + } + + View.prototype.bindingRegExp = function() { + return new RegExp("^" + this.config.prefix + "-"); + }; + + View.prototype.componentRegExp = function() { + return new RegExp("^" + (this.config.prefix.toUpperCase()) + "-"); + }; + + View.prototype.build = function() { + var bindingRegExp, buildBinding, componentRegExp, el, parse, skipNodes, _i, _len, _ref, + _this = this; + this.bindings = []; + skipNodes = []; + bindingRegExp = this.bindingRegExp(); + componentRegExp = this.componentRegExp(); + buildBinding = function(binding, node, type, declaration) { + var context, ctx, dependencies, keypath, options, pipe, pipes; + 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; + })(); + keypath = context.shift(); + options.formatters = pipes; + if (dependencies = context.shift()) { + options.dependencies = dependencies.split(/\s+/); + } + return _this.bindings.push(new Rivets[binding](_this, node, type, 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; + if (startToken.type === 0) { + node.data = startToken.value; + } else { + buildBinding('TextBinding', node, null, startToken.value); + } + for (_i = 0, _len = restTokens.length; _i < _len; _i++) { + token = restTokens[_i]; + text = document.createTextNode(token.value); + node.parentNode.appendChild(text); + if (token.type === 1) { + buildBinding('TextBinding', text, null, token.value); + } + } + } + } + } + } else if (componentRegExp.test(node.tagName)) { + type = node.tagName.replace(componentRegExp, '').toLowerCase(); + _this.bindings.push(new Rivets.ComponentBinding(_this, node, type)); + } 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('Binding', 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.Binding = (function() { + function Binding(view, el, type, keypath, options) { + this.view = view; + this.el = el; + this.type = type; + 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); + this.setObserver = __bind(this.setObserver, this); + this.setBinder = __bind(this.setBinder, this); + this.formatters = this.options.formatters || []; + this.dependencies = []; + this.setBinder(); + this.setObserver(); + } + + Binding.prototype.setBinder = function() { + var identifier, regexp, value, _ref; + if (!(this.binder = this.view.binders[this.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(this.type)) { + this.binder = value; + this.args = new RegExp("^" + (identifier.replace('*', '(.+)')) + "$").exec(this.type); + this.args.shift(); + } + } + } + } + this.binder || (this.binder = this.view.binders['*']); + if (this.binder instanceof Function) { + return this.binder = { + routine: this.binder + }; + } + }; + + Binding.prototype.setObserver = function() { + var _this = this; + this.observer = new KeypathObserver(this.view, this.view.models, this.keypath, function(obs) { + if (_this.key) { + _this.unbind(true); + } + _this.model = obs.target; + if (_this.key) { + _this.bind(true); + } + return _this.sync(); + }); + this.key = this.observer.key; + return this.model = this.observer.target; + }; + + 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.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.key ? this.view.adapters[this.key["interface"]].read(this.model, this.key.path) : this.model); + }; + + 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.adapters[this.key["interface"]].publish(this.model, this.key.path, value); + }; + + Binding.prototype.bind = function(silent) { + var dependency, key, observer, _i, _len, _ref, _ref1, _ref2, _results, + _this = this; + if (silent == null) { + silent = false; + } + if (!silent) { + if ((_ref = this.binder.bind) != null) { + _ref.call(this, this.el); + } + } + if (this.key) { + this.view.adapters[this.key["interface"]].subscribe(this.model, this.key.path, this.sync); + } + if (!silent ? this.view.config.preloadData : void 0) { + 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]; + observer = new KeypathObserver(this.view, this.model, dependency, function(obs, prev) { + var key; + key = obs.key; + _this.view.adapters[key["interface"]].unsubscribe(prev, key.path, _this.sync); + _this.view.adapters[key["interface"]].subscribe(obs.target, key.path, _this.sync); + return _this.sync(); + }); + key = observer.key; + this.view.adapters[key["interface"]].subscribe(observer.target, key.path, this.sync); + _results.push(this.dependencies.push(observer)); + } + return _results; + } + }; + + Binding.prototype.unbind = function(silent) { + var key, obs, _i, _len, _ref, _ref1; + if (silent == null) { + silent = false; + } + if (!silent) { + if ((_ref = this.binder.unbind) != null) { + _ref.call(this, this.el); + } + } + if (this.key) { + this.view.adapters[this.key["interface"]].unsubscribe(this.model, this.key.path, this.sync); + } + if (this.dependencies.length) { + _ref1 = this.dependencies; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + obs = _ref1[_i]; + key = obs.key; + this.view.adapters[key["interface"]].unsubscribe(obs.target, key.path, this.sync); + } + return this.dependencies = []; + } + }; + + Binding.prototype.update = function(models) { + var _ref; + if (models == null) { + models = {}; + } + return (_ref = this.binder.update) != null ? _ref.call(this, models) : void 0; + }; + + return Binding; + + })(); + + Rivets.ComponentBinding = (function(_super) { + __extends(ComponentBinding, _super); + + function ComponentBinding(view, el, type) { + var attribute, _i, _len, _ref, _ref1; + this.view = view; + this.el = el; + this.type = type; + this.unbind = __bind(this.unbind, this); + this.bind = __bind(this.bind, this); + this.update = __bind(this.update, this); + this.locals = __bind(this.locals, this); + this.component = Rivets.components[this.type]; + this.attributes = {}; + this.inflections = {}; + _ref = this.el.attributes || []; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + attribute = _ref[_i]; + if (_ref1 = attribute.name, __indexOf.call(this.component.attributes, _ref1) >= 0) { + this.attributes[attribute.name] = attribute.value; + } else { + this.inflections[attribute.name] = attribute.value; + } + } + } + + ComponentBinding.prototype.sync = function() {}; + + ComponentBinding.prototype.locals = function(models) { + var inverse, key, model, path, result, _i, _len, _ref, _ref1; + if (models == null) { + models = this.view.models; + } + result = {}; + _ref = this.inflections; + for (key in _ref) { + inverse = _ref[key]; + _ref1 = inverse.split('.'); + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + path = _ref1[_i]; + result[key] = (result[key] || models)[path]; + } + } + for (key in models) { + model = models[key]; + if (result[key] == null) { + result[key] = model; + } + } + return result; + }; + + ComponentBinding.prototype.update = function(models) { + var _ref; + return (_ref = this.componentView) != null ? _ref.update(this.locals(models)) : void 0; + }; + + ComponentBinding.prototype.bind = function() { + var el, _ref; + if (this.componentView != null) { + return (_ref = this.componentView) != null ? _ref.bind() : void 0; + } else { + el = this.component.build.call(this.attributes); + (this.componentView = new Rivets.View(el, this.locals(), this.view.options)).bind(); + return this.el.parentNode.replaceChild(el, this.el); + } + }; + + ComponentBinding.prototype.unbind = function() { + var _ref; + return (_ref = this.componentView) != null ? _ref.unbind() : void 0; + }; + + return ComponentBinding; + + })(Rivets.Binding); + + Rivets.TextBinding = (function(_super) { + __extends(TextBinding, _super); + + function TextBinding(view, el, type, keypath, options) { + this.view = view; + this.el = el; + this.type = type; + this.keypath = keypath; + this.options = options != null ? options : {}; + this.sync = __bind(this.sync, this); + this.formatters = this.options.formatters || []; + this.dependencies = []; + this.setObserver(); + } + + TextBinding.prototype.binder = { + routine: function(node, value) { + return node.data = value != null ? value : ''; + } + }; + + TextBinding.prototype.sync = function() { + return TextBinding.__super__.sync.apply(this, arguments); + }; + + return TextBinding; + + })(Rivets.Binding); + + Rivets.KeypathParser = (function() { + function KeypathParser() {} + + KeypathParser.parse = function(keypath, interfaces, root) { + var char, current, index, tokens; + tokens = []; + current = { + "interface": root, + path: '' + }; + for (index in keypath) { + char = keypath[index]; + if (__indexOf.call(interfaces, char) >= 0) { + tokens.push(current); + current = { + "interface": char, + path: '' + }; + } else { + current.path += char; + } + } + tokens.push(current); + return tokens; + }; + + return KeypathParser; + + })(); + + 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; + + })(); + + KeypathObserver = (function() { + function KeypathObserver(view, model, keypath, callback) { + this.view = view; + this.model = model; + this.keypath = keypath; + this.callback = callback; + this.realize = __bind(this.realize, this); + this.update = __bind(this.update, this); + this.parse = __bind(this.parse, this); + this.parse(); + this.objectPath = []; + this.target = this.realize(); + } + + KeypathObserver.prototype.parse = function() { + var interfaces, k, path, root, v, _ref; + interfaces = (function() { + var _ref, _results; + _ref = this.view.adapters; + _results = []; + for (k in _ref) { + v = _ref[k]; + _results.push(k); + } + return _results; + }).call(this); + if (_ref = this.keypath[0], __indexOf.call(interfaces, _ref) >= 0) { + root = this.keypath[0]; + path = this.keypath.substr(1); + } else { + root = this.view.config.rootInterface; + path = this.keypath; + } + this.tokens = Rivets.KeypathParser.parse(path, interfaces, root); + return this.key = this.tokens.pop(); + }; + + KeypathObserver.prototype.update = function() { + var next, prev; + if ((next = this.realize()) !== this.target) { + prev = this.target; + this.target = next; + return this.callback(this, prev); + } + }; + + KeypathObserver.prototype.realize = function() { + var current, index, prev, token, _i, _len, _ref; + current = this.model; + _ref = this.tokens; + for (index = _i = 0, _len = _ref.length; _i < _len; index = ++_i) { + token = _ref[index]; + if (this.objectPath[index] != null) { + if (current !== (prev = this.objectPath[index])) { + this.view.adapters[token["interface"]].unsubscribe(prev, token.path, this.update); + this.view.adapters[token["interface"]].subscribe(current, token.path, this.update); + this.objectPath[index] = current; + } + } else { + this.view.adapters[token["interface"]].subscribe(current, token.path, this.update); + this.objectPath[index] = current; + } + current = this.view.adapters[token["interface"]].read(current, token.path); + } + return current; + }; + + return KeypathObserver; + + })(); + + Rivets.binders.enabled = function(el, value) { + return el.disabled = !value; + }; + + Rivets.binders.disabled = function(el, value) { + return el.disabled = !!value; + }; + + Rivets.binders.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; + } + } + }; + + Rivets.binders.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; + } + } + }; + + Rivets.binders.show = function(el, value) { + return el.style.display = value ? '' : 'none'; + }; + + Rivets.binders.hide = function(el, value) { + return el.style.display = value ? 'none' : ''; + }; + + Rivets.binders.html = function(el, value) { + return el.innerHTML = value != null ? value : ''; + }; + + Rivets.binders.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 : ''; + } + } + } + }; + + Rivets.binders.text = function(el, value) { + if (el.innerText != null) { + return el.innerText = value != null ? value : ''; + } else { + return el.textContent = value != null ? value : ''; + } + }; + + Rivets.binders["if"] = { + block: true, + bind: function(el) { + var attr, declaration; + if (this.marker == null) { + attr = [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, + adapters: this.view.options.adapters, + 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) { + var _ref; + return (_ref = this.nested) != null ? _ref.update(models) : void 0; + } + }; + + Rivets.binders.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); + } + }; + + Rivets.binders['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)); + } + }; + + Rivets.binders['each-*'] = { + block: true, + bind: function(el) { + var attr; + if (this.marker == null) { + attr = [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 binding, data, i, index, k, key, model, modelName, options, previous, template, v, view, _i, _j, _k, _len, _len1, _len2, _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]); + } + } + 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 (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, + adapters: this.view.options.adapters, + 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); + this.marker.parentNode.insertBefore(template, previous.nextSibling); + } else if (this.iterated[index].models[modelName] !== model) { + this.iterated[index].update(data); + } + } + if (el.nodeName === 'OPTION') { + _ref3 = this.view.bindings; + _results = []; + for (_k = 0, _len2 = _ref3.length; _k < _len2; _k++) { + binding = _ref3[_k]; + if (binding.el === this.marker.parentNode && binding.type === 'value') { + _results.push(binding.sync()); + } 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; + } + }; + + Rivets.binders['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(); + } + }; + + Rivets.binders['*'] = function(el, value) { + if (value) { + return el.setAttribute(this.type, value); + } else { + return el.removeAttribute(this.type); + } + }; + + Rivets.adapters['.'] = { + id: '_rv', + counter: 0, + weakmap: {}, + weakReference: function(obj) { + var id; + if (obj[this.id] == null) { + id = this.counter++; + this.weakmap[id] = { + callbacks: {} + }; + Object.defineProperty(obj, this.id, { + value: id + }); + } + return this.weakmap[obj[this.id]]; + }, + stubFunction: function(obj, fn) { + var map, original, weakmap; + original = obj[fn]; + map = this.weakReference(obj); + weakmap = this.weakmap; + return obj[fn] = function() { + var callback, k, r, response, _i, _len, _ref, _ref1, _ref2, _ref3; + response = original.apply(obj, arguments); + _ref = map.pointers; + for (r in _ref) { + k = _ref[r]; + _ref3 = (_ref1 = (_ref2 = weakmap[r]) != null ? _ref2.callbacks[k] : void 0) != null ? _ref1 : []; + for (_i = 0, _len = _ref3.length; _i < _len; _i++) { + callback = _ref3[_i]; + callback(); + } + } + return response; + }; + }, + observeMutations: function(obj, ref, keypath) { + var fn, functions, map, _base, _i, _len; + if (Array.isArray(obj)) { + map = this.weakReference(obj); + if (map.pointers == null) { + map.pointers = {}; + functions = ['push', 'pop', 'shift', 'unshift', 'sort', 'reverse', 'splice']; + for (_i = 0, _len = functions.length; _i < _len; _i++) { + fn = functions[_i]; + this.stubFunction(obj, fn); + } + } + if ((_base = map.pointers)[ref] == null) { + _base[ref] = []; + } + if (__indexOf.call(map.pointers[ref], keypath) < 0) { + return map.pointers[ref].push(keypath); + } + } + }, + unobserveMutations: function(obj, ref, keypath) { + var keypaths, _ref; + if (Array.isArray(obj && (obj[this.id] != null))) { + if (keypaths = (_ref = this.weakReference(obj).pointers) != null ? _ref[ref] : void 0) { + return keypaths.splice(keypaths.indexOf(keypath), 1); + } + } + }, + subscribe: function(obj, keypath, callback) { + var callbacks, value, + _this = this; + callbacks = this.weakReference(obj).callbacks; + if (callbacks[keypath] == null) { + callbacks[keypath] = []; + value = obj[keypath]; + Object.defineProperty(obj, keypath, { + get: function() { + return value; + }, + set: function(newValue) { + var _i, _len, _ref; + if (newValue !== value) { + value = newValue; + _ref = callbacks[keypath]; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + callback = _ref[_i]; + callback(); + } + return _this.observeMutations(newValue, obj[_this.id], keypath); + } + } + }); + } + if (__indexOf.call(callbacks[keypath], callback) < 0) { + callbacks[keypath].push(callback); + } + return this.observeMutations(obj[keypath], obj[this.id], keypath); + }, + unsubscribe: function(obj, keypath, callback) { + var callbacks; + callbacks = this.weakmap[obj[this.id]].callbacks[keypath]; + callbacks.splice(callbacks.indexOf(callback), 1); + return this.unobserveMutations(obj[keypath], obj[this.id], keypath); + }, + read: function(obj, keypath) { + return obj[keypath]; + }, + publish: function(obj, keypath, value) { + return obj[keypath] = value; + } + }; + + Rivets.factory = function(exports) { + exports._ = Rivets; + exports.binders = Rivets.binders; + exports.components = Rivets.components; + exports.formatters = Rivets.formatters; + exports.adapters = Rivets.adapters; + 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); diff --git a/ajax/libs/rivets/0.6.0/rivets.min.js b/ajax/libs/rivets/0.6.0/rivets.min.js new file mode 100644 index 000000000..9a941c7db --- /dev/null +++ b/ajax/libs/rivets/0.6.0/rivets.min.js @@ -0,0 +1,5 @@ +// Rivets.js +// version: 0.6.0 +// author: Michael Richards +// license: MIT +(function(){var a,b,c=function(a,b){return function(){return a.apply(b,arguments)}},d=[].indexOf||function(a){for(var b=0,c=this.length;c>b;b++)if(b in this&&this[b]===a)return b;return-1},e=[].slice,f={}.hasOwnProperty,g=function(a,b){function c(){this.constructor=a}for(var d in b)f.call(b,d)&&(a[d]=b[d]);return c.prototype=b.prototype,a.prototype=new c,a.__super__=b.prototype,a};b={binders:{},components:{},formatters:{},adapters:{},config:{prefix:"rv",templateDelimiters:["{","}"],rootInterface:".",preloadData:!0,handler:function(a,b,c){return this.call(a,b,c.view.models)}}},b.Util={bindEvent:function(a,b,c){return null!=window.jQuery?(a=jQuery(a),null!=a.on?a.on(b,c):a.bind(b,c)):null!=window.addEventListener?a.addEventListener(b,c,!1):(b="on"+b,a.attachEvent(b,c))},unbindEvent:function(a,b,c){return null!=window.jQuery?(a=jQuery(a),null!=a.off?a.off(b,c):a.unbind(b,c)):null!=window.removeEventListener?a.removeEventListener(b,c,!1):(b="on"+b,a.detachEvent(b,c))},getInputValue:function(a){var b,c,d,e;if(null!=window.jQuery)switch(a=jQuery(a),a[0].type){case"checkbox":return a.is(":checked");default:return a.val()}else switch(a.type){case"checkbox":return a.checked;case"select-multiple":for(e=[],c=0,d=a.length;d>c;c++)b=a[c],b.selected&&e.push(b.value);return e;default:return a.value}}},b.View=function(){function a(a,d,e){var f,g,h,i,j,k,l,m,n;for(this.els=a,this.models=d,this.options=null!=e?e:{},this.update=c(this.update,this),this.publish=c(this.publish,this),this.sync=c(this.sync,this),this.unbind=c(this.unbind,this),this.bind=c(this.bind,this),this.select=c(this.select,this),this.build=c(this.build,this),this.componentRegExp=c(this.componentRegExp,this),this.bindingRegExp=c(this.bindingRegExp,this),this.els.jquery||this.els instanceof Array||(this.els=[this.els]),l=["config","binders","formatters","adapters"],j=0,k=l.length;k>j;j++){if(g=l[j],this[g]={},this.options[g]){m=this.options[g];for(f in m)h=m[f],this[g][f]=h}n=b[g];for(f in n)h=n[f],null==(i=this[g])[f]&&(i[f]=h)}this.build()}return a.prototype.bindingRegExp=function(){return new RegExp("^"+this.config.prefix+"-")},a.prototype.componentRegExp=function(){return new RegExp("^"+this.config.prefix.toUpperCase()+"-")},a.prototype.build=function(){var a,c,f,g,h,i,j,k,l,m=this;for(this.bindings=[],i=[],a=this.bindingRegExp(),f=this.componentRegExp(),c=function(a,c,d,e){var f,g,h,i,j,k,l;return j={},l=function(){var a,b,c,d;for(c=e.split("|"),d=[],a=0,b=c.length;b>a;a++)k=c[a],d.push(k.trim());return d}(),f=function(){var a,b,c,d;for(c=l.shift().split("<"),d=[],a=0,b=c.length;b>a;a++)g=c[a],d.push(g.trim());return d}(),i=f.shift(),j.formatters=l,(h=f.shift())&&(j.dependencies=h.split(/\s+/)),m.bindings.push(new b[a](m,c,d,i,j))},h=function(g){var j,k,l,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M,N,O,P;if(d.call(i,g)<0){if(g.nodeType===Node.TEXT_NODE){if(r=b.TextTemplateParser,(o=m.config.templateDelimiters)&&(x=r.parse(g.data,o)).length&&(1!==x.length||x[0].type!==r.types.text))for(u=x[0],t=2<=x.length?e.call(x,1):[],g.data=u.value,0===u.type?g.data=u.value:c("TextBinding",g,null,u.value),A=0,E=t.length;E>A;A++)w=t[A],v=document.createTextNode(w.value),g.parentNode.appendChild(v),1===w.type&&c("TextBinding",v,null,w.value)}else if(f.test(g.tagName))y=g.tagName.replace(f,"").toLowerCase(),m.bindings.push(new b.ComponentBinding(m,g,y));else if(null!=g.attributes){for(K=g.attributes,B=0,F=K.length;F>B;B++)if(j=K[B],a.test(j.name)){if(y=j.name.replace(a,""),!(l=m.binders[y])){L=m.binders;for(p in L)z=L[p],"*"!==p&&-1!==p.indexOf("*")&&(s=new RegExp("^"+p.replace("*",".+")+"$"),s.test(y)&&(l=z))}if(l||(l=m.binders["*"]),l.block){for(M=g.childNodes,C=0,G=M.length;G>C;C++)q=M[C],i.push(q);k=[j]}}for(N=k||g.attributes,D=0,H=N.length;H>D;D++)j=N[D],a.test(j.name)&&(y=j.name.replace(a,""),c("Binding",g,y,j.value))}for(O=g.childNodes,P=[],J=0,I=O.length;I>J;J++)n=O[J],P.push(h(n));return P}},l=this.els,j=0,k=l.length;k>j;j++)g=l[j],h(g)},a.prototype.select=function(a){var b,c,d,e,f;for(e=this.bindings,f=[],c=0,d=e.length;d>c;c++)b=e[c],a(b)&&f.push(b);return f},a.prototype.bind=function(){var a,b,c,d,e;for(d=this.bindings,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.bind());return e},a.prototype.unbind=function(){var a,b,c,d,e;for(d=this.bindings,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.unbind());return e},a.prototype.sync=function(){var a,b,c,d,e;for(d=this.bindings,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.sync());return e},a.prototype.publish=function(){var a,b,c,d,e;for(d=this.select(function(a){return a.binder.publishes}),e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.publish());return e},a.prototype.update=function(a){var b,c,d,e,f,g,h;null==a&&(a={});for(c in a)d=a[c],this.models[c]=d;for(g=this.bindings,h=[],e=0,f=g.length;f>e;e++)b=g[e],h.push(b.update(a));return h},a}(),b.Binding=function(){function d(a,b,d,e,f){this.view=a,this.el=b,this.type=d,this.keypath=e,this.options=null!=f?f:{},this.update=c(this.update,this),this.unbind=c(this.unbind,this),this.bind=c(this.bind,this),this.publish=c(this.publish,this),this.sync=c(this.sync,this),this.set=c(this.set,this),this.eventHandler=c(this.eventHandler,this),this.formattedValue=c(this.formattedValue,this),this.setObserver=c(this.setObserver,this),this.setBinder=c(this.setBinder,this),this.formatters=this.options.formatters||[],this.dependencies=[],this.setBinder(),this.setObserver()}return d.prototype.setBinder=function(){var a,b,c,d;if(!(this.binder=this.view.binders[this.type])){d=this.view.binders;for(a in d)c=d[a],"*"!==a&&-1!==a.indexOf("*")&&(b=new RegExp("^"+a.replace("*",".+")+"$"),b.test(this.type)&&(this.binder=c,this.args=new RegExp("^"+a.replace("*","(.+)")+"$").exec(this.type),this.args.shift()))}return this.binder||(this.binder=this.view.binders["*"]),this.binder instanceof Function?this.binder={routine:this.binder}:void 0},d.prototype.setObserver=function(){var b=this;return this.observer=new a(this.view,this.view.models,this.keypath,function(a){return b.key&&b.unbind(!0),b.model=a.target,b.key&&b.bind(!0),b.sync()}),this.key=this.observer.key,this.model=this.observer.target},d.prototype.formattedValue=function(a){var b,c,d,f,g,h;for(h=this.formatters,f=0,g=h.length;g>f;f++)c=h[f],b=c.split(/\s+/),d=b.shift(),c=this.view.formatters[d],(null!=c?c.read:void 0)instanceof Function?a=c.read.apply(c,[a].concat(e.call(b))):c instanceof Function&&(a=c.apply(null,[a].concat(e.call(b))));return a},d.prototype.eventHandler=function(a){var b,c;return c=(b=this).view.config.handler,function(d){return c.call(a,this,d,b)}},d.prototype.set=function(a){var b;return a=a instanceof Function&&!this.binder["function"]?this.formattedValue(a.call(this.model)):this.formattedValue(a),null!=(b=this.binder.routine)?b.call(this,this.el,a):void 0},d.prototype.sync=function(){return this.set(this.key?this.view.adapters[this.key["interface"]].read(this.model,this.key.path):this.model)},d.prototype.publish=function(){var a,c,d,f,g,h,i,j,k;for(f=b.Util.getInputValue(this.el),i=this.formatters.slice(0).reverse(),g=0,h=i.length;h>g;g++)c=i[g],a=c.split(/\s+/),d=a.shift(),(null!=(j=this.view.formatters[d])?j.publish:void 0)&&(f=(k=this.view.formatters[d]).publish.apply(k,[f].concat(e.call(a))));return this.view.adapters[this.key["interface"]].publish(this.model,this.key.path,f)},d.prototype.bind=function(b){var c,d,e,f,g,h,i,j,k,l=this;if(null==b&&(b=!1),b||null!=(h=this.binder.bind)&&h.call(this,this.el),this.key&&this.view.adapters[this.key["interface"]].subscribe(this.model,this.key.path,this.sync),(b?void 0:this.view.config.preloadData)&&this.sync(),null!=(i=this.options.dependencies)?i.length:void 0){for(j=this.options.dependencies,k=[],f=0,g=j.length;g>f;f++)c=j[f],e=new a(this.view,this.model,c,function(a,b){var c;return c=a.key,l.view.adapters[c["interface"]].unsubscribe(b,c.path,l.sync),l.view.adapters[c["interface"]].subscribe(a.target,c.path,l.sync),l.sync()}),d=e.key,this.view.adapters[d["interface"]].subscribe(e.target,d.path,this.sync),k.push(this.dependencies.push(e));return k}},d.prototype.unbind=function(a){var b,c,d,e,f,g;if(null==a&&(a=!1),a||null!=(f=this.binder.unbind)&&f.call(this,this.el),this.key&&this.view.adapters[this.key["interface"]].unsubscribe(this.model,this.key.path,this.sync),this.dependencies.length){for(g=this.dependencies,d=0,e=g.length;e>d;d++)c=g[d],b=c.key,this.view.adapters[b["interface"]].unsubscribe(c.target,b.path,this.sync);return this.dependencies=[]}},d.prototype.update=function(a){var b;return null==a&&(a={}),null!=(b=this.binder.update)?b.call(this,a):void 0},d}(),b.ComponentBinding=function(a){function e(a,e,f){var g,h,i,j,k;for(this.view=a,this.el=e,this.type=f,this.unbind=c(this.unbind,this),this.bind=c(this.bind,this),this.update=c(this.update,this),this.locals=c(this.locals,this),this.component=b.components[this.type],this.attributes={},this.inflections={},j=this.el.attributes||[],h=0,i=j.length;i>h;h++)g=j[h],k=g.name,d.call(this.component.attributes,k)>=0?this.attributes[g.name]=g.value:this.inflections[g.name]=g.value}return g(e,a),e.prototype.sync=function(){},e.prototype.locals=function(a){var b,c,d,e,f,g,h,i,j;null==a&&(a=this.view.models),f={},i=this.inflections;for(c in i)for(b=i[c],j=b.split("."),g=0,h=j.length;h>g;g++)e=j[g],f[c]=(f[c]||a)[e];for(c in a)d=a[c],null==f[c]&&(f[c]=d);return f},e.prototype.update=function(a){var b;return null!=(b=this.componentView)?b.update(this.locals(a)):void 0},e.prototype.bind=function(){var a,c;return null!=this.componentView?null!=(c=this.componentView)?c.bind():void 0:(a=this.component.build.call(this.attributes),(this.componentView=new b.View(a,this.locals(),this.view.options)).bind(),this.el.parentNode.replaceChild(a,this.el))},e.prototype.unbind=function(){var a;return null!=(a=this.componentView)?a.unbind():void 0},e}(b.Binding),b.TextBinding=function(a){function b(a,b,d,e,f){this.view=a,this.el=b,this.type=d,this.keypath=e,this.options=null!=f?f:{},this.sync=c(this.sync,this),this.formatters=this.options.formatters||[],this.dependencies=[],this.setObserver()}return g(b,a),b.prototype.binder={routine:function(a,b){return a.data=null!=b?b:""}},b.prototype.sync=function(){return b.__super__.sync.apply(this,arguments)},b}(b.Binding),b.KeypathParser=function(){function a(){}return a.parse=function(a,b,c){var e,f,g,h;h=[],f={"interface":c,path:""};for(g in a)e=a[g],d.call(b,e)>=0?(h.push(f),f={"interface":e,path:""}):f.path+=e;return h.push(f),h},a}(),b.TextTemplateParser=function(){function a(){}return a.types={text:0,binding:1},a.parse=function(a,b){var c,d,e,f,g,h,i;for(h=[],f=a.length,c=0,d=0;f>d;){if(c=a.indexOf(b[0],d),0>c){h.push({type:this.types.text,value:a.slice(d)});break}if(c>0&&c>d&&h.push({type:this.types.text,value:a.slice(d,c)}),d=c+2,c=a.indexOf(b[1],d),0>c){g=a.slice(d-2),e=h[h.length-1],(null!=e?e.type:void 0)===this.types.text?e.value+=g:h.push({type:this.types.text,value:g});break}i=a.slice(d,c).trim(),h.push({type:this.types.binding,value:i}),d=c+2}return h},a}(),a=function(){function a(a,b,d,e){this.view=a,this.model=b,this.keypath=d,this.callback=e,this.realize=c(this.realize,this),this.update=c(this.update,this),this.parse=c(this.parse,this),this.parse(),this.objectPath=[],this.target=this.realize()}return a.prototype.parse=function(){var a,c,e,f,g,h;return a=function(){var a,b;a=this.view.adapters,b=[];for(c in a)g=a[c],b.push(c);return b}.call(this),h=this.keypath[0],d.call(a,h)>=0?(f=this.keypath[0],e=this.keypath.substr(1)):(f=this.view.config.rootInterface,e=this.keypath),this.tokens=b.KeypathParser.parse(e,a,f),this.key=this.tokens.pop()},a.prototype.update=function(){var a,b;return(a=this.realize())!==this.target?(b=this.target,this.target=a,this.callback(this,b)):void 0},a.prototype.realize=function(){var a,b,c,d,e,f,g;for(a=this.model,g=this.tokens,b=e=0,f=g.length;f>e;b=++e)d=g[b],null!=this.objectPath[b]?a!==(c=this.objectPath[b])&&(this.view.adapters[d["interface"]].unsubscribe(c,d.path,this.update),this.view.adapters[d["interface"]].subscribe(a,d.path,this.update),this.objectPath[b]=a):(this.view.adapters[d["interface"]].subscribe(a,d.path,this.update),this.objectPath[b]=a),a=this.view.adapters[d["interface"]].read(a,d.path);return a},a}(),b.binders.enabled=function(a,b){return a.disabled=!b},b.binders.disabled=function(a,b){return a.disabled=!!b},b.binders.checked={publishes:!0,bind:function(a){return b.Util.bindEvent(a,"change",this.publish)},unbind:function(a){return b.Util.unbindEvent(a,"change",this.publish)},routine:function(a,b){var c;return a.checked="radio"===a.type?(null!=(c=a.value)?c.toString():void 0)===(null!=b?b.toString():void 0):!!b}},b.binders.unchecked={publishes:!0,bind:function(a){return b.Util.bindEvent(a,"change",this.publish)},unbind:function(a){return b.Util.unbindEvent(a,"change",this.publish)},routine:function(a,b){var c;return a.checked="radio"===a.type?(null!=(c=a.value)?c.toString():void 0)!==(null!=b?b.toString():void 0):!b}},b.binders.show=function(a,b){return a.style.display=b?"":"none"},b.binders.hide=function(a,b){return a.style.display=b?"none":""},b.binders.html=function(a,b){return a.innerHTML=null!=b?b:""},b.binders.value={publishes:!0,bind:function(a){return b.Util.bindEvent(a,"change",this.publish)},unbind:function(a){return b.Util.unbindEvent(a,"change",this.publish)},routine:function(a,b){var c,e,f,g,h,i,j;if(null!=window.jQuery){if(a=jQuery(a),(null!=b?b.toString():void 0)!==(null!=(g=a.val())?g.toString():void 0))return a.val(null!=b?b:"")}else if("select-multiple"===a.type){if(null!=b){for(j=[],e=0,f=a.length;f>e;e++)c=a[e],j.push(c.selected=(h=c.value,d.call(b,h)>=0));return j}}else if((null!=b?b.toString():void 0)!==(null!=(i=a.value)?i.toString():void 0))return a.value=null!=b?b:""}},b.binders.text=function(a,b){return null!=a.innerText?a.innerText=null!=b?b:"":a.textContent=null!=b?b:""},b.binders["if"]={block:!0,bind:function(a){var b,c;return null==this.marker?(b=[this.view.config.prefix,this.type].join("-").replace("--","-"),c=a.getAttribute(b),this.marker=document.createComment(" rivets: "+this.type+" "+c+" "),a.removeAttribute(b),a.parentNode.insertBefore(this.marker,a),a.parentNode.removeChild(a)):void 0},unbind:function(){var a;return null!=(a=this.nested)?a.unbind():void 0},routine:function(a,c){var d,e,f,g,h;if(!!c==(null==this.nested)){if(c){f={},h=this.view.models;for(d in h)e=h[d],f[d]=e;return g={binders:this.view.options.binders,formatters:this.view.options.formatters,adapters:this.view.options.adapters,config:this.view.options.config},(this.nested=new b.View(a,f,g)).bind(),this.marker.parentNode.insertBefore(a,this.marker.nextSibling)}return a.parentNode.removeChild(a),this.nested.unbind(),delete this.nested}},update:function(a){var b;return null!=(b=this.nested)?b.update(a):void 0}},b.binders.unless={block:!0,bind:function(a){return b.binders["if"].bind.call(this,a)},unbind:function(){return b.binders["if"].unbind.call(this)},routine:function(a,c){return b.binders["if"].routine.call(this,a,!c)},update:function(a){return b.binders["if"].update.call(this,a)}},b.binders["on-*"]={"function":!0,unbind:function(a){return this.handler?b.Util.unbindEvent(a,this.args[0],this.handler):void 0},routine:function(a,c){return this.handler&&b.Util.unbindEvent(a,this.args[0],this.handler),b.Util.bindEvent(a,this.args[0],this.handler=this.eventHandler(c))}},b.binders["each-*"]={block:!0,bind:function(a){var b;return null==this.marker?(b=[this.view.config.prefix,this.type].join("-").replace("--","-"),this.marker=document.createComment(" rivets: "+this.type+" "),this.iterated=[],a.removeAttribute(b),a.parentNode.insertBefore(this.marker,a),a.parentNode.removeChild(a)):void 0},unbind:function(){var a,b,c,d,e;if(null!=this.iterated){for(d=this.iterated,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.unbind());return e}},routine:function(a,c){var d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A;if(k=this.args[0],c=c||[],this.iterated.length>c.length)for(w=Array(this.iterated.length-c.length),q=0,t=w.length;t>q;q++)f=w[q],p=this.iterated.pop(),p.unbind(),this.marker.parentNode.removeChild(p.els[0]);for(g=r=0,u=c.length;u>r;g=++r)if(j=c[g],e={},e[k]=j,null==this.iterated[g]){x=this.view.models;for(i in x)j=x[i],null==e[i]&&(e[i]=j);m=this.iterated.length?this.iterated[this.iterated.length-1].els[0]:this.marker,l={binders:this.view.options.binders,formatters:this.view.options.formatters,adapters:this.view.options.adapters,config:{}},y=this.view.options.config;for(h in y)o=y[h],l.config[h]=o;l.config.preloadData=!0,n=a.cloneNode(!0),p=new b.View(n,e,l),p.bind(),this.iterated.push(p),this.marker.parentNode.insertBefore(n,m.nextSibling)}else this.iterated[g].models[k]!==j&&this.iterated[g].update(e);if("OPTION"===a.nodeName){for(z=this.view.bindings,A=[],s=0,v=z.length;v>s;s++)d=z[s],d.el===this.marker.parentNode&&"value"===d.type?A.push(d.sync()):A.push(void 0);return A}},update:function(a){var b,c,d,e,f,g,h,i;b={};for(c in a)d=a[c],c!==this.args[0]&&(b[c]=d);for(h=this.iterated,i=[],f=0,g=h.length;g>f;f++)e=h[f],i.push(e.update(b));return i}},b.binders["class-*"]=function(a,b){var c;return c=" "+a.className+" ",!b==(-1!==c.indexOf(" "+this.args[0]+" "))?a.className=b?""+a.className+" "+this.args[0]:c.replace(" "+this.args[0]+" "," ").trim():void 0},b.binders["*"]=function(a,b){return b?a.setAttribute(this.type,b):a.removeAttribute(this.type)},b.adapters["."]={id:"_rv",counter:0,weakmap:{},weakReference:function(a){var b;return null==a[this.id]&&(b=this.counter++,this.weakmap[b]={callbacks:{}},Object.defineProperty(a,this.id,{value:b})),this.weakmap[a[this.id]]},stubFunction:function(a,b){var c,d,e;return d=a[b],c=this.weakReference(a),e=this.weakmap,a[b]=function(){var b,f,g,h,i,j,k,l,m,n;h=d.apply(a,arguments),k=c.pointers;for(g in k)for(f=k[g],n=null!=(l=null!=(m=e[g])?m.callbacks[f]:void 0)?l:[],i=0,j=n.length;j>i;i++)b=n[i],b();return h}},observeMutations:function(a,b,c){var e,f,g,h,i,j;if(Array.isArray(a)){if(g=this.weakReference(a),null==g.pointers)for(g.pointers={},f=["push","pop","shift","unshift","sort","reverse","splice"],i=0,j=f.length;j>i;i++)e=f[i],this.stubFunction(a,e);if(null==(h=g.pointers)[b]&&(h[b]=[]),d.call(g.pointers[b],c)<0)return g.pointers[b].push(c)}},unobserveMutations:function(a,b,c){var d,e;return Array.isArray(a&&null!=a[this.id])&&(d=null!=(e=this.weakReference(a).pointers)?e[b]:void 0)?d.splice(d.indexOf(c),1):void 0},subscribe:function(a,b,c){var e,f,g=this;return e=this.weakReference(a).callbacks,null==e[b]&&(e[b]=[],f=a[b],Object.defineProperty(a,b,{get:function(){return f},set:function(d){var h,i,j;if(d!==f){for(f=d,j=e[b],h=0,i=j.length;i>h;h++)c=j[h],c();return g.observeMutations(d,a[g.id],b)}}})),d.call(e[b],c)<0&&e[b].push(c),this.observeMutations(a[b],a[this.id],b)},unsubscribe:function(a,b,c){var d;return d=this.weakmap[a[this.id]].callbacks[b],d.splice(d.indexOf(c),1),this.unobserveMutations(a[b],a[this.id],b)},read:function(a,b){return a[b]},publish:function(a,b,c){return a[b]=c}},b.factory=function(a){return a._=b,a.binders=b.binders,a.components=b.components,a.formatters=b.formatters,a.adapters=b.adapters,a.config=b.config,a.configure=function(a){var c,d;null==a&&(a={});for(c in a)d=a[c],b.config[c]=d},a.bind=function(a,c,d){var e;return null==c&&(c={}),null==d&&(d={}),e=new b.View(a,c,d),e.bind(),e}},"object"==typeof exports?b.factory(exports):"function"==typeof define&&define.amd?define(["exports"],function(a){return b.factory(this.rivets=a),a}):b.factory(this.rivets={})}).call(this); \ No newline at end of file diff --git a/ajax/libs/rivets/0.6.1/rivets.js b/ajax/libs/rivets/0.6.1/rivets.js new file mode 100644 index 000000000..ad6c19e9f --- /dev/null +++ b/ajax/libs/rivets/0.6.1/rivets.js @@ -0,0 +1,1243 @@ +// Rivets.js +// version: 0.6.1 +// author: Michael Richards +// license: MIT +(function() { + var KeypathObserver, Rivets, + __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, + __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; }, + __slice = [].slice, + __hasProp = {}.hasOwnProperty, + __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; + + Rivets = { + binders: {}, + components: {}, + formatters: {}, + adapters: {}, + config: { + prefix: 'rv', + templateDelimiters: ['{', '}'], + rootInterface: '.', + preloadData: true, + handler: function(context, ev, binding) { + return this.call(context, ev, binding.view.models); + } + } + }; + + 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.View = (function() { + function View(els, models, options) { + var k, option, v, _base, _i, _len, _ref, _ref1, _ref2; + 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.componentRegExp = __bind(this.componentRegExp, this); + this.bindingRegExp = __bind(this.bindingRegExp, this); + if (!(this.els.jquery || this.els instanceof Array)) { + this.els = [this.els]; + } + _ref = ['config', 'binders', 'formatters', 'adapters']; + 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 ((_base = this[option])[k] == null) { + _base[k] = v; + } + } + } + this.build(); + } + + View.prototype.bindingRegExp = function() { + return new RegExp("^" + this.config.prefix + "-"); + }; + + View.prototype.componentRegExp = function() { + return new RegExp("^" + (this.config.prefix.toUpperCase()) + "-"); + }; + + View.prototype.build = function() { + var bindingRegExp, buildBinding, componentRegExp, el, parse, skipNodes, _i, _len, _ref, + _this = this; + this.bindings = []; + skipNodes = []; + bindingRegExp = this.bindingRegExp(); + componentRegExp = this.componentRegExp(); + buildBinding = function(binding, node, type, declaration) { + var context, ctx, dependencies, keypath, options, pipe, pipes; + 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; + })(); + keypath = context.shift(); + options.formatters = pipes; + if (dependencies = context.shift()) { + options.dependencies = dependencies.split(/\s+/); + } + return _this.bindings.push(new Rivets[binding](_this, node, type, keypath, options)); + }; + parse = function(node) { + var attribute, attributes, binder, childNode, delimiters, identifier, n, parser, regexp, 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)) { + for (_i = 0, _len = tokens.length; _i < _len; _i++) { + token = tokens[_i]; + text = document.createTextNode(token.value); + node.parentNode.insertBefore(text, node); + if (token.type === 1) { + buildBinding('TextBinding', text, null, token.value); + } + } + node.parentNode.removeChild(node); + } + } + } + } else if (componentRegExp.test(node.tagName)) { + type = node.tagName.replace(componentRegExp, '').toLowerCase(); + _this.bindings.push(new Rivets.ComponentBinding(_this, node, type)); + } 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('Binding', node, type, attribute.value); + } + } + } + _ref4 = (function() { + var _len4, _n, _ref4, _results1; + _ref4 = node.childNodes; + _results1 = []; + for (_n = 0, _len4 = _ref4.length; _n < _len4; _n++) { + n = _ref4[_n]; + _results1.push(n); + } + return _results1; + })(); + _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.Binding = (function() { + function Binding(view, el, type, keypath, options) { + this.view = view; + this.el = el; + this.type = type; + 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); + this.setObserver = __bind(this.setObserver, this); + this.setBinder = __bind(this.setBinder, this); + this.formatters = this.options.formatters || []; + this.dependencies = []; + this.setBinder(); + this.setObserver(); + } + + Binding.prototype.setBinder = function() { + var identifier, regexp, value, _ref; + if (!(this.binder = this.view.binders[this.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(this.type)) { + this.binder = value; + this.args = new RegExp("^" + (identifier.replace('*', '(.+)')) + "$").exec(this.type); + this.args.shift(); + } + } + } + } + this.binder || (this.binder = this.view.binders['*']); + if (this.binder instanceof Function) { + return this.binder = { + routine: this.binder + }; + } + }; + + Binding.prototype.setObserver = function() { + var _this = this; + this.observer = new KeypathObserver(this.view, this.view.models, this.keypath, function(obs) { + if (_this.key) { + _this.unbind(true); + } + _this.model = obs.target; + if (_this.key) { + _this.bind(true); + } + return _this.sync(); + }); + this.key = this.observer.key; + return this.model = this.observer.target; + }; + + 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.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.key ? this.view.adapters[this.key["interface"]].read(this.model, this.key.path) : this.model); + }; + + 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.adapters[this.key["interface"]].publish(this.model, this.key.path, value); + }; + + Binding.prototype.bind = function(silent) { + var dependency, key, observer, _i, _len, _ref, _ref1, _ref2, _results, + _this = this; + if (silent == null) { + silent = false; + } + if (!silent) { + if ((_ref = this.binder.bind) != null) { + _ref.call(this, this.el); + } + } + if (this.key) { + this.view.adapters[this.key["interface"]].subscribe(this.model, this.key.path, this.sync); + } + if (!silent ? this.view.config.preloadData : void 0) { + 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]; + observer = new KeypathObserver(this.view, this.model, dependency, function(obs, prev) { + var key; + key = obs.key; + _this.view.adapters[key["interface"]].unsubscribe(prev, key.path, _this.sync); + _this.view.adapters[key["interface"]].subscribe(obs.target, key.path, _this.sync); + return _this.sync(); + }); + key = observer.key; + this.view.adapters[key["interface"]].subscribe(observer.target, key.path, this.sync); + _results.push(this.dependencies.push(observer)); + } + return _results; + } + }; + + Binding.prototype.unbind = function(silent) { + var key, obs, _i, _len, _ref, _ref1; + if (silent == null) { + silent = false; + } + if (!silent) { + if ((_ref = this.binder.unbind) != null) { + _ref.call(this, this.el); + } + } + if (this.key) { + this.view.adapters[this.key["interface"]].unsubscribe(this.model, this.key.path, this.sync); + } + if (this.dependencies.length) { + _ref1 = this.dependencies; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + obs = _ref1[_i]; + key = obs.key; + this.view.adapters[key["interface"]].unsubscribe(obs.target, key.path, this.sync); + } + return this.dependencies = []; + } + }; + + Binding.prototype.update = function(models) { + var _ref; + if (models == null) { + models = {}; + } + return (_ref = this.binder.update) != null ? _ref.call(this, models) : void 0; + }; + + return Binding; + + })(); + + Rivets.ComponentBinding = (function(_super) { + __extends(ComponentBinding, _super); + + function ComponentBinding(view, el, type) { + var attribute, _i, _len, _ref, _ref1; + this.view = view; + this.el = el; + this.type = type; + this.unbind = __bind(this.unbind, this); + this.bind = __bind(this.bind, this); + this.update = __bind(this.update, this); + this.locals = __bind(this.locals, this); + this.component = Rivets.components[this.type]; + this.attributes = {}; + this.inflections = {}; + _ref = this.el.attributes || []; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + attribute = _ref[_i]; + if (_ref1 = attribute.name, __indexOf.call(this.component.attributes, _ref1) >= 0) { + this.attributes[attribute.name] = attribute.value; + } else { + this.inflections[attribute.name] = attribute.value; + } + } + } + + ComponentBinding.prototype.sync = function() {}; + + ComponentBinding.prototype.locals = function(models) { + var inverse, key, model, path, result, _i, _len, _ref, _ref1; + if (models == null) { + models = this.view.models; + } + result = {}; + _ref = this.inflections; + for (key in _ref) { + inverse = _ref[key]; + _ref1 = inverse.split('.'); + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + path = _ref1[_i]; + result[key] = (result[key] || models)[path]; + } + } + for (key in models) { + model = models[key]; + if (result[key] == null) { + result[key] = model; + } + } + return result; + }; + + ComponentBinding.prototype.update = function(models) { + var _ref; + return (_ref = this.componentView) != null ? _ref.update(this.locals(models)) : void 0; + }; + + ComponentBinding.prototype.bind = function() { + var el, _ref; + if (this.componentView != null) { + return (_ref = this.componentView) != null ? _ref.bind() : void 0; + } else { + el = this.component.build.call(this.attributes); + (this.componentView = new Rivets.View(el, this.locals(), this.view.options)).bind(); + return this.el.parentNode.replaceChild(el, this.el); + } + }; + + ComponentBinding.prototype.unbind = function() { + var _ref; + return (_ref = this.componentView) != null ? _ref.unbind() : void 0; + }; + + return ComponentBinding; + + })(Rivets.Binding); + + Rivets.TextBinding = (function(_super) { + __extends(TextBinding, _super); + + function TextBinding(view, el, type, keypath, options) { + this.view = view; + this.el = el; + this.type = type; + this.keypath = keypath; + this.options = options != null ? options : {}; + this.sync = __bind(this.sync, this); + this.formatters = this.options.formatters || []; + this.dependencies = []; + this.setObserver(); + } + + TextBinding.prototype.binder = { + routine: function(node, value) { + return node.data = value != null ? value : ''; + } + }; + + TextBinding.prototype.sync = function() { + return TextBinding.__super__.sync.apply(this, arguments); + }; + + return TextBinding; + + })(Rivets.Binding); + + Rivets.KeypathParser = (function() { + function KeypathParser() {} + + KeypathParser.parse = function(keypath, interfaces, root) { + var char, current, index, tokens; + tokens = []; + current = { + "interface": root, + path: '' + }; + for (index in keypath) { + char = keypath[index]; + if (__indexOf.call(interfaces, char) >= 0) { + tokens.push(current); + current = { + "interface": char, + path: '' + }; + } else { + current.path += char; + } + } + tokens.push(current); + return tokens; + }; + + return KeypathParser; + + })(); + + Rivets.TextTemplateParser = (function() { + function TextTemplateParser() {} + + TextTemplateParser.types = { + text: 0, + binding: 1 + }; + + TextTemplateParser.parse = function(template, delimiters) { + var delimiterOffset, index, lastIndex, lastToken, length, substring, tokens, value; + tokens = []; + length = template.length; + delimiterOffset = delimiters[1].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 + delimiterOffset; + } + } + return tokens; + }; + + return TextTemplateParser; + + })(); + + KeypathObserver = (function() { + function KeypathObserver(view, model, keypath, callback) { + this.view = view; + this.model = model; + this.keypath = keypath; + this.callback = callback; + this.realize = __bind(this.realize, this); + this.update = __bind(this.update, this); + this.parse = __bind(this.parse, this); + this.parse(); + this.objectPath = []; + this.target = this.realize(); + } + + KeypathObserver.prototype.parse = function() { + var interfaces, k, path, root, v, _ref; + interfaces = (function() { + var _ref, _results; + _ref = this.view.adapters; + _results = []; + for (k in _ref) { + v = _ref[k]; + _results.push(k); + } + return _results; + }).call(this); + if (_ref = this.keypath[0], __indexOf.call(interfaces, _ref) >= 0) { + root = this.keypath[0]; + path = this.keypath.substr(1); + } else { + root = this.view.config.rootInterface; + path = this.keypath; + } + this.tokens = Rivets.KeypathParser.parse(path, interfaces, root); + return this.key = this.tokens.pop(); + }; + + KeypathObserver.prototype.update = function() { + var next, prev; + if ((next = this.realize()) !== this.target) { + prev = this.target; + this.target = next; + return this.callback(this, prev); + } + }; + + KeypathObserver.prototype.realize = function() { + var current, index, prev, token, _i, _len, _ref; + current = this.model; + _ref = this.tokens; + for (index = _i = 0, _len = _ref.length; _i < _len; index = ++_i) { + token = _ref[index]; + if (this.objectPath[index] != null) { + if (current !== (prev = this.objectPath[index])) { + this.view.adapters[token["interface"]].unsubscribe(prev, token.path, this.update); + this.view.adapters[token["interface"]].subscribe(current, token.path, this.update); + this.objectPath[index] = current; + } + } else { + this.view.adapters[token["interface"]].subscribe(current, token.path, this.update); + this.objectPath[index] = current; + } + current = this.view.adapters[token["interface"]].read(current, token.path); + } + return current; + }; + + return KeypathObserver; + + })(); + + Rivets.binders.enabled = function(el, value) { + return el.disabled = !value; + }; + + Rivets.binders.disabled = function(el, value) { + return el.disabled = !!value; + }; + + Rivets.binders.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; + } + } + }; + + Rivets.binders.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; + } + } + }; + + Rivets.binders.show = function(el, value) { + return el.style.display = value ? '' : 'none'; + }; + + Rivets.binders.hide = function(el, value) { + return el.style.display = value ? 'none' : ''; + }; + + Rivets.binders.html = function(el, value) { + return el.innerHTML = value != null ? value : ''; + }; + + Rivets.binders.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 : ''; + } + } + } + }; + + Rivets.binders.text = function(el, value) { + if (el.innerText != null) { + return el.innerText = value != null ? value : ''; + } else { + return el.textContent = value != null ? value : ''; + } + }; + + Rivets.binders["if"] = { + block: true, + bind: function(el) { + var attr, declaration; + if (this.marker == null) { + attr = [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, + adapters: this.view.options.adapters, + 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) { + var _ref; + return (_ref = this.nested) != null ? _ref.update(models) : void 0; + } + }; + + Rivets.binders.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); + } + }; + + Rivets.binders['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)); + } + }; + + Rivets.binders['each-*'] = { + block: true, + bind: function(el) { + var attr; + if (this.marker == null) { + attr = [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 binding, data, i, index, k, key, model, modelName, options, previous, template, v, view, _i, _j, _k, _len, _len1, _len2, _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]); + } + } + 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 (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, + adapters: this.view.options.adapters, + 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); + this.marker.parentNode.insertBefore(template, previous.nextSibling); + } else if (this.iterated[index].models[modelName] !== model) { + this.iterated[index].update(data); + } + } + if (el.nodeName === 'OPTION') { + _ref3 = this.view.bindings; + _results = []; + for (_k = 0, _len2 = _ref3.length; _k < _len2; _k++) { + binding = _ref3[_k]; + if (binding.el === this.marker.parentNode && binding.type === 'value') { + _results.push(binding.sync()); + } 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; + } + }; + + Rivets.binders['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(); + } + }; + + Rivets.binders['*'] = function(el, value) { + if (value) { + return el.setAttribute(this.type, value); + } else { + return el.removeAttribute(this.type); + } + }; + + Rivets.adapters['.'] = { + id: '_rv', + counter: 0, + weakmap: {}, + weakReference: function(obj) { + var id; + if (obj[this.id] == null) { + id = this.counter++; + this.weakmap[id] = { + callbacks: {} + }; + Object.defineProperty(obj, this.id, { + value: id + }); + } + return this.weakmap[obj[this.id]]; + }, + stubFunction: function(obj, fn) { + var map, original, weakmap; + original = obj[fn]; + map = this.weakReference(obj); + weakmap = this.weakmap; + return obj[fn] = function() { + var callback, k, r, response, _i, _len, _ref, _ref1, _ref2, _ref3; + response = original.apply(obj, arguments); + _ref = map.pointers; + for (r in _ref) { + k = _ref[r]; + _ref3 = (_ref1 = (_ref2 = weakmap[r]) != null ? _ref2.callbacks[k] : void 0) != null ? _ref1 : []; + for (_i = 0, _len = _ref3.length; _i < _len; _i++) { + callback = _ref3[_i]; + callback(); + } + } + return response; + }; + }, + observeMutations: function(obj, ref, keypath) { + var fn, functions, map, _base, _i, _len; + if (Array.isArray(obj)) { + map = this.weakReference(obj); + if (map.pointers == null) { + map.pointers = {}; + functions = ['push', 'pop', 'shift', 'unshift', 'sort', 'reverse', 'splice']; + for (_i = 0, _len = functions.length; _i < _len; _i++) { + fn = functions[_i]; + this.stubFunction(obj, fn); + } + } + if ((_base = map.pointers)[ref] == null) { + _base[ref] = []; + } + if (__indexOf.call(map.pointers[ref], keypath) < 0) { + return map.pointers[ref].push(keypath); + } + } + }, + unobserveMutations: function(obj, ref, keypath) { + var keypaths, _ref; + if (Array.isArray(obj && (obj[this.id] != null))) { + if (keypaths = (_ref = this.weakReference(obj).pointers) != null ? _ref[ref] : void 0) { + return keypaths.splice(keypaths.indexOf(keypath), 1); + } + } + }, + subscribe: function(obj, keypath, callback) { + var callbacks, value, + _this = this; + callbacks = this.weakReference(obj).callbacks; + if (callbacks[keypath] == null) { + callbacks[keypath] = []; + value = obj[keypath]; + Object.defineProperty(obj, keypath, { + get: function() { + return value; + }, + set: function(newValue) { + var _i, _len, _ref; + if (newValue !== value) { + value = newValue; + _ref = callbacks[keypath]; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + callback = _ref[_i]; + callback(); + } + return _this.observeMutations(newValue, obj[_this.id], keypath); + } + } + }); + } + if (__indexOf.call(callbacks[keypath], callback) < 0) { + callbacks[keypath].push(callback); + } + return this.observeMutations(obj[keypath], obj[this.id], keypath); + }, + unsubscribe: function(obj, keypath, callback) { + var callbacks; + callbacks = this.weakmap[obj[this.id]].callbacks[keypath]; + callbacks.splice(callbacks.indexOf(callback), 1); + return this.unobserveMutations(obj[keypath], obj[this.id], keypath); + }, + read: function(obj, keypath) { + return obj[keypath]; + }, + publish: function(obj, keypath, value) { + return obj[keypath] = value; + } + }; + + Rivets.factory = function(exports) { + exports._ = Rivets; + exports.binders = Rivets.binders; + exports.components = Rivets.components; + exports.formatters = Rivets.formatters; + exports.adapters = Rivets.adapters; + 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); diff --git a/ajax/libs/rivets/0.6.1/rivets.min.js b/ajax/libs/rivets/0.6.1/rivets.min.js new file mode 100644 index 000000000..6c31f8455 --- /dev/null +++ b/ajax/libs/rivets/0.6.1/rivets.min.js @@ -0,0 +1,5 @@ +// Rivets.js +// version: 0.6.1 +// author: Michael Richards +// license: MIT +(function(){var a,b,c=function(a,b){return function(){return a.apply(b,arguments)}},d=[].indexOf||function(a){for(var b=0,c=this.length;c>b;b++)if(b in this&&this[b]===a)return b;return-1},e=[].slice,f={}.hasOwnProperty,g=function(a,b){function c(){this.constructor=a}for(var d in b)f.call(b,d)&&(a[d]=b[d]);return c.prototype=b.prototype,a.prototype=new c,a.__super__=b.prototype,a};b={binders:{},components:{},formatters:{},adapters:{},config:{prefix:"rv",templateDelimiters:["{","}"],rootInterface:".",preloadData:!0,handler:function(a,b,c){return this.call(a,b,c.view.models)}}},b.Util={bindEvent:function(a,b,c){return null!=window.jQuery?(a=jQuery(a),null!=a.on?a.on(b,c):a.bind(b,c)):null!=window.addEventListener?a.addEventListener(b,c,!1):(b="on"+b,a.attachEvent(b,c))},unbindEvent:function(a,b,c){return null!=window.jQuery?(a=jQuery(a),null!=a.off?a.off(b,c):a.unbind(b,c)):null!=window.removeEventListener?a.removeEventListener(b,c,!1):(b="on"+b,a.detachEvent(b,c))},getInputValue:function(a){var b,c,d,e;if(null!=window.jQuery)switch(a=jQuery(a),a[0].type){case"checkbox":return a.is(":checked");default:return a.val()}else switch(a.type){case"checkbox":return a.checked;case"select-multiple":for(e=[],c=0,d=a.length;d>c;c++)b=a[c],b.selected&&e.push(b.value);return e;default:return a.value}}},b.View=function(){function a(a,d,e){var f,g,h,i,j,k,l,m,n;for(this.els=a,this.models=d,this.options=null!=e?e:{},this.update=c(this.update,this),this.publish=c(this.publish,this),this.sync=c(this.sync,this),this.unbind=c(this.unbind,this),this.bind=c(this.bind,this),this.select=c(this.select,this),this.build=c(this.build,this),this.componentRegExp=c(this.componentRegExp,this),this.bindingRegExp=c(this.bindingRegExp,this),this.els.jquery||this.els instanceof Array||(this.els=[this.els]),l=["config","binders","formatters","adapters"],j=0,k=l.length;k>j;j++){if(g=l[j],this[g]={},this.options[g]){m=this.options[g];for(f in m)h=m[f],this[g][f]=h}n=b[g];for(f in n)h=n[f],null==(i=this[g])[f]&&(i[f]=h)}this.build()}return a.prototype.bindingRegExp=function(){return new RegExp("^"+this.config.prefix+"-")},a.prototype.componentRegExp=function(){return new RegExp("^"+this.config.prefix.toUpperCase()+"-")},a.prototype.build=function(){var a,c,e,f,g,h,i,j,k,l=this;for(this.bindings=[],h=[],a=this.bindingRegExp(),e=this.componentRegExp(),c=function(a,c,d,e){var f,g,h,i,j,k,m;return j={},m=function(){var a,b,c,d;for(c=e.split("|"),d=[],a=0,b=c.length;b>a;a++)k=c[a],d.push(k.trim());return d}(),f=function(){var a,b,c,d;for(c=m.shift().split("<"),d=[],a=0,b=c.length;b>a;a++)g=c[a],d.push(g.trim());return d}(),i=f.shift(),j.formatters=m,(h=f.shift())&&(j.dependencies=h.split(/\s+/)),l.bindings.push(new b[a](l,c,d,i,j))},g=function(f){var i,j,k,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M;if(d.call(h,f)<0){if(f.nodeType===Node.TEXT_NODE){if(q=b.TextTemplateParser,(n=l.config.templateDelimiters)&&(u=q.parse(f.data,n)).length&&(1!==u.length||u[0].type!==q.types.text)){for(x=0,B=u.length;B>x;x++)t=u[x],s=document.createTextNode(t.value),f.parentNode.insertBefore(s,f),1===t.type&&c("TextBinding",s,null,t.value);f.parentNode.removeChild(f)}}else if(e.test(f.tagName))v=f.tagName.replace(e,"").toLowerCase(),l.bindings.push(new b.ComponentBinding(l,f,v));else if(null!=f.attributes){for(H=f.attributes,y=0,C=H.length;C>y;y++)if(i=H[y],a.test(i.name)){if(v=i.name.replace(a,""),!(k=l.binders[v])){I=l.binders;for(o in I)w=I[o],"*"!==o&&-1!==o.indexOf("*")&&(r=new RegExp("^"+o.replace("*",".+")+"$"),r.test(v)&&(k=w))}if(k||(k=l.binders["*"]),k.block){for(J=f.childNodes,z=0,D=J.length;D>z;z++)p=J[z],h.push(p);j=[i]}}for(K=j||f.attributes,A=0,E=K.length;E>A;A++)i=K[A],a.test(i.name)&&(v=i.name.replace(a,""),c("Binding",f,v,i.value))}for(L=function(){var a,b,c,d;for(c=f.childNodes,d=[],b=0,a=c.length;a>b;b++)p=c[b],d.push(p);return d}(),M=[],G=0,F=L.length;F>G;G++)m=L[G],M.push(g(m));return M}},k=this.els,i=0,j=k.length;j>i;i++)f=k[i],g(f)},a.prototype.select=function(a){var b,c,d,e,f;for(e=this.bindings,f=[],c=0,d=e.length;d>c;c++)b=e[c],a(b)&&f.push(b);return f},a.prototype.bind=function(){var a,b,c,d,e;for(d=this.bindings,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.bind());return e},a.prototype.unbind=function(){var a,b,c,d,e;for(d=this.bindings,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.unbind());return e},a.prototype.sync=function(){var a,b,c,d,e;for(d=this.bindings,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.sync());return e},a.prototype.publish=function(){var a,b,c,d,e;for(d=this.select(function(a){return a.binder.publishes}),e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.publish());return e},a.prototype.update=function(a){var b,c,d,e,f,g,h;null==a&&(a={});for(c in a)d=a[c],this.models[c]=d;for(g=this.bindings,h=[],e=0,f=g.length;f>e;e++)b=g[e],h.push(b.update(a));return h},a}(),b.Binding=function(){function d(a,b,d,e,f){this.view=a,this.el=b,this.type=d,this.keypath=e,this.options=null!=f?f:{},this.update=c(this.update,this),this.unbind=c(this.unbind,this),this.bind=c(this.bind,this),this.publish=c(this.publish,this),this.sync=c(this.sync,this),this.set=c(this.set,this),this.eventHandler=c(this.eventHandler,this),this.formattedValue=c(this.formattedValue,this),this.setObserver=c(this.setObserver,this),this.setBinder=c(this.setBinder,this),this.formatters=this.options.formatters||[],this.dependencies=[],this.setBinder(),this.setObserver()}return d.prototype.setBinder=function(){var a,b,c,d;if(!(this.binder=this.view.binders[this.type])){d=this.view.binders;for(a in d)c=d[a],"*"!==a&&-1!==a.indexOf("*")&&(b=new RegExp("^"+a.replace("*",".+")+"$"),b.test(this.type)&&(this.binder=c,this.args=new RegExp("^"+a.replace("*","(.+)")+"$").exec(this.type),this.args.shift()))}return this.binder||(this.binder=this.view.binders["*"]),this.binder instanceof Function?this.binder={routine:this.binder}:void 0},d.prototype.setObserver=function(){var b=this;return this.observer=new a(this.view,this.view.models,this.keypath,function(a){return b.key&&b.unbind(!0),b.model=a.target,b.key&&b.bind(!0),b.sync()}),this.key=this.observer.key,this.model=this.observer.target},d.prototype.formattedValue=function(a){var b,c,d,f,g,h;for(h=this.formatters,f=0,g=h.length;g>f;f++)c=h[f],b=c.split(/\s+/),d=b.shift(),c=this.view.formatters[d],(null!=c?c.read:void 0)instanceof Function?a=c.read.apply(c,[a].concat(e.call(b))):c instanceof Function&&(a=c.apply(null,[a].concat(e.call(b))));return a},d.prototype.eventHandler=function(a){var b,c;return c=(b=this).view.config.handler,function(d){return c.call(a,this,d,b)}},d.prototype.set=function(a){var b;return a=a instanceof Function&&!this.binder["function"]?this.formattedValue(a.call(this.model)):this.formattedValue(a),null!=(b=this.binder.routine)?b.call(this,this.el,a):void 0},d.prototype.sync=function(){return this.set(this.key?this.view.adapters[this.key["interface"]].read(this.model,this.key.path):this.model)},d.prototype.publish=function(){var a,c,d,f,g,h,i,j,k;for(f=b.Util.getInputValue(this.el),i=this.formatters.slice(0).reverse(),g=0,h=i.length;h>g;g++)c=i[g],a=c.split(/\s+/),d=a.shift(),(null!=(j=this.view.formatters[d])?j.publish:void 0)&&(f=(k=this.view.formatters[d]).publish.apply(k,[f].concat(e.call(a))));return this.view.adapters[this.key["interface"]].publish(this.model,this.key.path,f)},d.prototype.bind=function(b){var c,d,e,f,g,h,i,j,k,l=this;if(null==b&&(b=!1),b||null!=(h=this.binder.bind)&&h.call(this,this.el),this.key&&this.view.adapters[this.key["interface"]].subscribe(this.model,this.key.path,this.sync),(b?void 0:this.view.config.preloadData)&&this.sync(),null!=(i=this.options.dependencies)?i.length:void 0){for(j=this.options.dependencies,k=[],f=0,g=j.length;g>f;f++)c=j[f],e=new a(this.view,this.model,c,function(a,b){var c;return c=a.key,l.view.adapters[c["interface"]].unsubscribe(b,c.path,l.sync),l.view.adapters[c["interface"]].subscribe(a.target,c.path,l.sync),l.sync()}),d=e.key,this.view.adapters[d["interface"]].subscribe(e.target,d.path,this.sync),k.push(this.dependencies.push(e));return k}},d.prototype.unbind=function(a){var b,c,d,e,f,g;if(null==a&&(a=!1),a||null!=(f=this.binder.unbind)&&f.call(this,this.el),this.key&&this.view.adapters[this.key["interface"]].unsubscribe(this.model,this.key.path,this.sync),this.dependencies.length){for(g=this.dependencies,d=0,e=g.length;e>d;d++)c=g[d],b=c.key,this.view.adapters[b["interface"]].unsubscribe(c.target,b.path,this.sync);return this.dependencies=[]}},d.prototype.update=function(a){var b;return null==a&&(a={}),null!=(b=this.binder.update)?b.call(this,a):void 0},d}(),b.ComponentBinding=function(a){function e(a,e,f){var g,h,i,j,k;for(this.view=a,this.el=e,this.type=f,this.unbind=c(this.unbind,this),this.bind=c(this.bind,this),this.update=c(this.update,this),this.locals=c(this.locals,this),this.component=b.components[this.type],this.attributes={},this.inflections={},j=this.el.attributes||[],h=0,i=j.length;i>h;h++)g=j[h],k=g.name,d.call(this.component.attributes,k)>=0?this.attributes[g.name]=g.value:this.inflections[g.name]=g.value}return g(e,a),e.prototype.sync=function(){},e.prototype.locals=function(a){var b,c,d,e,f,g,h,i,j;null==a&&(a=this.view.models),f={},i=this.inflections;for(c in i)for(b=i[c],j=b.split("."),g=0,h=j.length;h>g;g++)e=j[g],f[c]=(f[c]||a)[e];for(c in a)d=a[c],null==f[c]&&(f[c]=d);return f},e.prototype.update=function(a){var b;return null!=(b=this.componentView)?b.update(this.locals(a)):void 0},e.prototype.bind=function(){var a,c;return null!=this.componentView?null!=(c=this.componentView)?c.bind():void 0:(a=this.component.build.call(this.attributes),(this.componentView=new b.View(a,this.locals(),this.view.options)).bind(),this.el.parentNode.replaceChild(a,this.el))},e.prototype.unbind=function(){var a;return null!=(a=this.componentView)?a.unbind():void 0},e}(b.Binding),b.TextBinding=function(a){function b(a,b,d,e,f){this.view=a,this.el=b,this.type=d,this.keypath=e,this.options=null!=f?f:{},this.sync=c(this.sync,this),this.formatters=this.options.formatters||[],this.dependencies=[],this.setObserver()}return g(b,a),b.prototype.binder={routine:function(a,b){return a.data=null!=b?b:""}},b.prototype.sync=function(){return b.__super__.sync.apply(this,arguments)},b}(b.Binding),b.KeypathParser=function(){function a(){}return a.parse=function(a,b,c){var e,f,g,h;h=[],f={"interface":c,path:""};for(g in a)e=a[g],d.call(b,e)>=0?(h.push(f),f={"interface":e,path:""}):f.path+=e;return h.push(f),h},a}(),b.TextTemplateParser=function(){function a(){}return a.types={text:0,binding:1},a.parse=function(a,b){var c,d,e,f,g,h,i,j;for(i=[],g=a.length,c=b[1].length,d=0,e=0;g>e;){if(d=a.indexOf(b[0],e),0>d){i.push({type:this.types.text,value:a.slice(e)});break}if(d>0&&d>e&&i.push({type:this.types.text,value:a.slice(e,d)}),e=d+2,d=a.indexOf(b[1],e),0>d){h=a.slice(e-2),f=i[i.length-1],(null!=f?f.type:void 0)===this.types.text?f.value+=h:i.push({type:this.types.text,value:h});break}j=a.slice(e,d).trim(),i.push({type:this.types.binding,value:j}),e=d+c}return i},a}(),a=function(){function a(a,b,d,e){this.view=a,this.model=b,this.keypath=d,this.callback=e,this.realize=c(this.realize,this),this.update=c(this.update,this),this.parse=c(this.parse,this),this.parse(),this.objectPath=[],this.target=this.realize()}return a.prototype.parse=function(){var a,c,e,f,g,h;return a=function(){var a,b;a=this.view.adapters,b=[];for(c in a)g=a[c],b.push(c);return b}.call(this),h=this.keypath[0],d.call(a,h)>=0?(f=this.keypath[0],e=this.keypath.substr(1)):(f=this.view.config.rootInterface,e=this.keypath),this.tokens=b.KeypathParser.parse(e,a,f),this.key=this.tokens.pop()},a.prototype.update=function(){var a,b;return(a=this.realize())!==this.target?(b=this.target,this.target=a,this.callback(this,b)):void 0},a.prototype.realize=function(){var a,b,c,d,e,f,g;for(a=this.model,g=this.tokens,b=e=0,f=g.length;f>e;b=++e)d=g[b],null!=this.objectPath[b]?a!==(c=this.objectPath[b])&&(this.view.adapters[d["interface"]].unsubscribe(c,d.path,this.update),this.view.adapters[d["interface"]].subscribe(a,d.path,this.update),this.objectPath[b]=a):(this.view.adapters[d["interface"]].subscribe(a,d.path,this.update),this.objectPath[b]=a),a=this.view.adapters[d["interface"]].read(a,d.path);return a},a}(),b.binders.enabled=function(a,b){return a.disabled=!b},b.binders.disabled=function(a,b){return a.disabled=!!b},b.binders.checked={publishes:!0,bind:function(a){return b.Util.bindEvent(a,"change",this.publish)},unbind:function(a){return b.Util.unbindEvent(a,"change",this.publish)},routine:function(a,b){var c;return a.checked="radio"===a.type?(null!=(c=a.value)?c.toString():void 0)===(null!=b?b.toString():void 0):!!b}},b.binders.unchecked={publishes:!0,bind:function(a){return b.Util.bindEvent(a,"change",this.publish)},unbind:function(a){return b.Util.unbindEvent(a,"change",this.publish)},routine:function(a,b){var c;return a.checked="radio"===a.type?(null!=(c=a.value)?c.toString():void 0)!==(null!=b?b.toString():void 0):!b}},b.binders.show=function(a,b){return a.style.display=b?"":"none"},b.binders.hide=function(a,b){return a.style.display=b?"none":""},b.binders.html=function(a,b){return a.innerHTML=null!=b?b:""},b.binders.value={publishes:!0,bind:function(a){return b.Util.bindEvent(a,"change",this.publish)},unbind:function(a){return b.Util.unbindEvent(a,"change",this.publish)},routine:function(a,b){var c,e,f,g,h,i,j;if(null!=window.jQuery){if(a=jQuery(a),(null!=b?b.toString():void 0)!==(null!=(g=a.val())?g.toString():void 0))return a.val(null!=b?b:"")}else if("select-multiple"===a.type){if(null!=b){for(j=[],e=0,f=a.length;f>e;e++)c=a[e],j.push(c.selected=(h=c.value,d.call(b,h)>=0));return j}}else if((null!=b?b.toString():void 0)!==(null!=(i=a.value)?i.toString():void 0))return a.value=null!=b?b:""}},b.binders.text=function(a,b){return null!=a.innerText?a.innerText=null!=b?b:"":a.textContent=null!=b?b:""},b.binders["if"]={block:!0,bind:function(a){var b,c;return null==this.marker?(b=[this.view.config.prefix,this.type].join("-").replace("--","-"),c=a.getAttribute(b),this.marker=document.createComment(" rivets: "+this.type+" "+c+" "),a.removeAttribute(b),a.parentNode.insertBefore(this.marker,a),a.parentNode.removeChild(a)):void 0},unbind:function(){var a;return null!=(a=this.nested)?a.unbind():void 0},routine:function(a,c){var d,e,f,g,h;if(!!c==(null==this.nested)){if(c){f={},h=this.view.models;for(d in h)e=h[d],f[d]=e;return g={binders:this.view.options.binders,formatters:this.view.options.formatters,adapters:this.view.options.adapters,config:this.view.options.config},(this.nested=new b.View(a,f,g)).bind(),this.marker.parentNode.insertBefore(a,this.marker.nextSibling)}return a.parentNode.removeChild(a),this.nested.unbind(),delete this.nested}},update:function(a){var b;return null!=(b=this.nested)?b.update(a):void 0}},b.binders.unless={block:!0,bind:function(a){return b.binders["if"].bind.call(this,a)},unbind:function(){return b.binders["if"].unbind.call(this)},routine:function(a,c){return b.binders["if"].routine.call(this,a,!c)},update:function(a){return b.binders["if"].update.call(this,a)}},b.binders["on-*"]={"function":!0,unbind:function(a){return this.handler?b.Util.unbindEvent(a,this.args[0],this.handler):void 0},routine:function(a,c){return this.handler&&b.Util.unbindEvent(a,this.args[0],this.handler),b.Util.bindEvent(a,this.args[0],this.handler=this.eventHandler(c))}},b.binders["each-*"]={block:!0,bind:function(a){var b;return null==this.marker?(b=[this.view.config.prefix,this.type].join("-").replace("--","-"),this.marker=document.createComment(" rivets: "+this.type+" "),this.iterated=[],a.removeAttribute(b),a.parentNode.insertBefore(this.marker,a),a.parentNode.removeChild(a)):void 0},unbind:function(){var a,b,c,d,e;if(null!=this.iterated){for(d=this.iterated,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.unbind());return e}},routine:function(a,c){var d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A;if(k=this.args[0],c=c||[],this.iterated.length>c.length)for(w=Array(this.iterated.length-c.length),q=0,t=w.length;t>q;q++)f=w[q],p=this.iterated.pop(),p.unbind(),this.marker.parentNode.removeChild(p.els[0]);for(g=r=0,u=c.length;u>r;g=++r)if(j=c[g],e={},e[k]=j,null==this.iterated[g]){x=this.view.models;for(i in x)j=x[i],null==e[i]&&(e[i]=j);m=this.iterated.length?this.iterated[this.iterated.length-1].els[0]:this.marker,l={binders:this.view.options.binders,formatters:this.view.options.formatters,adapters:this.view.options.adapters,config:{}},y=this.view.options.config;for(h in y)o=y[h],l.config[h]=o;l.config.preloadData=!0,n=a.cloneNode(!0),p=new b.View(n,e,l),p.bind(),this.iterated.push(p),this.marker.parentNode.insertBefore(n,m.nextSibling)}else this.iterated[g].models[k]!==j&&this.iterated[g].update(e);if("OPTION"===a.nodeName){for(z=this.view.bindings,A=[],s=0,v=z.length;v>s;s++)d=z[s],d.el===this.marker.parentNode&&"value"===d.type?A.push(d.sync()):A.push(void 0);return A}},update:function(a){var b,c,d,e,f,g,h,i;b={};for(c in a)d=a[c],c!==this.args[0]&&(b[c]=d);for(h=this.iterated,i=[],f=0,g=h.length;g>f;f++)e=h[f],i.push(e.update(b));return i}},b.binders["class-*"]=function(a,b){var c;return c=" "+a.className+" ",!b==(-1!==c.indexOf(" "+this.args[0]+" "))?a.className=b?""+a.className+" "+this.args[0]:c.replace(" "+this.args[0]+" "," ").trim():void 0},b.binders["*"]=function(a,b){return b?a.setAttribute(this.type,b):a.removeAttribute(this.type)},b.adapters["."]={id:"_rv",counter:0,weakmap:{},weakReference:function(a){var b;return null==a[this.id]&&(b=this.counter++,this.weakmap[b]={callbacks:{}},Object.defineProperty(a,this.id,{value:b})),this.weakmap[a[this.id]]},stubFunction:function(a,b){var c,d,e;return d=a[b],c=this.weakReference(a),e=this.weakmap,a[b]=function(){var b,f,g,h,i,j,k,l,m,n;h=d.apply(a,arguments),k=c.pointers;for(g in k)for(f=k[g],n=null!=(l=null!=(m=e[g])?m.callbacks[f]:void 0)?l:[],i=0,j=n.length;j>i;i++)b=n[i],b();return h}},observeMutations:function(a,b,c){var e,f,g,h,i,j;if(Array.isArray(a)){if(g=this.weakReference(a),null==g.pointers)for(g.pointers={},f=["push","pop","shift","unshift","sort","reverse","splice"],i=0,j=f.length;j>i;i++)e=f[i],this.stubFunction(a,e);if(null==(h=g.pointers)[b]&&(h[b]=[]),d.call(g.pointers[b],c)<0)return g.pointers[b].push(c)}},unobserveMutations:function(a,b,c){var d,e;return Array.isArray(a&&null!=a[this.id])&&(d=null!=(e=this.weakReference(a).pointers)?e[b]:void 0)?d.splice(d.indexOf(c),1):void 0},subscribe:function(a,b,c){var e,f,g=this;return e=this.weakReference(a).callbacks,null==e[b]&&(e[b]=[],f=a[b],Object.defineProperty(a,b,{get:function(){return f},set:function(d){var h,i,j;if(d!==f){for(f=d,j=e[b],h=0,i=j.length;i>h;h++)c=j[h],c();return g.observeMutations(d,a[g.id],b)}}})),d.call(e[b],c)<0&&e[b].push(c),this.observeMutations(a[b],a[this.id],b)},unsubscribe:function(a,b,c){var d;return d=this.weakmap[a[this.id]].callbacks[b],d.splice(d.indexOf(c),1),this.unobserveMutations(a[b],a[this.id],b)},read:function(a,b){return a[b]},publish:function(a,b,c){return a[b]=c}},b.factory=function(a){return a._=b,a.binders=b.binders,a.components=b.components,a.formatters=b.formatters,a.adapters=b.adapters,a.config=b.config,a.configure=function(a){var c,d;null==a&&(a={});for(c in a)d=a[c],b.config[c]=d},a.bind=function(a,c,d){var e;return null==c&&(c={}),null==d&&(d={}),e=new b.View(a,c,d),e.bind(),e}},"object"==typeof exports?b.factory(exports):"function"==typeof define&&define.amd?define(["exports"],function(a){return b.factory(this.rivets=a),a}):b.factory(this.rivets={})}).call(this); \ No newline at end of file diff --git a/ajax/libs/rivets/0.6.2/rivets.js b/ajax/libs/rivets/0.6.2/rivets.js new file mode 100644 index 000000000..4f08182da --- /dev/null +++ b/ajax/libs/rivets/0.6.2/rivets.js @@ -0,0 +1,1242 @@ +// Rivets.js +// version: 0.6.2 +// author: Michael Richards +// license: MIT +(function() { + var KeypathObserver, Rivets, + __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, + __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; }, + __slice = [].slice, + __hasProp = {}.hasOwnProperty, + __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; + + Rivets = { + binders: {}, + components: {}, + formatters: {}, + adapters: {}, + config: { + prefix: 'rv', + templateDelimiters: ['{', '}'], + rootInterface: '.', + preloadData: true, + handler: function(context, ev, binding) { + return this.call(context, ev, binding.view.models); + } + } + }; + + 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.View = (function() { + function View(els, models, options) { + var k, option, v, _base, _i, _len, _ref, _ref1, _ref2; + 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.componentRegExp = __bind(this.componentRegExp, this); + this.bindingRegExp = __bind(this.bindingRegExp, this); + if (!(this.els.jquery || this.els instanceof Array)) { + this.els = [this.els]; + } + _ref = ['config', 'binders', 'formatters', 'adapters']; + 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 ((_base = this[option])[k] == null) { + _base[k] = v; + } + } + } + this.build(); + } + + View.prototype.bindingRegExp = function() { + return new RegExp("^" + this.config.prefix + "-"); + }; + + View.prototype.componentRegExp = function() { + return new RegExp("^" + (this.config.prefix.toUpperCase()) + "-"); + }; + + View.prototype.build = function() { + var bindingRegExp, buildBinding, componentRegExp, el, parse, skipNodes, _i, _len, _ref, + _this = this; + this.bindings = []; + skipNodes = []; + bindingRegExp = this.bindingRegExp(); + componentRegExp = this.componentRegExp(); + buildBinding = function(binding, node, type, declaration) { + var context, ctx, dependencies, keypath, options, pipe, pipes; + 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; + })(); + keypath = context.shift(); + options.formatters = pipes; + if (dependencies = context.shift()) { + options.dependencies = dependencies.split(/\s+/); + } + return _this.bindings.push(new Rivets[binding](_this, node, type, keypath, options)); + }; + parse = function(node) { + var attribute, attributes, binder, childNode, delimiters, identifier, n, parser, regexp, 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)) { + for (_i = 0, _len = tokens.length; _i < _len; _i++) { + token = tokens[_i]; + text = document.createTextNode(token.value); + node.parentNode.insertBefore(text, node); + if (token.type === 1) { + buildBinding('TextBinding', text, null, token.value); + } + } + node.parentNode.removeChild(node); + } + } + } + } else if (componentRegExp.test(node.tagName)) { + type = node.tagName.replace(componentRegExp, '').toLowerCase(); + _this.bindings.push(new Rivets.ComponentBinding(_this, node, type)); + } 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('Binding', node, type, attribute.value); + } + } + } + _ref4 = (function() { + var _len4, _n, _ref4, _results1; + _ref4 = node.childNodes; + _results1 = []; + for (_n = 0, _len4 = _ref4.length; _n < _len4; _n++) { + n = _ref4[_n]; + _results1.push(n); + } + return _results1; + })(); + _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.Binding = (function() { + function Binding(view, el, type, keypath, options) { + this.view = view; + this.el = el; + this.type = type; + 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); + this.setObserver = __bind(this.setObserver, this); + this.setBinder = __bind(this.setBinder, this); + this.formatters = this.options.formatters || []; + this.dependencies = []; + this.setBinder(); + this.setObserver(); + } + + Binding.prototype.setBinder = function() { + var identifier, regexp, value, _ref; + if (!(this.binder = this.view.binders[this.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(this.type)) { + this.binder = value; + this.args = new RegExp("^" + (identifier.replace('*', '(.+)')) + "$").exec(this.type); + this.args.shift(); + } + } + } + } + this.binder || (this.binder = this.view.binders['*']); + if (this.binder instanceof Function) { + return this.binder = { + routine: this.binder + }; + } + }; + + Binding.prototype.setObserver = function() { + var _this = this; + this.observer = new KeypathObserver(this.view, this.view.models, this.keypath, function(obs) { + if (_this.key) { + _this.unbind(true); + } + _this.model = obs.target; + if (_this.key) { + _this.bind(true); + } + return _this.sync(); + }); + this.key = this.observer.key; + return this.model = this.observer.target; + }; + + 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.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.key ? this.view.adapters[this.key["interface"]].read(this.model, this.key.path) : this.model); + }; + + 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.adapters[this.key["interface"]].publish(this.model, this.key.path, value); + }; + + Binding.prototype.bind = function(silent) { + var dependency, key, observer, _i, _len, _ref, _ref1, _ref2, _results, + _this = this; + if (silent == null) { + silent = false; + } + if (!silent) { + if ((_ref = this.binder.bind) != null) { + _ref.call(this, this.el); + } + } + if (this.key) { + this.view.adapters[this.key["interface"]].subscribe(this.model, this.key.path, this.sync); + } + if (!silent ? this.view.config.preloadData : void 0) { + 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]; + observer = new KeypathObserver(this.view, this.model, dependency, function(obs, prev) { + var key; + key = obs.key; + _this.view.adapters[key["interface"]].unsubscribe(prev, key.path, _this.sync); + _this.view.adapters[key["interface"]].subscribe(obs.target, key.path, _this.sync); + return _this.sync(); + }); + key = observer.key; + this.view.adapters[key["interface"]].subscribe(observer.target, key.path, this.sync); + _results.push(this.dependencies.push(observer)); + } + return _results; + } + }; + + Binding.prototype.unbind = function(silent) { + var key, obs, _i, _len, _ref, _ref1; + if (silent == null) { + silent = false; + } + if (!silent) { + if ((_ref = this.binder.unbind) != null) { + _ref.call(this, this.el); + } + } + if (this.key) { + this.view.adapters[this.key["interface"]].unsubscribe(this.model, this.key.path, this.sync); + } + if (this.dependencies.length) { + _ref1 = this.dependencies; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + obs = _ref1[_i]; + key = obs.key; + this.view.adapters[key["interface"]].unsubscribe(obs.target, key.path, this.sync); + } + return this.dependencies = []; + } + }; + + Binding.prototype.update = function(models) { + var _ref; + if (models == null) { + models = {}; + } + return (_ref = this.binder.update) != null ? _ref.call(this, models) : void 0; + }; + + return Binding; + + })(); + + Rivets.ComponentBinding = (function(_super) { + __extends(ComponentBinding, _super); + + function ComponentBinding(view, el, type) { + var attribute, _i, _len, _ref, _ref1; + this.view = view; + this.el = el; + this.type = type; + this.unbind = __bind(this.unbind, this); + this.bind = __bind(this.bind, this); + this.update = __bind(this.update, this); + this.locals = __bind(this.locals, this); + this.component = Rivets.components[this.type]; + this.attributes = {}; + this.inflections = {}; + _ref = this.el.attributes || []; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + attribute = _ref[_i]; + if (_ref1 = attribute.name, __indexOf.call(this.component.attributes, _ref1) >= 0) { + this.attributes[attribute.name] = attribute.value; + } else { + this.inflections[attribute.name] = attribute.value; + } + } + } + + ComponentBinding.prototype.sync = function() {}; + + ComponentBinding.prototype.locals = function(models) { + var inverse, key, model, path, result, _i, _len, _ref, _ref1; + if (models == null) { + models = this.view.models; + } + result = {}; + _ref = this.inflections; + for (key in _ref) { + inverse = _ref[key]; + _ref1 = inverse.split('.'); + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + path = _ref1[_i]; + result[key] = (result[key] || models)[path]; + } + } + for (key in models) { + model = models[key]; + if (result[key] == null) { + result[key] = model; + } + } + return result; + }; + + ComponentBinding.prototype.update = function(models) { + var _ref; + return (_ref = this.componentView) != null ? _ref.update(this.locals(models)) : void 0; + }; + + ComponentBinding.prototype.bind = function() { + var el, _ref; + if (this.componentView != null) { + return (_ref = this.componentView) != null ? _ref.bind() : void 0; + } else { + el = this.component.build.call(this.attributes); + (this.componentView = new Rivets.View(el, this.locals(), this.view.options)).bind(); + return this.el.parentNode.replaceChild(el, this.el); + } + }; + + ComponentBinding.prototype.unbind = function() { + var _ref; + return (_ref = this.componentView) != null ? _ref.unbind() : void 0; + }; + + return ComponentBinding; + + })(Rivets.Binding); + + Rivets.TextBinding = (function(_super) { + __extends(TextBinding, _super); + + function TextBinding(view, el, type, keypath, options) { + this.view = view; + this.el = el; + this.type = type; + this.keypath = keypath; + this.options = options != null ? options : {}; + this.sync = __bind(this.sync, this); + this.formatters = this.options.formatters || []; + this.dependencies = []; + this.setObserver(); + } + + TextBinding.prototype.binder = { + routine: function(node, value) { + return node.data = value != null ? value : ''; + } + }; + + TextBinding.prototype.sync = function() { + return TextBinding.__super__.sync.apply(this, arguments); + }; + + return TextBinding; + + })(Rivets.Binding); + + Rivets.KeypathParser = (function() { + function KeypathParser() {} + + KeypathParser.parse = function(keypath, interfaces, root) { + var char, current, index, tokens; + tokens = []; + current = { + "interface": root, + path: '' + }; + for (index in keypath) { + char = keypath[index]; + if (__indexOf.call(interfaces, char) >= 0) { + tokens.push(current); + current = { + "interface": char, + path: '' + }; + } else { + current.path += char; + } + } + tokens.push(current); + return tokens; + }; + + return KeypathParser; + + })(); + + 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 + delimiters[0].length; + index = template.indexOf(delimiters[1], lastIndex); + if (index < 0) { + substring = template.slice(lastIndex - delimiters[1].length); + 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 + delimiters[1].length; + } + } + return tokens; + }; + + return TextTemplateParser; + + })(); + + KeypathObserver = (function() { + function KeypathObserver(view, model, keypath, callback) { + this.view = view; + this.model = model; + this.keypath = keypath; + this.callback = callback; + this.realize = __bind(this.realize, this); + this.update = __bind(this.update, this); + this.parse = __bind(this.parse, this); + this.parse(); + this.objectPath = []; + this.target = this.realize(); + } + + KeypathObserver.prototype.parse = function() { + var interfaces, k, path, root, v, _ref; + interfaces = (function() { + var _ref, _results; + _ref = this.view.adapters; + _results = []; + for (k in _ref) { + v = _ref[k]; + _results.push(k); + } + return _results; + }).call(this); + if (_ref = this.keypath[0], __indexOf.call(interfaces, _ref) >= 0) { + root = this.keypath[0]; + path = this.keypath.substr(1); + } else { + root = this.view.config.rootInterface; + path = this.keypath; + } + this.tokens = Rivets.KeypathParser.parse(path, interfaces, root); + return this.key = this.tokens.pop(); + }; + + KeypathObserver.prototype.update = function() { + var next, prev; + if ((next = this.realize()) !== this.target) { + prev = this.target; + this.target = next; + return this.callback(this, prev); + } + }; + + KeypathObserver.prototype.realize = function() { + var current, index, prev, token, _i, _len, _ref; + current = this.model; + _ref = this.tokens; + for (index = _i = 0, _len = _ref.length; _i < _len; index = ++_i) { + token = _ref[index]; + if (this.objectPath[index] != null) { + if (current !== (prev = this.objectPath[index])) { + this.view.adapters[token["interface"]].unsubscribe(prev, token.path, this.update); + this.view.adapters[token["interface"]].subscribe(current, token.path, this.update); + this.objectPath[index] = current; + } + } else { + this.view.adapters[token["interface"]].subscribe(current, token.path, this.update); + this.objectPath[index] = current; + } + current = this.view.adapters[token["interface"]].read(current, token.path); + } + return current; + }; + + return KeypathObserver; + + })(); + + Rivets.binders.enabled = function(el, value) { + return el.disabled = !value; + }; + + Rivets.binders.disabled = function(el, value) { + return el.disabled = !!value; + }; + + Rivets.binders.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; + } + } + }; + + Rivets.binders.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; + } + } + }; + + Rivets.binders.show = function(el, value) { + return el.style.display = value ? '' : 'none'; + }; + + Rivets.binders.hide = function(el, value) { + return el.style.display = value ? 'none' : ''; + }; + + Rivets.binders.html = function(el, value) { + return el.innerHTML = value != null ? value : ''; + }; + + Rivets.binders.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 : ''; + } + } + } + }; + + Rivets.binders.text = function(el, value) { + if (el.innerText != null) { + return el.innerText = value != null ? value : ''; + } else { + return el.textContent = value != null ? value : ''; + } + }; + + Rivets.binders["if"] = { + block: true, + bind: function(el) { + var attr, declaration; + if (this.marker == null) { + attr = [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, + adapters: this.view.options.adapters, + 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) { + var _ref; + return (_ref = this.nested) != null ? _ref.update(models) : void 0; + } + }; + + Rivets.binders.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); + } + }; + + Rivets.binders['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)); + } + }; + + Rivets.binders['each-*'] = { + block: true, + bind: function(el) { + var attr; + if (this.marker == null) { + attr = [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 binding, data, i, index, k, key, model, modelName, options, previous, template, v, view, _i, _j, _k, _len, _len1, _len2, _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]); + } + } + 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 (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, + adapters: this.view.options.adapters, + 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); + this.marker.parentNode.insertBefore(template, previous.nextSibling); + } else if (this.iterated[index].models[modelName] !== model) { + this.iterated[index].update(data); + } + } + if (el.nodeName === 'OPTION') { + _ref3 = this.view.bindings; + _results = []; + for (_k = 0, _len2 = _ref3.length; _k < _len2; _k++) { + binding = _ref3[_k]; + if (binding.el === this.marker.parentNode && binding.type === 'value') { + _results.push(binding.sync()); + } 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; + } + }; + + Rivets.binders['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(); + } + }; + + Rivets.binders['*'] = function(el, value) { + if (value) { + return el.setAttribute(this.type, value); + } else { + return el.removeAttribute(this.type); + } + }; + + Rivets.adapters['.'] = { + id: '_rv', + counter: 0, + weakmap: {}, + weakReference: function(obj) { + var id; + if (obj[this.id] == null) { + id = this.counter++; + this.weakmap[id] = { + callbacks: {} + }; + Object.defineProperty(obj, this.id, { + value: id + }); + } + return this.weakmap[obj[this.id]]; + }, + stubFunction: function(obj, fn) { + var map, original, weakmap; + original = obj[fn]; + map = this.weakReference(obj); + weakmap = this.weakmap; + return obj[fn] = function() { + var callback, k, r, response, _i, _len, _ref, _ref1, _ref2, _ref3; + response = original.apply(obj, arguments); + _ref = map.pointers; + for (r in _ref) { + k = _ref[r]; + _ref3 = (_ref1 = (_ref2 = weakmap[r]) != null ? _ref2.callbacks[k] : void 0) != null ? _ref1 : []; + for (_i = 0, _len = _ref3.length; _i < _len; _i++) { + callback = _ref3[_i]; + callback(); + } + } + return response; + }; + }, + observeMutations: function(obj, ref, keypath) { + var fn, functions, map, _base, _i, _len; + if (Array.isArray(obj)) { + map = this.weakReference(obj); + if (map.pointers == null) { + map.pointers = {}; + functions = ['push', 'pop', 'shift', 'unshift', 'sort', 'reverse', 'splice']; + for (_i = 0, _len = functions.length; _i < _len; _i++) { + fn = functions[_i]; + this.stubFunction(obj, fn); + } + } + if ((_base = map.pointers)[ref] == null) { + _base[ref] = []; + } + if (__indexOf.call(map.pointers[ref], keypath) < 0) { + return map.pointers[ref].push(keypath); + } + } + }, + unobserveMutations: function(obj, ref, keypath) { + var keypaths, _ref; + if (Array.isArray(obj && (obj[this.id] != null))) { + if (keypaths = (_ref = this.weakReference(obj).pointers) != null ? _ref[ref] : void 0) { + return keypaths.splice(keypaths.indexOf(keypath), 1); + } + } + }, + subscribe: function(obj, keypath, callback) { + var callbacks, value, + _this = this; + callbacks = this.weakReference(obj).callbacks; + if (callbacks[keypath] == null) { + callbacks[keypath] = []; + value = obj[keypath]; + Object.defineProperty(obj, keypath, { + get: function() { + return value; + }, + set: function(newValue) { + var _i, _len, _ref; + if (newValue !== value) { + value = newValue; + _ref = callbacks[keypath]; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + callback = _ref[_i]; + callback(); + } + return _this.observeMutations(newValue, obj[_this.id], keypath); + } + } + }); + } + if (__indexOf.call(callbacks[keypath], callback) < 0) { + callbacks[keypath].push(callback); + } + return this.observeMutations(obj[keypath], obj[this.id], keypath); + }, + unsubscribe: function(obj, keypath, callback) { + var callbacks; + callbacks = this.weakmap[obj[this.id]].callbacks[keypath]; + callbacks.splice(callbacks.indexOf(callback), 1); + return this.unobserveMutations(obj[keypath], obj[this.id], keypath); + }, + read: function(obj, keypath) { + return obj[keypath]; + }, + publish: function(obj, keypath, value) { + return obj[keypath] = value; + } + }; + + Rivets.factory = function(exports) { + exports._ = Rivets; + exports.binders = Rivets.binders; + exports.components = Rivets.components; + exports.formatters = Rivets.formatters; + exports.adapters = Rivets.adapters; + 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); diff --git a/ajax/libs/rivets/0.6.2/rivets.min.js b/ajax/libs/rivets/0.6.2/rivets.min.js new file mode 100644 index 000000000..6dfe5630a --- /dev/null +++ b/ajax/libs/rivets/0.6.2/rivets.min.js @@ -0,0 +1,5 @@ +// Rivets.js +// version: 0.6.2 +// author: Michael Richards +// license: MIT +(function(){var a,b,c=function(a,b){return function(){return a.apply(b,arguments)}},d=[].indexOf||function(a){for(var b=0,c=this.length;c>b;b++)if(b in this&&this[b]===a)return b;return-1},e=[].slice,f={}.hasOwnProperty,g=function(a,b){function c(){this.constructor=a}for(var d in b)f.call(b,d)&&(a[d]=b[d]);return c.prototype=b.prototype,a.prototype=new c,a.__super__=b.prototype,a};b={binders:{},components:{},formatters:{},adapters:{},config:{prefix:"rv",templateDelimiters:["{","}"],rootInterface:".",preloadData:!0,handler:function(a,b,c){return this.call(a,b,c.view.models)}}},b.Util={bindEvent:function(a,b,c){return null!=window.jQuery?(a=jQuery(a),null!=a.on?a.on(b,c):a.bind(b,c)):null!=window.addEventListener?a.addEventListener(b,c,!1):(b="on"+b,a.attachEvent(b,c))},unbindEvent:function(a,b,c){return null!=window.jQuery?(a=jQuery(a),null!=a.off?a.off(b,c):a.unbind(b,c)):null!=window.removeEventListener?a.removeEventListener(b,c,!1):(b="on"+b,a.detachEvent(b,c))},getInputValue:function(a){var b,c,d,e;if(null!=window.jQuery)switch(a=jQuery(a),a[0].type){case"checkbox":return a.is(":checked");default:return a.val()}else switch(a.type){case"checkbox":return a.checked;case"select-multiple":for(e=[],c=0,d=a.length;d>c;c++)b=a[c],b.selected&&e.push(b.value);return e;default:return a.value}}},b.View=function(){function a(a,d,e){var f,g,h,i,j,k,l,m,n;for(this.els=a,this.models=d,this.options=null!=e?e:{},this.update=c(this.update,this),this.publish=c(this.publish,this),this.sync=c(this.sync,this),this.unbind=c(this.unbind,this),this.bind=c(this.bind,this),this.select=c(this.select,this),this.build=c(this.build,this),this.componentRegExp=c(this.componentRegExp,this),this.bindingRegExp=c(this.bindingRegExp,this),this.els.jquery||this.els instanceof Array||(this.els=[this.els]),l=["config","binders","formatters","adapters"],j=0,k=l.length;k>j;j++){if(g=l[j],this[g]={},this.options[g]){m=this.options[g];for(f in m)h=m[f],this[g][f]=h}n=b[g];for(f in n)h=n[f],null==(i=this[g])[f]&&(i[f]=h)}this.build()}return a.prototype.bindingRegExp=function(){return new RegExp("^"+this.config.prefix+"-")},a.prototype.componentRegExp=function(){return new RegExp("^"+this.config.prefix.toUpperCase()+"-")},a.prototype.build=function(){var a,c,e,f,g,h,i,j,k,l=this;for(this.bindings=[],h=[],a=this.bindingRegExp(),e=this.componentRegExp(),c=function(a,c,d,e){var f,g,h,i,j,k,m;return j={},m=function(){var a,b,c,d;for(c=e.split("|"),d=[],a=0,b=c.length;b>a;a++)k=c[a],d.push(k.trim());return d}(),f=function(){var a,b,c,d;for(c=m.shift().split("<"),d=[],a=0,b=c.length;b>a;a++)g=c[a],d.push(g.trim());return d}(),i=f.shift(),j.formatters=m,(h=f.shift())&&(j.dependencies=h.split(/\s+/)),l.bindings.push(new b[a](l,c,d,i,j))},g=function(f){var i,j,k,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M;if(d.call(h,f)<0){if(f.nodeType===Node.TEXT_NODE){if(q=b.TextTemplateParser,(n=l.config.templateDelimiters)&&(u=q.parse(f.data,n)).length&&(1!==u.length||u[0].type!==q.types.text)){for(x=0,B=u.length;B>x;x++)t=u[x],s=document.createTextNode(t.value),f.parentNode.insertBefore(s,f),1===t.type&&c("TextBinding",s,null,t.value);f.parentNode.removeChild(f)}}else if(e.test(f.tagName))v=f.tagName.replace(e,"").toLowerCase(),l.bindings.push(new b.ComponentBinding(l,f,v));else if(null!=f.attributes){for(H=f.attributes,y=0,C=H.length;C>y;y++)if(i=H[y],a.test(i.name)){if(v=i.name.replace(a,""),!(k=l.binders[v])){I=l.binders;for(o in I)w=I[o],"*"!==o&&-1!==o.indexOf("*")&&(r=new RegExp("^"+o.replace("*",".+")+"$"),r.test(v)&&(k=w))}if(k||(k=l.binders["*"]),k.block){for(J=f.childNodes,z=0,D=J.length;D>z;z++)p=J[z],h.push(p);j=[i]}}for(K=j||f.attributes,A=0,E=K.length;E>A;A++)i=K[A],a.test(i.name)&&(v=i.name.replace(a,""),c("Binding",f,v,i.value))}for(L=function(){var a,b,c,d;for(c=f.childNodes,d=[],b=0,a=c.length;a>b;b++)p=c[b],d.push(p);return d}(),M=[],G=0,F=L.length;F>G;G++)m=L[G],M.push(g(m));return M}},k=this.els,i=0,j=k.length;j>i;i++)f=k[i],g(f)},a.prototype.select=function(a){var b,c,d,e,f;for(e=this.bindings,f=[],c=0,d=e.length;d>c;c++)b=e[c],a(b)&&f.push(b);return f},a.prototype.bind=function(){var a,b,c,d,e;for(d=this.bindings,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.bind());return e},a.prototype.unbind=function(){var a,b,c,d,e;for(d=this.bindings,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.unbind());return e},a.prototype.sync=function(){var a,b,c,d,e;for(d=this.bindings,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.sync());return e},a.prototype.publish=function(){var a,b,c,d,e;for(d=this.select(function(a){return a.binder.publishes}),e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.publish());return e},a.prototype.update=function(a){var b,c,d,e,f,g,h;null==a&&(a={});for(c in a)d=a[c],this.models[c]=d;for(g=this.bindings,h=[],e=0,f=g.length;f>e;e++)b=g[e],h.push(b.update(a));return h},a}(),b.Binding=function(){function d(a,b,d,e,f){this.view=a,this.el=b,this.type=d,this.keypath=e,this.options=null!=f?f:{},this.update=c(this.update,this),this.unbind=c(this.unbind,this),this.bind=c(this.bind,this),this.publish=c(this.publish,this),this.sync=c(this.sync,this),this.set=c(this.set,this),this.eventHandler=c(this.eventHandler,this),this.formattedValue=c(this.formattedValue,this),this.setObserver=c(this.setObserver,this),this.setBinder=c(this.setBinder,this),this.formatters=this.options.formatters||[],this.dependencies=[],this.setBinder(),this.setObserver()}return d.prototype.setBinder=function(){var a,b,c,d;if(!(this.binder=this.view.binders[this.type])){d=this.view.binders;for(a in d)c=d[a],"*"!==a&&-1!==a.indexOf("*")&&(b=new RegExp("^"+a.replace("*",".+")+"$"),b.test(this.type)&&(this.binder=c,this.args=new RegExp("^"+a.replace("*","(.+)")+"$").exec(this.type),this.args.shift()))}return this.binder||(this.binder=this.view.binders["*"]),this.binder instanceof Function?this.binder={routine:this.binder}:void 0},d.prototype.setObserver=function(){var b=this;return this.observer=new a(this.view,this.view.models,this.keypath,function(a){return b.key&&b.unbind(!0),b.model=a.target,b.key&&b.bind(!0),b.sync()}),this.key=this.observer.key,this.model=this.observer.target},d.prototype.formattedValue=function(a){var b,c,d,f,g,h;for(h=this.formatters,f=0,g=h.length;g>f;f++)c=h[f],b=c.split(/\s+/),d=b.shift(),c=this.view.formatters[d],(null!=c?c.read:void 0)instanceof Function?a=c.read.apply(c,[a].concat(e.call(b))):c instanceof Function&&(a=c.apply(null,[a].concat(e.call(b))));return a},d.prototype.eventHandler=function(a){var b,c;return c=(b=this).view.config.handler,function(d){return c.call(a,this,d,b)}},d.prototype.set=function(a){var b;return a=a instanceof Function&&!this.binder["function"]?this.formattedValue(a.call(this.model)):this.formattedValue(a),null!=(b=this.binder.routine)?b.call(this,this.el,a):void 0},d.prototype.sync=function(){return this.set(this.key?this.view.adapters[this.key["interface"]].read(this.model,this.key.path):this.model)},d.prototype.publish=function(){var a,c,d,f,g,h,i,j,k;for(f=b.Util.getInputValue(this.el),i=this.formatters.slice(0).reverse(),g=0,h=i.length;h>g;g++)c=i[g],a=c.split(/\s+/),d=a.shift(),(null!=(j=this.view.formatters[d])?j.publish:void 0)&&(f=(k=this.view.formatters[d]).publish.apply(k,[f].concat(e.call(a))));return this.view.adapters[this.key["interface"]].publish(this.model,this.key.path,f)},d.prototype.bind=function(b){var c,d,e,f,g,h,i,j,k,l=this;if(null==b&&(b=!1),b||null!=(h=this.binder.bind)&&h.call(this,this.el),this.key&&this.view.adapters[this.key["interface"]].subscribe(this.model,this.key.path,this.sync),(b?void 0:this.view.config.preloadData)&&this.sync(),null!=(i=this.options.dependencies)?i.length:void 0){for(j=this.options.dependencies,k=[],f=0,g=j.length;g>f;f++)c=j[f],e=new a(this.view,this.model,c,function(a,b){var c;return c=a.key,l.view.adapters[c["interface"]].unsubscribe(b,c.path,l.sync),l.view.adapters[c["interface"]].subscribe(a.target,c.path,l.sync),l.sync()}),d=e.key,this.view.adapters[d["interface"]].subscribe(e.target,d.path,this.sync),k.push(this.dependencies.push(e));return k}},d.prototype.unbind=function(a){var b,c,d,e,f,g;if(null==a&&(a=!1),a||null!=(f=this.binder.unbind)&&f.call(this,this.el),this.key&&this.view.adapters[this.key["interface"]].unsubscribe(this.model,this.key.path,this.sync),this.dependencies.length){for(g=this.dependencies,d=0,e=g.length;e>d;d++)c=g[d],b=c.key,this.view.adapters[b["interface"]].unsubscribe(c.target,b.path,this.sync);return this.dependencies=[]}},d.prototype.update=function(a){var b;return null==a&&(a={}),null!=(b=this.binder.update)?b.call(this,a):void 0},d}(),b.ComponentBinding=function(a){function e(a,e,f){var g,h,i,j,k;for(this.view=a,this.el=e,this.type=f,this.unbind=c(this.unbind,this),this.bind=c(this.bind,this),this.update=c(this.update,this),this.locals=c(this.locals,this),this.component=b.components[this.type],this.attributes={},this.inflections={},j=this.el.attributes||[],h=0,i=j.length;i>h;h++)g=j[h],k=g.name,d.call(this.component.attributes,k)>=0?this.attributes[g.name]=g.value:this.inflections[g.name]=g.value}return g(e,a),e.prototype.sync=function(){},e.prototype.locals=function(a){var b,c,d,e,f,g,h,i,j;null==a&&(a=this.view.models),f={},i=this.inflections;for(c in i)for(b=i[c],j=b.split("."),g=0,h=j.length;h>g;g++)e=j[g],f[c]=(f[c]||a)[e];for(c in a)d=a[c],null==f[c]&&(f[c]=d);return f},e.prototype.update=function(a){var b;return null!=(b=this.componentView)?b.update(this.locals(a)):void 0},e.prototype.bind=function(){var a,c;return null!=this.componentView?null!=(c=this.componentView)?c.bind():void 0:(a=this.component.build.call(this.attributes),(this.componentView=new b.View(a,this.locals(),this.view.options)).bind(),this.el.parentNode.replaceChild(a,this.el))},e.prototype.unbind=function(){var a;return null!=(a=this.componentView)?a.unbind():void 0},e}(b.Binding),b.TextBinding=function(a){function b(a,b,d,e,f){this.view=a,this.el=b,this.type=d,this.keypath=e,this.options=null!=f?f:{},this.sync=c(this.sync,this),this.formatters=this.options.formatters||[],this.dependencies=[],this.setObserver()}return g(b,a),b.prototype.binder={routine:function(a,b){return a.data=null!=b?b:""}},b.prototype.sync=function(){return b.__super__.sync.apply(this,arguments)},b}(b.Binding),b.KeypathParser=function(){function a(){}return a.parse=function(a,b,c){var e,f,g,h;h=[],f={"interface":c,path:""};for(g in a)e=a[g],d.call(b,e)>=0?(h.push(f),f={"interface":e,path:""}):f.path+=e;return h.push(f),h},a}(),b.TextTemplateParser=function(){function a(){}return a.types={text:0,binding:1},a.parse=function(a,b){var c,d,e,f,g,h,i;for(h=[],f=a.length,c=0,d=0;f>d;){if(c=a.indexOf(b[0],d),0>c){h.push({type:this.types.text,value:a.slice(d)});break}if(c>0&&c>d&&h.push({type:this.types.text,value:a.slice(d,c)}),d=c+b[0].length,c=a.indexOf(b[1],d),0>c){g=a.slice(d-b[1].length),e=h[h.length-1],(null!=e?e.type:void 0)===this.types.text?e.value+=g:h.push({type:this.types.text,value:g});break}i=a.slice(d,c).trim(),h.push({type:this.types.binding,value:i}),d=c+b[1].length}return h},a}(),a=function(){function a(a,b,d,e){this.view=a,this.model=b,this.keypath=d,this.callback=e,this.realize=c(this.realize,this),this.update=c(this.update,this),this.parse=c(this.parse,this),this.parse(),this.objectPath=[],this.target=this.realize()}return a.prototype.parse=function(){var a,c,e,f,g,h;return a=function(){var a,b;a=this.view.adapters,b=[];for(c in a)g=a[c],b.push(c);return b}.call(this),h=this.keypath[0],d.call(a,h)>=0?(f=this.keypath[0],e=this.keypath.substr(1)):(f=this.view.config.rootInterface,e=this.keypath),this.tokens=b.KeypathParser.parse(e,a,f),this.key=this.tokens.pop()},a.prototype.update=function(){var a,b;return(a=this.realize())!==this.target?(b=this.target,this.target=a,this.callback(this,b)):void 0},a.prototype.realize=function(){var a,b,c,d,e,f,g;for(a=this.model,g=this.tokens,b=e=0,f=g.length;f>e;b=++e)d=g[b],null!=this.objectPath[b]?a!==(c=this.objectPath[b])&&(this.view.adapters[d["interface"]].unsubscribe(c,d.path,this.update),this.view.adapters[d["interface"]].subscribe(a,d.path,this.update),this.objectPath[b]=a):(this.view.adapters[d["interface"]].subscribe(a,d.path,this.update),this.objectPath[b]=a),a=this.view.adapters[d["interface"]].read(a,d.path);return a},a}(),b.binders.enabled=function(a,b){return a.disabled=!b},b.binders.disabled=function(a,b){return a.disabled=!!b},b.binders.checked={publishes:!0,bind:function(a){return b.Util.bindEvent(a,"change",this.publish)},unbind:function(a){return b.Util.unbindEvent(a,"change",this.publish)},routine:function(a,b){var c;return a.checked="radio"===a.type?(null!=(c=a.value)?c.toString():void 0)===(null!=b?b.toString():void 0):!!b}},b.binders.unchecked={publishes:!0,bind:function(a){return b.Util.bindEvent(a,"change",this.publish)},unbind:function(a){return b.Util.unbindEvent(a,"change",this.publish)},routine:function(a,b){var c;return a.checked="radio"===a.type?(null!=(c=a.value)?c.toString():void 0)!==(null!=b?b.toString():void 0):!b}},b.binders.show=function(a,b){return a.style.display=b?"":"none"},b.binders.hide=function(a,b){return a.style.display=b?"none":""},b.binders.html=function(a,b){return a.innerHTML=null!=b?b:""},b.binders.value={publishes:!0,bind:function(a){return b.Util.bindEvent(a,"change",this.publish)},unbind:function(a){return b.Util.unbindEvent(a,"change",this.publish)},routine:function(a,b){var c,e,f,g,h,i,j;if(null!=window.jQuery){if(a=jQuery(a),(null!=b?b.toString():void 0)!==(null!=(g=a.val())?g.toString():void 0))return a.val(null!=b?b:"")}else if("select-multiple"===a.type){if(null!=b){for(j=[],e=0,f=a.length;f>e;e++)c=a[e],j.push(c.selected=(h=c.value,d.call(b,h)>=0));return j}}else if((null!=b?b.toString():void 0)!==(null!=(i=a.value)?i.toString():void 0))return a.value=null!=b?b:""}},b.binders.text=function(a,b){return null!=a.innerText?a.innerText=null!=b?b:"":a.textContent=null!=b?b:""},b.binders["if"]={block:!0,bind:function(a){var b,c;return null==this.marker?(b=[this.view.config.prefix,this.type].join("-").replace("--","-"),c=a.getAttribute(b),this.marker=document.createComment(" rivets: "+this.type+" "+c+" "),a.removeAttribute(b),a.parentNode.insertBefore(this.marker,a),a.parentNode.removeChild(a)):void 0},unbind:function(){var a;return null!=(a=this.nested)?a.unbind():void 0},routine:function(a,c){var d,e,f,g,h;if(!!c==(null==this.nested)){if(c){f={},h=this.view.models;for(d in h)e=h[d],f[d]=e;return g={binders:this.view.options.binders,formatters:this.view.options.formatters,adapters:this.view.options.adapters,config:this.view.options.config},(this.nested=new b.View(a,f,g)).bind(),this.marker.parentNode.insertBefore(a,this.marker.nextSibling)}return a.parentNode.removeChild(a),this.nested.unbind(),delete this.nested}},update:function(a){var b;return null!=(b=this.nested)?b.update(a):void 0}},b.binders.unless={block:!0,bind:function(a){return b.binders["if"].bind.call(this,a)},unbind:function(){return b.binders["if"].unbind.call(this)},routine:function(a,c){return b.binders["if"].routine.call(this,a,!c)},update:function(a){return b.binders["if"].update.call(this,a)}},b.binders["on-*"]={"function":!0,unbind:function(a){return this.handler?b.Util.unbindEvent(a,this.args[0],this.handler):void 0},routine:function(a,c){return this.handler&&b.Util.unbindEvent(a,this.args[0],this.handler),b.Util.bindEvent(a,this.args[0],this.handler=this.eventHandler(c))}},b.binders["each-*"]={block:!0,bind:function(a){var b;return null==this.marker?(b=[this.view.config.prefix,this.type].join("-").replace("--","-"),this.marker=document.createComment(" rivets: "+this.type+" "),this.iterated=[],a.removeAttribute(b),a.parentNode.insertBefore(this.marker,a),a.parentNode.removeChild(a)):void 0},unbind:function(){var a,b,c,d,e;if(null!=this.iterated){for(d=this.iterated,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.unbind());return e}},routine:function(a,c){var d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A;if(k=this.args[0],c=c||[],this.iterated.length>c.length)for(w=Array(this.iterated.length-c.length),q=0,t=w.length;t>q;q++)f=w[q],p=this.iterated.pop(),p.unbind(),this.marker.parentNode.removeChild(p.els[0]);for(g=r=0,u=c.length;u>r;g=++r)if(j=c[g],e={},e[k]=j,null==this.iterated[g]){x=this.view.models;for(i in x)j=x[i],null==e[i]&&(e[i]=j);m=this.iterated.length?this.iterated[this.iterated.length-1].els[0]:this.marker,l={binders:this.view.options.binders,formatters:this.view.options.formatters,adapters:this.view.options.adapters,config:{}},y=this.view.options.config;for(h in y)o=y[h],l.config[h]=o;l.config.preloadData=!0,n=a.cloneNode(!0),p=new b.View(n,e,l),p.bind(),this.iterated.push(p),this.marker.parentNode.insertBefore(n,m.nextSibling)}else this.iterated[g].models[k]!==j&&this.iterated[g].update(e);if("OPTION"===a.nodeName){for(z=this.view.bindings,A=[],s=0,v=z.length;v>s;s++)d=z[s],d.el===this.marker.parentNode&&"value"===d.type?A.push(d.sync()):A.push(void 0);return A}},update:function(a){var b,c,d,e,f,g,h,i;b={};for(c in a)d=a[c],c!==this.args[0]&&(b[c]=d);for(h=this.iterated,i=[],f=0,g=h.length;g>f;f++)e=h[f],i.push(e.update(b));return i}},b.binders["class-*"]=function(a,b){var c;return c=" "+a.className+" ",!b==(-1!==c.indexOf(" "+this.args[0]+" "))?a.className=b?""+a.className+" "+this.args[0]:c.replace(" "+this.args[0]+" "," ").trim():void 0},b.binders["*"]=function(a,b){return b?a.setAttribute(this.type,b):a.removeAttribute(this.type)},b.adapters["."]={id:"_rv",counter:0,weakmap:{},weakReference:function(a){var b;return null==a[this.id]&&(b=this.counter++,this.weakmap[b]={callbacks:{}},Object.defineProperty(a,this.id,{value:b})),this.weakmap[a[this.id]]},stubFunction:function(a,b){var c,d,e;return d=a[b],c=this.weakReference(a),e=this.weakmap,a[b]=function(){var b,f,g,h,i,j,k,l,m,n;h=d.apply(a,arguments),k=c.pointers;for(g in k)for(f=k[g],n=null!=(l=null!=(m=e[g])?m.callbacks[f]:void 0)?l:[],i=0,j=n.length;j>i;i++)b=n[i],b();return h}},observeMutations:function(a,b,c){var e,f,g,h,i,j;if(Array.isArray(a)){if(g=this.weakReference(a),null==g.pointers)for(g.pointers={},f=["push","pop","shift","unshift","sort","reverse","splice"],i=0,j=f.length;j>i;i++)e=f[i],this.stubFunction(a,e);if(null==(h=g.pointers)[b]&&(h[b]=[]),d.call(g.pointers[b],c)<0)return g.pointers[b].push(c)}},unobserveMutations:function(a,b,c){var d,e;return Array.isArray(a&&null!=a[this.id])&&(d=null!=(e=this.weakReference(a).pointers)?e[b]:void 0)?d.splice(d.indexOf(c),1):void 0},subscribe:function(a,b,c){var e,f,g=this;return e=this.weakReference(a).callbacks,null==e[b]&&(e[b]=[],f=a[b],Object.defineProperty(a,b,{get:function(){return f},set:function(d){var h,i,j;if(d!==f){for(f=d,j=e[b],h=0,i=j.length;i>h;h++)c=j[h],c();return g.observeMutations(d,a[g.id],b)}}})),d.call(e[b],c)<0&&e[b].push(c),this.observeMutations(a[b],a[this.id],b)},unsubscribe:function(a,b,c){var d;return d=this.weakmap[a[this.id]].callbacks[b],d.splice(d.indexOf(c),1),this.unobserveMutations(a[b],a[this.id],b)},read:function(a,b){return a[b]},publish:function(a,b,c){return a[b]=c}},b.factory=function(a){return a._=b,a.binders=b.binders,a.components=b.components,a.formatters=b.formatters,a.adapters=b.adapters,a.config=b.config,a.configure=function(a){var c,d;null==a&&(a={});for(c in a)d=a[c],b.config[c]=d},a.bind=function(a,c,d){var e;return null==c&&(c={}),null==d&&(d={}),e=new b.View(a,c,d),e.bind(),e}},"object"==typeof exports?b.factory(exports):"function"==typeof define&&define.amd?define(["exports"],function(a){return b.factory(this.rivets=a),a}):b.factory(this.rivets={})}).call(this); \ No newline at end of file diff --git a/ajax/libs/rivets/0.6.3/rivets.js b/ajax/libs/rivets/0.6.3/rivets.js new file mode 100644 index 000000000..615a163c5 --- /dev/null +++ b/ajax/libs/rivets/0.6.3/rivets.js @@ -0,0 +1,1242 @@ +// Rivets.js +// version: 0.6.3 +// author: Michael Richards +// license: MIT +(function() { + var KeypathObserver, Rivets, + __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, + __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; }, + __slice = [].slice, + __hasProp = {}.hasOwnProperty, + __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; + + Rivets = { + binders: {}, + components: {}, + formatters: {}, + adapters: {}, + config: { + prefix: 'rv', + templateDelimiters: ['{', '}'], + rootInterface: '.', + preloadData: true, + handler: function(context, ev, binding) { + return this.call(context, ev, binding.view.models); + } + } + }; + + 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.View = (function() { + function View(els, models, options) { + var k, option, v, _base, _i, _len, _ref, _ref1, _ref2; + 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.componentRegExp = __bind(this.componentRegExp, this); + this.bindingRegExp = __bind(this.bindingRegExp, this); + if (!(this.els.jquery || this.els instanceof Array)) { + this.els = [this.els]; + } + _ref = ['config', 'binders', 'formatters', 'adapters']; + 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 ((_base = this[option])[k] == null) { + _base[k] = v; + } + } + } + this.build(); + } + + View.prototype.bindingRegExp = function() { + return new RegExp("^" + this.config.prefix + "-"); + }; + + View.prototype.componentRegExp = function() { + return new RegExp("^" + (this.config.prefix.toUpperCase()) + "-"); + }; + + View.prototype.build = function() { + var bindingRegExp, buildBinding, componentRegExp, el, parse, skipNodes, _i, _len, _ref, + _this = this; + this.bindings = []; + skipNodes = []; + bindingRegExp = this.bindingRegExp(); + componentRegExp = this.componentRegExp(); + buildBinding = function(binding, node, type, declaration) { + var context, ctx, dependencies, keypath, options, pipe, pipes; + 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; + })(); + keypath = context.shift(); + options.formatters = pipes; + if (dependencies = context.shift()) { + options.dependencies = dependencies.split(/\s+/); + } + return _this.bindings.push(new Rivets[binding](_this, node, type, keypath, options)); + }; + parse = function(node) { + var attribute, attributes, binder, childNode, delimiters, identifier, n, parser, regexp, 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 === 3) { + 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)) { + for (_i = 0, _len = tokens.length; _i < _len; _i++) { + token = tokens[_i]; + text = document.createTextNode(token.value); + node.parentNode.insertBefore(text, node); + if (token.type === 1) { + buildBinding('TextBinding', text, null, token.value); + } + } + node.parentNode.removeChild(node); + } + } + } + } else if (componentRegExp.test(node.tagName)) { + type = node.tagName.replace(componentRegExp, '').toLowerCase(); + _this.bindings.push(new Rivets.ComponentBinding(_this, node, type)); + } 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('Binding', node, type, attribute.value); + } + } + } + _ref4 = (function() { + var _len4, _n, _ref4, _results1; + _ref4 = node.childNodes; + _results1 = []; + for (_n = 0, _len4 = _ref4.length; _n < _len4; _n++) { + n = _ref4[_n]; + _results1.push(n); + } + return _results1; + })(); + _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.Binding = (function() { + function Binding(view, el, type, keypath, options) { + this.view = view; + this.el = el; + this.type = type; + 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); + this.setObserver = __bind(this.setObserver, this); + this.setBinder = __bind(this.setBinder, this); + this.formatters = this.options.formatters || []; + this.dependencies = []; + this.setBinder(); + this.setObserver(); + } + + Binding.prototype.setBinder = function() { + var identifier, regexp, value, _ref; + if (!(this.binder = this.view.binders[this.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(this.type)) { + this.binder = value; + this.args = new RegExp("^" + (identifier.replace('*', '(.+)')) + "$").exec(this.type); + this.args.shift(); + } + } + } + } + this.binder || (this.binder = this.view.binders['*']); + if (this.binder instanceof Function) { + return this.binder = { + routine: this.binder + }; + } + }; + + Binding.prototype.setObserver = function() { + var _this = this; + this.observer = new KeypathObserver(this.view, this.view.models, this.keypath, function(obs) { + if (_this.key) { + _this.unbind(true); + } + _this.model = obs.target; + if (_this.key) { + _this.bind(true); + } + return _this.sync(); + }); + this.key = this.observer.key; + return this.model = this.observer.target; + }; + + 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.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.key ? this.view.adapters[this.key["interface"]].read(this.model, this.key.path) : this.model); + }; + + 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.adapters[this.key["interface"]].publish(this.model, this.key.path, value); + }; + + Binding.prototype.bind = function(silent) { + var dependency, key, observer, _i, _len, _ref, _ref1, _ref2, _results, + _this = this; + if (silent == null) { + silent = false; + } + if (!silent) { + if ((_ref = this.binder.bind) != null) { + _ref.call(this, this.el); + } + } + if (this.key) { + this.view.adapters[this.key["interface"]].subscribe(this.model, this.key.path, this.sync); + } + if (!silent ? this.view.config.preloadData : void 0) { + 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]; + observer = new KeypathObserver(this.view, this.model, dependency, function(obs, prev) { + var key; + key = obs.key; + _this.view.adapters[key["interface"]].unsubscribe(prev, key.path, _this.sync); + _this.view.adapters[key["interface"]].subscribe(obs.target, key.path, _this.sync); + return _this.sync(); + }); + key = observer.key; + this.view.adapters[key["interface"]].subscribe(observer.target, key.path, this.sync); + _results.push(this.dependencies.push(observer)); + } + return _results; + } + }; + + Binding.prototype.unbind = function(silent) { + var key, obs, _i, _len, _ref, _ref1; + if (silent == null) { + silent = false; + } + if (!silent) { + if ((_ref = this.binder.unbind) != null) { + _ref.call(this, this.el); + } + } + if (this.key) { + this.view.adapters[this.key["interface"]].unsubscribe(this.model, this.key.path, this.sync); + } + if (this.dependencies.length) { + _ref1 = this.dependencies; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + obs = _ref1[_i]; + key = obs.key; + this.view.adapters[key["interface"]].unsubscribe(obs.target, key.path, this.sync); + } + return this.dependencies = []; + } + }; + + Binding.prototype.update = function(models) { + var _ref; + if (models == null) { + models = {}; + } + return (_ref = this.binder.update) != null ? _ref.call(this, models) : void 0; + }; + + return Binding; + + })(); + + Rivets.ComponentBinding = (function(_super) { + __extends(ComponentBinding, _super); + + function ComponentBinding(view, el, type) { + var attribute, _i, _len, _ref, _ref1; + this.view = view; + this.el = el; + this.type = type; + this.unbind = __bind(this.unbind, this); + this.bind = __bind(this.bind, this); + this.update = __bind(this.update, this); + this.locals = __bind(this.locals, this); + this.component = Rivets.components[this.type]; + this.attributes = {}; + this.inflections = {}; + _ref = this.el.attributes || []; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + attribute = _ref[_i]; + if (_ref1 = attribute.name, __indexOf.call(this.component.attributes, _ref1) >= 0) { + this.attributes[attribute.name] = attribute.value; + } else { + this.inflections[attribute.name] = attribute.value; + } + } + } + + ComponentBinding.prototype.sync = function() {}; + + ComponentBinding.prototype.locals = function(models) { + var inverse, key, model, path, result, _i, _len, _ref, _ref1; + if (models == null) { + models = this.view.models; + } + result = {}; + _ref = this.inflections; + for (key in _ref) { + inverse = _ref[key]; + _ref1 = inverse.split('.'); + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + path = _ref1[_i]; + result[key] = (result[key] || models)[path]; + } + } + for (key in models) { + model = models[key]; + if (result[key] == null) { + result[key] = model; + } + } + return result; + }; + + ComponentBinding.prototype.update = function(models) { + var _ref; + return (_ref = this.componentView) != null ? _ref.update(this.locals(models)) : void 0; + }; + + ComponentBinding.prototype.bind = function() { + var el, _ref; + if (this.componentView != null) { + return (_ref = this.componentView) != null ? _ref.bind() : void 0; + } else { + el = this.component.build.call(this.attributes); + (this.componentView = new Rivets.View(el, this.locals(), this.view.options)).bind(); + return this.el.parentNode.replaceChild(el, this.el); + } + }; + + ComponentBinding.prototype.unbind = function() { + var _ref; + return (_ref = this.componentView) != null ? _ref.unbind() : void 0; + }; + + return ComponentBinding; + + })(Rivets.Binding); + + Rivets.TextBinding = (function(_super) { + __extends(TextBinding, _super); + + function TextBinding(view, el, type, keypath, options) { + this.view = view; + this.el = el; + this.type = type; + this.keypath = keypath; + this.options = options != null ? options : {}; + this.sync = __bind(this.sync, this); + this.formatters = this.options.formatters || []; + this.dependencies = []; + this.setObserver(); + } + + TextBinding.prototype.binder = { + routine: function(node, value) { + return node.data = value != null ? value : ''; + } + }; + + TextBinding.prototype.sync = function() { + return TextBinding.__super__.sync.apply(this, arguments); + }; + + return TextBinding; + + })(Rivets.Binding); + + Rivets.KeypathParser = (function() { + function KeypathParser() {} + + KeypathParser.parse = function(keypath, interfaces, root) { + var char, current, index, tokens; + tokens = []; + current = { + "interface": root, + path: '' + }; + for (index in keypath) { + char = keypath[index]; + if (__indexOf.call(interfaces, char) >= 0) { + tokens.push(current); + current = { + "interface": char, + path: '' + }; + } else { + current.path += char; + } + } + tokens.push(current); + return tokens; + }; + + return KeypathParser; + + })(); + + 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 + delimiters[0].length; + index = template.indexOf(delimiters[1], lastIndex); + if (index < 0) { + substring = template.slice(lastIndex - delimiters[1].length); + 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 + delimiters[1].length; + } + } + return tokens; + }; + + return TextTemplateParser; + + })(); + + KeypathObserver = (function() { + function KeypathObserver(view, model, keypath, callback) { + this.view = view; + this.model = model; + this.keypath = keypath; + this.callback = callback; + this.realize = __bind(this.realize, this); + this.update = __bind(this.update, this); + this.parse = __bind(this.parse, this); + this.parse(); + this.objectPath = []; + this.target = this.realize(); + } + + KeypathObserver.prototype.parse = function() { + var interfaces, k, path, root, v, _ref; + interfaces = (function() { + var _ref, _results; + _ref = this.view.adapters; + _results = []; + for (k in _ref) { + v = _ref[k]; + _results.push(k); + } + return _results; + }).call(this); + if (_ref = this.keypath[0], __indexOf.call(interfaces, _ref) >= 0) { + root = this.keypath[0]; + path = this.keypath.substr(1); + } else { + root = this.view.config.rootInterface; + path = this.keypath; + } + this.tokens = Rivets.KeypathParser.parse(path, interfaces, root); + return this.key = this.tokens.pop(); + }; + + KeypathObserver.prototype.update = function() { + var next, prev; + if ((next = this.realize()) !== this.target) { + prev = this.target; + this.target = next; + return this.callback(this, prev); + } + }; + + KeypathObserver.prototype.realize = function() { + var current, index, prev, token, _i, _len, _ref; + current = this.model; + _ref = this.tokens; + for (index = _i = 0, _len = _ref.length; _i < _len; index = ++_i) { + token = _ref[index]; + if (this.objectPath[index] != null) { + if (current !== (prev = this.objectPath[index])) { + this.view.adapters[token["interface"]].unsubscribe(prev, token.path, this.update); + this.view.adapters[token["interface"]].subscribe(current, token.path, this.update); + this.objectPath[index] = current; + } + } else { + this.view.adapters[token["interface"]].subscribe(current, token.path, this.update); + this.objectPath[index] = current; + } + current = this.view.adapters[token["interface"]].read(current, token.path); + } + return current; + }; + + return KeypathObserver; + + })(); + + Rivets.binders.enabled = function(el, value) { + return el.disabled = !value; + }; + + Rivets.binders.disabled = function(el, value) { + return el.disabled = !!value; + }; + + Rivets.binders.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; + } + } + }; + + Rivets.binders.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; + } + } + }; + + Rivets.binders.show = function(el, value) { + return el.style.display = value ? '' : 'none'; + }; + + Rivets.binders.hide = function(el, value) { + return el.style.display = value ? 'none' : ''; + }; + + Rivets.binders.html = function(el, value) { + return el.innerHTML = value != null ? value : ''; + }; + + Rivets.binders.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 : ''; + } + } + } + }; + + Rivets.binders.text = function(el, value) { + if (el.textContent != null) { + return el.textContent = value != null ? value : ''; + } else { + return el.innerText = value != null ? value : ''; + } + }; + + Rivets.binders["if"] = { + block: true, + bind: function(el) { + var attr, declaration; + if (this.marker == null) { + attr = [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, + adapters: this.view.options.adapters, + 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) { + var _ref; + return (_ref = this.nested) != null ? _ref.update(models) : void 0; + } + }; + + Rivets.binders.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); + } + }; + + Rivets.binders['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)); + } + }; + + Rivets.binders['each-*'] = { + block: true, + bind: function(el) { + var attr; + if (this.marker == null) { + attr = [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 binding, data, i, index, k, key, model, modelName, options, previous, template, v, view, _i, _j, _k, _len, _len1, _len2, _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]); + } + } + 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 (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, + adapters: this.view.options.adapters, + 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); + this.marker.parentNode.insertBefore(template, previous.nextSibling); + } else if (this.iterated[index].models[modelName] !== model) { + this.iterated[index].update(data); + } + } + if (el.nodeName === 'OPTION') { + _ref3 = this.view.bindings; + _results = []; + for (_k = 0, _len2 = _ref3.length; _k < _len2; _k++) { + binding = _ref3[_k]; + if (binding.el === this.marker.parentNode && binding.type === 'value') { + _results.push(binding.sync()); + } 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; + } + }; + + Rivets.binders['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(); + } + }; + + Rivets.binders['*'] = function(el, value) { + if (value) { + return el.setAttribute(this.type, value); + } else { + return el.removeAttribute(this.type); + } + }; + + Rivets.adapters['.'] = { + id: '_rv', + counter: 0, + weakmap: {}, + weakReference: function(obj) { + var id; + if (obj[this.id] == null) { + id = this.counter++; + this.weakmap[id] = { + callbacks: {} + }; + Object.defineProperty(obj, this.id, { + value: id + }); + } + return this.weakmap[obj[this.id]]; + }, + stubFunction: function(obj, fn) { + var map, original, weakmap; + original = obj[fn]; + map = this.weakReference(obj); + weakmap = this.weakmap; + return obj[fn] = function() { + var callback, k, r, response, _i, _len, _ref, _ref1, _ref2, _ref3; + response = original.apply(obj, arguments); + _ref = map.pointers; + for (r in _ref) { + k = _ref[r]; + _ref3 = (_ref1 = (_ref2 = weakmap[r]) != null ? _ref2.callbacks[k] : void 0) != null ? _ref1 : []; + for (_i = 0, _len = _ref3.length; _i < _len; _i++) { + callback = _ref3[_i]; + callback(); + } + } + return response; + }; + }, + observeMutations: function(obj, ref, keypath) { + var fn, functions, map, _base, _i, _len; + if (Array.isArray(obj)) { + map = this.weakReference(obj); + if (map.pointers == null) { + map.pointers = {}; + functions = ['push', 'pop', 'shift', 'unshift', 'sort', 'reverse', 'splice']; + for (_i = 0, _len = functions.length; _i < _len; _i++) { + fn = functions[_i]; + this.stubFunction(obj, fn); + } + } + if ((_base = map.pointers)[ref] == null) { + _base[ref] = []; + } + if (__indexOf.call(map.pointers[ref], keypath) < 0) { + return map.pointers[ref].push(keypath); + } + } + }, + unobserveMutations: function(obj, ref, keypath) { + var keypaths, _ref; + if (Array.isArray(obj && (obj[this.id] != null))) { + if (keypaths = (_ref = this.weakReference(obj).pointers) != null ? _ref[ref] : void 0) { + return keypaths.splice(keypaths.indexOf(keypath), 1); + } + } + }, + subscribe: function(obj, keypath, callback) { + var callbacks, value, + _this = this; + callbacks = this.weakReference(obj).callbacks; + if (callbacks[keypath] == null) { + callbacks[keypath] = []; + value = obj[keypath]; + Object.defineProperty(obj, keypath, { + get: function() { + return value; + }, + set: function(newValue) { + var _i, _len, _ref; + if (newValue !== value) { + value = newValue; + _ref = callbacks[keypath]; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + callback = _ref[_i]; + callback(); + } + return _this.observeMutations(newValue, obj[_this.id], keypath); + } + } + }); + } + if (__indexOf.call(callbacks[keypath], callback) < 0) { + callbacks[keypath].push(callback); + } + return this.observeMutations(obj[keypath], obj[this.id], keypath); + }, + unsubscribe: function(obj, keypath, callback) { + var callbacks; + callbacks = this.weakmap[obj[this.id]].callbacks[keypath]; + callbacks.splice(callbacks.indexOf(callback), 1); + return this.unobserveMutations(obj[keypath], obj[this.id], keypath); + }, + read: function(obj, keypath) { + return obj[keypath]; + }, + publish: function(obj, keypath, value) { + return obj[keypath] = value; + } + }; + + Rivets.factory = function(exports) { + exports._ = Rivets; + exports.binders = Rivets.binders; + exports.components = Rivets.components; + exports.formatters = Rivets.formatters; + exports.adapters = Rivets.adapters; + 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); diff --git a/ajax/libs/rivets/0.6.3/rivets.min.js b/ajax/libs/rivets/0.6.3/rivets.min.js new file mode 100644 index 000000000..6015d355c --- /dev/null +++ b/ajax/libs/rivets/0.6.3/rivets.min.js @@ -0,0 +1,5 @@ +// Rivets.js +// version: 0.6.3 +// author: Michael Richards +// license: MIT +(function(){var a,b,c=function(a,b){return function(){return a.apply(b,arguments)}},d=[].indexOf||function(a){for(var b=0,c=this.length;c>b;b++)if(b in this&&this[b]===a)return b;return-1},e=[].slice,f={}.hasOwnProperty,g=function(a,b){function c(){this.constructor=a}for(var d in b)f.call(b,d)&&(a[d]=b[d]);return c.prototype=b.prototype,a.prototype=new c,a.__super__=b.prototype,a};b={binders:{},components:{},formatters:{},adapters:{},config:{prefix:"rv",templateDelimiters:["{","}"],rootInterface:".",preloadData:!0,handler:function(a,b,c){return this.call(a,b,c.view.models)}}},b.Util={bindEvent:function(a,b,c){return null!=window.jQuery?(a=jQuery(a),null!=a.on?a.on(b,c):a.bind(b,c)):null!=window.addEventListener?a.addEventListener(b,c,!1):(b="on"+b,a.attachEvent(b,c))},unbindEvent:function(a,b,c){return null!=window.jQuery?(a=jQuery(a),null!=a.off?a.off(b,c):a.unbind(b,c)):null!=window.removeEventListener?a.removeEventListener(b,c,!1):(b="on"+b,a.detachEvent(b,c))},getInputValue:function(a){var b,c,d,e;if(null!=window.jQuery)switch(a=jQuery(a),a[0].type){case"checkbox":return a.is(":checked");default:return a.val()}else switch(a.type){case"checkbox":return a.checked;case"select-multiple":for(e=[],c=0,d=a.length;d>c;c++)b=a[c],b.selected&&e.push(b.value);return e;default:return a.value}}},b.View=function(){function a(a,d,e){var f,g,h,i,j,k,l,m,n;for(this.els=a,this.models=d,this.options=null!=e?e:{},this.update=c(this.update,this),this.publish=c(this.publish,this),this.sync=c(this.sync,this),this.unbind=c(this.unbind,this),this.bind=c(this.bind,this),this.select=c(this.select,this),this.build=c(this.build,this),this.componentRegExp=c(this.componentRegExp,this),this.bindingRegExp=c(this.bindingRegExp,this),this.els.jquery||this.els instanceof Array||(this.els=[this.els]),l=["config","binders","formatters","adapters"],j=0,k=l.length;k>j;j++){if(g=l[j],this[g]={},this.options[g]){m=this.options[g];for(f in m)h=m[f],this[g][f]=h}n=b[g];for(f in n)h=n[f],null==(i=this[g])[f]&&(i[f]=h)}this.build()}return a.prototype.bindingRegExp=function(){return new RegExp("^"+this.config.prefix+"-")},a.prototype.componentRegExp=function(){return new RegExp("^"+this.config.prefix.toUpperCase()+"-")},a.prototype.build=function(){var a,c,e,f,g,h,i,j,k,l=this;for(this.bindings=[],h=[],a=this.bindingRegExp(),e=this.componentRegExp(),c=function(a,c,d,e){var f,g,h,i,j,k,m;return j={},m=function(){var a,b,c,d;for(c=e.split("|"),d=[],a=0,b=c.length;b>a;a++)k=c[a],d.push(k.trim());return d}(),f=function(){var a,b,c,d;for(c=m.shift().split("<"),d=[],a=0,b=c.length;b>a;a++)g=c[a],d.push(g.trim());return d}(),i=f.shift(),j.formatters=m,(h=f.shift())&&(j.dependencies=h.split(/\s+/)),l.bindings.push(new b[a](l,c,d,i,j))},g=function(f){var i,j,k,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M;if(d.call(h,f)<0){if(3===f.nodeType){if(q=b.TextTemplateParser,(n=l.config.templateDelimiters)&&(u=q.parse(f.data,n)).length&&(1!==u.length||u[0].type!==q.types.text)){for(x=0,B=u.length;B>x;x++)t=u[x],s=document.createTextNode(t.value),f.parentNode.insertBefore(s,f),1===t.type&&c("TextBinding",s,null,t.value);f.parentNode.removeChild(f)}}else if(e.test(f.tagName))v=f.tagName.replace(e,"").toLowerCase(),l.bindings.push(new b.ComponentBinding(l,f,v));else if(null!=f.attributes){for(H=f.attributes,y=0,C=H.length;C>y;y++)if(i=H[y],a.test(i.name)){if(v=i.name.replace(a,""),!(k=l.binders[v])){I=l.binders;for(o in I)w=I[o],"*"!==o&&-1!==o.indexOf("*")&&(r=new RegExp("^"+o.replace("*",".+")+"$"),r.test(v)&&(k=w))}if(k||(k=l.binders["*"]),k.block){for(J=f.childNodes,z=0,D=J.length;D>z;z++)p=J[z],h.push(p);j=[i]}}for(K=j||f.attributes,A=0,E=K.length;E>A;A++)i=K[A],a.test(i.name)&&(v=i.name.replace(a,""),c("Binding",f,v,i.value))}for(L=function(){var a,b,c,d;for(c=f.childNodes,d=[],b=0,a=c.length;a>b;b++)p=c[b],d.push(p);return d}(),M=[],G=0,F=L.length;F>G;G++)m=L[G],M.push(g(m));return M}},k=this.els,i=0,j=k.length;j>i;i++)f=k[i],g(f)},a.prototype.select=function(a){var b,c,d,e,f;for(e=this.bindings,f=[],c=0,d=e.length;d>c;c++)b=e[c],a(b)&&f.push(b);return f},a.prototype.bind=function(){var a,b,c,d,e;for(d=this.bindings,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.bind());return e},a.prototype.unbind=function(){var a,b,c,d,e;for(d=this.bindings,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.unbind());return e},a.prototype.sync=function(){var a,b,c,d,e;for(d=this.bindings,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.sync());return e},a.prototype.publish=function(){var a,b,c,d,e;for(d=this.select(function(a){return a.binder.publishes}),e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.publish());return e},a.prototype.update=function(a){var b,c,d,e,f,g,h;null==a&&(a={});for(c in a)d=a[c],this.models[c]=d;for(g=this.bindings,h=[],e=0,f=g.length;f>e;e++)b=g[e],h.push(b.update(a));return h},a}(),b.Binding=function(){function d(a,b,d,e,f){this.view=a,this.el=b,this.type=d,this.keypath=e,this.options=null!=f?f:{},this.update=c(this.update,this),this.unbind=c(this.unbind,this),this.bind=c(this.bind,this),this.publish=c(this.publish,this),this.sync=c(this.sync,this),this.set=c(this.set,this),this.eventHandler=c(this.eventHandler,this),this.formattedValue=c(this.formattedValue,this),this.setObserver=c(this.setObserver,this),this.setBinder=c(this.setBinder,this),this.formatters=this.options.formatters||[],this.dependencies=[],this.setBinder(),this.setObserver()}return d.prototype.setBinder=function(){var a,b,c,d;if(!(this.binder=this.view.binders[this.type])){d=this.view.binders;for(a in d)c=d[a],"*"!==a&&-1!==a.indexOf("*")&&(b=new RegExp("^"+a.replace("*",".+")+"$"),b.test(this.type)&&(this.binder=c,this.args=new RegExp("^"+a.replace("*","(.+)")+"$").exec(this.type),this.args.shift()))}return this.binder||(this.binder=this.view.binders["*"]),this.binder instanceof Function?this.binder={routine:this.binder}:void 0},d.prototype.setObserver=function(){var b=this;return this.observer=new a(this.view,this.view.models,this.keypath,function(a){return b.key&&b.unbind(!0),b.model=a.target,b.key&&b.bind(!0),b.sync()}),this.key=this.observer.key,this.model=this.observer.target},d.prototype.formattedValue=function(a){var b,c,d,f,g,h;for(h=this.formatters,f=0,g=h.length;g>f;f++)c=h[f],b=c.split(/\s+/),d=b.shift(),c=this.view.formatters[d],(null!=c?c.read:void 0)instanceof Function?a=c.read.apply(c,[a].concat(e.call(b))):c instanceof Function&&(a=c.apply(null,[a].concat(e.call(b))));return a},d.prototype.eventHandler=function(a){var b,c;return c=(b=this).view.config.handler,function(d){return c.call(a,this,d,b)}},d.prototype.set=function(a){var b;return a=a instanceof Function&&!this.binder["function"]?this.formattedValue(a.call(this.model)):this.formattedValue(a),null!=(b=this.binder.routine)?b.call(this,this.el,a):void 0},d.prototype.sync=function(){return this.set(this.key?this.view.adapters[this.key["interface"]].read(this.model,this.key.path):this.model)},d.prototype.publish=function(){var a,c,d,f,g,h,i,j,k;for(f=b.Util.getInputValue(this.el),i=this.formatters.slice(0).reverse(),g=0,h=i.length;h>g;g++)c=i[g],a=c.split(/\s+/),d=a.shift(),(null!=(j=this.view.formatters[d])?j.publish:void 0)&&(f=(k=this.view.formatters[d]).publish.apply(k,[f].concat(e.call(a))));return this.view.adapters[this.key["interface"]].publish(this.model,this.key.path,f)},d.prototype.bind=function(b){var c,d,e,f,g,h,i,j,k,l=this;if(null==b&&(b=!1),b||null!=(h=this.binder.bind)&&h.call(this,this.el),this.key&&this.view.adapters[this.key["interface"]].subscribe(this.model,this.key.path,this.sync),(b?void 0:this.view.config.preloadData)&&this.sync(),null!=(i=this.options.dependencies)?i.length:void 0){for(j=this.options.dependencies,k=[],f=0,g=j.length;g>f;f++)c=j[f],e=new a(this.view,this.model,c,function(a,b){var c;return c=a.key,l.view.adapters[c["interface"]].unsubscribe(b,c.path,l.sync),l.view.adapters[c["interface"]].subscribe(a.target,c.path,l.sync),l.sync()}),d=e.key,this.view.adapters[d["interface"]].subscribe(e.target,d.path,this.sync),k.push(this.dependencies.push(e));return k}},d.prototype.unbind=function(a){var b,c,d,e,f,g;if(null==a&&(a=!1),a||null!=(f=this.binder.unbind)&&f.call(this,this.el),this.key&&this.view.adapters[this.key["interface"]].unsubscribe(this.model,this.key.path,this.sync),this.dependencies.length){for(g=this.dependencies,d=0,e=g.length;e>d;d++)c=g[d],b=c.key,this.view.adapters[b["interface"]].unsubscribe(c.target,b.path,this.sync);return this.dependencies=[]}},d.prototype.update=function(a){var b;return null==a&&(a={}),null!=(b=this.binder.update)?b.call(this,a):void 0},d}(),b.ComponentBinding=function(a){function e(a,e,f){var g,h,i,j,k;for(this.view=a,this.el=e,this.type=f,this.unbind=c(this.unbind,this),this.bind=c(this.bind,this),this.update=c(this.update,this),this.locals=c(this.locals,this),this.component=b.components[this.type],this.attributes={},this.inflections={},j=this.el.attributes||[],h=0,i=j.length;i>h;h++)g=j[h],k=g.name,d.call(this.component.attributes,k)>=0?this.attributes[g.name]=g.value:this.inflections[g.name]=g.value}return g(e,a),e.prototype.sync=function(){},e.prototype.locals=function(a){var b,c,d,e,f,g,h,i,j;null==a&&(a=this.view.models),f={},i=this.inflections;for(c in i)for(b=i[c],j=b.split("."),g=0,h=j.length;h>g;g++)e=j[g],f[c]=(f[c]||a)[e];for(c in a)d=a[c],null==f[c]&&(f[c]=d);return f},e.prototype.update=function(a){var b;return null!=(b=this.componentView)?b.update(this.locals(a)):void 0},e.prototype.bind=function(){var a,c;return null!=this.componentView?null!=(c=this.componentView)?c.bind():void 0:(a=this.component.build.call(this.attributes),(this.componentView=new b.View(a,this.locals(),this.view.options)).bind(),this.el.parentNode.replaceChild(a,this.el))},e.prototype.unbind=function(){var a;return null!=(a=this.componentView)?a.unbind():void 0},e}(b.Binding),b.TextBinding=function(a){function b(a,b,d,e,f){this.view=a,this.el=b,this.type=d,this.keypath=e,this.options=null!=f?f:{},this.sync=c(this.sync,this),this.formatters=this.options.formatters||[],this.dependencies=[],this.setObserver()}return g(b,a),b.prototype.binder={routine:function(a,b){return a.data=null!=b?b:""}},b.prototype.sync=function(){return b.__super__.sync.apply(this,arguments)},b}(b.Binding),b.KeypathParser=function(){function a(){}return a.parse=function(a,b,c){var e,f,g,h;h=[],f={"interface":c,path:""};for(g in a)e=a[g],d.call(b,e)>=0?(h.push(f),f={"interface":e,path:""}):f.path+=e;return h.push(f),h},a}(),b.TextTemplateParser=function(){function a(){}return a.types={text:0,binding:1},a.parse=function(a,b){var c,d,e,f,g,h,i;for(h=[],f=a.length,c=0,d=0;f>d;){if(c=a.indexOf(b[0],d),0>c){h.push({type:this.types.text,value:a.slice(d)});break}if(c>0&&c>d&&h.push({type:this.types.text,value:a.slice(d,c)}),d=c+b[0].length,c=a.indexOf(b[1],d),0>c){g=a.slice(d-b[1].length),e=h[h.length-1],(null!=e?e.type:void 0)===this.types.text?e.value+=g:h.push({type:this.types.text,value:g});break}i=a.slice(d,c).trim(),h.push({type:this.types.binding,value:i}),d=c+b[1].length}return h},a}(),a=function(){function a(a,b,d,e){this.view=a,this.model=b,this.keypath=d,this.callback=e,this.realize=c(this.realize,this),this.update=c(this.update,this),this.parse=c(this.parse,this),this.parse(),this.objectPath=[],this.target=this.realize()}return a.prototype.parse=function(){var a,c,e,f,g,h;return a=function(){var a,b;a=this.view.adapters,b=[];for(c in a)g=a[c],b.push(c);return b}.call(this),h=this.keypath[0],d.call(a,h)>=0?(f=this.keypath[0],e=this.keypath.substr(1)):(f=this.view.config.rootInterface,e=this.keypath),this.tokens=b.KeypathParser.parse(e,a,f),this.key=this.tokens.pop()},a.prototype.update=function(){var a,b;return(a=this.realize())!==this.target?(b=this.target,this.target=a,this.callback(this,b)):void 0},a.prototype.realize=function(){var a,b,c,d,e,f,g;for(a=this.model,g=this.tokens,b=e=0,f=g.length;f>e;b=++e)d=g[b],null!=this.objectPath[b]?a!==(c=this.objectPath[b])&&(this.view.adapters[d["interface"]].unsubscribe(c,d.path,this.update),this.view.adapters[d["interface"]].subscribe(a,d.path,this.update),this.objectPath[b]=a):(this.view.adapters[d["interface"]].subscribe(a,d.path,this.update),this.objectPath[b]=a),a=this.view.adapters[d["interface"]].read(a,d.path);return a},a}(),b.binders.enabled=function(a,b){return a.disabled=!b},b.binders.disabled=function(a,b){return a.disabled=!!b},b.binders.checked={publishes:!0,bind:function(a){return b.Util.bindEvent(a,"change",this.publish)},unbind:function(a){return b.Util.unbindEvent(a,"change",this.publish)},routine:function(a,b){var c;return a.checked="radio"===a.type?(null!=(c=a.value)?c.toString():void 0)===(null!=b?b.toString():void 0):!!b}},b.binders.unchecked={publishes:!0,bind:function(a){return b.Util.bindEvent(a,"change",this.publish)},unbind:function(a){return b.Util.unbindEvent(a,"change",this.publish)},routine:function(a,b){var c;return a.checked="radio"===a.type?(null!=(c=a.value)?c.toString():void 0)!==(null!=b?b.toString():void 0):!b}},b.binders.show=function(a,b){return a.style.display=b?"":"none"},b.binders.hide=function(a,b){return a.style.display=b?"none":""},b.binders.html=function(a,b){return a.innerHTML=null!=b?b:""},b.binders.value={publishes:!0,bind:function(a){return b.Util.bindEvent(a,"change",this.publish)},unbind:function(a){return b.Util.unbindEvent(a,"change",this.publish)},routine:function(a,b){var c,e,f,g,h,i,j;if(null!=window.jQuery){if(a=jQuery(a),(null!=b?b.toString():void 0)!==(null!=(g=a.val())?g.toString():void 0))return a.val(null!=b?b:"")}else if("select-multiple"===a.type){if(null!=b){for(j=[],e=0,f=a.length;f>e;e++)c=a[e],j.push(c.selected=(h=c.value,d.call(b,h)>=0));return j}}else if((null!=b?b.toString():void 0)!==(null!=(i=a.value)?i.toString():void 0))return a.value=null!=b?b:""}},b.binders.text=function(a,b){return null!=a.textContent?a.textContent=null!=b?b:"":a.innerText=null!=b?b:""},b.binders["if"]={block:!0,bind:function(a){var b,c;return null==this.marker?(b=[this.view.config.prefix,this.type].join("-").replace("--","-"),c=a.getAttribute(b),this.marker=document.createComment(" rivets: "+this.type+" "+c+" "),a.removeAttribute(b),a.parentNode.insertBefore(this.marker,a),a.parentNode.removeChild(a)):void 0},unbind:function(){var a;return null!=(a=this.nested)?a.unbind():void 0},routine:function(a,c){var d,e,f,g,h;if(!!c==(null==this.nested)){if(c){f={},h=this.view.models;for(d in h)e=h[d],f[d]=e;return g={binders:this.view.options.binders,formatters:this.view.options.formatters,adapters:this.view.options.adapters,config:this.view.options.config},(this.nested=new b.View(a,f,g)).bind(),this.marker.parentNode.insertBefore(a,this.marker.nextSibling)}return a.parentNode.removeChild(a),this.nested.unbind(),delete this.nested}},update:function(a){var b;return null!=(b=this.nested)?b.update(a):void 0}},b.binders.unless={block:!0,bind:function(a){return b.binders["if"].bind.call(this,a)},unbind:function(){return b.binders["if"].unbind.call(this)},routine:function(a,c){return b.binders["if"].routine.call(this,a,!c)},update:function(a){return b.binders["if"].update.call(this,a)}},b.binders["on-*"]={"function":!0,unbind:function(a){return this.handler?b.Util.unbindEvent(a,this.args[0],this.handler):void 0},routine:function(a,c){return this.handler&&b.Util.unbindEvent(a,this.args[0],this.handler),b.Util.bindEvent(a,this.args[0],this.handler=this.eventHandler(c))}},b.binders["each-*"]={block:!0,bind:function(a){var b;return null==this.marker?(b=[this.view.config.prefix,this.type].join("-").replace("--","-"),this.marker=document.createComment(" rivets: "+this.type+" "),this.iterated=[],a.removeAttribute(b),a.parentNode.insertBefore(this.marker,a),a.parentNode.removeChild(a)):void 0},unbind:function(){var a,b,c,d,e;if(null!=this.iterated){for(d=this.iterated,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.unbind());return e}},routine:function(a,c){var d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A;if(k=this.args[0],c=c||[],this.iterated.length>c.length)for(w=Array(this.iterated.length-c.length),q=0,t=w.length;t>q;q++)f=w[q],p=this.iterated.pop(),p.unbind(),this.marker.parentNode.removeChild(p.els[0]);for(g=r=0,u=c.length;u>r;g=++r)if(j=c[g],e={},e[k]=j,null==this.iterated[g]){x=this.view.models;for(i in x)j=x[i],null==e[i]&&(e[i]=j);m=this.iterated.length?this.iterated[this.iterated.length-1].els[0]:this.marker,l={binders:this.view.options.binders,formatters:this.view.options.formatters,adapters:this.view.options.adapters,config:{}},y=this.view.options.config;for(h in y)o=y[h],l.config[h]=o;l.config.preloadData=!0,n=a.cloneNode(!0),p=new b.View(n,e,l),p.bind(),this.iterated.push(p),this.marker.parentNode.insertBefore(n,m.nextSibling)}else this.iterated[g].models[k]!==j&&this.iterated[g].update(e);if("OPTION"===a.nodeName){for(z=this.view.bindings,A=[],s=0,v=z.length;v>s;s++)d=z[s],d.el===this.marker.parentNode&&"value"===d.type?A.push(d.sync()):A.push(void 0);return A}},update:function(a){var b,c,d,e,f,g,h,i;b={};for(c in a)d=a[c],c!==this.args[0]&&(b[c]=d);for(h=this.iterated,i=[],f=0,g=h.length;g>f;f++)e=h[f],i.push(e.update(b));return i}},b.binders["class-*"]=function(a,b){var c;return c=" "+a.className+" ",!b==(-1!==c.indexOf(" "+this.args[0]+" "))?a.className=b?""+a.className+" "+this.args[0]:c.replace(" "+this.args[0]+" "," ").trim():void 0},b.binders["*"]=function(a,b){return b?a.setAttribute(this.type,b):a.removeAttribute(this.type)},b.adapters["."]={id:"_rv",counter:0,weakmap:{},weakReference:function(a){var b;return null==a[this.id]&&(b=this.counter++,this.weakmap[b]={callbacks:{}},Object.defineProperty(a,this.id,{value:b})),this.weakmap[a[this.id]]},stubFunction:function(a,b){var c,d,e;return d=a[b],c=this.weakReference(a),e=this.weakmap,a[b]=function(){var b,f,g,h,i,j,k,l,m,n;h=d.apply(a,arguments),k=c.pointers;for(g in k)for(f=k[g],n=null!=(l=null!=(m=e[g])?m.callbacks[f]:void 0)?l:[],i=0,j=n.length;j>i;i++)b=n[i],b();return h}},observeMutations:function(a,b,c){var e,f,g,h,i,j;if(Array.isArray(a)){if(g=this.weakReference(a),null==g.pointers)for(g.pointers={},f=["push","pop","shift","unshift","sort","reverse","splice"],i=0,j=f.length;j>i;i++)e=f[i],this.stubFunction(a,e);if(null==(h=g.pointers)[b]&&(h[b]=[]),d.call(g.pointers[b],c)<0)return g.pointers[b].push(c)}},unobserveMutations:function(a,b,c){var d,e;return Array.isArray(a&&null!=a[this.id])&&(d=null!=(e=this.weakReference(a).pointers)?e[b]:void 0)?d.splice(d.indexOf(c),1):void 0},subscribe:function(a,b,c){var e,f,g=this;return e=this.weakReference(a).callbacks,null==e[b]&&(e[b]=[],f=a[b],Object.defineProperty(a,b,{get:function(){return f},set:function(d){var h,i,j;if(d!==f){for(f=d,j=e[b],h=0,i=j.length;i>h;h++)c=j[h],c();return g.observeMutations(d,a[g.id],b)}}})),d.call(e[b],c)<0&&e[b].push(c),this.observeMutations(a[b],a[this.id],b)},unsubscribe:function(a,b,c){var d;return d=this.weakmap[a[this.id]].callbacks[b],d.splice(d.indexOf(c),1),this.unobserveMutations(a[b],a[this.id],b)},read:function(a,b){return a[b]},publish:function(a,b,c){return a[b]=c}},b.factory=function(a){return a._=b,a.binders=b.binders,a.components=b.components,a.formatters=b.formatters,a.adapters=b.adapters,a.config=b.config,a.configure=function(a){var c,d;null==a&&(a={});for(c in a)d=a[c],b.config[c]=d},a.bind=function(a,c,d){var e;return null==c&&(c={}),null==d&&(d={}),e=new b.View(a,c,d),e.bind(),e}},"object"==typeof exports?b.factory(exports):"function"==typeof define&&define.amd?define(["exports"],function(a){return b.factory(this.rivets=a),a}):b.factory(this.rivets={})}).call(this); \ No newline at end of file diff --git a/ajax/libs/rivets/0.6.4/rivets.js b/ajax/libs/rivets/0.6.4/rivets.js new file mode 100644 index 000000000..9e842f54c --- /dev/null +++ b/ajax/libs/rivets/0.6.4/rivets.js @@ -0,0 +1,1259 @@ +// Rivets.js +// version: 0.6.4 +// author: Michael Richards +// license: MIT +(function() { + var Rivets, + __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, + __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; }, + __slice = [].slice, + __hasProp = {}.hasOwnProperty, + __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; + + Rivets = { + binders: {}, + components: {}, + formatters: {}, + adapters: {}, + config: { + prefix: 'rv', + templateDelimiters: ['{', '}'], + rootInterface: '.', + preloadData: true, + handler: function(context, ev, binding) { + return this.call(context, ev, binding.view.models); + } + } + }; + + 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.View = (function() { + function View(els, models, options) { + var k, option, v, _base, _i, _len, _ref, _ref1, _ref2; + 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.componentRegExp = __bind(this.componentRegExp, this); + this.bindingRegExp = __bind(this.bindingRegExp, this); + if (!(this.els.jquery || this.els instanceof Array)) { + this.els = [this.els]; + } + _ref = ['config', 'binders', 'formatters', 'adapters']; + 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 ((_base = this[option])[k] == null) { + _base[k] = v; + } + } + } + this.build(); + } + + View.prototype.bindingRegExp = function() { + return new RegExp("^" + this.config.prefix + "-"); + }; + + View.prototype.componentRegExp = function() { + return new RegExp("^" + (this.config.prefix.toUpperCase()) + "-"); + }; + + View.prototype.build = function() { + var bindingRegExp, buildBinding, componentRegExp, el, parse, skipNodes, _i, _len, _ref, + _this = this; + this.bindings = []; + skipNodes = []; + bindingRegExp = this.bindingRegExp(); + componentRegExp = this.componentRegExp(); + buildBinding = function(binding, node, type, declaration) { + var context, ctx, dependencies, keypath, options, pipe, pipes; + 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; + })(); + keypath = context.shift(); + options.formatters = pipes; + if (dependencies = context.shift()) { + options.dependencies = dependencies.split(/\s+/); + } + return _this.bindings.push(new Rivets[binding](_this, node, type, keypath, options)); + }; + parse = function(node) { + var attribute, attributes, binder, childNode, delimiters, identifier, n, parser, regexp, 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 === 3) { + 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)) { + for (_i = 0, _len = tokens.length; _i < _len; _i++) { + token = tokens[_i]; + text = document.createTextNode(token.value); + node.parentNode.insertBefore(text, node); + if (token.type === 1) { + buildBinding('TextBinding', text, null, token.value); + } + } + node.parentNode.removeChild(node); + } + } + } + } else if (componentRegExp.test(node.tagName)) { + type = node.tagName.replace(componentRegExp, '').toLowerCase(); + _this.bindings.push(new Rivets.ComponentBinding(_this, node, type)); + } 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('Binding', node, type, attribute.value); + } + } + } + _ref4 = (function() { + var _len4, _n, _ref4, _results1; + _ref4 = node.childNodes; + _results1 = []; + for (_n = 0, _len4 = _ref4.length; _n < _len4; _n++) { + n = _ref4[_n]; + _results1.push(n); + } + return _results1; + })(); + _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.Binding = (function() { + function Binding(view, el, type, keypath, options) { + this.view = view; + this.el = el; + this.type = type; + 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); + this.setObserver = __bind(this.setObserver, this); + this.setBinder = __bind(this.setBinder, this); + this.formatters = this.options.formatters || []; + this.dependencies = []; + this.setBinder(); + this.setObserver(); + } + + Binding.prototype.setBinder = function() { + var identifier, regexp, value, _ref; + if (!(this.binder = this.view.binders[this.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(this.type)) { + this.binder = value; + this.args = new RegExp("^" + (identifier.replace('*', '(.+)')) + "$").exec(this.type); + this.args.shift(); + } + } + } + } + this.binder || (this.binder = this.view.binders['*']); + if (this.binder instanceof Function) { + return this.binder = { + routine: this.binder + }; + } + }; + + Binding.prototype.setObserver = function() { + var _this = this; + this.observer = new Rivets.KeypathObserver(this.view, this.view.models, this.keypath, function(obs) { + if (_this.key) { + _this.unbind(true); + } + _this.model = obs.target; + if (_this.key) { + _this.bind(true); + } + return _this.sync(); + }); + this.key = this.observer.key; + return this.model = this.observer.target; + }; + + 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.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.key ? this.view.adapters[this.key["interface"]].read(this.model, this.key.path) : this.model); + }; + + 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.adapters[this.key["interface"]].publish(this.model, this.key.path, value); + }; + + Binding.prototype.bind = function(silent) { + var dependency, key, observer, _i, _len, _ref, _ref1, _ref2, _results, + _this = this; + if (silent == null) { + silent = false; + } + if (!silent) { + if ((_ref = this.binder.bind) != null) { + _ref.call(this, this.el); + } + } + if (this.key) { + this.view.adapters[this.key["interface"]].subscribe(this.model, this.key.path, this.sync); + } + if (!silent ? this.view.config.preloadData : void 0) { + 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]; + observer = new Rivets.KeypathObserver(this.view, this.model, dependency, function(obs, prev) { + var key; + key = obs.key; + _this.view.adapters[key["interface"]].unsubscribe(prev, key.path, _this.sync); + _this.view.adapters[key["interface"]].subscribe(obs.target, key.path, _this.sync); + return _this.sync(); + }); + key = observer.key; + this.view.adapters[key["interface"]].subscribe(observer.target, key.path, this.sync); + _results.push(this.dependencies.push(observer)); + } + return _results; + } + }; + + Binding.prototype.unbind = function(silent) { + var key, obs, _i, _len, _ref, _ref1; + if (silent == null) { + silent = false; + } + if (!silent) { + if ((_ref = this.binder.unbind) != null) { + _ref.call(this, this.el); + } + this.observer.unobserve(); + } + if (this.key) { + this.view.adapters[this.key["interface"]].unsubscribe(this.model, this.key.path, this.sync); + } + if (this.dependencies.length) { + _ref1 = this.dependencies; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + obs = _ref1[_i]; + key = obs.key; + this.view.adapters[key["interface"]].unsubscribe(obs.target, key.path, this.sync); + } + return this.dependencies = []; + } + }; + + Binding.prototype.update = function(models) { + var _ref; + if (models == null) { + models = {}; + } + return (_ref = this.binder.update) != null ? _ref.call(this, models) : void 0; + }; + + return Binding; + + })(); + + Rivets.ComponentBinding = (function(_super) { + __extends(ComponentBinding, _super); + + function ComponentBinding(view, el, type) { + var attribute, _i, _len, _ref, _ref1; + this.view = view; + this.el = el; + this.type = type; + this.unbind = __bind(this.unbind, this); + this.bind = __bind(this.bind, this); + this.update = __bind(this.update, this); + this.locals = __bind(this.locals, this); + this.component = Rivets.components[this.type]; + this.attributes = {}; + this.inflections = {}; + _ref = this.el.attributes || []; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + attribute = _ref[_i]; + if (_ref1 = attribute.name, __indexOf.call(this.component.attributes, _ref1) >= 0) { + this.attributes[attribute.name] = attribute.value; + } else { + this.inflections[attribute.name] = attribute.value; + } + } + } + + ComponentBinding.prototype.sync = function() {}; + + ComponentBinding.prototype.locals = function(models) { + var inverse, key, model, path, result, _i, _len, _ref, _ref1; + if (models == null) { + models = this.view.models; + } + result = {}; + _ref = this.inflections; + for (key in _ref) { + inverse = _ref[key]; + _ref1 = inverse.split('.'); + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + path = _ref1[_i]; + result[key] = (result[key] || models)[path]; + } + } + for (key in models) { + model = models[key]; + if (result[key] == null) { + result[key] = model; + } + } + return result; + }; + + ComponentBinding.prototype.update = function(models) { + var _ref; + return (_ref = this.componentView) != null ? _ref.update(this.locals(models)) : void 0; + }; + + ComponentBinding.prototype.bind = function() { + var el, _ref; + if (this.componentView != null) { + return (_ref = this.componentView) != null ? _ref.bind() : void 0; + } else { + el = this.component.build.call(this.attributes); + (this.componentView = new Rivets.View(el, this.locals(), this.view.options)).bind(); + return this.el.parentNode.replaceChild(el, this.el); + } + }; + + ComponentBinding.prototype.unbind = function() { + var _ref; + return (_ref = this.componentView) != null ? _ref.unbind() : void 0; + }; + + return ComponentBinding; + + })(Rivets.Binding); + + Rivets.TextBinding = (function(_super) { + __extends(TextBinding, _super); + + function TextBinding(view, el, type, keypath, options) { + this.view = view; + this.el = el; + this.type = type; + this.keypath = keypath; + this.options = options != null ? options : {}; + this.sync = __bind(this.sync, this); + this.formatters = this.options.formatters || []; + this.dependencies = []; + this.setObserver(); + } + + TextBinding.prototype.binder = { + routine: function(node, value) { + return node.data = value != null ? value : ''; + } + }; + + TextBinding.prototype.sync = function() { + return TextBinding.__super__.sync.apply(this, arguments); + }; + + return TextBinding; + + })(Rivets.Binding); + + Rivets.KeypathParser = (function() { + function KeypathParser() {} + + KeypathParser.parse = function(keypath, interfaces, root) { + var char, current, index, tokens; + tokens = []; + current = { + "interface": root, + path: '' + }; + for (index in keypath) { + char = keypath[index]; + if (__indexOf.call(interfaces, char) >= 0) { + tokens.push(current); + current = { + "interface": char, + path: '' + }; + } else { + current.path += char; + } + } + tokens.push(current); + return tokens; + }; + + return KeypathParser; + + })(); + + 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 + delimiters[0].length; + index = template.indexOf(delimiters[1], lastIndex); + if (index < 0) { + substring = template.slice(lastIndex - delimiters[1].length); + 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 + delimiters[1].length; + } + } + return tokens; + }; + + return TextTemplateParser; + + })(); + + Rivets.KeypathObserver = (function() { + function KeypathObserver(view, model, keypath, callback) { + this.view = view; + this.model = model; + this.keypath = keypath; + this.callback = callback; + this.unobserve = __bind(this.unobserve, this); + this.realize = __bind(this.realize, this); + this.update = __bind(this.update, this); + this.parse = __bind(this.parse, this); + this.parse(); + this.objectPath = []; + this.target = this.realize(); + } + + KeypathObserver.prototype.parse = function() { + var interfaces, k, path, root, v, _ref; + interfaces = (function() { + var _ref, _results; + _ref = this.view.adapters; + _results = []; + for (k in _ref) { + v = _ref[k]; + _results.push(k); + } + return _results; + }).call(this); + if (_ref = this.keypath[0], __indexOf.call(interfaces, _ref) >= 0) { + root = this.keypath[0]; + path = this.keypath.substr(1); + } else { + root = this.view.config.rootInterface; + path = this.keypath; + } + this.tokens = Rivets.KeypathParser.parse(path, interfaces, root); + return this.key = this.tokens.pop(); + }; + + KeypathObserver.prototype.update = function() { + var next, prev; + if ((next = this.realize()) !== this.target) { + prev = this.target; + this.target = next; + return this.callback(this, prev); + } + }; + + KeypathObserver.prototype.realize = function() { + var current, index, prev, token, _i, _len, _ref; + current = this.model; + _ref = this.tokens; + for (index = _i = 0, _len = _ref.length; _i < _len; index = ++_i) { + token = _ref[index]; + if (this.objectPath[index] != null) { + if (current !== (prev = this.objectPath[index])) { + this.view.adapters[token["interface"]].unsubscribe(prev, token.path, this.update); + this.view.adapters[token["interface"]].subscribe(current, token.path, this.update); + this.objectPath[index] = current; + } + } else { + this.view.adapters[token["interface"]].subscribe(current, token.path, this.update); + this.objectPath[index] = current; + } + current = this.view.adapters[token["interface"]].read(current, token.path); + } + return current; + }; + + KeypathObserver.prototype.unobserve = function() { + var index, obj, token, _i, _len, _ref, _results; + _ref = this.tokens; + _results = []; + for (index = _i = 0, _len = _ref.length; _i < _len; index = ++_i) { + token = _ref[index]; + if (obj = this.objectPath[index]) { + _results.push(this.view.adapters[token["interface"]].unsubscribe(obj, token.path, this.update)); + } else { + _results.push(void 0); + } + } + return _results; + }; + + return KeypathObserver; + + })(); + + Rivets.binders.text = function(el, value) { + if (el.textContent != null) { + return el.textContent = value != null ? value : ''; + } else { + return el.innerText = value != null ? value : ''; + } + }; + + Rivets.binders.html = function(el, value) { + return el.innerHTML = value != null ? value : ''; + }; + + Rivets.binders.show = function(el, value) { + return el.style.display = value ? '' : 'none'; + }; + + Rivets.binders.hide = function(el, value) { + return el.style.display = value ? 'none' : ''; + }; + + Rivets.binders.enabled = function(el, value) { + return el.disabled = !value; + }; + + Rivets.binders.disabled = function(el, value) { + return el.disabled = !!value; + }; + + Rivets.binders.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; + } + } + }; + + Rivets.binders.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; + } + } + }; + + Rivets.binders.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 : ''; + } + } + } + }; + + Rivets.binders["if"] = { + block: true, + bind: function(el) { + var attr, declaration; + if (this.marker == null) { + attr = [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, + adapters: this.view.options.adapters, + 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) { + var _ref; + return (_ref = this.nested) != null ? _ref.update(models) : void 0; + } + }; + + Rivets.binders.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); + } + }; + + Rivets.binders['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)); + } + }; + + Rivets.binders['each-*'] = { + block: true, + bind: function(el) { + var attr; + if (this.marker == null) { + attr = [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 binding, data, i, index, k, key, model, modelName, options, previous, template, v, view, _i, _j, _k, _len, _len1, _len2, _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]); + } + } + 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 (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, + adapters: this.view.options.adapters, + 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); + this.marker.parentNode.insertBefore(template, previous.nextSibling); + } else if (this.iterated[index].models[modelName] !== model) { + this.iterated[index].update(data); + } + } + if (el.nodeName === 'OPTION') { + _ref3 = this.view.bindings; + _results = []; + for (_k = 0, _len2 = _ref3.length; _k < _len2; _k++) { + binding = _ref3[_k]; + if (binding.el === this.marker.parentNode && binding.type === 'value') { + _results.push(binding.sync()); + } 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; + } + }; + + Rivets.binders['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(); + } + }; + + Rivets.binders['*'] = function(el, value) { + if (value) { + return el.setAttribute(this.type, value); + } else { + return el.removeAttribute(this.type); + } + }; + + Rivets.adapters['.'] = { + id: '_rv', + counter: 0, + weakmap: {}, + weakReference: function(obj) { + var id; + if (obj[this.id] == null) { + id = this.counter++; + this.weakmap[id] = { + callbacks: {} + }; + Object.defineProperty(obj, this.id, { + value: id + }); + } + return this.weakmap[obj[this.id]]; + }, + stubFunction: function(obj, fn) { + var map, original, weakmap; + original = obj[fn]; + map = this.weakReference(obj); + weakmap = this.weakmap; + return obj[fn] = function() { + var callback, k, r, response, _i, _len, _ref, _ref1, _ref2, _ref3; + response = original.apply(obj, arguments); + _ref = map.pointers; + for (r in _ref) { + k = _ref[r]; + _ref3 = (_ref1 = (_ref2 = weakmap[r]) != null ? _ref2.callbacks[k] : void 0) != null ? _ref1 : []; + for (_i = 0, _len = _ref3.length; _i < _len; _i++) { + callback = _ref3[_i]; + callback(); + } + } + return response; + }; + }, + observeMutations: function(obj, ref, keypath) { + var fn, functions, map, _base, _i, _len; + if (Array.isArray(obj)) { + map = this.weakReference(obj); + if (map.pointers == null) { + map.pointers = {}; + functions = ['push', 'pop', 'shift', 'unshift', 'sort', 'reverse', 'splice']; + for (_i = 0, _len = functions.length; _i < _len; _i++) { + fn = functions[_i]; + this.stubFunction(obj, fn); + } + } + if ((_base = map.pointers)[ref] == null) { + _base[ref] = []; + } + if (__indexOf.call(map.pointers[ref], keypath) < 0) { + return map.pointers[ref].push(keypath); + } + } + }, + unobserveMutations: function(obj, ref, keypath) { + var keypaths, _ref; + if (Array.isArray(obj && (obj[this.id] != null))) { + if (keypaths = (_ref = this.weakReference(obj).pointers) != null ? _ref[ref] : void 0) { + return keypaths.splice(keypaths.indexOf(keypath), 1); + } + } + }, + subscribe: function(obj, keypath, callback) { + var callbacks, value, + _this = this; + callbacks = this.weakReference(obj).callbacks; + if (callbacks[keypath] == null) { + callbacks[keypath] = []; + value = obj[keypath]; + Object.defineProperty(obj, keypath, { + get: function() { + return value; + }, + set: function(newValue) { + var _i, _len, _ref; + if (newValue !== value) { + value = newValue; + _ref = callbacks[keypath]; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + callback = _ref[_i]; + callback(); + } + return _this.observeMutations(newValue, obj[_this.id], keypath); + } + } + }); + } + if (__indexOf.call(callbacks[keypath], callback) < 0) { + callbacks[keypath].push(callback); + } + return this.observeMutations(obj[keypath], obj[this.id], keypath); + }, + unsubscribe: function(obj, keypath, callback) { + var callbacks; + callbacks = this.weakmap[obj[this.id]].callbacks[keypath]; + callbacks.splice(callbacks.indexOf(callback), 1); + return this.unobserveMutations(obj[keypath], obj[this.id], keypath); + }, + read: function(obj, keypath) { + return obj[keypath]; + }, + publish: function(obj, keypath, value) { + return obj[keypath] = value; + } + }; + + Rivets.factory = function(exports) { + exports._ = Rivets; + exports.binders = Rivets.binders; + exports.components = Rivets.components; + exports.formatters = Rivets.formatters; + exports.adapters = Rivets.adapters; + 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); diff --git a/ajax/libs/rivets/0.6.4/rivets.min.js b/ajax/libs/rivets/0.6.4/rivets.min.js new file mode 100644 index 000000000..2c3b78f39 --- /dev/null +++ b/ajax/libs/rivets/0.6.4/rivets.min.js @@ -0,0 +1,5 @@ +// Rivets.js +// version: 0.6.4 +// author: Michael Richards +// license: MIT +(function(){var a,b=function(a,b){return function(){return a.apply(b,arguments)}},c=[].indexOf||function(a){for(var b=0,c=this.length;c>b;b++)if(b in this&&this[b]===a)return b;return-1},d=[].slice,e={}.hasOwnProperty,f=function(a,b){function c(){this.constructor=a}for(var d in b)e.call(b,d)&&(a[d]=b[d]);return c.prototype=b.prototype,a.prototype=new c,a.__super__=b.prototype,a};a={binders:{},components:{},formatters:{},adapters:{},config:{prefix:"rv",templateDelimiters:["{","}"],rootInterface:".",preloadData:!0,handler:function(a,b,c){return this.call(a,b,c.view.models)}}},a.Util={bindEvent:function(a,b,c){return null!=window.jQuery?(a=jQuery(a),null!=a.on?a.on(b,c):a.bind(b,c)):null!=window.addEventListener?a.addEventListener(b,c,!1):(b="on"+b,a.attachEvent(b,c))},unbindEvent:function(a,b,c){return null!=window.jQuery?(a=jQuery(a),null!=a.off?a.off(b,c):a.unbind(b,c)):null!=window.removeEventListener?a.removeEventListener(b,c,!1):(b="on"+b,a.detachEvent(b,c))},getInputValue:function(a){var b,c,d,e;if(null!=window.jQuery)switch(a=jQuery(a),a[0].type){case"checkbox":return a.is(":checked");default:return a.val()}else switch(a.type){case"checkbox":return a.checked;case"select-multiple":for(e=[],c=0,d=a.length;d>c;c++)b=a[c],b.selected&&e.push(b.value);return e;default:return a.value}}},a.View=function(){function d(c,d,e){var f,g,h,i,j,k,l,m,n;for(this.els=c,this.models=d,this.options=null!=e?e:{},this.update=b(this.update,this),this.publish=b(this.publish,this),this.sync=b(this.sync,this),this.unbind=b(this.unbind,this),this.bind=b(this.bind,this),this.select=b(this.select,this),this.build=b(this.build,this),this.componentRegExp=b(this.componentRegExp,this),this.bindingRegExp=b(this.bindingRegExp,this),this.els.jquery||this.els instanceof Array||(this.els=[this.els]),l=["config","binders","formatters","adapters"],j=0,k=l.length;k>j;j++){if(g=l[j],this[g]={},this.options[g]){m=this.options[g];for(f in m)h=m[f],this[g][f]=h}n=a[g];for(f in n)h=n[f],null==(i=this[g])[f]&&(i[f]=h)}this.build()}return d.prototype.bindingRegExp=function(){return new RegExp("^"+this.config.prefix+"-")},d.prototype.componentRegExp=function(){return new RegExp("^"+this.config.prefix.toUpperCase()+"-")},d.prototype.build=function(){var b,d,e,f,g,h,i,j,k,l=this;for(this.bindings=[],h=[],b=this.bindingRegExp(),e=this.componentRegExp(),d=function(b,c,d,e){var f,g,h,i,j,k,m;return j={},m=function(){var a,b,c,d;for(c=e.split("|"),d=[],a=0,b=c.length;b>a;a++)k=c[a],d.push(k.trim());return d}(),f=function(){var a,b,c,d;for(c=m.shift().split("<"),d=[],a=0,b=c.length;b>a;a++)g=c[a],d.push(g.trim());return d}(),i=f.shift(),j.formatters=m,(h=f.shift())&&(j.dependencies=h.split(/\s+/)),l.bindings.push(new a[b](l,c,d,i,j))},g=function(f){var i,j,k,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M;if(c.call(h,f)<0){if(3===f.nodeType){if(q=a.TextTemplateParser,(n=l.config.templateDelimiters)&&(u=q.parse(f.data,n)).length&&(1!==u.length||u[0].type!==q.types.text)){for(x=0,B=u.length;B>x;x++)t=u[x],s=document.createTextNode(t.value),f.parentNode.insertBefore(s,f),1===t.type&&d("TextBinding",s,null,t.value);f.parentNode.removeChild(f)}}else if(e.test(f.tagName))v=f.tagName.replace(e,"").toLowerCase(),l.bindings.push(new a.ComponentBinding(l,f,v));else if(null!=f.attributes){for(H=f.attributes,y=0,C=H.length;C>y;y++)if(i=H[y],b.test(i.name)){if(v=i.name.replace(b,""),!(k=l.binders[v])){I=l.binders;for(o in I)w=I[o],"*"!==o&&-1!==o.indexOf("*")&&(r=new RegExp("^"+o.replace("*",".+")+"$"),r.test(v)&&(k=w))}if(k||(k=l.binders["*"]),k.block){for(J=f.childNodes,z=0,D=J.length;D>z;z++)p=J[z],h.push(p);j=[i]}}for(K=j||f.attributes,A=0,E=K.length;E>A;A++)i=K[A],b.test(i.name)&&(v=i.name.replace(b,""),d("Binding",f,v,i.value))}for(L=function(){var a,b,c,d;for(c=f.childNodes,d=[],b=0,a=c.length;a>b;b++)p=c[b],d.push(p);return d}(),M=[],G=0,F=L.length;F>G;G++)m=L[G],M.push(g(m));return M}},k=this.els,i=0,j=k.length;j>i;i++)f=k[i],g(f)},d.prototype.select=function(a){var b,c,d,e,f;for(e=this.bindings,f=[],c=0,d=e.length;d>c;c++)b=e[c],a(b)&&f.push(b);return f},d.prototype.bind=function(){var a,b,c,d,e;for(d=this.bindings,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.bind());return e},d.prototype.unbind=function(){var a,b,c,d,e;for(d=this.bindings,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.unbind());return e},d.prototype.sync=function(){var a,b,c,d,e;for(d=this.bindings,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.sync());return e},d.prototype.publish=function(){var a,b,c,d,e;for(d=this.select(function(a){return a.binder.publishes}),e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.publish());return e},d.prototype.update=function(a){var b,c,d,e,f,g,h;null==a&&(a={});for(c in a)d=a[c],this.models[c]=d;for(g=this.bindings,h=[],e=0,f=g.length;f>e;e++)b=g[e],h.push(b.update(a));return h},d}(),a.Binding=function(){function c(a,c,d,e,f){this.view=a,this.el=c,this.type=d,this.keypath=e,this.options=null!=f?f:{},this.update=b(this.update,this),this.unbind=b(this.unbind,this),this.bind=b(this.bind,this),this.publish=b(this.publish,this),this.sync=b(this.sync,this),this.set=b(this.set,this),this.eventHandler=b(this.eventHandler,this),this.formattedValue=b(this.formattedValue,this),this.setObserver=b(this.setObserver,this),this.setBinder=b(this.setBinder,this),this.formatters=this.options.formatters||[],this.dependencies=[],this.setBinder(),this.setObserver()}return c.prototype.setBinder=function(){var a,b,c,d;if(!(this.binder=this.view.binders[this.type])){d=this.view.binders;for(a in d)c=d[a],"*"!==a&&-1!==a.indexOf("*")&&(b=new RegExp("^"+a.replace("*",".+")+"$"),b.test(this.type)&&(this.binder=c,this.args=new RegExp("^"+a.replace("*","(.+)")+"$").exec(this.type),this.args.shift()))}return this.binder||(this.binder=this.view.binders["*"]),this.binder instanceof Function?this.binder={routine:this.binder}:void 0},c.prototype.setObserver=function(){var b=this;return this.observer=new a.KeypathObserver(this.view,this.view.models,this.keypath,function(a){return b.key&&b.unbind(!0),b.model=a.target,b.key&&b.bind(!0),b.sync()}),this.key=this.observer.key,this.model=this.observer.target},c.prototype.formattedValue=function(a){var b,c,e,f,g,h;for(h=this.formatters,f=0,g=h.length;g>f;f++)c=h[f],b=c.split(/\s+/),e=b.shift(),c=this.view.formatters[e],(null!=c?c.read:void 0)instanceof Function?a=c.read.apply(c,[a].concat(d.call(b))):c instanceof Function&&(a=c.apply(null,[a].concat(d.call(b))));return a},c.prototype.eventHandler=function(a){var b,c;return c=(b=this).view.config.handler,function(d){return c.call(a,this,d,b)}},c.prototype.set=function(a){var b;return a=a instanceof Function&&!this.binder["function"]?this.formattedValue(a.call(this.model)):this.formattedValue(a),null!=(b=this.binder.routine)?b.call(this,this.el,a):void 0},c.prototype.sync=function(){return this.set(this.key?this.view.adapters[this.key["interface"]].read(this.model,this.key.path):this.model)},c.prototype.publish=function(){var b,c,e,f,g,h,i,j,k;for(f=a.Util.getInputValue(this.el),i=this.formatters.slice(0).reverse(),g=0,h=i.length;h>g;g++)c=i[g],b=c.split(/\s+/),e=b.shift(),(null!=(j=this.view.formatters[e])?j.publish:void 0)&&(f=(k=this.view.formatters[e]).publish.apply(k,[f].concat(d.call(b))));return this.view.adapters[this.key["interface"]].publish(this.model,this.key.path,f)},c.prototype.bind=function(b){var c,d,e,f,g,h,i,j,k,l=this;if(null==b&&(b=!1),b||null!=(h=this.binder.bind)&&h.call(this,this.el),this.key&&this.view.adapters[this.key["interface"]].subscribe(this.model,this.key.path,this.sync),(b?void 0:this.view.config.preloadData)&&this.sync(),null!=(i=this.options.dependencies)?i.length:void 0){for(j=this.options.dependencies,k=[],f=0,g=j.length;g>f;f++)c=j[f],e=new a.KeypathObserver(this.view,this.model,c,function(a,b){var c;return c=a.key,l.view.adapters[c["interface"]].unsubscribe(b,c.path,l.sync),l.view.adapters[c["interface"]].subscribe(a.target,c.path,l.sync),l.sync()}),d=e.key,this.view.adapters[d["interface"]].subscribe(e.target,d.path,this.sync),k.push(this.dependencies.push(e));return k}},c.prototype.unbind=function(a){var b,c,d,e,f,g;if(null==a&&(a=!1),a||(null!=(f=this.binder.unbind)&&f.call(this,this.el),this.observer.unobserve()),this.key&&this.view.adapters[this.key["interface"]].unsubscribe(this.model,this.key.path,this.sync),this.dependencies.length){for(g=this.dependencies,d=0,e=g.length;e>d;d++)c=g[d],b=c.key,this.view.adapters[b["interface"]].unsubscribe(c.target,b.path,this.sync);return this.dependencies=[]}},c.prototype.update=function(a){var b;return null==a&&(a={}),null!=(b=this.binder.update)?b.call(this,a):void 0},c}(),a.ComponentBinding=function(d){function e(d,e,f){var g,h,i,j,k;for(this.view=d,this.el=e,this.type=f,this.unbind=b(this.unbind,this),this.bind=b(this.bind,this),this.update=b(this.update,this),this.locals=b(this.locals,this),this.component=a.components[this.type],this.attributes={},this.inflections={},j=this.el.attributes||[],h=0,i=j.length;i>h;h++)g=j[h],k=g.name,c.call(this.component.attributes,k)>=0?this.attributes[g.name]=g.value:this.inflections[g.name]=g.value}return f(e,d),e.prototype.sync=function(){},e.prototype.locals=function(a){var b,c,d,e,f,g,h,i,j;null==a&&(a=this.view.models),f={},i=this.inflections;for(c in i)for(b=i[c],j=b.split("."),g=0,h=j.length;h>g;g++)e=j[g],f[c]=(f[c]||a)[e];for(c in a)d=a[c],null==f[c]&&(f[c]=d);return f},e.prototype.update=function(a){var b;return null!=(b=this.componentView)?b.update(this.locals(a)):void 0},e.prototype.bind=function(){var b,c;return null!=this.componentView?null!=(c=this.componentView)?c.bind():void 0:(b=this.component.build.call(this.attributes),(this.componentView=new a.View(b,this.locals(),this.view.options)).bind(),this.el.parentNode.replaceChild(b,this.el))},e.prototype.unbind=function(){var a;return null!=(a=this.componentView)?a.unbind():void 0},e}(a.Binding),a.TextBinding=function(a){function c(a,c,d,e,f){this.view=a,this.el=c,this.type=d,this.keypath=e,this.options=null!=f?f:{},this.sync=b(this.sync,this),this.formatters=this.options.formatters||[],this.dependencies=[],this.setObserver()}return f(c,a),c.prototype.binder={routine:function(a,b){return a.data=null!=b?b:""}},c.prototype.sync=function(){return c.__super__.sync.apply(this,arguments)},c}(a.Binding),a.KeypathParser=function(){function a(){}return a.parse=function(a,b,d){var e,f,g,h;h=[],f={"interface":d,path:""};for(g in a)e=a[g],c.call(b,e)>=0?(h.push(f),f={"interface":e,path:""}):f.path+=e;return h.push(f),h},a}(),a.TextTemplateParser=function(){function a(){}return a.types={text:0,binding:1},a.parse=function(a,b){var c,d,e,f,g,h,i;for(h=[],f=a.length,c=0,d=0;f>d;){if(c=a.indexOf(b[0],d),0>c){h.push({type:this.types.text,value:a.slice(d)});break}if(c>0&&c>d&&h.push({type:this.types.text,value:a.slice(d,c)}),d=c+b[0].length,c=a.indexOf(b[1],d),0>c){g=a.slice(d-b[1].length),e=h[h.length-1],(null!=e?e.type:void 0)===this.types.text?e.value+=g:h.push({type:this.types.text,value:g});break}i=a.slice(d,c).trim(),h.push({type:this.types.binding,value:i}),d=c+b[1].length}return h},a}(),a.KeypathObserver=function(){function d(a,c,d,e){this.view=a,this.model=c,this.keypath=d,this.callback=e,this.unobserve=b(this.unobserve,this),this.realize=b(this.realize,this),this.update=b(this.update,this),this.parse=b(this.parse,this),this.parse(),this.objectPath=[],this.target=this.realize()}return d.prototype.parse=function(){var b,d,e,f,g,h;return b=function(){var a,b;a=this.view.adapters,b=[];for(d in a)g=a[d],b.push(d);return b}.call(this),h=this.keypath[0],c.call(b,h)>=0?(f=this.keypath[0],e=this.keypath.substr(1)):(f=this.view.config.rootInterface,e=this.keypath),this.tokens=a.KeypathParser.parse(e,b,f),this.key=this.tokens.pop()},d.prototype.update=function(){var a,b;return(a=this.realize())!==this.target?(b=this.target,this.target=a,this.callback(this,b)):void 0},d.prototype.realize=function(){var a,b,c,d,e,f,g;for(a=this.model,g=this.tokens,b=e=0,f=g.length;f>e;b=++e)d=g[b],null!=this.objectPath[b]?a!==(c=this.objectPath[b])&&(this.view.adapters[d["interface"]].unsubscribe(c,d.path,this.update),this.view.adapters[d["interface"]].subscribe(a,d.path,this.update),this.objectPath[b]=a):(this.view.adapters[d["interface"]].subscribe(a,d.path,this.update),this.objectPath[b]=a),a=this.view.adapters[d["interface"]].read(a,d.path);return a},d.prototype.unobserve=function(){var a,b,c,d,e,f,g;for(f=this.tokens,g=[],a=d=0,e=f.length;e>d;a=++d)c=f[a],(b=this.objectPath[a])?g.push(this.view.adapters[c["interface"]].unsubscribe(b,c.path,this.update)):g.push(void 0);return g},d}(),a.binders.text=function(a,b){return null!=a.textContent?a.textContent=null!=b?b:"":a.innerText=null!=b?b:""},a.binders.html=function(a,b){return a.innerHTML=null!=b?b:""},a.binders.show=function(a,b){return a.style.display=b?"":"none"},a.binders.hide=function(a,b){return a.style.display=b?"none":""},a.binders.enabled=function(a,b){return a.disabled=!b},a.binders.disabled=function(a,b){return a.disabled=!!b},a.binders.checked={publishes:!0,bind:function(b){return a.Util.bindEvent(b,"change",this.publish)},unbind:function(b){return a.Util.unbindEvent(b,"change",this.publish)},routine:function(a,b){var c;return a.checked="radio"===a.type?(null!=(c=a.value)?c.toString():void 0)===(null!=b?b.toString():void 0):!!b}},a.binders.unchecked={publishes:!0,bind:function(b){return a.Util.bindEvent(b,"change",this.publish)},unbind:function(b){return a.Util.unbindEvent(b,"change",this.publish)},routine:function(a,b){var c;return a.checked="radio"===a.type?(null!=(c=a.value)?c.toString():void 0)!==(null!=b?b.toString():void 0):!b}},a.binders.value={publishes:!0,bind:function(b){return a.Util.bindEvent(b,"change",this.publish)},unbind:function(b){return a.Util.unbindEvent(b,"change",this.publish)},routine:function(a,b){var d,e,f,g,h,i,j;if(null!=window.jQuery){if(a=jQuery(a),(null!=b?b.toString():void 0)!==(null!=(g=a.val())?g.toString():void 0))return a.val(null!=b?b:"")}else if("select-multiple"===a.type){if(null!=b){for(j=[],e=0,f=a.length;f>e;e++)d=a[e],j.push(d.selected=(h=d.value,c.call(b,h)>=0));return j}}else if((null!=b?b.toString():void 0)!==(null!=(i=a.value)?i.toString():void 0))return a.value=null!=b?b:""}},a.binders["if"]={block:!0,bind:function(a){var b,c;return null==this.marker?(b=[this.view.config.prefix,this.type].join("-").replace("--","-"),c=a.getAttribute(b),this.marker=document.createComment(" rivets: "+this.type+" "+c+" "),a.removeAttribute(b),a.parentNode.insertBefore(this.marker,a),a.parentNode.removeChild(a)):void 0},unbind:function(){var a;return null!=(a=this.nested)?a.unbind():void 0},routine:function(b,c){var d,e,f,g,h;if(!!c==(null==this.nested)){if(c){f={},h=this.view.models;for(d in h)e=h[d],f[d]=e;return g={binders:this.view.options.binders,formatters:this.view.options.formatters,adapters:this.view.options.adapters,config:this.view.options.config},(this.nested=new a.View(b,f,g)).bind(),this.marker.parentNode.insertBefore(b,this.marker.nextSibling)}return b.parentNode.removeChild(b),this.nested.unbind(),delete this.nested}},update:function(a){var b;return null!=(b=this.nested)?b.update(a):void 0}},a.binders.unless={block:!0,bind:function(b){return a.binders["if"].bind.call(this,b)},unbind:function(){return a.binders["if"].unbind.call(this)},routine:function(b,c){return a.binders["if"].routine.call(this,b,!c)},update:function(b){return a.binders["if"].update.call(this,b)}},a.binders["on-*"]={"function":!0,unbind:function(b){return this.handler?a.Util.unbindEvent(b,this.args[0],this.handler):void 0},routine:function(b,c){return this.handler&&a.Util.unbindEvent(b,this.args[0],this.handler),a.Util.bindEvent(b,this.args[0],this.handler=this.eventHandler(c))}},a.binders["each-*"]={block:!0,bind:function(a){var b;return null==this.marker?(b=[this.view.config.prefix,this.type].join("-").replace("--","-"),this.marker=document.createComment(" rivets: "+this.type+" "),this.iterated=[],a.removeAttribute(b),a.parentNode.insertBefore(this.marker,a),a.parentNode.removeChild(a)):void 0},unbind:function(){var a,b,c,d,e;if(null!=this.iterated){for(d=this.iterated,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.unbind());return e}},routine:function(b,c){var d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A;if(k=this.args[0],c=c||[],this.iterated.length>c.length)for(w=Array(this.iterated.length-c.length),q=0,t=w.length;t>q;q++)f=w[q],p=this.iterated.pop(),p.unbind(),this.marker.parentNode.removeChild(p.els[0]);for(g=r=0,u=c.length;u>r;g=++r)if(j=c[g],e={},e[k]=j,null==this.iterated[g]){x=this.view.models;for(i in x)j=x[i],null==e[i]&&(e[i]=j);m=this.iterated.length?this.iterated[this.iterated.length-1].els[0]:this.marker,l={binders:this.view.options.binders,formatters:this.view.options.formatters,adapters:this.view.options.adapters,config:{}},y=this.view.options.config;for(h in y)o=y[h],l.config[h]=o;l.config.preloadData=!0,n=b.cloneNode(!0),p=new a.View(n,e,l),p.bind(),this.iterated.push(p),this.marker.parentNode.insertBefore(n,m.nextSibling)}else this.iterated[g].models[k]!==j&&this.iterated[g].update(e);if("OPTION"===b.nodeName){for(z=this.view.bindings,A=[],s=0,v=z.length;v>s;s++)d=z[s],d.el===this.marker.parentNode&&"value"===d.type?A.push(d.sync()):A.push(void 0);return A}},update:function(a){var b,c,d,e,f,g,h,i;b={};for(c in a)d=a[c],c!==this.args[0]&&(b[c]=d);for(h=this.iterated,i=[],f=0,g=h.length;g>f;f++)e=h[f],i.push(e.update(b));return i}},a.binders["class-*"]=function(a,b){var c;return c=" "+a.className+" ",!b==(-1!==c.indexOf(" "+this.args[0]+" "))?a.className=b?""+a.className+" "+this.args[0]:c.replace(" "+this.args[0]+" "," ").trim():void 0},a.binders["*"]=function(a,b){return b?a.setAttribute(this.type,b):a.removeAttribute(this.type)},a.adapters["."]={id:"_rv",counter:0,weakmap:{},weakReference:function(a){var b;return null==a[this.id]&&(b=this.counter++,this.weakmap[b]={callbacks:{}},Object.defineProperty(a,this.id,{value:b})),this.weakmap[a[this.id]]},stubFunction:function(a,b){var c,d,e;return d=a[b],c=this.weakReference(a),e=this.weakmap,a[b]=function(){var b,f,g,h,i,j,k,l,m,n;h=d.apply(a,arguments),k=c.pointers;for(g in k)for(f=k[g],n=null!=(l=null!=(m=e[g])?m.callbacks[f]:void 0)?l:[],i=0,j=n.length;j>i;i++)b=n[i],b();return h}},observeMutations:function(a,b,d){var e,f,g,h,i,j;if(Array.isArray(a)){if(g=this.weakReference(a),null==g.pointers)for(g.pointers={},f=["push","pop","shift","unshift","sort","reverse","splice"],i=0,j=f.length;j>i;i++)e=f[i],this.stubFunction(a,e);if(null==(h=g.pointers)[b]&&(h[b]=[]),c.call(g.pointers[b],d)<0)return g.pointers[b].push(d)}},unobserveMutations:function(a,b,c){var d,e;return Array.isArray(a&&null!=a[this.id])&&(d=null!=(e=this.weakReference(a).pointers)?e[b]:void 0)?d.splice(d.indexOf(c),1):void 0},subscribe:function(a,b,d){var e,f,g=this;return e=this.weakReference(a).callbacks,null==e[b]&&(e[b]=[],f=a[b],Object.defineProperty(a,b,{get:function(){return f},set:function(c){var h,i,j;if(c!==f){for(f=c,j=e[b],h=0,i=j.length;i>h;h++)d=j[h],d();return g.observeMutations(c,a[g.id],b)}}})),c.call(e[b],d)<0&&e[b].push(d),this.observeMutations(a[b],a[this.id],b)},unsubscribe:function(a,b,c){var d;return d=this.weakmap[a[this.id]].callbacks[b],d.splice(d.indexOf(c),1),this.unobserveMutations(a[b],a[this.id],b)},read:function(a,b){return a[b]},publish:function(a,b,c){return a[b]=c}},a.factory=function(b){return b._=a,b.binders=a.binders,b.components=a.components,b.formatters=a.formatters,b.adapters=a.adapters,b.config=a.config,b.configure=function(b){var c,d;null==b&&(b={});for(c in b)d=b[c],a.config[c]=d},b.bind=function(b,c,d){var e;return null==c&&(c={}),null==d&&(d={}),e=new a.View(b,c,d),e.bind(),e}},"object"==typeof exports?a.factory(exports):"function"==typeof define&&define.amd?define(["exports"],function(b){return a.factory(this.rivets=b),b}):a.factory(this.rivets={})}).call(this); \ No newline at end of file diff --git a/ajax/libs/rivets/0.6.5/rivets.js b/ajax/libs/rivets/0.6.5/rivets.js new file mode 100644 index 000000000..9d09d3d7e --- /dev/null +++ b/ajax/libs/rivets/0.6.5/rivets.js @@ -0,0 +1,1259 @@ +// Rivets.js +// version: 0.6.5 +// author: Michael Richards +// license: MIT +(function() { + var Rivets, + __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, + __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; }, + __slice = [].slice, + __hasProp = {}.hasOwnProperty, + __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; + + Rivets = { + binders: {}, + components: {}, + formatters: {}, + adapters: {}, + config: { + prefix: 'rv', + templateDelimiters: ['{', '}'], + rootInterface: '.', + preloadData: true, + handler: function(context, ev, binding) { + return this.call(context, ev, binding.view.models); + } + } + }; + + 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.View = (function() { + function View(els, models, options) { + var k, option, v, _base, _i, _len, _ref, _ref1, _ref2; + 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.componentRegExp = __bind(this.componentRegExp, this); + this.bindingRegExp = __bind(this.bindingRegExp, this); + if (!(this.els.jquery || this.els instanceof Array)) { + this.els = [this.els]; + } + _ref = ['config', 'binders', 'formatters', 'adapters']; + 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 ((_base = this[option])[k] == null) { + _base[k] = v; + } + } + } + this.build(); + } + + View.prototype.bindingRegExp = function() { + return new RegExp("^" + this.config.prefix + "-"); + }; + + View.prototype.componentRegExp = function() { + return new RegExp("^" + (this.config.prefix.toUpperCase()) + "-"); + }; + + View.prototype.build = function() { + var bindingRegExp, buildBinding, componentRegExp, el, parse, skipNodes, _i, _len, _ref, + _this = this; + this.bindings = []; + skipNodes = []; + bindingRegExp = this.bindingRegExp(); + componentRegExp = this.componentRegExp(); + buildBinding = function(binding, node, type, declaration) { + var context, ctx, dependencies, keypath, options, pipe, pipes; + 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; + })(); + keypath = context.shift(); + options.formatters = pipes; + if (dependencies = context.shift()) { + options.dependencies = dependencies.split(/\s+/); + } + return _this.bindings.push(new Rivets[binding](_this, node, type, keypath, options)); + }; + parse = function(node) { + var attribute, attributes, binder, childNode, delimiters, identifier, n, parser, regexp, 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 === 3) { + 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)) { + for (_i = 0, _len = tokens.length; _i < _len; _i++) { + token = tokens[_i]; + text = document.createTextNode(token.value); + node.parentNode.insertBefore(text, node); + if (token.type === 1) { + buildBinding('TextBinding', text, null, token.value); + } + } + node.parentNode.removeChild(node); + } + } + } + } else if (componentRegExp.test(node.tagName)) { + type = node.tagName.replace(componentRegExp, '').toLowerCase(); + _this.bindings.push(new Rivets.ComponentBinding(_this, node, type)); + } 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('Binding', node, type, attribute.value); + } + } + } + _ref4 = (function() { + var _len4, _n, _ref4, _results1; + _ref4 = node.childNodes; + _results1 = []; + for (_n = 0, _len4 = _ref4.length; _n < _len4; _n++) { + n = _ref4[_n]; + _results1.push(n); + } + return _results1; + })(); + _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.Binding = (function() { + function Binding(view, el, type, keypath, options) { + this.view = view; + this.el = el; + this.type = type; + 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); + this.setObserver = __bind(this.setObserver, this); + this.setBinder = __bind(this.setBinder, this); + this.formatters = this.options.formatters || []; + this.dependencies = []; + this.setBinder(); + this.setObserver(); + } + + Binding.prototype.setBinder = function() { + var identifier, regexp, value, _ref; + if (!(this.binder = this.view.binders[this.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(this.type)) { + this.binder = value; + this.args = new RegExp("^" + (identifier.replace('*', '(.+)')) + "$").exec(this.type); + this.args.shift(); + } + } + } + } + this.binder || (this.binder = this.view.binders['*']); + if (this.binder instanceof Function) { + return this.binder = { + routine: this.binder + }; + } + }; + + Binding.prototype.setObserver = function() { + var _this = this; + this.observer = new Rivets.KeypathObserver(this.view, this.view.models, this.keypath, function(obs) { + if (_this.key) { + _this.unbind(true); + } + _this.model = obs.target; + if (_this.key) { + _this.bind(true); + } + return _this.sync(); + }); + this.key = this.observer.key; + return this.model = this.observer.target; + }; + + 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.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.key ? this.view.adapters[this.key["interface"]].read(this.model, this.key.path) : this.model); + }; + + 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.adapters[this.key["interface"]].publish(this.model, this.key.path, value); + }; + + Binding.prototype.bind = function(silent) { + var dependency, key, observer, _i, _len, _ref, _ref1, _ref2, _results, + _this = this; + if (silent == null) { + silent = false; + } + if (!silent) { + if ((_ref = this.binder.bind) != null) { + _ref.call(this, this.el); + } + } + if (this.key) { + this.view.adapters[this.key["interface"]].subscribe(this.model, this.key.path, this.sync); + } + if (!silent ? this.view.config.preloadData : void 0) { + 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]; + observer = new Rivets.KeypathObserver(this.view, this.model, dependency, function(obs, prev) { + var key; + key = obs.key; + _this.view.adapters[key["interface"]].unsubscribe(prev, key.path, _this.sync); + _this.view.adapters[key["interface"]].subscribe(obs.target, key.path, _this.sync); + return _this.sync(); + }); + key = observer.key; + this.view.adapters[key["interface"]].subscribe(observer.target, key.path, this.sync); + _results.push(this.dependencies.push(observer)); + } + return _results; + } + }; + + Binding.prototype.unbind = function(silent) { + var key, obs, _i, _len, _ref, _ref1; + if (silent == null) { + silent = false; + } + if (!silent) { + if ((_ref = this.binder.unbind) != null) { + _ref.call(this, this.el); + } + this.observer.unobserve(); + } + if (this.key) { + this.view.adapters[this.key["interface"]].unsubscribe(this.model, this.key.path, this.sync); + } + if (this.dependencies.length) { + _ref1 = this.dependencies; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + obs = _ref1[_i]; + key = obs.key; + this.view.adapters[key["interface"]].unsubscribe(obs.target, key.path, this.sync); + } + return this.dependencies = []; + } + }; + + Binding.prototype.update = function(models) { + var _ref; + if (models == null) { + models = {}; + } + return (_ref = this.binder.update) != null ? _ref.call(this, models) : void 0; + }; + + return Binding; + + })(); + + Rivets.ComponentBinding = (function(_super) { + __extends(ComponentBinding, _super); + + function ComponentBinding(view, el, type) { + var attribute, _i, _len, _ref, _ref1; + this.view = view; + this.el = el; + this.type = type; + this.unbind = __bind(this.unbind, this); + this.bind = __bind(this.bind, this); + this.update = __bind(this.update, this); + this.locals = __bind(this.locals, this); + this.component = Rivets.components[this.type]; + this.attributes = {}; + this.inflections = {}; + _ref = this.el.attributes || []; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + attribute = _ref[_i]; + if (_ref1 = attribute.name, __indexOf.call(this.component.attributes, _ref1) >= 0) { + this.attributes[attribute.name] = attribute.value; + } else { + this.inflections[attribute.name] = attribute.value; + } + } + } + + ComponentBinding.prototype.sync = function() {}; + + ComponentBinding.prototype.locals = function(models) { + var inverse, key, model, path, result, _i, _len, _ref, _ref1; + if (models == null) { + models = this.view.models; + } + result = {}; + _ref = this.inflections; + for (key in _ref) { + inverse = _ref[key]; + _ref1 = inverse.split('.'); + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + path = _ref1[_i]; + result[key] = (result[key] || models)[path]; + } + } + for (key in models) { + model = models[key]; + if (result[key] == null) { + result[key] = model; + } + } + return result; + }; + + ComponentBinding.prototype.update = function(models) { + var _ref; + return (_ref = this.componentView) != null ? _ref.update(this.locals(models)) : void 0; + }; + + ComponentBinding.prototype.bind = function() { + var el, _ref; + if (this.componentView != null) { + return (_ref = this.componentView) != null ? _ref.bind() : void 0; + } else { + el = this.component.build.call(this.attributes); + (this.componentView = new Rivets.View(el, this.locals(), this.view.options)).bind(); + return this.el.parentNode.replaceChild(el, this.el); + } + }; + + ComponentBinding.prototype.unbind = function() { + var _ref; + return (_ref = this.componentView) != null ? _ref.unbind() : void 0; + }; + + return ComponentBinding; + + })(Rivets.Binding); + + Rivets.TextBinding = (function(_super) { + __extends(TextBinding, _super); + + function TextBinding(view, el, type, keypath, options) { + this.view = view; + this.el = el; + this.type = type; + this.keypath = keypath; + this.options = options != null ? options : {}; + this.sync = __bind(this.sync, this); + this.formatters = this.options.formatters || []; + this.dependencies = []; + this.setObserver(); + } + + TextBinding.prototype.binder = { + routine: function(node, value) { + return node.data = value != null ? value : ''; + } + }; + + TextBinding.prototype.sync = function() { + return TextBinding.__super__.sync.apply(this, arguments); + }; + + return TextBinding; + + })(Rivets.Binding); + + Rivets.KeypathParser = (function() { + function KeypathParser() {} + + KeypathParser.parse = function(keypath, interfaces, root) { + var char, current, tokens, _i, _len; + tokens = []; + current = { + "interface": root, + path: '' + }; + for (_i = 0, _len = keypath.length; _i < _len; _i++) { + char = keypath[_i]; + if (__indexOf.call(interfaces, char) >= 0) { + tokens.push(current); + current = { + "interface": char, + path: '' + }; + } else { + current.path += char; + } + } + tokens.push(current); + return tokens; + }; + + return KeypathParser; + + })(); + + 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 + delimiters[0].length; + index = template.indexOf(delimiters[1], lastIndex); + if (index < 0) { + substring = template.slice(lastIndex - delimiters[1].length); + 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 + delimiters[1].length; + } + } + return tokens; + }; + + return TextTemplateParser; + + })(); + + Rivets.KeypathObserver = (function() { + function KeypathObserver(view, model, keypath, callback) { + this.view = view; + this.model = model; + this.keypath = keypath; + this.callback = callback; + this.unobserve = __bind(this.unobserve, this); + this.realize = __bind(this.realize, this); + this.update = __bind(this.update, this); + this.parse = __bind(this.parse, this); + this.parse(); + this.objectPath = []; + this.target = this.realize(); + } + + KeypathObserver.prototype.parse = function() { + var interfaces, k, path, root, v, _ref; + interfaces = (function() { + var _ref, _results; + _ref = this.view.adapters; + _results = []; + for (k in _ref) { + v = _ref[k]; + _results.push(k); + } + return _results; + }).call(this); + if (_ref = this.keypath[0], __indexOf.call(interfaces, _ref) >= 0) { + root = this.keypath[0]; + path = this.keypath.substr(1); + } else { + root = this.view.config.rootInterface; + path = this.keypath; + } + this.tokens = Rivets.KeypathParser.parse(path, interfaces, root); + return this.key = this.tokens.pop(); + }; + + KeypathObserver.prototype.update = function() { + var next, prev; + if ((next = this.realize()) !== this.target) { + prev = this.target; + this.target = next; + return this.callback(this, prev); + } + }; + + KeypathObserver.prototype.realize = function() { + var current, index, prev, token, _i, _len, _ref; + current = this.model; + _ref = this.tokens; + for (index = _i = 0, _len = _ref.length; _i < _len; index = ++_i) { + token = _ref[index]; + if (this.objectPath[index] != null) { + if (current !== (prev = this.objectPath[index])) { + this.view.adapters[token["interface"]].unsubscribe(prev, token.path, this.update); + this.view.adapters[token["interface"]].subscribe(current, token.path, this.update); + this.objectPath[index] = current; + } + } else { + this.view.adapters[token["interface"]].subscribe(current, token.path, this.update); + this.objectPath[index] = current; + } + current = this.view.adapters[token["interface"]].read(current, token.path); + } + return current; + }; + + KeypathObserver.prototype.unobserve = function() { + var index, obj, token, _i, _len, _ref, _results; + _ref = this.tokens; + _results = []; + for (index = _i = 0, _len = _ref.length; _i < _len; index = ++_i) { + token = _ref[index]; + if (obj = this.objectPath[index]) { + _results.push(this.view.adapters[token["interface"]].unsubscribe(obj, token.path, this.update)); + } else { + _results.push(void 0); + } + } + return _results; + }; + + return KeypathObserver; + + })(); + + Rivets.binders.text = function(el, value) { + if (el.textContent != null) { + return el.textContent = value != null ? value : ''; + } else { + return el.innerText = value != null ? value : ''; + } + }; + + Rivets.binders.html = function(el, value) { + return el.innerHTML = value != null ? value : ''; + }; + + Rivets.binders.show = function(el, value) { + return el.style.display = value ? '' : 'none'; + }; + + Rivets.binders.hide = function(el, value) { + return el.style.display = value ? 'none' : ''; + }; + + Rivets.binders.enabled = function(el, value) { + return el.disabled = !value; + }; + + Rivets.binders.disabled = function(el, value) { + return el.disabled = !!value; + }; + + Rivets.binders.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; + } + } + }; + + Rivets.binders.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; + } + } + }; + + Rivets.binders.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 : ''; + } + } + } + }; + + Rivets.binders["if"] = { + block: true, + bind: function(el) { + var attr, declaration; + if (this.marker == null) { + attr = [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, + adapters: this.view.options.adapters, + 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) { + var _ref; + return (_ref = this.nested) != null ? _ref.update(models) : void 0; + } + }; + + Rivets.binders.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); + } + }; + + Rivets.binders['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)); + } + }; + + Rivets.binders['each-*'] = { + block: true, + bind: function(el) { + var attr; + if (this.marker == null) { + attr = [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 binding, data, i, index, k, key, model, modelName, options, previous, template, v, view, _i, _j, _k, _len, _len1, _len2, _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]); + } + } + 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 (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, + adapters: this.view.options.adapters, + 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); + this.marker.parentNode.insertBefore(template, previous.nextSibling); + } else if (this.iterated[index].models[modelName] !== model) { + this.iterated[index].update(data); + } + } + if (el.nodeName === 'OPTION') { + _ref3 = this.view.bindings; + _results = []; + for (_k = 0, _len2 = _ref3.length; _k < _len2; _k++) { + binding = _ref3[_k]; + if (binding.el === this.marker.parentNode && binding.type === 'value') { + _results.push(binding.sync()); + } 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; + } + }; + + Rivets.binders['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(); + } + }; + + Rivets.binders['*'] = function(el, value) { + if (value) { + return el.setAttribute(this.type, value); + } else { + return el.removeAttribute(this.type); + } + }; + + Rivets.adapters['.'] = { + id: '_rv', + counter: 0, + weakmap: {}, + weakReference: function(obj) { + var id; + if (obj[this.id] == null) { + id = this.counter++; + this.weakmap[id] = { + callbacks: {} + }; + Object.defineProperty(obj, this.id, { + value: id + }); + } + return this.weakmap[obj[this.id]]; + }, + stubFunction: function(obj, fn) { + var map, original, weakmap; + original = obj[fn]; + map = this.weakReference(obj); + weakmap = this.weakmap; + return obj[fn] = function() { + var callback, k, r, response, _i, _len, _ref, _ref1, _ref2, _ref3; + response = original.apply(obj, arguments); + _ref = map.pointers; + for (r in _ref) { + k = _ref[r]; + _ref3 = (_ref1 = (_ref2 = weakmap[r]) != null ? _ref2.callbacks[k] : void 0) != null ? _ref1 : []; + for (_i = 0, _len = _ref3.length; _i < _len; _i++) { + callback = _ref3[_i]; + callback(); + } + } + return response; + }; + }, + observeMutations: function(obj, ref, keypath) { + var fn, functions, map, _base, _i, _len; + if (Array.isArray(obj)) { + map = this.weakReference(obj); + if (map.pointers == null) { + map.pointers = {}; + functions = ['push', 'pop', 'shift', 'unshift', 'sort', 'reverse', 'splice']; + for (_i = 0, _len = functions.length; _i < _len; _i++) { + fn = functions[_i]; + this.stubFunction(obj, fn); + } + } + if ((_base = map.pointers)[ref] == null) { + _base[ref] = []; + } + if (__indexOf.call(map.pointers[ref], keypath) < 0) { + return map.pointers[ref].push(keypath); + } + } + }, + unobserveMutations: function(obj, ref, keypath) { + var keypaths, _ref; + if (Array.isArray(obj && (obj[this.id] != null))) { + if (keypaths = (_ref = this.weakReference(obj).pointers) != null ? _ref[ref] : void 0) { + return keypaths.splice(keypaths.indexOf(keypath), 1); + } + } + }, + subscribe: function(obj, keypath, callback) { + var callbacks, value, + _this = this; + callbacks = this.weakReference(obj).callbacks; + if (callbacks[keypath] == null) { + callbacks[keypath] = []; + value = obj[keypath]; + Object.defineProperty(obj, keypath, { + get: function() { + return value; + }, + set: function(newValue) { + var _i, _len, _ref; + if (newValue !== value) { + value = newValue; + _ref = callbacks[keypath]; + for (_i = 0, _len = _ref.length; _i < _len; _i++) { + callback = _ref[_i]; + callback(); + } + return _this.observeMutations(newValue, obj[_this.id], keypath); + } + } + }); + } + if (__indexOf.call(callbacks[keypath], callback) < 0) { + callbacks[keypath].push(callback); + } + return this.observeMutations(obj[keypath], obj[this.id], keypath); + }, + unsubscribe: function(obj, keypath, callback) { + var callbacks; + callbacks = this.weakmap[obj[this.id]].callbacks[keypath]; + callbacks.splice(callbacks.indexOf(callback), 1); + return this.unobserveMutations(obj[keypath], obj[this.id], keypath); + }, + read: function(obj, keypath) { + return obj[keypath]; + }, + publish: function(obj, keypath, value) { + return obj[keypath] = value; + } + }; + + Rivets.factory = function(exports) { + exports._ = Rivets; + exports.binders = Rivets.binders; + exports.components = Rivets.components; + exports.formatters = Rivets.formatters; + exports.adapters = Rivets.adapters; + 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); diff --git a/ajax/libs/rivets/0.6.5/rivets.min.js b/ajax/libs/rivets/0.6.5/rivets.min.js new file mode 100644 index 000000000..706e3ed87 --- /dev/null +++ b/ajax/libs/rivets/0.6.5/rivets.min.js @@ -0,0 +1,5 @@ +// Rivets.js +// version: 0.6.5 +// author: Michael Richards +// license: MIT +(function(){var a,b=function(a,b){return function(){return a.apply(b,arguments)}},c=[].indexOf||function(a){for(var b=0,c=this.length;c>b;b++)if(b in this&&this[b]===a)return b;return-1},d=[].slice,e={}.hasOwnProperty,f=function(a,b){function c(){this.constructor=a}for(var d in b)e.call(b,d)&&(a[d]=b[d]);return c.prototype=b.prototype,a.prototype=new c,a.__super__=b.prototype,a};a={binders:{},components:{},formatters:{},adapters:{},config:{prefix:"rv",templateDelimiters:["{","}"],rootInterface:".",preloadData:!0,handler:function(a,b,c){return this.call(a,b,c.view.models)}}},a.Util={bindEvent:function(a,b,c){return null!=window.jQuery?(a=jQuery(a),null!=a.on?a.on(b,c):a.bind(b,c)):null!=window.addEventListener?a.addEventListener(b,c,!1):(b="on"+b,a.attachEvent(b,c))},unbindEvent:function(a,b,c){return null!=window.jQuery?(a=jQuery(a),null!=a.off?a.off(b,c):a.unbind(b,c)):null!=window.removeEventListener?a.removeEventListener(b,c,!1):(b="on"+b,a.detachEvent(b,c))},getInputValue:function(a){var b,c,d,e;if(null!=window.jQuery)switch(a=jQuery(a),a[0].type){case"checkbox":return a.is(":checked");default:return a.val()}else switch(a.type){case"checkbox":return a.checked;case"select-multiple":for(e=[],c=0,d=a.length;d>c;c++)b=a[c],b.selected&&e.push(b.value);return e;default:return a.value}}},a.View=function(){function d(c,d,e){var f,g,h,i,j,k,l,m,n;for(this.els=c,this.models=d,this.options=null!=e?e:{},this.update=b(this.update,this),this.publish=b(this.publish,this),this.sync=b(this.sync,this),this.unbind=b(this.unbind,this),this.bind=b(this.bind,this),this.select=b(this.select,this),this.build=b(this.build,this),this.componentRegExp=b(this.componentRegExp,this),this.bindingRegExp=b(this.bindingRegExp,this),this.els.jquery||this.els instanceof Array||(this.els=[this.els]),l=["config","binders","formatters","adapters"],j=0,k=l.length;k>j;j++){if(g=l[j],this[g]={},this.options[g]){m=this.options[g];for(f in m)h=m[f],this[g][f]=h}n=a[g];for(f in n)h=n[f],null==(i=this[g])[f]&&(i[f]=h)}this.build()}return d.prototype.bindingRegExp=function(){return new RegExp("^"+this.config.prefix+"-")},d.prototype.componentRegExp=function(){return new RegExp("^"+this.config.prefix.toUpperCase()+"-")},d.prototype.build=function(){var b,d,e,f,g,h,i,j,k,l=this;for(this.bindings=[],h=[],b=this.bindingRegExp(),e=this.componentRegExp(),d=function(b,c,d,e){var f,g,h,i,j,k,m;return j={},m=function(){var a,b,c,d;for(c=e.split("|"),d=[],a=0,b=c.length;b>a;a++)k=c[a],d.push(k.trim());return d}(),f=function(){var a,b,c,d;for(c=m.shift().split("<"),d=[],a=0,b=c.length;b>a;a++)g=c[a],d.push(g.trim());return d}(),i=f.shift(),j.formatters=m,(h=f.shift())&&(j.dependencies=h.split(/\s+/)),l.bindings.push(new a[b](l,c,d,i,j))},g=function(f){var i,j,k,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M;if(c.call(h,f)<0){if(3===f.nodeType){if(q=a.TextTemplateParser,(n=l.config.templateDelimiters)&&(u=q.parse(f.data,n)).length&&(1!==u.length||u[0].type!==q.types.text)){for(x=0,B=u.length;B>x;x++)t=u[x],s=document.createTextNode(t.value),f.parentNode.insertBefore(s,f),1===t.type&&d("TextBinding",s,null,t.value);f.parentNode.removeChild(f)}}else if(e.test(f.tagName))v=f.tagName.replace(e,"").toLowerCase(),l.bindings.push(new a.ComponentBinding(l,f,v));else if(null!=f.attributes){for(H=f.attributes,y=0,C=H.length;C>y;y++)if(i=H[y],b.test(i.name)){if(v=i.name.replace(b,""),!(k=l.binders[v])){I=l.binders;for(o in I)w=I[o],"*"!==o&&-1!==o.indexOf("*")&&(r=new RegExp("^"+o.replace("*",".+")+"$"),r.test(v)&&(k=w))}if(k||(k=l.binders["*"]),k.block){for(J=f.childNodes,z=0,D=J.length;D>z;z++)p=J[z],h.push(p);j=[i]}}for(K=j||f.attributes,A=0,E=K.length;E>A;A++)i=K[A],b.test(i.name)&&(v=i.name.replace(b,""),d("Binding",f,v,i.value))}for(L=function(){var a,b,c,d;for(c=f.childNodes,d=[],b=0,a=c.length;a>b;b++)p=c[b],d.push(p);return d}(),M=[],G=0,F=L.length;F>G;G++)m=L[G],M.push(g(m));return M}},k=this.els,i=0,j=k.length;j>i;i++)f=k[i],g(f)},d.prototype.select=function(a){var b,c,d,e,f;for(e=this.bindings,f=[],c=0,d=e.length;d>c;c++)b=e[c],a(b)&&f.push(b);return f},d.prototype.bind=function(){var a,b,c,d,e;for(d=this.bindings,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.bind());return e},d.prototype.unbind=function(){var a,b,c,d,e;for(d=this.bindings,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.unbind());return e},d.prototype.sync=function(){var a,b,c,d,e;for(d=this.bindings,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.sync());return e},d.prototype.publish=function(){var a,b,c,d,e;for(d=this.select(function(a){return a.binder.publishes}),e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.publish());return e},d.prototype.update=function(a){var b,c,d,e,f,g,h;null==a&&(a={});for(c in a)d=a[c],this.models[c]=d;for(g=this.bindings,h=[],e=0,f=g.length;f>e;e++)b=g[e],h.push(b.update(a));return h},d}(),a.Binding=function(){function c(a,c,d,e,f){this.view=a,this.el=c,this.type=d,this.keypath=e,this.options=null!=f?f:{},this.update=b(this.update,this),this.unbind=b(this.unbind,this),this.bind=b(this.bind,this),this.publish=b(this.publish,this),this.sync=b(this.sync,this),this.set=b(this.set,this),this.eventHandler=b(this.eventHandler,this),this.formattedValue=b(this.formattedValue,this),this.setObserver=b(this.setObserver,this),this.setBinder=b(this.setBinder,this),this.formatters=this.options.formatters||[],this.dependencies=[],this.setBinder(),this.setObserver()}return c.prototype.setBinder=function(){var a,b,c,d;if(!(this.binder=this.view.binders[this.type])){d=this.view.binders;for(a in d)c=d[a],"*"!==a&&-1!==a.indexOf("*")&&(b=new RegExp("^"+a.replace("*",".+")+"$"),b.test(this.type)&&(this.binder=c,this.args=new RegExp("^"+a.replace("*","(.+)")+"$").exec(this.type),this.args.shift()))}return this.binder||(this.binder=this.view.binders["*"]),this.binder instanceof Function?this.binder={routine:this.binder}:void 0},c.prototype.setObserver=function(){var b=this;return this.observer=new a.KeypathObserver(this.view,this.view.models,this.keypath,function(a){return b.key&&b.unbind(!0),b.model=a.target,b.key&&b.bind(!0),b.sync()}),this.key=this.observer.key,this.model=this.observer.target},c.prototype.formattedValue=function(a){var b,c,e,f,g,h;for(h=this.formatters,f=0,g=h.length;g>f;f++)c=h[f],b=c.split(/\s+/),e=b.shift(),c=this.view.formatters[e],(null!=c?c.read:void 0)instanceof Function?a=c.read.apply(c,[a].concat(d.call(b))):c instanceof Function&&(a=c.apply(null,[a].concat(d.call(b))));return a},c.prototype.eventHandler=function(a){var b,c;return c=(b=this).view.config.handler,function(d){return c.call(a,this,d,b)}},c.prototype.set=function(a){var b;return a=a instanceof Function&&!this.binder["function"]?this.formattedValue(a.call(this.model)):this.formattedValue(a),null!=(b=this.binder.routine)?b.call(this,this.el,a):void 0},c.prototype.sync=function(){return this.set(this.key?this.view.adapters[this.key["interface"]].read(this.model,this.key.path):this.model)},c.prototype.publish=function(){var b,c,e,f,g,h,i,j,k;for(f=a.Util.getInputValue(this.el),i=this.formatters.slice(0).reverse(),g=0,h=i.length;h>g;g++)c=i[g],b=c.split(/\s+/),e=b.shift(),(null!=(j=this.view.formatters[e])?j.publish:void 0)&&(f=(k=this.view.formatters[e]).publish.apply(k,[f].concat(d.call(b))));return this.view.adapters[this.key["interface"]].publish(this.model,this.key.path,f)},c.prototype.bind=function(b){var c,d,e,f,g,h,i,j,k,l=this;if(null==b&&(b=!1),b||null!=(h=this.binder.bind)&&h.call(this,this.el),this.key&&this.view.adapters[this.key["interface"]].subscribe(this.model,this.key.path,this.sync),(b?void 0:this.view.config.preloadData)&&this.sync(),null!=(i=this.options.dependencies)?i.length:void 0){for(j=this.options.dependencies,k=[],f=0,g=j.length;g>f;f++)c=j[f],e=new a.KeypathObserver(this.view,this.model,c,function(a,b){var c;return c=a.key,l.view.adapters[c["interface"]].unsubscribe(b,c.path,l.sync),l.view.adapters[c["interface"]].subscribe(a.target,c.path,l.sync),l.sync()}),d=e.key,this.view.adapters[d["interface"]].subscribe(e.target,d.path,this.sync),k.push(this.dependencies.push(e));return k}},c.prototype.unbind=function(a){var b,c,d,e,f,g;if(null==a&&(a=!1),a||(null!=(f=this.binder.unbind)&&f.call(this,this.el),this.observer.unobserve()),this.key&&this.view.adapters[this.key["interface"]].unsubscribe(this.model,this.key.path,this.sync),this.dependencies.length){for(g=this.dependencies,d=0,e=g.length;e>d;d++)c=g[d],b=c.key,this.view.adapters[b["interface"]].unsubscribe(c.target,b.path,this.sync);return this.dependencies=[]}},c.prototype.update=function(a){var b;return null==a&&(a={}),null!=(b=this.binder.update)?b.call(this,a):void 0},c}(),a.ComponentBinding=function(d){function e(d,e,f){var g,h,i,j,k;for(this.view=d,this.el=e,this.type=f,this.unbind=b(this.unbind,this),this.bind=b(this.bind,this),this.update=b(this.update,this),this.locals=b(this.locals,this),this.component=a.components[this.type],this.attributes={},this.inflections={},j=this.el.attributes||[],h=0,i=j.length;i>h;h++)g=j[h],k=g.name,c.call(this.component.attributes,k)>=0?this.attributes[g.name]=g.value:this.inflections[g.name]=g.value}return f(e,d),e.prototype.sync=function(){},e.prototype.locals=function(a){var b,c,d,e,f,g,h,i,j;null==a&&(a=this.view.models),f={},i=this.inflections;for(c in i)for(b=i[c],j=b.split("."),g=0,h=j.length;h>g;g++)e=j[g],f[c]=(f[c]||a)[e];for(c in a)d=a[c],null==f[c]&&(f[c]=d);return f},e.prototype.update=function(a){var b;return null!=(b=this.componentView)?b.update(this.locals(a)):void 0},e.prototype.bind=function(){var b,c;return null!=this.componentView?null!=(c=this.componentView)?c.bind():void 0:(b=this.component.build.call(this.attributes),(this.componentView=new a.View(b,this.locals(),this.view.options)).bind(),this.el.parentNode.replaceChild(b,this.el))},e.prototype.unbind=function(){var a;return null!=(a=this.componentView)?a.unbind():void 0},e}(a.Binding),a.TextBinding=function(a){function c(a,c,d,e,f){this.view=a,this.el=c,this.type=d,this.keypath=e,this.options=null!=f?f:{},this.sync=b(this.sync,this),this.formatters=this.options.formatters||[],this.dependencies=[],this.setObserver()}return f(c,a),c.prototype.binder={routine:function(a,b){return a.data=null!=b?b:""}},c.prototype.sync=function(){return c.__super__.sync.apply(this,arguments)},c}(a.Binding),a.KeypathParser=function(){function a(){}return a.parse=function(a,b,d){var e,f,g,h,i;for(g=[],f={"interface":d,path:""},h=0,i=a.length;i>h;h++)e=a[h],c.call(b,e)>=0?(g.push(f),f={"interface":e,path:""}):f.path+=e;return g.push(f),g},a}(),a.TextTemplateParser=function(){function a(){}return a.types={text:0,binding:1},a.parse=function(a,b){var c,d,e,f,g,h,i;for(h=[],f=a.length,c=0,d=0;f>d;){if(c=a.indexOf(b[0],d),0>c){h.push({type:this.types.text,value:a.slice(d)});break}if(c>0&&c>d&&h.push({type:this.types.text,value:a.slice(d,c)}),d=c+b[0].length,c=a.indexOf(b[1],d),0>c){g=a.slice(d-b[1].length),e=h[h.length-1],(null!=e?e.type:void 0)===this.types.text?e.value+=g:h.push({type:this.types.text,value:g});break}i=a.slice(d,c).trim(),h.push({type:this.types.binding,value:i}),d=c+b[1].length}return h},a}(),a.KeypathObserver=function(){function d(a,c,d,e){this.view=a,this.model=c,this.keypath=d,this.callback=e,this.unobserve=b(this.unobserve,this),this.realize=b(this.realize,this),this.update=b(this.update,this),this.parse=b(this.parse,this),this.parse(),this.objectPath=[],this.target=this.realize()}return d.prototype.parse=function(){var b,d,e,f,g,h;return b=function(){var a,b;a=this.view.adapters,b=[];for(d in a)g=a[d],b.push(d);return b}.call(this),h=this.keypath[0],c.call(b,h)>=0?(f=this.keypath[0],e=this.keypath.substr(1)):(f=this.view.config.rootInterface,e=this.keypath),this.tokens=a.KeypathParser.parse(e,b,f),this.key=this.tokens.pop()},d.prototype.update=function(){var a,b;return(a=this.realize())!==this.target?(b=this.target,this.target=a,this.callback(this,b)):void 0},d.prototype.realize=function(){var a,b,c,d,e,f,g;for(a=this.model,g=this.tokens,b=e=0,f=g.length;f>e;b=++e)d=g[b],null!=this.objectPath[b]?a!==(c=this.objectPath[b])&&(this.view.adapters[d["interface"]].unsubscribe(c,d.path,this.update),this.view.adapters[d["interface"]].subscribe(a,d.path,this.update),this.objectPath[b]=a):(this.view.adapters[d["interface"]].subscribe(a,d.path,this.update),this.objectPath[b]=a),a=this.view.adapters[d["interface"]].read(a,d.path);return a},d.prototype.unobserve=function(){var a,b,c,d,e,f,g;for(f=this.tokens,g=[],a=d=0,e=f.length;e>d;a=++d)c=f[a],(b=this.objectPath[a])?g.push(this.view.adapters[c["interface"]].unsubscribe(b,c.path,this.update)):g.push(void 0);return g},d}(),a.binders.text=function(a,b){return null!=a.textContent?a.textContent=null!=b?b:"":a.innerText=null!=b?b:""},a.binders.html=function(a,b){return a.innerHTML=null!=b?b:""},a.binders.show=function(a,b){return a.style.display=b?"":"none"},a.binders.hide=function(a,b){return a.style.display=b?"none":""},a.binders.enabled=function(a,b){return a.disabled=!b},a.binders.disabled=function(a,b){return a.disabled=!!b},a.binders.checked={publishes:!0,bind:function(b){return a.Util.bindEvent(b,"change",this.publish)},unbind:function(b){return a.Util.unbindEvent(b,"change",this.publish)},routine:function(a,b){var c;return a.checked="radio"===a.type?(null!=(c=a.value)?c.toString():void 0)===(null!=b?b.toString():void 0):!!b}},a.binders.unchecked={publishes:!0,bind:function(b){return a.Util.bindEvent(b,"change",this.publish)},unbind:function(b){return a.Util.unbindEvent(b,"change",this.publish)},routine:function(a,b){var c;return a.checked="radio"===a.type?(null!=(c=a.value)?c.toString():void 0)!==(null!=b?b.toString():void 0):!b}},a.binders.value={publishes:!0,bind:function(b){return a.Util.bindEvent(b,"change",this.publish)},unbind:function(b){return a.Util.unbindEvent(b,"change",this.publish)},routine:function(a,b){var d,e,f,g,h,i,j;if(null!=window.jQuery){if(a=jQuery(a),(null!=b?b.toString():void 0)!==(null!=(g=a.val())?g.toString():void 0))return a.val(null!=b?b:"")}else if("select-multiple"===a.type){if(null!=b){for(j=[],e=0,f=a.length;f>e;e++)d=a[e],j.push(d.selected=(h=d.value,c.call(b,h)>=0));return j}}else if((null!=b?b.toString():void 0)!==(null!=(i=a.value)?i.toString():void 0))return a.value=null!=b?b:""}},a.binders["if"]={block:!0,bind:function(a){var b,c;return null==this.marker?(b=[this.view.config.prefix,this.type].join("-").replace("--","-"),c=a.getAttribute(b),this.marker=document.createComment(" rivets: "+this.type+" "+c+" "),a.removeAttribute(b),a.parentNode.insertBefore(this.marker,a),a.parentNode.removeChild(a)):void 0},unbind:function(){var a;return null!=(a=this.nested)?a.unbind():void 0},routine:function(b,c){var d,e,f,g,h;if(!!c==(null==this.nested)){if(c){f={},h=this.view.models;for(d in h)e=h[d],f[d]=e;return g={binders:this.view.options.binders,formatters:this.view.options.formatters,adapters:this.view.options.adapters,config:this.view.options.config},(this.nested=new a.View(b,f,g)).bind(),this.marker.parentNode.insertBefore(b,this.marker.nextSibling)}return b.parentNode.removeChild(b),this.nested.unbind(),delete this.nested}},update:function(a){var b;return null!=(b=this.nested)?b.update(a):void 0}},a.binders.unless={block:!0,bind:function(b){return a.binders["if"].bind.call(this,b)},unbind:function(){return a.binders["if"].unbind.call(this)},routine:function(b,c){return a.binders["if"].routine.call(this,b,!c)},update:function(b){return a.binders["if"].update.call(this,b)}},a.binders["on-*"]={"function":!0,unbind:function(b){return this.handler?a.Util.unbindEvent(b,this.args[0],this.handler):void 0},routine:function(b,c){return this.handler&&a.Util.unbindEvent(b,this.args[0],this.handler),a.Util.bindEvent(b,this.args[0],this.handler=this.eventHandler(c))}},a.binders["each-*"]={block:!0,bind:function(a){var b;return null==this.marker?(b=[this.view.config.prefix,this.type].join("-").replace("--","-"),this.marker=document.createComment(" rivets: "+this.type+" "),this.iterated=[],a.removeAttribute(b),a.parentNode.insertBefore(this.marker,a),a.parentNode.removeChild(a)):void 0},unbind:function(){var a,b,c,d,e;if(null!=this.iterated){for(d=this.iterated,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.unbind());return e}},routine:function(b,c){var d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A;if(k=this.args[0],c=c||[],this.iterated.length>c.length)for(w=Array(this.iterated.length-c.length),q=0,t=w.length;t>q;q++)f=w[q],p=this.iterated.pop(),p.unbind(),this.marker.parentNode.removeChild(p.els[0]);for(g=r=0,u=c.length;u>r;g=++r)if(j=c[g],e={},e[k]=j,null==this.iterated[g]){x=this.view.models;for(i in x)j=x[i],null==e[i]&&(e[i]=j);m=this.iterated.length?this.iterated[this.iterated.length-1].els[0]:this.marker,l={binders:this.view.options.binders,formatters:this.view.options.formatters,adapters:this.view.options.adapters,config:{}},y=this.view.options.config;for(h in y)o=y[h],l.config[h]=o;l.config.preloadData=!0,n=b.cloneNode(!0),p=new a.View(n,e,l),p.bind(),this.iterated.push(p),this.marker.parentNode.insertBefore(n,m.nextSibling)}else this.iterated[g].models[k]!==j&&this.iterated[g].update(e);if("OPTION"===b.nodeName){for(z=this.view.bindings,A=[],s=0,v=z.length;v>s;s++)d=z[s],d.el===this.marker.parentNode&&"value"===d.type?A.push(d.sync()):A.push(void 0);return A}},update:function(a){var b,c,d,e,f,g,h,i;b={};for(c in a)d=a[c],c!==this.args[0]&&(b[c]=d);for(h=this.iterated,i=[],f=0,g=h.length;g>f;f++)e=h[f],i.push(e.update(b));return i}},a.binders["class-*"]=function(a,b){var c;return c=" "+a.className+" ",!b==(-1!==c.indexOf(" "+this.args[0]+" "))?a.className=b?""+a.className+" "+this.args[0]:c.replace(" "+this.args[0]+" "," ").trim():void 0},a.binders["*"]=function(a,b){return b?a.setAttribute(this.type,b):a.removeAttribute(this.type)},a.adapters["."]={id:"_rv",counter:0,weakmap:{},weakReference:function(a){var b;return null==a[this.id]&&(b=this.counter++,this.weakmap[b]={callbacks:{}},Object.defineProperty(a,this.id,{value:b})),this.weakmap[a[this.id]]},stubFunction:function(a,b){var c,d,e;return d=a[b],c=this.weakReference(a),e=this.weakmap,a[b]=function(){var b,f,g,h,i,j,k,l,m,n;h=d.apply(a,arguments),k=c.pointers;for(g in k)for(f=k[g],n=null!=(l=null!=(m=e[g])?m.callbacks[f]:void 0)?l:[],i=0,j=n.length;j>i;i++)b=n[i],b();return h}},observeMutations:function(a,b,d){var e,f,g,h,i,j;if(Array.isArray(a)){if(g=this.weakReference(a),null==g.pointers)for(g.pointers={},f=["push","pop","shift","unshift","sort","reverse","splice"],i=0,j=f.length;j>i;i++)e=f[i],this.stubFunction(a,e);if(null==(h=g.pointers)[b]&&(h[b]=[]),c.call(g.pointers[b],d)<0)return g.pointers[b].push(d)}},unobserveMutations:function(a,b,c){var d,e;return Array.isArray(a&&null!=a[this.id])&&(d=null!=(e=this.weakReference(a).pointers)?e[b]:void 0)?d.splice(d.indexOf(c),1):void 0},subscribe:function(a,b,d){var e,f,g=this;return e=this.weakReference(a).callbacks,null==e[b]&&(e[b]=[],f=a[b],Object.defineProperty(a,b,{get:function(){return f},set:function(c){var h,i,j;if(c!==f){for(f=c,j=e[b],h=0,i=j.length;i>h;h++)d=j[h],d();return g.observeMutations(c,a[g.id],b)}}})),c.call(e[b],d)<0&&e[b].push(d),this.observeMutations(a[b],a[this.id],b)},unsubscribe:function(a,b,c){var d;return d=this.weakmap[a[this.id]].callbacks[b],d.splice(d.indexOf(c),1),this.unobserveMutations(a[b],a[this.id],b)},read:function(a,b){return a[b]},publish:function(a,b,c){return a[b]=c}},a.factory=function(b){return b._=a,b.binders=a.binders,b.components=a.components,b.formatters=a.formatters,b.adapters=a.adapters,b.config=a.config,b.configure=function(b){var c,d;null==b&&(b={});for(c in b)d=b[c],a.config[c]=d},b.bind=function(b,c,d){var e;return null==c&&(c={}),null==d&&(d={}),e=new a.View(b,c,d),e.bind(),e}},"object"==typeof exports?a.factory(exports):"function"==typeof define&&define.amd?define(["exports"],function(b){return a.factory(this.rivets=b),b}):a.factory(this.rivets={})}).call(this); \ No newline at end of file diff --git a/ajax/libs/rivets/0.6.6/rivets.js b/ajax/libs/rivets/0.6.6/rivets.js new file mode 100644 index 000000000..1ad48a64e --- /dev/null +++ b/ajax/libs/rivets/0.6.6/rivets.js @@ -0,0 +1,1289 @@ +// Rivets.js +// version: 0.6.6 +// author: Michael Richards +// license: MIT +(function() { + var Rivets, bindMethod, unbindMethod, _ref, + __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, + __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; }, + __slice = [].slice, + __hasProp = {}.hasOwnProperty, + __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; + + Rivets = { + binders: {}, + components: {}, + formatters: {}, + adapters: {}, + config: { + prefix: 'rv', + templateDelimiters: ['{', '}'], + rootInterface: '.', + preloadData: true, + handler: function(context, ev, binding) { + return this.call(context, ev, binding.view.models); + } + } + }; + + if ('jQuery' in window) { + _ref = 'on' in jQuery ? ['on', 'off'] : ['bind', 'unbind'], bindMethod = _ref[0], unbindMethod = _ref[1]; + Rivets.Util = { + bindEvent: function(el, event, handler) { + return jQuery(el)[bindMethod](event, handler); + }, + unbindEvent: function(el, event, handler) { + return jQuery(el)[unbindMethod](event, handler); + }, + getInputValue: function(el) { + var $el; + $el = jQuery(el); + if ($el.attr('type') === 'checkbox') { + return $el.is(':checked'); + } else { + return $el.val(); + } + } + }; + } else { + Rivets.Util = { + bindEvent: (function() { + if ('addEventListener' in window) { + return function(el, event, handler) { + return el.addEventListener(event, handler, false); + }; + } + return function(el, event, handler) { + return el.attachEvent('on' + event, handler); + }; + })(), + unbindEvent: (function() { + if ('removeEventListener' in window) { + return function(el, event, handler) { + return el.removeEventListener(event, handler, false); + }; + } + return function(el, event, handler) { + return el.detachEvent('on' + event, handler); + }; + })(), + getInputValue: function(el) { + var o, _i, _len, _results; + if (el.type === 'checkbox') { + return el.checked; + } else if (el.type === 'select-multiple') { + _results = []; + for (_i = 0, _len = el.length; _i < _len; _i++) { + o = el[_i]; + if (o.selected) { + _results.push(o.value); + } + } + return _results; + } else { + return el.value; + } + } + }; + } + + Rivets.View = (function() { + function View(els, models, options) { + var k, option, v, _base, _i, _len, _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.componentRegExp = __bind(this.componentRegExp, this); + this.bindingRegExp = __bind(this.bindingRegExp, this); + if (!(this.els.jquery || this.els instanceof Array)) { + this.els = [this.els]; + } + _ref1 = ['config', 'binders', 'formatters', 'adapters']; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + option = _ref1[_i]; + this[option] = {}; + if (this.options[option]) { + _ref2 = this.options[option]; + for (k in _ref2) { + v = _ref2[k]; + this[option][k] = v; + } + } + _ref3 = Rivets[option]; + for (k in _ref3) { + v = _ref3[k]; + if ((_base = this[option])[k] == null) { + _base[k] = v; + } + } + } + this.build(); + } + + View.prototype.bindingRegExp = function() { + return new RegExp("^" + this.config.prefix + "-"); + }; + + View.prototype.componentRegExp = function() { + return new RegExp("^" + (this.config.prefix.toUpperCase()) + "-"); + }; + + View.prototype.build = function() { + var bindingRegExp, buildBinding, componentRegExp, el, parse, skipNodes, _i, _len, _ref1, + _this = this; + this.bindings = []; + skipNodes = []; + bindingRegExp = this.bindingRegExp(); + componentRegExp = this.componentRegExp(); + buildBinding = function(binding, node, type, declaration) { + var context, ctx, dependencies, keypath, options, pipe, pipes; + options = {}; + pipes = (function() { + var _i, _len, _ref1, _results; + _ref1 = declaration.split('|'); + _results = []; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + pipe = _ref1[_i]; + _results.push(pipe.trim()); + } + return _results; + })(); + context = (function() { + var _i, _len, _ref1, _results; + _ref1 = pipes.shift().split('<'); + _results = []; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + ctx = _ref1[_i]; + _results.push(ctx.trim()); + } + return _results; + })(); + keypath = context.shift(); + options.formatters = pipes; + if (dependencies = context.shift()) { + options.dependencies = dependencies.split(/\s+/); + } + return _this.bindings.push(new Rivets[binding](_this, node, type, keypath, options)); + }; + parse = function(node) { + var attribute, attributes, binder, childNode, delimiters, identifier, n, parser, regexp, text, token, tokens, type, value, _i, _j, _k, _l, _len, _len1, _len2, _len3, _len4, _m, _ref1, _ref2, _ref3, _ref4, _ref5, _results; + if (__indexOf.call(skipNodes, node) < 0) { + if (node.nodeType === 3) { + 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)) { + for (_i = 0, _len = tokens.length; _i < _len; _i++) { + token = tokens[_i]; + text = document.createTextNode(token.value); + node.parentNode.insertBefore(text, node); + if (token.type === 1) { + buildBinding('TextBinding', text, null, token.value); + } + } + node.parentNode.removeChild(node); + } + } + } + } else if (componentRegExp.test(node.tagName)) { + type = node.tagName.replace(componentRegExp, '').toLowerCase(); + _this.bindings.push(new Rivets.ComponentBinding(_this, node, type)); + } else if (node.attributes != null) { + _ref1 = node.attributes; + for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { + attribute = _ref1[_j]; + if (bindingRegExp.test(attribute.name)) { + type = attribute.name.replace(bindingRegExp, ''); + if (!(binder = _this.binders[type])) { + _ref2 = _this.binders; + for (identifier in _ref2) { + value = _ref2[identifier]; + if (identifier !== '*' && identifier.indexOf('*') !== -1) { + regexp = new RegExp("^" + (identifier.replace('*', '.+')) + "$"); + if (regexp.test(type)) { + binder = value; + } + } + } + } + binder || (binder = _this.binders['*']); + if (binder.block) { + _ref3 = node.childNodes; + for (_k = 0, _len2 = _ref3.length; _k < _len2; _k++) { + n = _ref3[_k]; + skipNodes.push(n); + } + attributes = [attribute]; + } + } + } + _ref4 = attributes || node.attributes; + for (_l = 0, _len3 = _ref4.length; _l < _len3; _l++) { + attribute = _ref4[_l]; + if (bindingRegExp.test(attribute.name)) { + type = attribute.name.replace(bindingRegExp, ''); + buildBinding('Binding', node, type, attribute.value); + } + } + } + _ref5 = (function() { + var _len4, _n, _ref5, _results1; + _ref5 = node.childNodes; + _results1 = []; + for (_n = 0, _len4 = _ref5.length; _n < _len4; _n++) { + n = _ref5[_n]; + _results1.push(n); + } + return _results1; + })(); + _results = []; + for (_m = 0, _len4 = _ref5.length; _m < _len4; _m++) { + childNode = _ref5[_m]; + _results.push(parse(childNode)); + } + return _results; + } + }; + _ref1 = this.els; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + el = _ref1[_i]; + parse(el); + } + }; + + View.prototype.select = function(fn) { + var binding, _i, _len, _ref1, _results; + _ref1 = this.bindings; + _results = []; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + binding = _ref1[_i]; + if (fn(binding)) { + _results.push(binding); + } + } + return _results; + }; + + View.prototype.bind = function() { + var binding, _i, _len, _ref1, _results; + _ref1 = this.bindings; + _results = []; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + binding = _ref1[_i]; + _results.push(binding.bind()); + } + return _results; + }; + + View.prototype.unbind = function() { + var binding, _i, _len, _ref1, _results; + _ref1 = this.bindings; + _results = []; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + binding = _ref1[_i]; + _results.push(binding.unbind()); + } + return _results; + }; + + View.prototype.sync = function() { + var binding, _i, _len, _ref1, _results; + _ref1 = this.bindings; + _results = []; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + binding = _ref1[_i]; + _results.push(binding.sync()); + } + return _results; + }; + + View.prototype.publish = function() { + var binding, _i, _len, _ref1, _results; + _ref1 = this.select(function(b) { + return b.binder.publishes; + }); + _results = []; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + binding = _ref1[_i]; + _results.push(binding.publish()); + } + return _results; + }; + + View.prototype.update = function(models) { + var binding, key, model, _i, _len, _ref1, _results; + if (models == null) { + models = {}; + } + for (key in models) { + model = models[key]; + this.models[key] = model; + } + _ref1 = this.bindings; + _results = []; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + binding = _ref1[_i]; + _results.push(binding.update(models)); + } + return _results; + }; + + return View; + + })(); + + Rivets.Binding = (function() { + function Binding(view, el, type, keypath, options) { + this.view = view; + this.el = el; + this.type = type; + 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); + this.setBinder = __bind(this.setBinder, this); + this.formatters = this.options.formatters || []; + this.dependencies = []; + this.model = void 0; + this.setBinder(); + } + + Binding.prototype.setBinder = function() { + var identifier, regexp, value, _ref1; + if (!(this.binder = this.view.binders[this.type])) { + _ref1 = this.view.binders; + for (identifier in _ref1) { + value = _ref1[identifier]; + if (identifier !== '*' && identifier.indexOf('*') !== -1) { + regexp = new RegExp("^" + (identifier.replace('*', '.+')) + "$"); + if (regexp.test(this.type)) { + this.binder = value; + this.args = new RegExp("^" + (identifier.replace('*', '(.+)')) + "$").exec(this.type); + this.args.shift(); + } + } + } + } + this.binder || (this.binder = this.view.binders['*']); + if (this.binder instanceof Function) { + return this.binder = { + routine: this.binder + }; + } + }; + + Binding.prototype.formattedValue = function(value) { + var args, formatter, id, _i, _len, _ref1; + _ref1 = this.formatters; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + formatter = _ref1[_i]; + args = formatter.split(/\s+/); + id = args.shift(); + formatter = 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 _ref1; + value = value instanceof Function && !this.binder["function"] ? this.formattedValue(value.call(this.model)) : this.formattedValue(value); + return (_ref1 = this.binder.routine) != null ? _ref1.call(this, this.el, value) : void 0; + }; + + Binding.prototype.sync = function() { + var dependency, observer, _i, _j, _len, _len1, _ref1, _ref2, _ref3; + if (this.model !== this.observer.target) { + _ref1 = this.dependencies; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + observer = _ref1[_i]; + observer.unobserve(); + } + this.dependencies = []; + if (((this.model = this.observer.target) != null) && ((_ref2 = this.options.dependencies) != null ? _ref2.length : void 0)) { + _ref3 = this.options.dependencies; + for (_j = 0, _len1 = _ref3.length; _j < _len1; _j++) { + dependency = _ref3[_j]; + observer = new Rivets.Observer(this.view, this.model, dependency, this.sync); + this.dependencies.push(observer); + } + } + } + return this.set(this.observer.value()); + }; + + Binding.prototype.publish = function() { + var args, formatter, id, value, _i, _len, _ref1, _ref2, _ref3; + value = Rivets.Util.getInputValue(this.el); + _ref1 = this.formatters.slice(0).reverse(); + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + formatter = _ref1[_i]; + args = formatter.split(/\s+/); + id = args.shift(); + if ((_ref2 = this.view.formatters[id]) != null ? _ref2.publish : void 0) { + value = (_ref3 = this.view.formatters[id]).publish.apply(_ref3, [value].concat(__slice.call(args))); + } + } + return this.observer.publish(value); + }; + + Binding.prototype.bind = function() { + var dependency, observer, _i, _len, _ref1, _ref2, _ref3; + if ((_ref1 = this.binder.bind) != null) { + _ref1.call(this, this.el); + } + this.observer = new Rivets.Observer(this.view, this.view.models, this.keypath, this.sync); + this.model = this.observer.target; + if ((this.model != null) && ((_ref2 = this.options.dependencies) != null ? _ref2.length : void 0)) { + _ref3 = this.options.dependencies; + for (_i = 0, _len = _ref3.length; _i < _len; _i++) { + dependency = _ref3[_i]; + observer = new Rivets.Observer(this.view, this.model, dependency, this.sync); + this.dependencies.push(observer); + } + } + if (this.view.config.preloadData) { + return this.sync(); + } + }; + + Binding.prototype.unbind = function() { + var observer, _i, _len, _ref1, _ref2; + if ((_ref1 = this.binder.unbind) != null) { + _ref1.call(this, this.el); + } + this.observer.unobserve(); + _ref2 = this.dependencies; + for (_i = 0, _len = _ref2.length; _i < _len; _i++) { + observer = _ref2[_i]; + observer.unobserve(); + } + return this.dependencies = []; + }; + + Binding.prototype.update = function(models) { + var _ref1; + if (models == null) { + models = {}; + } + return (_ref1 = this.binder.update) != null ? _ref1.call(this, models) : void 0; + }; + + return Binding; + + })(); + + Rivets.ComponentBinding = (function(_super) { + __extends(ComponentBinding, _super); + + function ComponentBinding(view, el, type) { + var attribute, _i, _len, _ref1, _ref2; + this.view = view; + this.el = el; + this.type = type; + this.unbind = __bind(this.unbind, this); + this.bind = __bind(this.bind, this); + this.update = __bind(this.update, this); + this.locals = __bind(this.locals, this); + this.component = Rivets.components[this.type]; + this.attributes = {}; + this.inflections = {}; + _ref1 = this.el.attributes || []; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + attribute = _ref1[_i]; + if (_ref2 = attribute.name, __indexOf.call(this.component.attributes, _ref2) >= 0) { + this.attributes[attribute.name] = attribute.value; + } else { + this.inflections[attribute.name] = attribute.value; + } + } + } + + ComponentBinding.prototype.sync = function() {}; + + ComponentBinding.prototype.locals = function(models) { + var inverse, key, model, path, result, _i, _len, _ref1, _ref2; + if (models == null) { + models = this.view.models; + } + result = {}; + _ref1 = this.inflections; + for (key in _ref1) { + inverse = _ref1[key]; + _ref2 = inverse.split('.'); + for (_i = 0, _len = _ref2.length; _i < _len; _i++) { + path = _ref2[_i]; + result[key] = (result[key] || models)[path]; + } + } + for (key in models) { + model = models[key]; + if (result[key] == null) { + result[key] = model; + } + } + return result; + }; + + ComponentBinding.prototype.update = function(models) { + var _ref1; + return (_ref1 = this.componentView) != null ? _ref1.update(this.locals(models)) : void 0; + }; + + ComponentBinding.prototype.bind = function() { + var el, _ref1; + if (this.componentView != null) { + return (_ref1 = this.componentView) != null ? _ref1.bind() : void 0; + } else { + el = this.component.build.call(this.attributes); + (this.componentView = new Rivets.View(el, this.locals(), this.view.options)).bind(); + return this.el.parentNode.replaceChild(el, this.el); + } + }; + + ComponentBinding.prototype.unbind = function() { + var _ref1; + return (_ref1 = this.componentView) != null ? _ref1.unbind() : void 0; + }; + + return ComponentBinding; + + })(Rivets.Binding); + + Rivets.TextBinding = (function(_super) { + __extends(TextBinding, _super); + + function TextBinding(view, el, type, keypath, options) { + this.view = view; + this.el = el; + this.type = type; + this.keypath = keypath; + this.options = options != null ? options : {}; + this.sync = __bind(this.sync, this); + this.formatters = this.options.formatters || []; + this.dependencies = []; + } + + TextBinding.prototype.binder = { + routine: function(node, value) { + return node.data = value != null ? value : ''; + } + }; + + TextBinding.prototype.sync = function() { + return TextBinding.__super__.sync.apply(this, arguments); + }; + + return TextBinding; + + })(Rivets.Binding); + + Rivets.KeypathParser = (function() { + function KeypathParser() {} + + KeypathParser.parse = function(keypath, interfaces, root) { + var char, current, index, tokens, _i, _ref1; + tokens = []; + current = { + "interface": root, + path: '' + }; + for (index = _i = 0, _ref1 = keypath.length; _i < _ref1; index = _i += 1) { + char = keypath.charAt(index); + if (__indexOf.call(interfaces, char) >= 0) { + tokens.push(current); + current = { + "interface": char, + path: '' + }; + } else { + current.path += char; + } + } + tokens.push(current); + return tokens; + }; + + return KeypathParser; + + })(); + + 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 + delimiters[0].length; + index = template.indexOf(delimiters[1], lastIndex); + if (index < 0) { + substring = template.slice(lastIndex - delimiters[1].length); + 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 + delimiters[1].length; + } + } + return tokens; + }; + + return TextTemplateParser; + + })(); + + Rivets.Observer = (function() { + function Observer(view, model, keypath, callback) { + this.view = view; + this.model = model; + this.keypath = keypath; + this.callback = callback; + this.unobserve = __bind(this.unobserve, this); + this.realize = __bind(this.realize, this); + this.value = __bind(this.value, this); + this.publish = __bind(this.publish, this); + this.read = __bind(this.read, this); + this.set = __bind(this.set, this); + this.adapter = __bind(this.adapter, this); + this.update = __bind(this.update, this); + this.initialize = __bind(this.initialize, this); + this.parse = __bind(this.parse, this); + this.parse(); + this.initialize(); + } + + Observer.prototype.parse = function() { + var interfaces, k, path, root, v, _ref1; + interfaces = (function() { + var _ref1, _results; + _ref1 = this.view.adapters; + _results = []; + for (k in _ref1) { + v = _ref1[k]; + _results.push(k); + } + return _results; + }).call(this); + if (_ref1 = this.keypath[0], __indexOf.call(interfaces, _ref1) >= 0) { + root = this.keypath[0]; + path = this.keypath.substr(1); + } else { + root = this.view.config.rootInterface; + path = this.keypath; + } + this.tokens = Rivets.KeypathParser.parse(path, interfaces, root); + return this.key = this.tokens.pop(); + }; + + Observer.prototype.initialize = function() { + this.objectPath = []; + this.target = this.realize(); + if (this.target != null) { + return this.set(true, this.key, this.target, this.callback); + } + }; + + Observer.prototype.update = function() { + var next, oldValue; + if ((next = this.realize()) !== this.target) { + if (this.target != null) { + this.set(false, this.key, this.target, this.callback); + } + if (next != null) { + this.set(true, this.key, next, this.callback); + } + oldValue = this.value(); + this.target = next; + if (this.value() !== oldValue) { + return this.callback(); + } + } + }; + + Observer.prototype.adapter = function(key) { + return this.view.adapters[key["interface"]]; + }; + + Observer.prototype.set = function(active, key, obj, callback) { + var action; + action = active ? 'subscribe' : 'unsubscribe'; + return this.adapter(key)[action](obj, key.path, callback); + }; + + Observer.prototype.read = function(key, obj) { + return this.adapter(key).read(obj, key.path); + }; + + Observer.prototype.publish = function(value) { + if (this.target != null) { + return this.adapter(this.key).publish(this.target, this.key.path, value); + } + }; + + Observer.prototype.value = function() { + if (this.target != null) { + return this.read(this.key, this.target); + } + }; + + Observer.prototype.realize = function() { + var current, index, prev, token, unreached, _i, _len, _ref1; + current = this.model; + unreached = null; + _ref1 = this.tokens; + for (index = _i = 0, _len = _ref1.length; _i < _len; index = ++_i) { + token = _ref1[index]; + if (current != null) { + if (this.objectPath[index] != null) { + if (current !== (prev = this.objectPath[index])) { + this.set(false, token, prev, this.update); + this.set(true, token, current, this.update); + this.objectPath[index] = current; + } + } else { + this.set(true, token, current, this.update); + this.objectPath[index] = current; + } + current = this.read(token, current); + } else { + if (unreached == null) { + unreached = index; + } + if (prev = this.objectPath[index]) { + this.set(false, token, prev, this.update); + } + } + } + if (unreached != null) { + this.objectPath.splice(unreached); + } + return current; + }; + + Observer.prototype.unobserve = function() { + var index, obj, token, _i, _len, _ref1, _results; + _ref1 = this.tokens; + _results = []; + for (index = _i = 0, _len = _ref1.length; _i < _len; index = ++_i) { + token = _ref1[index]; + if (obj = this.objectPath[index]) { + _results.push(this.set(false, token, obj, this.update)); + } else { + _results.push(void 0); + } + } + return _results; + }; + + return Observer; + + })(); + + Rivets.binders.text = function(el, value) { + if (el.textContent != null) { + return el.textContent = value != null ? value : ''; + } else { + return el.innerText = value != null ? value : ''; + } + }; + + Rivets.binders.html = function(el, value) { + return el.innerHTML = value != null ? value : ''; + }; + + Rivets.binders.show = function(el, value) { + return el.style.display = value ? '' : 'none'; + }; + + Rivets.binders.hide = function(el, value) { + return el.style.display = value ? 'none' : ''; + }; + + Rivets.binders.enabled = function(el, value) { + return el.disabled = !value; + }; + + Rivets.binders.disabled = function(el, value) { + return el.disabled = !!value; + }; + + Rivets.binders.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 _ref1; + if (el.type === 'radio') { + return el.checked = ((_ref1 = el.value) != null ? _ref1.toString() : void 0) === (value != null ? value.toString() : void 0); + } else { + return el.checked = !!value; + } + } + }; + + Rivets.binders.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 _ref1; + if (el.type === 'radio') { + return el.checked = ((_ref1 = el.value) != null ? _ref1.toString() : void 0) !== (value != null ? value.toString() : void 0); + } else { + return el.checked = !value; + } + } + }; + + Rivets.binders.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, _ref1, _ref2, _ref3, _results; + if (window.jQuery != null) { + el = jQuery(el); + if ((value != null ? value.toString() : void 0) !== ((_ref1 = el.val()) != null ? _ref1.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 = (_ref2 = o.value, __indexOf.call(value, _ref2) >= 0)); + } + return _results; + } + } else if ((value != null ? value.toString() : void 0) !== ((_ref3 = el.value) != null ? _ref3.toString() : void 0)) { + return el.value = value != null ? value : ''; + } + } + } + }; + + Rivets.binders["if"] = { + block: true, + bind: function(el) { + var attr, declaration; + if (this.marker == null) { + attr = [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 _ref1; + return (_ref1 = this.nested) != null ? _ref1.unbind() : void 0; + }, + routine: function(el, value) { + var key, model, models, options, _ref1; + if (!!value === (this.nested == null)) { + if (value) { + models = {}; + _ref1 = this.view.models; + for (key in _ref1) { + model = _ref1[key]; + models[key] = model; + } + options = { + binders: this.view.options.binders, + formatters: this.view.options.formatters, + adapters: this.view.options.adapters, + 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) { + var _ref1; + return (_ref1 = this.nested) != null ? _ref1.update(models) : void 0; + } + }; + + Rivets.binders.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); + } + }; + + Rivets.binders['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)); + } + }; + + Rivets.binders['each-*'] = { + block: true, + bind: function(el) { + var attr; + if (this.marker == null) { + attr = [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, _ref1, _results; + if (this.iterated != null) { + _ref1 = this.iterated; + _results = []; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + view = _ref1[_i]; + _results.push(view.unbind()); + } + return _results; + } + }, + routine: function(el, collection) { + var binding, data, i, index, k, key, model, modelName, options, previous, template, v, view, _i, _j, _k, _len, _len1, _len2, _ref1, _ref2, _ref3, _ref4, _results; + modelName = this.args[0]; + collection = collection || []; + if (this.iterated.length > collection.length) { + _ref1 = Array(this.iterated.length - collection.length); + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + i = _ref1[_i]; + view = this.iterated.pop(); + view.unbind(); + this.marker.parentNode.removeChild(view.els[0]); + } + } + for (index = _j = 0, _len1 = collection.length; _j < _len1; index = ++_j) { + model = collection[index]; + data = {}; + data[modelName] = model; + if (this.iterated[index] == null) { + _ref2 = this.view.models; + for (key in _ref2) { + model = _ref2[key]; + if (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, + adapters: this.view.options.adapters, + 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); + this.marker.parentNode.insertBefore(template, previous.nextSibling); + } else if (this.iterated[index].models[modelName] !== model) { + this.iterated[index].update(data); + } + } + if (el.nodeName === 'OPTION') { + _ref4 = this.view.bindings; + _results = []; + for (_k = 0, _len2 = _ref4.length; _k < _len2; _k++) { + binding = _ref4[_k]; + if (binding.el === this.marker.parentNode && binding.type === 'value') { + _results.push(binding.sync()); + } else { + _results.push(void 0); + } + } + return _results; + } + }, + update: function(models) { + var data, key, model, view, _i, _len, _ref1, _results; + data = {}; + for (key in models) { + model = models[key]; + if (key !== this.args[0]) { + data[key] = model; + } + } + _ref1 = this.iterated; + _results = []; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + view = _ref1[_i]; + _results.push(view.update(data)); + } + return _results; + } + }; + + Rivets.binders['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(); + } + }; + + Rivets.binders['*'] = function(el, value) { + if (value) { + return el.setAttribute(this.type, value); + } else { + return el.removeAttribute(this.type); + } + }; + + Rivets.adapters['.'] = { + id: '_rv', + counter: 0, + weakmap: {}, + weakReference: function(obj) { + var id; + if (obj[this.id] == null) { + id = this.counter++; + this.weakmap[id] = { + callbacks: {} + }; + Object.defineProperty(obj, this.id, { + value: id + }); + } + return this.weakmap[obj[this.id]]; + }, + stubFunction: function(obj, fn) { + var map, original, weakmap; + original = obj[fn]; + map = this.weakReference(obj); + weakmap = this.weakmap; + return obj[fn] = function() { + var callback, k, r, response, _i, _len, _ref1, _ref2, _ref3, _ref4; + response = original.apply(obj, arguments); + _ref1 = map.pointers; + for (r in _ref1) { + k = _ref1[r]; + _ref4 = (_ref2 = (_ref3 = weakmap[r]) != null ? _ref3.callbacks[k] : void 0) != null ? _ref2 : []; + for (_i = 0, _len = _ref4.length; _i < _len; _i++) { + callback = _ref4[_i]; + callback(); + } + } + return response; + }; + }, + observeMutations: function(obj, ref, keypath) { + var fn, functions, map, _base, _i, _len; + if (Array.isArray(obj)) { + map = this.weakReference(obj); + if (map.pointers == null) { + map.pointers = {}; + functions = ['push', 'pop', 'shift', 'unshift', 'sort', 'reverse', 'splice']; + for (_i = 0, _len = functions.length; _i < _len; _i++) { + fn = functions[_i]; + this.stubFunction(obj, fn); + } + } + if ((_base = map.pointers)[ref] == null) { + _base[ref] = []; + } + if (__indexOf.call(map.pointers[ref], keypath) < 0) { + return map.pointers[ref].push(keypath); + } + } + }, + unobserveMutations: function(obj, ref, keypath) { + var keypaths, _ref1; + if (Array.isArray(obj && (obj[this.id] != null))) { + if (keypaths = (_ref1 = this.weakReference(obj).pointers) != null ? _ref1[ref] : void 0) { + return keypaths.splice(keypaths.indexOf(keypath), 1); + } + } + }, + subscribe: function(obj, keypath, callback) { + var callbacks, value, + _this = this; + callbacks = this.weakReference(obj).callbacks; + if (callbacks[keypath] == null) { + callbacks[keypath] = []; + value = obj[keypath]; + Object.defineProperty(obj, keypath, { + get: function() { + return value; + }, + set: function(newValue) { + var _i, _len, _ref1; + if (newValue !== value) { + value = newValue; + _ref1 = callbacks[keypath]; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + callback = _ref1[_i]; + callback(); + } + return _this.observeMutations(newValue, obj[_this.id], keypath); + } + } + }); + } + if (__indexOf.call(callbacks[keypath], callback) < 0) { + callbacks[keypath].push(callback); + } + return this.observeMutations(obj[keypath], obj[this.id], keypath); + }, + unsubscribe: function(obj, keypath, callback) { + var callbacks; + callbacks = this.weakmap[obj[this.id]].callbacks[keypath]; + callbacks.splice(callbacks.indexOf(callback), 1); + return this.unobserveMutations(obj[keypath], obj[this.id], keypath); + }, + read: function(obj, keypath) { + return obj[keypath]; + }, + publish: function(obj, keypath, value) { + return obj[keypath] = value; + } + }; + + Rivets.factory = function(exports) { + exports._ = Rivets; + exports.binders = Rivets.binders; + exports.components = Rivets.components; + exports.formatters = Rivets.formatters; + exports.adapters = Rivets.adapters; + 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); diff --git a/ajax/libs/rivets/0.6.6/rivets.min.js b/ajax/libs/rivets/0.6.6/rivets.min.js new file mode 100644 index 000000000..cd74be5e2 --- /dev/null +++ b/ajax/libs/rivets/0.6.6/rivets.min.js @@ -0,0 +1,5 @@ +// Rivets.js +// version: 0.6.6 +// author: Michael Richards +// license: MIT +(function(){var a,b,c,d,e=function(a,b){return function(){return a.apply(b,arguments)}},f=[].indexOf||function(a){for(var b=0,c=this.length;c>b;b++)if(b in this&&this[b]===a)return b;return-1},g=[].slice,h={}.hasOwnProperty,i=function(a,b){function c(){this.constructor=a}for(var d in b)h.call(b,d)&&(a[d]=b[d]);return c.prototype=b.prototype,a.prototype=new c,a.__super__=b.prototype,a};a={binders:{},components:{},formatters:{},adapters:{},config:{prefix:"rv",templateDelimiters:["{","}"],rootInterface:".",preloadData:!0,handler:function(a,b,c){return this.call(a,b,c.view.models)}}},"jQuery"in window?(d="on"in jQuery?["on","off"]:["bind","unbind"],b=d[0],c=d[1],a.Util={bindEvent:function(a,c,d){return jQuery(a)[b](c,d)},unbindEvent:function(a,b,d){return jQuery(a)[c](b,d)},getInputValue:function(a){var b;return b=jQuery(a),"checkbox"===b.attr("type")?b.is(":checked"):b.val()}}):a.Util={bindEvent:function(){return"addEventListener"in window?function(a,b,c){return a.addEventListener(b,c,!1)}:function(a,b,c){return a.attachEvent("on"+b,c)}}(),unbindEvent:function(){return"removeEventListener"in window?function(a,b,c){return a.removeEventListener(b,c,!1)}:function(a,b,c){return a.detachEvent("on"+b,c)}}(),getInputValue:function(a){var b,c,d,e;if("checkbox"===a.type)return a.checked;if("select-multiple"===a.type){for(e=[],c=0,d=a.length;d>c;c++)b=a[c],b.selected&&e.push(b.value);return e}return a.value}},a.View=function(){function b(b,c,d){var f,g,h,i,j,k,l,m,n;for(this.els=b,this.models=c,this.options=null!=d?d:{},this.update=e(this.update,this),this.publish=e(this.publish,this),this.sync=e(this.sync,this),this.unbind=e(this.unbind,this),this.bind=e(this.bind,this),this.select=e(this.select,this),this.build=e(this.build,this),this.componentRegExp=e(this.componentRegExp,this),this.bindingRegExp=e(this.bindingRegExp,this),this.els.jquery||this.els instanceof Array||(this.els=[this.els]),l=["config","binders","formatters","adapters"],j=0,k=l.length;k>j;j++){if(g=l[j],this[g]={},this.options[g]){m=this.options[g];for(f in m)h=m[f],this[g][f]=h}n=a[g];for(f in n)h=n[f],null==(i=this[g])[f]&&(i[f]=h)}this.build()}return b.prototype.bindingRegExp=function(){return new RegExp("^"+this.config.prefix+"-")},b.prototype.componentRegExp=function(){return new RegExp("^"+this.config.prefix.toUpperCase()+"-")},b.prototype.build=function(){var b,c,d,e,g,h,i,j,k,l=this;for(this.bindings=[],h=[],b=this.bindingRegExp(),d=this.componentRegExp(),c=function(b,c,d,e){var f,g,h,i,j,k,m;return j={},m=function(){var a,b,c,d;for(c=e.split("|"),d=[],a=0,b=c.length;b>a;a++)k=c[a],d.push(k.trim());return d}(),f=function(){var a,b,c,d;for(c=m.shift().split("<"),d=[],a=0,b=c.length;b>a;a++)g=c[a],d.push(g.trim());return d}(),i=f.shift(),j.formatters=m,(h=f.shift())&&(j.dependencies=h.split(/\s+/)),l.bindings.push(new a[b](l,c,d,i,j))},g=function(e){var i,j,k,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M;if(f.call(h,e)<0){if(3===e.nodeType){if(q=a.TextTemplateParser,(n=l.config.templateDelimiters)&&(u=q.parse(e.data,n)).length&&(1!==u.length||u[0].type!==q.types.text)){for(x=0,B=u.length;B>x;x++)t=u[x],s=document.createTextNode(t.value),e.parentNode.insertBefore(s,e),1===t.type&&c("TextBinding",s,null,t.value);e.parentNode.removeChild(e)}}else if(d.test(e.tagName))v=e.tagName.replace(d,"").toLowerCase(),l.bindings.push(new a.ComponentBinding(l,e,v));else if(null!=e.attributes){for(H=e.attributes,y=0,C=H.length;C>y;y++)if(i=H[y],b.test(i.name)){if(v=i.name.replace(b,""),!(k=l.binders[v])){I=l.binders;for(o in I)w=I[o],"*"!==o&&-1!==o.indexOf("*")&&(r=new RegExp("^"+o.replace("*",".+")+"$"),r.test(v)&&(k=w))}if(k||(k=l.binders["*"]),k.block){for(J=e.childNodes,z=0,D=J.length;D>z;z++)p=J[z],h.push(p);j=[i]}}for(K=j||e.attributes,A=0,E=K.length;E>A;A++)i=K[A],b.test(i.name)&&(v=i.name.replace(b,""),c("Binding",e,v,i.value))}for(L=function(){var a,b,c,d;for(c=e.childNodes,d=[],b=0,a=c.length;a>b;b++)p=c[b],d.push(p);return d}(),M=[],G=0,F=L.length;F>G;G++)m=L[G],M.push(g(m));return M}},k=this.els,i=0,j=k.length;j>i;i++)e=k[i],g(e)},b.prototype.select=function(a){var b,c,d,e,f;for(e=this.bindings,f=[],c=0,d=e.length;d>c;c++)b=e[c],a(b)&&f.push(b);return f},b.prototype.bind=function(){var a,b,c,d,e;for(d=this.bindings,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.bind());return e},b.prototype.unbind=function(){var a,b,c,d,e;for(d=this.bindings,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.unbind());return e},b.prototype.sync=function(){var a,b,c,d,e;for(d=this.bindings,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.sync());return e},b.prototype.publish=function(){var a,b,c,d,e;for(d=this.select(function(a){return a.binder.publishes}),e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.publish());return e},b.prototype.update=function(a){var b,c,d,e,f,g,h;null==a&&(a={});for(c in a)d=a[c],this.models[c]=d;for(g=this.bindings,h=[],e=0,f=g.length;f>e;e++)b=g[e],h.push(b.update(a));return h},b}(),a.Binding=function(){function b(a,b,c,d,f){this.view=a,this.el=b,this.type=c,this.keypath=d,this.options=null!=f?f:{},this.update=e(this.update,this),this.unbind=e(this.unbind,this),this.bind=e(this.bind,this),this.publish=e(this.publish,this),this.sync=e(this.sync,this),this.set=e(this.set,this),this.eventHandler=e(this.eventHandler,this),this.formattedValue=e(this.formattedValue,this),this.setBinder=e(this.setBinder,this),this.formatters=this.options.formatters||[],this.dependencies=[],this.model=void 0,this.setBinder()}return b.prototype.setBinder=function(){var a,b,c,d;if(!(this.binder=this.view.binders[this.type])){d=this.view.binders;for(a in d)c=d[a],"*"!==a&&-1!==a.indexOf("*")&&(b=new RegExp("^"+a.replace("*",".+")+"$"),b.test(this.type)&&(this.binder=c,this.args=new RegExp("^"+a.replace("*","(.+)")+"$").exec(this.type),this.args.shift()))}return this.binder||(this.binder=this.view.binders["*"]),this.binder instanceof Function?this.binder={routine:this.binder}:void 0},b.prototype.formattedValue=function(a){var b,c,d,e,f,h;for(h=this.formatters,e=0,f=h.length;f>e;e++)c=h[e],b=c.split(/\s+/),d=b.shift(),c=this.view.formatters[d],(null!=c?c.read:void 0)instanceof Function?a=c.read.apply(c,[a].concat(g.call(b))):c instanceof Function&&(a=c.apply(null,[a].concat(g.call(b))));return a},b.prototype.eventHandler=function(a){var b,c;return c=(b=this).view.config.handler,function(d){return c.call(a,this,d,b)}},b.prototype.set=function(a){var b;return a=a instanceof Function&&!this.binder["function"]?this.formattedValue(a.call(this.model)):this.formattedValue(a),null!=(b=this.binder.routine)?b.call(this,this.el,a):void 0},b.prototype.sync=function(){var b,c,d,e,f,g,h,i,j;if(this.model!==this.observer.target){for(h=this.dependencies,d=0,f=h.length;f>d;d++)c=h[d],c.unobserve();if(this.dependencies=[],null!=(this.model=this.observer.target)&&(null!=(i=this.options.dependencies)?i.length:void 0))for(j=this.options.dependencies,e=0,g=j.length;g>e;e++)b=j[e],c=new a.Observer(this.view,this.model,b,this.sync),this.dependencies.push(c)}return this.set(this.observer.value())},b.prototype.publish=function(){var b,c,d,e,f,h,i,j,k;for(e=a.Util.getInputValue(this.el),i=this.formatters.slice(0).reverse(),f=0,h=i.length;h>f;f++)c=i[f],b=c.split(/\s+/),d=b.shift(),(null!=(j=this.view.formatters[d])?j.publish:void 0)&&(e=(k=this.view.formatters[d]).publish.apply(k,[e].concat(g.call(b))));return this.observer.publish(e)},b.prototype.bind=function(){var b,c,d,e,f,g,h;if(null!=(f=this.binder.bind)&&f.call(this,this.el),this.observer=new a.Observer(this.view,this.view.models,this.keypath,this.sync),this.model=this.observer.target,null!=this.model&&(null!=(g=this.options.dependencies)?g.length:void 0))for(h=this.options.dependencies,d=0,e=h.length;e>d;d++)b=h[d],c=new a.Observer(this.view,this.model,b,this.sync),this.dependencies.push(c);return this.view.config.preloadData?this.sync():void 0},b.prototype.unbind=function(){var a,b,c,d,e;for(null!=(d=this.binder.unbind)&&d.call(this,this.el),this.observer.unobserve(),e=this.dependencies,b=0,c=e.length;c>b;b++)a=e[b],a.unobserve();return this.dependencies=[]},b.prototype.update=function(a){var b;return null==a&&(a={}),null!=(b=this.binder.update)?b.call(this,a):void 0},b}(),a.ComponentBinding=function(b){function c(b,c,d){var g,h,i,j,k;for(this.view=b,this.el=c,this.type=d,this.unbind=e(this.unbind,this),this.bind=e(this.bind,this),this.update=e(this.update,this),this.locals=e(this.locals,this),this.component=a.components[this.type],this.attributes={},this.inflections={},j=this.el.attributes||[],h=0,i=j.length;i>h;h++)g=j[h],k=g.name,f.call(this.component.attributes,k)>=0?this.attributes[g.name]=g.value:this.inflections[g.name]=g.value}return i(c,b),c.prototype.sync=function(){},c.prototype.locals=function(a){var b,c,d,e,f,g,h,i,j;null==a&&(a=this.view.models),f={},i=this.inflections;for(c in i)for(b=i[c],j=b.split("."),g=0,h=j.length;h>g;g++)e=j[g],f[c]=(f[c]||a)[e];for(c in a)d=a[c],null==f[c]&&(f[c]=d);return f},c.prototype.update=function(a){var b;return null!=(b=this.componentView)?b.update(this.locals(a)):void 0},c.prototype.bind=function(){var b,c;return null!=this.componentView?null!=(c=this.componentView)?c.bind():void 0:(b=this.component.build.call(this.attributes),(this.componentView=new a.View(b,this.locals(),this.view.options)).bind(),this.el.parentNode.replaceChild(b,this.el))},c.prototype.unbind=function(){var a;return null!=(a=this.componentView)?a.unbind():void 0},c}(a.Binding),a.TextBinding=function(a){function b(a,b,c,d,f){this.view=a,this.el=b,this.type=c,this.keypath=d,this.options=null!=f?f:{},this.sync=e(this.sync,this),this.formatters=this.options.formatters||[],this.dependencies=[]}return i(b,a),b.prototype.binder={routine:function(a,b){return a.data=null!=b?b:""}},b.prototype.sync=function(){return b.__super__.sync.apply(this,arguments)},b}(a.Binding),a.KeypathParser=function(){function a(){}return a.parse=function(a,b,c){var d,e,g,h,i,j;for(h=[],e={"interface":c,path:""},g=i=0,j=a.length;j>i;g=i+=1)d=a.charAt(g),f.call(b,d)>=0?(h.push(e),e={"interface":d,path:""}):e.path+=d;return h.push(e),h},a}(),a.TextTemplateParser=function(){function a(){}return a.types={text:0,binding:1},a.parse=function(a,b){var c,d,e,f,g,h,i;for(h=[],f=a.length,c=0,d=0;f>d;){if(c=a.indexOf(b[0],d),0>c){h.push({type:this.types.text,value:a.slice(d)});break}if(c>0&&c>d&&h.push({type:this.types.text,value:a.slice(d,c)}),d=c+b[0].length,c=a.indexOf(b[1],d),0>c){g=a.slice(d-b[1].length),e=h[h.length-1],(null!=e?e.type:void 0)===this.types.text?e.value+=g:h.push({type:this.types.text,value:g});break}i=a.slice(d,c).trim(),h.push({type:this.types.binding,value:i}),d=c+b[1].length}return h},a}(),a.Observer=function(){function b(a,b,c,d){this.view=a,this.model=b,this.keypath=c,this.callback=d,this.unobserve=e(this.unobserve,this),this.realize=e(this.realize,this),this.value=e(this.value,this),this.publish=e(this.publish,this),this.read=e(this.read,this),this.set=e(this.set,this),this.adapter=e(this.adapter,this),this.update=e(this.update,this),this.initialize=e(this.initialize,this),this.parse=e(this.parse,this),this.parse(),this.initialize()}return b.prototype.parse=function(){var b,c,d,e,g,h;return b=function(){var a,b;a=this.view.adapters,b=[];for(c in a)g=a[c],b.push(c);return b}.call(this),h=this.keypath[0],f.call(b,h)>=0?(e=this.keypath[0],d=this.keypath.substr(1)):(e=this.view.config.rootInterface,d=this.keypath),this.tokens=a.KeypathParser.parse(d,b,e),this.key=this.tokens.pop()},b.prototype.initialize=function(){return this.objectPath=[],this.target=this.realize(),null!=this.target?this.set(!0,this.key,this.target,this.callback):void 0},b.prototype.update=function(){var a,b;return(a=this.realize())!==this.target&&(null!=this.target&&this.set(!1,this.key,this.target,this.callback),null!=a&&this.set(!0,this.key,a,this.callback),b=this.value(),this.target=a,this.value()!==b)?this.callback():void 0},b.prototype.adapter=function(a){return this.view.adapters[a["interface"]]},b.prototype.set=function(a,b,c,d){var e;return e=a?"subscribe":"unsubscribe",this.adapter(b)[e](c,b.path,d)},b.prototype.read=function(a,b){return this.adapter(a).read(b,a.path)},b.prototype.publish=function(a){return null!=this.target?this.adapter(this.key).publish(this.target,this.key.path,a):void 0},b.prototype.value=function(){return null!=this.target?this.read(this.key,this.target):void 0},b.prototype.realize=function(){var a,b,c,d,e,f,g,h;for(a=this.model,e=null,h=this.tokens,b=f=0,g=h.length;g>f;b=++f)d=h[b],null!=a?(null!=this.objectPath[b]?a!==(c=this.objectPath[b])&&(this.set(!1,d,c,this.update),this.set(!0,d,a,this.update),this.objectPath[b]=a):(this.set(!0,d,a,this.update),this.objectPath[b]=a),a=this.read(d,a)):(null==e&&(e=b),(c=this.objectPath[b])&&this.set(!1,d,c,this.update));return null!=e&&this.objectPath.splice(e),a},b.prototype.unobserve=function(){var a,b,c,d,e,f,g;for(f=this.tokens,g=[],a=d=0,e=f.length;e>d;a=++d)c=f[a],(b=this.objectPath[a])?g.push(this.set(!1,c,b,this.update)):g.push(void 0);return g},b}(),a.binders.text=function(a,b){return null!=a.textContent?a.textContent=null!=b?b:"":a.innerText=null!=b?b:""},a.binders.html=function(a,b){return a.innerHTML=null!=b?b:""},a.binders.show=function(a,b){return a.style.display=b?"":"none"},a.binders.hide=function(a,b){return a.style.display=b?"none":""},a.binders.enabled=function(a,b){return a.disabled=!b},a.binders.disabled=function(a,b){return a.disabled=!!b},a.binders.checked={publishes:!0,bind:function(b){return a.Util.bindEvent(b,"change",this.publish)},unbind:function(b){return a.Util.unbindEvent(b,"change",this.publish)},routine:function(a,b){var c;return a.checked="radio"===a.type?(null!=(c=a.value)?c.toString():void 0)===(null!=b?b.toString():void 0):!!b}},a.binders.unchecked={publishes:!0,bind:function(b){return a.Util.bindEvent(b,"change",this.publish)},unbind:function(b){return a.Util.unbindEvent(b,"change",this.publish)},routine:function(a,b){var c;return a.checked="radio"===a.type?(null!=(c=a.value)?c.toString():void 0)!==(null!=b?b.toString():void 0):!b}},a.binders.value={publishes:!0,bind:function(b){return a.Util.bindEvent(b,"change",this.publish)},unbind:function(b){return a.Util.unbindEvent(b,"change",this.publish)},routine:function(a,b){var c,d,e,g,h,i,j;if(null!=window.jQuery){if(a=jQuery(a),(null!=b?b.toString():void 0)!==(null!=(g=a.val())?g.toString():void 0))return a.val(null!=b?b:"")}else if("select-multiple"===a.type){if(null!=b){for(j=[],d=0,e=a.length;e>d;d++)c=a[d],j.push(c.selected=(h=c.value,f.call(b,h)>=0));return j}}else if((null!=b?b.toString():void 0)!==(null!=(i=a.value)?i.toString():void 0))return a.value=null!=b?b:""}},a.binders["if"]={block:!0,bind:function(a){var b,c;return null==this.marker?(b=[this.view.config.prefix,this.type].join("-").replace("--","-"),c=a.getAttribute(b),this.marker=document.createComment(" rivets: "+this.type+" "+c+" "),a.removeAttribute(b),a.parentNode.insertBefore(this.marker,a),a.parentNode.removeChild(a)):void 0},unbind:function(){var a;return null!=(a=this.nested)?a.unbind():void 0},routine:function(b,c){var d,e,f,g,h;if(!!c==(null==this.nested)){if(c){f={},h=this.view.models;for(d in h)e=h[d],f[d]=e;return g={binders:this.view.options.binders,formatters:this.view.options.formatters,adapters:this.view.options.adapters,config:this.view.options.config},(this.nested=new a.View(b,f,g)).bind(),this.marker.parentNode.insertBefore(b,this.marker.nextSibling)}return b.parentNode.removeChild(b),this.nested.unbind(),delete this.nested}},update:function(a){var b;return null!=(b=this.nested)?b.update(a):void 0}},a.binders.unless={block:!0,bind:function(b){return a.binders["if"].bind.call(this,b)},unbind:function(){return a.binders["if"].unbind.call(this)},routine:function(b,c){return a.binders["if"].routine.call(this,b,!c)},update:function(b){return a.binders["if"].update.call(this,b)}},a.binders["on-*"]={"function":!0,unbind:function(b){return this.handler?a.Util.unbindEvent(b,this.args[0],this.handler):void 0},routine:function(b,c){return this.handler&&a.Util.unbindEvent(b,this.args[0],this.handler),a.Util.bindEvent(b,this.args[0],this.handler=this.eventHandler(c))}},a.binders["each-*"]={block:!0,bind:function(a){var b;return null==this.marker?(b=[this.view.config.prefix,this.type].join("-").replace("--","-"),this.marker=document.createComment(" rivets: "+this.type+" "),this.iterated=[],a.removeAttribute(b),a.parentNode.insertBefore(this.marker,a),a.parentNode.removeChild(a)):void 0},unbind:function(){var a,b,c,d,e;if(null!=this.iterated){for(d=this.iterated,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.unbind());return e}},routine:function(b,c){var d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A;if(k=this.args[0],c=c||[],this.iterated.length>c.length)for(w=Array(this.iterated.length-c.length),q=0,t=w.length;t>q;q++)f=w[q],p=this.iterated.pop(),p.unbind(),this.marker.parentNode.removeChild(p.els[0]);for(g=r=0,u=c.length;u>r;g=++r)if(j=c[g],e={},e[k]=j,null==this.iterated[g]){x=this.view.models;for(i in x)j=x[i],null==e[i]&&(e[i]=j);m=this.iterated.length?this.iterated[this.iterated.length-1].els[0]:this.marker,l={binders:this.view.options.binders,formatters:this.view.options.formatters,adapters:this.view.options.adapters,config:{}},y=this.view.options.config;for(h in y)o=y[h],l.config[h]=o;l.config.preloadData=!0,n=b.cloneNode(!0),p=new a.View(n,e,l),p.bind(),this.iterated.push(p),this.marker.parentNode.insertBefore(n,m.nextSibling)}else this.iterated[g].models[k]!==j&&this.iterated[g].update(e);if("OPTION"===b.nodeName){for(z=this.view.bindings,A=[],s=0,v=z.length;v>s;s++)d=z[s],d.el===this.marker.parentNode&&"value"===d.type?A.push(d.sync()):A.push(void 0);return A}},update:function(a){var b,c,d,e,f,g,h,i;b={};for(c in a)d=a[c],c!==this.args[0]&&(b[c]=d);for(h=this.iterated,i=[],f=0,g=h.length;g>f;f++)e=h[f],i.push(e.update(b));return i}},a.binders["class-*"]=function(a,b){var c;return c=" "+a.className+" ",!b==(-1!==c.indexOf(" "+this.args[0]+" "))?a.className=b?""+a.className+" "+this.args[0]:c.replace(" "+this.args[0]+" "," ").trim():void 0},a.binders["*"]=function(a,b){return b?a.setAttribute(this.type,b):a.removeAttribute(this.type)},a.adapters["."]={id:"_rv",counter:0,weakmap:{},weakReference:function(a){var b;return null==a[this.id]&&(b=this.counter++,this.weakmap[b]={callbacks:{}},Object.defineProperty(a,this.id,{value:b})),this.weakmap[a[this.id]]},stubFunction:function(a,b){var c,d,e;return d=a[b],c=this.weakReference(a),e=this.weakmap,a[b]=function(){var b,f,g,h,i,j,k,l,m,n;h=d.apply(a,arguments),k=c.pointers;for(g in k)for(f=k[g],n=null!=(l=null!=(m=e[g])?m.callbacks[f]:void 0)?l:[],i=0,j=n.length;j>i;i++)b=n[i],b();return h}},observeMutations:function(a,b,c){var d,e,g,h,i,j;if(Array.isArray(a)){if(g=this.weakReference(a),null==g.pointers)for(g.pointers={},e=["push","pop","shift","unshift","sort","reverse","splice"],i=0,j=e.length;j>i;i++)d=e[i],this.stubFunction(a,d);if(null==(h=g.pointers)[b]&&(h[b]=[]),f.call(g.pointers[b],c)<0)return g.pointers[b].push(c)}},unobserveMutations:function(a,b,c){var d,e;return Array.isArray(a&&null!=a[this.id])&&(d=null!=(e=this.weakReference(a).pointers)?e[b]:void 0)?d.splice(d.indexOf(c),1):void 0},subscribe:function(a,b,c){var d,e,g=this;return d=this.weakReference(a).callbacks,null==d[b]&&(d[b]=[],e=a[b],Object.defineProperty(a,b,{get:function(){return e},set:function(f){var h,i,j;if(f!==e){for(e=f,j=d[b],h=0,i=j.length;i>h;h++)c=j[h],c();return g.observeMutations(f,a[g.id],b)}}})),f.call(d[b],c)<0&&d[b].push(c),this.observeMutations(a[b],a[this.id],b)},unsubscribe:function(a,b,c){var d;return d=this.weakmap[a[this.id]].callbacks[b],d.splice(d.indexOf(c),1),this.unobserveMutations(a[b],a[this.id],b)},read:function(a,b){return a[b]},publish:function(a,b,c){return a[b]=c}},a.factory=function(b){return b._=a,b.binders=a.binders,b.components=a.components,b.formatters=a.formatters,b.adapters=a.adapters,b.config=a.config,b.configure=function(b){var c,d;null==b&&(b={});for(c in b)d=b[c],a.config[c]=d},b.bind=function(b,c,d){var e;return null==c&&(c={}),null==d&&(d={}),e=new a.View(b,c,d),e.bind(),e}},"object"==typeof exports?a.factory(exports):"function"==typeof define&&define.amd?define(["exports"],function(b){return a.factory(this.rivets=b),b}):a.factory(this.rivets={})}).call(this); \ No newline at end of file diff --git a/ajax/libs/rivets/0.6.7/rivets.js b/ajax/libs/rivets/0.6.7/rivets.js new file mode 100644 index 000000000..6f1bb070f --- /dev/null +++ b/ajax/libs/rivets/0.6.7/rivets.js @@ -0,0 +1,1293 @@ +// Rivets.js +// version: 0.6.7 +// author: Michael Richards +// license: MIT +(function() { + var Rivets, bindMethod, unbindMethod, _ref, + __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, + __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; }, + __slice = [].slice, + __hasProp = {}.hasOwnProperty, + __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; + + Rivets = { + binders: {}, + components: {}, + formatters: {}, + adapters: {}, + config: { + prefix: 'rv', + templateDelimiters: ['{', '}'], + rootInterface: '.', + preloadData: true, + handler: function(context, ev, binding) { + return this.call(context, ev, binding.view.models); + } + } + }; + + if ('jQuery' in window) { + _ref = 'on' in jQuery ? ['on', 'off'] : ['bind', 'unbind'], bindMethod = _ref[0], unbindMethod = _ref[1]; + Rivets.Util = { + bindEvent: function(el, event, handler) { + return jQuery(el)[bindMethod](event, handler); + }, + unbindEvent: function(el, event, handler) { + return jQuery(el)[unbindMethod](event, handler); + }, + getInputValue: function(el) { + var $el; + $el = jQuery(el); + if ($el.attr('type') === 'checkbox') { + return $el.is(':checked'); + } else { + return $el.val(); + } + } + }; + } else { + Rivets.Util = { + bindEvent: (function() { + if ('addEventListener' in window) { + return function(el, event, handler) { + return el.addEventListener(event, handler, false); + }; + } + return function(el, event, handler) { + return el.attachEvent('on' + event, handler); + }; + })(), + unbindEvent: (function() { + if ('removeEventListener' in window) { + return function(el, event, handler) { + return el.removeEventListener(event, handler, false); + }; + } + return function(el, event, handler) { + return el.detachEvent('on' + event, handler); + }; + })(), + getInputValue: function(el) { + var o, _i, _len, _results; + if (el.type === 'checkbox') { + return el.checked; + } else if (el.type === 'select-multiple') { + _results = []; + for (_i = 0, _len = el.length; _i < _len; _i++) { + o = el[_i]; + if (o.selected) { + _results.push(o.value); + } + } + return _results; + } else { + return el.value; + } + } + }; + } + + Rivets.View = (function() { + function View(els, models, options) { + var k, option, v, _base, _i, _len, _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.componentRegExp = __bind(this.componentRegExp, this); + this.bindingRegExp = __bind(this.bindingRegExp, this); + if (!(this.els.jquery || this.els instanceof Array)) { + this.els = [this.els]; + } + _ref1 = ['config', 'binders', 'formatters', 'adapters']; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + option = _ref1[_i]; + this[option] = {}; + if (this.options[option]) { + _ref2 = this.options[option]; + for (k in _ref2) { + v = _ref2[k]; + this[option][k] = v; + } + } + _ref3 = Rivets[option]; + for (k in _ref3) { + v = _ref3[k]; + if ((_base = this[option])[k] == null) { + _base[k] = v; + } + } + } + this.build(); + } + + View.prototype.bindingRegExp = function() { + return new RegExp("^" + this.config.prefix + "-"); + }; + + View.prototype.componentRegExp = function() { + return new RegExp("^" + (this.config.prefix.toUpperCase()) + "-"); + }; + + View.prototype.build = function() { + var bindingRegExp, buildBinding, componentRegExp, el, parse, skipNodes, _i, _len, _ref1, + _this = this; + this.bindings = []; + skipNodes = []; + bindingRegExp = this.bindingRegExp(); + componentRegExp = this.componentRegExp(); + buildBinding = function(binding, node, type, declaration) { + var context, ctx, dependencies, keypath, options, pipe, pipes; + options = {}; + pipes = (function() { + var _i, _len, _ref1, _results; + _ref1 = declaration.split('|'); + _results = []; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + pipe = _ref1[_i]; + _results.push(pipe.trim()); + } + return _results; + })(); + context = (function() { + var _i, _len, _ref1, _results; + _ref1 = pipes.shift().split('<'); + _results = []; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + ctx = _ref1[_i]; + _results.push(ctx.trim()); + } + return _results; + })(); + keypath = context.shift(); + options.formatters = pipes; + if (dependencies = context.shift()) { + options.dependencies = dependencies.split(/\s+/); + } + return _this.bindings.push(new Rivets[binding](_this, node, type, keypath, options)); + }; + parse = function(node) { + var attribute, attributes, binder, childNode, delimiters, identifier, n, parser, regexp, text, token, tokens, type, value, _i, _j, _k, _l, _len, _len1, _len2, _len3, _len4, _m, _ref1, _ref2, _ref3, _ref4, _ref5, _results; + if (__indexOf.call(skipNodes, node) < 0) { + if (node.nodeType === 3) { + 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)) { + for (_i = 0, _len = tokens.length; _i < _len; _i++) { + token = tokens[_i]; + text = document.createTextNode(token.value); + node.parentNode.insertBefore(text, node); + if (token.type === 1) { + buildBinding('TextBinding', text, null, token.value); + } + } + node.parentNode.removeChild(node); + } + } + } + } else if (componentRegExp.test(node.tagName)) { + type = node.tagName.replace(componentRegExp, '').toLowerCase(); + _this.bindings.push(new Rivets.ComponentBinding(_this, node, type)); + } else if (node.attributes != null) { + _ref1 = node.attributes; + for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { + attribute = _ref1[_j]; + if (bindingRegExp.test(attribute.name)) { + type = attribute.name.replace(bindingRegExp, ''); + if (!(binder = _this.binders[type])) { + _ref2 = _this.binders; + for (identifier in _ref2) { + value = _ref2[identifier]; + if (identifier !== '*' && identifier.indexOf('*') !== -1) { + regexp = new RegExp("^" + (identifier.replace('*', '.+')) + "$"); + if (regexp.test(type)) { + binder = value; + } + } + } + } + binder || (binder = _this.binders['*']); + if (binder.block) { + _ref3 = node.childNodes; + for (_k = 0, _len2 = _ref3.length; _k < _len2; _k++) { + n = _ref3[_k]; + skipNodes.push(n); + } + attributes = [attribute]; + } + } + } + _ref4 = attributes || node.attributes; + for (_l = 0, _len3 = _ref4.length; _l < _len3; _l++) { + attribute = _ref4[_l]; + if (bindingRegExp.test(attribute.name)) { + type = attribute.name.replace(bindingRegExp, ''); + buildBinding('Binding', node, type, attribute.value); + } + } + } + _ref5 = (function() { + var _len4, _n, _ref5, _results1; + _ref5 = node.childNodes; + _results1 = []; + for (_n = 0, _len4 = _ref5.length; _n < _len4; _n++) { + n = _ref5[_n]; + _results1.push(n); + } + return _results1; + })(); + _results = []; + for (_m = 0, _len4 = _ref5.length; _m < _len4; _m++) { + childNode = _ref5[_m]; + _results.push(parse(childNode)); + } + return _results; + } + }; + _ref1 = this.els; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + el = _ref1[_i]; + parse(el); + } + }; + + View.prototype.select = function(fn) { + var binding, _i, _len, _ref1, _results; + _ref1 = this.bindings; + _results = []; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + binding = _ref1[_i]; + if (fn(binding)) { + _results.push(binding); + } + } + return _results; + }; + + View.prototype.bind = function() { + var binding, _i, _len, _ref1, _results; + _ref1 = this.bindings; + _results = []; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + binding = _ref1[_i]; + _results.push(binding.bind()); + } + return _results; + }; + + View.prototype.unbind = function() { + var binding, _i, _len, _ref1, _results; + _ref1 = this.bindings; + _results = []; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + binding = _ref1[_i]; + _results.push(binding.unbind()); + } + return _results; + }; + + View.prototype.sync = function() { + var binding, _i, _len, _ref1, _results; + _ref1 = this.bindings; + _results = []; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + binding = _ref1[_i]; + _results.push(binding.sync()); + } + return _results; + }; + + View.prototype.publish = function() { + var binding, _i, _len, _ref1, _results; + _ref1 = this.select(function(b) { + return b.binder.publishes; + }); + _results = []; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + binding = _ref1[_i]; + _results.push(binding.publish()); + } + return _results; + }; + + View.prototype.update = function(models) { + var binding, key, model, _i, _len, _ref1, _results; + if (models == null) { + models = {}; + } + for (key in models) { + model = models[key]; + this.models[key] = model; + } + _ref1 = this.bindings; + _results = []; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + binding = _ref1[_i]; + _results.push(binding.update(models)); + } + return _results; + }; + + return View; + + })(); + + Rivets.Binding = (function() { + function Binding(view, el, type, keypath, options) { + this.view = view; + this.el = el; + this.type = type; + 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); + this.setBinder = __bind(this.setBinder, this); + this.formatters = this.options.formatters || []; + this.dependencies = []; + this.model = void 0; + this.setBinder(); + } + + Binding.prototype.setBinder = function() { + var identifier, regexp, value, _ref1; + if (!(this.binder = this.view.binders[this.type])) { + _ref1 = this.view.binders; + for (identifier in _ref1) { + value = _ref1[identifier]; + if (identifier !== '*' && identifier.indexOf('*') !== -1) { + regexp = new RegExp("^" + (identifier.replace('*', '.+')) + "$"); + if (regexp.test(this.type)) { + this.binder = value; + this.args = new RegExp("^" + (identifier.replace('*', '(.+)')) + "$").exec(this.type); + this.args.shift(); + } + } + } + } + this.binder || (this.binder = this.view.binders['*']); + if (this.binder instanceof Function) { + return this.binder = { + routine: this.binder + }; + } + }; + + Binding.prototype.formattedValue = function(value) { + var args, formatter, id, _i, _len, _ref1; + _ref1 = this.formatters; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + formatter = _ref1[_i]; + args = formatter.split(/\s+/); + id = args.shift(); + formatter = 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 _ref1; + value = value instanceof Function && !this.binder["function"] ? this.formattedValue(value.call(this.model)) : this.formattedValue(value); + return (_ref1 = this.binder.routine) != null ? _ref1.call(this, this.el, value) : void 0; + }; + + Binding.prototype.sync = function() { + var dependency, observer, _i, _j, _len, _len1, _ref1, _ref2, _ref3; + if (this.model !== this.observer.target) { + _ref1 = this.dependencies; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + observer = _ref1[_i]; + observer.unobserve(); + } + this.dependencies = []; + if (((this.model = this.observer.target) != null) && ((_ref2 = this.options.dependencies) != null ? _ref2.length : void 0)) { + _ref3 = this.options.dependencies; + for (_j = 0, _len1 = _ref3.length; _j < _len1; _j++) { + dependency = _ref3[_j]; + observer = new Rivets.Observer(this.view, this.model, dependency, this.sync); + this.dependencies.push(observer); + } + } + } + return this.set(this.observer.value()); + }; + + Binding.prototype.publish = function() { + var args, formatter, id, value, _i, _len, _ref1, _ref2, _ref3; + value = Rivets.Util.getInputValue(this.el); + _ref1 = this.formatters.slice(0).reverse(); + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + formatter = _ref1[_i]; + args = formatter.split(/\s+/); + id = args.shift(); + if ((_ref2 = this.view.formatters[id]) != null ? _ref2.publish : void 0) { + value = (_ref3 = this.view.formatters[id]).publish.apply(_ref3, [value].concat(__slice.call(args))); + } + } + return this.observer.publish(value); + }; + + Binding.prototype.bind = function() { + var dependency, observer, _i, _len, _ref1, _ref2, _ref3; + if ((_ref1 = this.binder.bind) != null) { + _ref1.call(this, this.el); + } + this.observer = new Rivets.Observer(this.view, this.view.models, this.keypath, this.sync); + this.model = this.observer.target; + if ((this.model != null) && ((_ref2 = this.options.dependencies) != null ? _ref2.length : void 0)) { + _ref3 = this.options.dependencies; + for (_i = 0, _len = _ref3.length; _i < _len; _i++) { + dependency = _ref3[_i]; + observer = new Rivets.Observer(this.view, this.model, dependency, this.sync); + this.dependencies.push(observer); + } + } + if (this.view.config.preloadData) { + return this.sync(); + } + }; + + Binding.prototype.unbind = function() { + var observer, _i, _len, _ref1, _ref2; + if ((_ref1 = this.binder.unbind) != null) { + _ref1.call(this, this.el); + } + this.observer.unobserve(); + _ref2 = this.dependencies; + for (_i = 0, _len = _ref2.length; _i < _len; _i++) { + observer = _ref2[_i]; + observer.unobserve(); + } + return this.dependencies = []; + }; + + Binding.prototype.update = function(models) { + var _ref1; + if (models == null) { + models = {}; + } + this.model = this.observer.target; + return (_ref1 = this.binder.update) != null ? _ref1.call(this, models) : void 0; + }; + + return Binding; + + })(); + + Rivets.ComponentBinding = (function(_super) { + __extends(ComponentBinding, _super); + + function ComponentBinding(view, el, type) { + var attribute, _i, _len, _ref1, _ref2; + this.view = view; + this.el = el; + this.type = type; + this.unbind = __bind(this.unbind, this); + this.bind = __bind(this.bind, this); + this.update = __bind(this.update, this); + this.locals = __bind(this.locals, this); + this.component = Rivets.components[this.type]; + this.attributes = {}; + this.inflections = {}; + _ref1 = this.el.attributes || []; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + attribute = _ref1[_i]; + if (_ref2 = attribute.name, __indexOf.call(this.component.attributes, _ref2) >= 0) { + this.attributes[attribute.name] = attribute.value; + } else { + this.inflections[attribute.name] = attribute.value; + } + } + } + + ComponentBinding.prototype.sync = function() {}; + + ComponentBinding.prototype.locals = function(models) { + var inverse, key, model, path, result, _i, _len, _ref1, _ref2; + if (models == null) { + models = this.view.models; + } + result = {}; + _ref1 = this.inflections; + for (key in _ref1) { + inverse = _ref1[key]; + _ref2 = inverse.split('.'); + for (_i = 0, _len = _ref2.length; _i < _len; _i++) { + path = _ref2[_i]; + result[key] = (result[key] || models)[path]; + } + } + for (key in models) { + model = models[key]; + if (result[key] == null) { + result[key] = model; + } + } + return result; + }; + + ComponentBinding.prototype.update = function(models) { + var _ref1; + return (_ref1 = this.componentView) != null ? _ref1.update(this.locals(models)) : void 0; + }; + + ComponentBinding.prototype.bind = function() { + var el, _ref1; + if (this.componentView != null) { + return (_ref1 = this.componentView) != null ? _ref1.bind() : void 0; + } else { + el = this.component.build.call(this.attributes); + (this.componentView = new Rivets.View(el, this.locals(), this.view.options)).bind(); + return this.el.parentNode.replaceChild(el, this.el); + } + }; + + ComponentBinding.prototype.unbind = function() { + var _ref1; + return (_ref1 = this.componentView) != null ? _ref1.unbind() : void 0; + }; + + return ComponentBinding; + + })(Rivets.Binding); + + Rivets.TextBinding = (function(_super) { + __extends(TextBinding, _super); + + function TextBinding(view, el, type, keypath, options) { + this.view = view; + this.el = el; + this.type = type; + this.keypath = keypath; + this.options = options != null ? options : {}; + this.sync = __bind(this.sync, this); + this.formatters = this.options.formatters || []; + this.dependencies = []; + } + + TextBinding.prototype.binder = { + routine: function(node, value) { + return node.data = value != null ? value : ''; + } + }; + + TextBinding.prototype.sync = function() { + return TextBinding.__super__.sync.apply(this, arguments); + }; + + return TextBinding; + + })(Rivets.Binding); + + Rivets.KeypathParser = (function() { + function KeypathParser() {} + + KeypathParser.parse = function(keypath, interfaces, root) { + var char, current, index, tokens, _i, _ref1; + tokens = []; + current = { + "interface": root, + path: '' + }; + for (index = _i = 0, _ref1 = keypath.length; _i < _ref1; index = _i += 1) { + char = keypath.charAt(index); + if (__indexOf.call(interfaces, char) >= 0) { + tokens.push(current); + current = { + "interface": char, + path: '' + }; + } else { + current.path += char; + } + } + tokens.push(current); + return tokens; + }; + + return KeypathParser; + + })(); + + 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 + delimiters[0].length; + index = template.indexOf(delimiters[1], lastIndex); + if (index < 0) { + substring = template.slice(lastIndex - delimiters[1].length); + 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 + delimiters[1].length; + } + } + return tokens; + }; + + return TextTemplateParser; + + })(); + + Rivets.Observer = (function() { + function Observer(view, model, keypath, callback) { + this.view = view; + this.model = model; + this.keypath = keypath; + this.callback = callback; + this.unobserve = __bind(this.unobserve, this); + this.realize = __bind(this.realize, this); + this.value = __bind(this.value, this); + this.publish = __bind(this.publish, this); + this.read = __bind(this.read, this); + this.set = __bind(this.set, this); + this.adapter = __bind(this.adapter, this); + this.update = __bind(this.update, this); + this.initialize = __bind(this.initialize, this); + this.parse = __bind(this.parse, this); + this.parse(); + this.initialize(); + } + + Observer.prototype.parse = function() { + var interfaces, k, path, root, v, _ref1; + interfaces = (function() { + var _ref1, _results; + _ref1 = this.view.adapters; + _results = []; + for (k in _ref1) { + v = _ref1[k]; + _results.push(k); + } + return _results; + }).call(this); + if (_ref1 = this.keypath[0], __indexOf.call(interfaces, _ref1) >= 0) { + root = this.keypath[0]; + path = this.keypath.substr(1); + } else { + root = this.view.config.rootInterface; + path = this.keypath; + } + this.tokens = Rivets.KeypathParser.parse(path, interfaces, root); + return this.key = this.tokens.pop(); + }; + + Observer.prototype.initialize = function() { + this.objectPath = []; + this.target = this.realize(); + if (this.target != null) { + return this.set(true, this.key, this.target, this.callback); + } + }; + + Observer.prototype.update = function() { + var next, oldValue; + if ((next = this.realize()) !== this.target) { + if (this.target != null) { + this.set(false, this.key, this.target, this.callback); + } + if (next != null) { + this.set(true, this.key, next, this.callback); + } + oldValue = this.value(); + this.target = next; + if (this.value() !== oldValue) { + return this.callback(); + } + } + }; + + Observer.prototype.adapter = function(key) { + return this.view.adapters[key["interface"]]; + }; + + Observer.prototype.set = function(active, key, obj, callback) { + var action; + action = active ? 'subscribe' : 'unsubscribe'; + return this.adapter(key)[action](obj, key.path, callback); + }; + + Observer.prototype.read = function(key, obj) { + return this.adapter(key).read(obj, key.path); + }; + + Observer.prototype.publish = function(value) { + if (this.target != null) { + return this.adapter(this.key).publish(this.target, this.key.path, value); + } + }; + + Observer.prototype.value = function() { + if (this.target != null) { + return this.read(this.key, this.target); + } + }; + + Observer.prototype.realize = function() { + var current, index, prev, token, unreached, _i, _len, _ref1; + current = this.model; + unreached = null; + _ref1 = this.tokens; + for (index = _i = 0, _len = _ref1.length; _i < _len; index = ++_i) { + token = _ref1[index]; + if (current != null) { + if (this.objectPath[index] != null) { + if (current !== (prev = this.objectPath[index])) { + this.set(false, token, prev, this.update); + this.set(true, token, current, this.update); + this.objectPath[index] = current; + } + } else { + this.set(true, token, current, this.update); + this.objectPath[index] = current; + } + current = this.read(token, current); + } else { + if (unreached == null) { + unreached = index; + } + if (prev = this.objectPath[index]) { + this.set(false, token, prev, this.update); + } + } + } + if (unreached != null) { + this.objectPath.splice(unreached); + } + return current; + }; + + Observer.prototype.unobserve = function() { + var index, obj, token, _i, _len, _ref1, _results; + _ref1 = this.tokens; + _results = []; + for (index = _i = 0, _len = _ref1.length; _i < _len; index = ++_i) { + token = _ref1[index]; + if (obj = this.objectPath[index]) { + _results.push(this.set(false, token, obj, this.update)); + } else { + _results.push(void 0); + } + } + return _results; + }; + + return Observer; + + })(); + + Rivets.binders.text = function(el, value) { + if (el.textContent != null) { + return el.textContent = value != null ? value : ''; + } else { + return el.innerText = value != null ? value : ''; + } + }; + + Rivets.binders.html = function(el, value) { + return el.innerHTML = value != null ? value : ''; + }; + + Rivets.binders.show = function(el, value) { + return el.style.display = value ? '' : 'none'; + }; + + Rivets.binders.hide = function(el, value) { + return el.style.display = value ? 'none' : ''; + }; + + Rivets.binders.enabled = function(el, value) { + return el.disabled = !value; + }; + + Rivets.binders.disabled = function(el, value) { + return el.disabled = !!value; + }; + + Rivets.binders.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 _ref1; + if (el.type === 'radio') { + return el.checked = ((_ref1 = el.value) != null ? _ref1.toString() : void 0) === (value != null ? value.toString() : void 0); + } else { + return el.checked = !!value; + } + } + }; + + Rivets.binders.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 _ref1; + if (el.type === 'radio') { + return el.checked = ((_ref1 = el.value) != null ? _ref1.toString() : void 0) !== (value != null ? value.toString() : void 0); + } else { + return el.checked = !value; + } + } + }; + + Rivets.binders.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, _ref1, _ref2, _ref3, _results; + if (window.jQuery != null) { + el = jQuery(el); + if ((value != null ? value.toString() : void 0) !== ((_ref1 = el.val()) != null ? _ref1.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 = (_ref2 = o.value, __indexOf.call(value, _ref2) >= 0)); + } + return _results; + } + } else if ((value != null ? value.toString() : void 0) !== ((_ref3 = el.value) != null ? _ref3.toString() : void 0)) { + return el.value = value != null ? value : ''; + } + } + } + }; + + Rivets.binders["if"] = { + block: true, + bind: function(el) { + var attr, declaration; + if (this.marker == null) { + attr = [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 _ref1; + return (_ref1 = this.nested) != null ? _ref1.unbind() : void 0; + }, + routine: function(el, value) { + var key, model, models, options, _ref1; + if (!!value === (this.nested == null)) { + if (value) { + models = {}; + _ref1 = this.view.models; + for (key in _ref1) { + model = _ref1[key]; + models[key] = model; + } + options = { + binders: this.view.options.binders, + formatters: this.view.options.formatters, + adapters: this.view.options.adapters, + 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) { + var _ref1; + return (_ref1 = this.nested) != null ? _ref1.update(models) : void 0; + } + }; + + Rivets.binders.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); + } + }; + + Rivets.binders['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)); + } + }; + + Rivets.binders['each-*'] = { + block: true, + bind: function(el) { + var attr; + if (this.marker == null) { + attr = [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, _ref1, _results; + if (this.iterated != null) { + _ref1 = this.iterated; + _results = []; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + view = _ref1[_i]; + _results.push(view.unbind()); + } + return _results; + } + }, + routine: function(el, collection) { + var binding, data, i, index, k, key, model, modelName, options, previous, template, v, view, _i, _j, _k, _len, _len1, _len2, _ref1, _ref2, _ref3, _ref4, _results; + modelName = this.args[0]; + collection = collection || []; + if (this.iterated.length > collection.length) { + _ref1 = Array(this.iterated.length - collection.length); + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + i = _ref1[_i]; + view = this.iterated.pop(); + view.unbind(); + this.marker.parentNode.removeChild(view.els[0]); + } + } + for (index = _j = 0, _len1 = collection.length; _j < _len1; index = ++_j) { + model = collection[index]; + data = { + index: index + }; + data[modelName] = model; + if (this.iterated[index] == null) { + _ref2 = this.view.models; + for (key in _ref2) { + model = _ref2[key]; + if (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, + adapters: this.view.options.adapters, + 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); + this.marker.parentNode.insertBefore(template, previous.nextSibling); + } else if (this.iterated[index].models[modelName] !== model) { + this.iterated[index].update(data); + } + } + if (el.nodeName === 'OPTION') { + _ref4 = this.view.bindings; + _results = []; + for (_k = 0, _len2 = _ref4.length; _k < _len2; _k++) { + binding = _ref4[_k]; + if (binding.el === this.marker.parentNode && binding.type === 'value') { + _results.push(binding.sync()); + } else { + _results.push(void 0); + } + } + return _results; + } + }, + update: function(models) { + var data, key, model, view, _i, _len, _ref1, _results; + data = {}; + for (key in models) { + model = models[key]; + if (key !== this.args[0]) { + data[key] = model; + } + } + _ref1 = this.iterated; + _results = []; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + view = _ref1[_i]; + _results.push(view.update(data)); + } + return _results; + } + }; + + Rivets.binders['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(); + } + }; + + Rivets.binders['*'] = function(el, value) { + if (value != null) { + return el.setAttribute(this.type, value); + } else { + return el.removeAttribute(this.type); + } + }; + + Rivets.adapters['.'] = { + id: '_rv', + counter: 0, + weakmap: {}, + weakReference: function(obj) { + var id; + if (obj[this.id] == null) { + id = this.counter++; + this.weakmap[id] = { + callbacks: {} + }; + Object.defineProperty(obj, this.id, { + value: id + }); + } + return this.weakmap[obj[this.id]]; + }, + stubFunction: function(obj, fn) { + var map, original, weakmap; + original = obj[fn]; + map = this.weakReference(obj); + weakmap = this.weakmap; + return obj[fn] = function() { + var callback, k, r, response, _i, _len, _ref1, _ref2, _ref3, _ref4; + response = original.apply(obj, arguments); + _ref1 = map.pointers; + for (r in _ref1) { + k = _ref1[r]; + _ref4 = (_ref2 = (_ref3 = weakmap[r]) != null ? _ref3.callbacks[k] : void 0) != null ? _ref2 : []; + for (_i = 0, _len = _ref4.length; _i < _len; _i++) { + callback = _ref4[_i]; + callback(); + } + } + return response; + }; + }, + observeMutations: function(obj, ref, keypath) { + var fn, functions, map, _base, _i, _len; + if (Array.isArray(obj)) { + map = this.weakReference(obj); + if (map.pointers == null) { + map.pointers = {}; + functions = ['push', 'pop', 'shift', 'unshift', 'sort', 'reverse', 'splice']; + for (_i = 0, _len = functions.length; _i < _len; _i++) { + fn = functions[_i]; + this.stubFunction(obj, fn); + } + } + if ((_base = map.pointers)[ref] == null) { + _base[ref] = []; + } + if (__indexOf.call(map.pointers[ref], keypath) < 0) { + return map.pointers[ref].push(keypath); + } + } + }, + unobserveMutations: function(obj, ref, keypath) { + var keypaths, _ref1; + if (Array.isArray(obj && (obj[this.id] != null))) { + if (keypaths = (_ref1 = this.weakReference(obj).pointers) != null ? _ref1[ref] : void 0) { + return keypaths.splice(keypaths.indexOf(keypath), 1); + } + } + }, + subscribe: function(obj, keypath, callback) { + var callbacks, value, + _this = this; + callbacks = this.weakReference(obj).callbacks; + if (callbacks[keypath] == null) { + callbacks[keypath] = []; + value = obj[keypath]; + Object.defineProperty(obj, keypath, { + enumerable: true, + get: function() { + return value; + }, + set: function(newValue) { + var _i, _len, _ref1; + if (newValue !== value) { + value = newValue; + _ref1 = callbacks[keypath]; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + callback = _ref1[_i]; + callback(); + } + return _this.observeMutations(newValue, obj[_this.id], keypath); + } + } + }); + } + if (__indexOf.call(callbacks[keypath], callback) < 0) { + callbacks[keypath].push(callback); + } + return this.observeMutations(obj[keypath], obj[this.id], keypath); + }, + unsubscribe: function(obj, keypath, callback) { + var callbacks; + callbacks = this.weakmap[obj[this.id]].callbacks[keypath]; + callbacks.splice(callbacks.indexOf(callback), 1); + return this.unobserveMutations(obj[keypath], obj[this.id], keypath); + }, + read: function(obj, keypath) { + return obj[keypath]; + }, + publish: function(obj, keypath, value) { + return obj[keypath] = value; + } + }; + + Rivets.factory = function(exports) { + exports._ = Rivets; + exports.binders = Rivets.binders; + exports.components = Rivets.components; + exports.formatters = Rivets.formatters; + exports.adapters = Rivets.adapters; + 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); diff --git a/ajax/libs/rivets/0.6.7/rivets.min.js b/ajax/libs/rivets/0.6.7/rivets.min.js new file mode 100644 index 000000000..621cf7f7e --- /dev/null +++ b/ajax/libs/rivets/0.6.7/rivets.min.js @@ -0,0 +1,5 @@ +// Rivets.js +// version: 0.6.7 +// author: Michael Richards +// license: MIT +(function(){var a,b,c,d,e=function(a,b){return function(){return a.apply(b,arguments)}},f=[].indexOf||function(a){for(var b=0,c=this.length;c>b;b++)if(b in this&&this[b]===a)return b;return-1},g=[].slice,h={}.hasOwnProperty,i=function(a,b){function c(){this.constructor=a}for(var d in b)h.call(b,d)&&(a[d]=b[d]);return c.prototype=b.prototype,a.prototype=new c,a.__super__=b.prototype,a};a={binders:{},components:{},formatters:{},adapters:{},config:{prefix:"rv",templateDelimiters:["{","}"],rootInterface:".",preloadData:!0,handler:function(a,b,c){return this.call(a,b,c.view.models)}}},"jQuery"in window?(d="on"in jQuery?["on","off"]:["bind","unbind"],b=d[0],c=d[1],a.Util={bindEvent:function(a,c,d){return jQuery(a)[b](c,d)},unbindEvent:function(a,b,d){return jQuery(a)[c](b,d)},getInputValue:function(a){var b;return b=jQuery(a),"checkbox"===b.attr("type")?b.is(":checked"):b.val()}}):a.Util={bindEvent:function(){return"addEventListener"in window?function(a,b,c){return a.addEventListener(b,c,!1)}:function(a,b,c){return a.attachEvent("on"+b,c)}}(),unbindEvent:function(){return"removeEventListener"in window?function(a,b,c){return a.removeEventListener(b,c,!1)}:function(a,b,c){return a.detachEvent("on"+b,c)}}(),getInputValue:function(a){var b,c,d,e;if("checkbox"===a.type)return a.checked;if("select-multiple"===a.type){for(e=[],c=0,d=a.length;d>c;c++)b=a[c],b.selected&&e.push(b.value);return e}return a.value}},a.View=function(){function b(b,c,d){var f,g,h,i,j,k,l,m,n;for(this.els=b,this.models=c,this.options=null!=d?d:{},this.update=e(this.update,this),this.publish=e(this.publish,this),this.sync=e(this.sync,this),this.unbind=e(this.unbind,this),this.bind=e(this.bind,this),this.select=e(this.select,this),this.build=e(this.build,this),this.componentRegExp=e(this.componentRegExp,this),this.bindingRegExp=e(this.bindingRegExp,this),this.els.jquery||this.els instanceof Array||(this.els=[this.els]),l=["config","binders","formatters","adapters"],j=0,k=l.length;k>j;j++){if(g=l[j],this[g]={},this.options[g]){m=this.options[g];for(f in m)h=m[f],this[g][f]=h}n=a[g];for(f in n)h=n[f],null==(i=this[g])[f]&&(i[f]=h)}this.build()}return b.prototype.bindingRegExp=function(){return new RegExp("^"+this.config.prefix+"-")},b.prototype.componentRegExp=function(){return new RegExp("^"+this.config.prefix.toUpperCase()+"-")},b.prototype.build=function(){var b,c,d,e,g,h,i,j,k,l=this;for(this.bindings=[],h=[],b=this.bindingRegExp(),d=this.componentRegExp(),c=function(b,c,d,e){var f,g,h,i,j,k,m;return j={},m=function(){var a,b,c,d;for(c=e.split("|"),d=[],a=0,b=c.length;b>a;a++)k=c[a],d.push(k.trim());return d}(),f=function(){var a,b,c,d;for(c=m.shift().split("<"),d=[],a=0,b=c.length;b>a;a++)g=c[a],d.push(g.trim());return d}(),i=f.shift(),j.formatters=m,(h=f.shift())&&(j.dependencies=h.split(/\s+/)),l.bindings.push(new a[b](l,c,d,i,j))},g=function(e){var i,j,k,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M;if(f.call(h,e)<0){if(3===e.nodeType){if(q=a.TextTemplateParser,(n=l.config.templateDelimiters)&&(u=q.parse(e.data,n)).length&&(1!==u.length||u[0].type!==q.types.text)){for(x=0,B=u.length;B>x;x++)t=u[x],s=document.createTextNode(t.value),e.parentNode.insertBefore(s,e),1===t.type&&c("TextBinding",s,null,t.value);e.parentNode.removeChild(e)}}else if(d.test(e.tagName))v=e.tagName.replace(d,"").toLowerCase(),l.bindings.push(new a.ComponentBinding(l,e,v));else if(null!=e.attributes){for(H=e.attributes,y=0,C=H.length;C>y;y++)if(i=H[y],b.test(i.name)){if(v=i.name.replace(b,""),!(k=l.binders[v])){I=l.binders;for(o in I)w=I[o],"*"!==o&&-1!==o.indexOf("*")&&(r=new RegExp("^"+o.replace("*",".+")+"$"),r.test(v)&&(k=w))}if(k||(k=l.binders["*"]),k.block){for(J=e.childNodes,z=0,D=J.length;D>z;z++)p=J[z],h.push(p);j=[i]}}for(K=j||e.attributes,A=0,E=K.length;E>A;A++)i=K[A],b.test(i.name)&&(v=i.name.replace(b,""),c("Binding",e,v,i.value))}for(L=function(){var a,b,c,d;for(c=e.childNodes,d=[],b=0,a=c.length;a>b;b++)p=c[b],d.push(p);return d}(),M=[],G=0,F=L.length;F>G;G++)m=L[G],M.push(g(m));return M}},k=this.els,i=0,j=k.length;j>i;i++)e=k[i],g(e)},b.prototype.select=function(a){var b,c,d,e,f;for(e=this.bindings,f=[],c=0,d=e.length;d>c;c++)b=e[c],a(b)&&f.push(b);return f},b.prototype.bind=function(){var a,b,c,d,e;for(d=this.bindings,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.bind());return e},b.prototype.unbind=function(){var a,b,c,d,e;for(d=this.bindings,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.unbind());return e},b.prototype.sync=function(){var a,b,c,d,e;for(d=this.bindings,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.sync());return e},b.prototype.publish=function(){var a,b,c,d,e;for(d=this.select(function(a){return a.binder.publishes}),e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.publish());return e},b.prototype.update=function(a){var b,c,d,e,f,g,h;null==a&&(a={});for(c in a)d=a[c],this.models[c]=d;for(g=this.bindings,h=[],e=0,f=g.length;f>e;e++)b=g[e],h.push(b.update(a));return h},b}(),a.Binding=function(){function b(a,b,c,d,f){this.view=a,this.el=b,this.type=c,this.keypath=d,this.options=null!=f?f:{},this.update=e(this.update,this),this.unbind=e(this.unbind,this),this.bind=e(this.bind,this),this.publish=e(this.publish,this),this.sync=e(this.sync,this),this.set=e(this.set,this),this.eventHandler=e(this.eventHandler,this),this.formattedValue=e(this.formattedValue,this),this.setBinder=e(this.setBinder,this),this.formatters=this.options.formatters||[],this.dependencies=[],this.model=void 0,this.setBinder()}return b.prototype.setBinder=function(){var a,b,c,d;if(!(this.binder=this.view.binders[this.type])){d=this.view.binders;for(a in d)c=d[a],"*"!==a&&-1!==a.indexOf("*")&&(b=new RegExp("^"+a.replace("*",".+")+"$"),b.test(this.type)&&(this.binder=c,this.args=new RegExp("^"+a.replace("*","(.+)")+"$").exec(this.type),this.args.shift()))}return this.binder||(this.binder=this.view.binders["*"]),this.binder instanceof Function?this.binder={routine:this.binder}:void 0},b.prototype.formattedValue=function(a){var b,c,d,e,f,h;for(h=this.formatters,e=0,f=h.length;f>e;e++)c=h[e],b=c.split(/\s+/),d=b.shift(),c=this.view.formatters[d],(null!=c?c.read:void 0)instanceof Function?a=c.read.apply(c,[a].concat(g.call(b))):c instanceof Function&&(a=c.apply(null,[a].concat(g.call(b))));return a},b.prototype.eventHandler=function(a){var b,c;return c=(b=this).view.config.handler,function(d){return c.call(a,this,d,b)}},b.prototype.set=function(a){var b;return a=a instanceof Function&&!this.binder["function"]?this.formattedValue(a.call(this.model)):this.formattedValue(a),null!=(b=this.binder.routine)?b.call(this,this.el,a):void 0},b.prototype.sync=function(){var b,c,d,e,f,g,h,i,j;if(this.model!==this.observer.target){for(h=this.dependencies,d=0,f=h.length;f>d;d++)c=h[d],c.unobserve();if(this.dependencies=[],null!=(this.model=this.observer.target)&&(null!=(i=this.options.dependencies)?i.length:void 0))for(j=this.options.dependencies,e=0,g=j.length;g>e;e++)b=j[e],c=new a.Observer(this.view,this.model,b,this.sync),this.dependencies.push(c)}return this.set(this.observer.value())},b.prototype.publish=function(){var b,c,d,e,f,h,i,j,k;for(e=a.Util.getInputValue(this.el),i=this.formatters.slice(0).reverse(),f=0,h=i.length;h>f;f++)c=i[f],b=c.split(/\s+/),d=b.shift(),(null!=(j=this.view.formatters[d])?j.publish:void 0)&&(e=(k=this.view.formatters[d]).publish.apply(k,[e].concat(g.call(b))));return this.observer.publish(e)},b.prototype.bind=function(){var b,c,d,e,f,g,h;if(null!=(f=this.binder.bind)&&f.call(this,this.el),this.observer=new a.Observer(this.view,this.view.models,this.keypath,this.sync),this.model=this.observer.target,null!=this.model&&(null!=(g=this.options.dependencies)?g.length:void 0))for(h=this.options.dependencies,d=0,e=h.length;e>d;d++)b=h[d],c=new a.Observer(this.view,this.model,b,this.sync),this.dependencies.push(c);return this.view.config.preloadData?this.sync():void 0},b.prototype.unbind=function(){var a,b,c,d,e;for(null!=(d=this.binder.unbind)&&d.call(this,this.el),this.observer.unobserve(),e=this.dependencies,b=0,c=e.length;c>b;b++)a=e[b],a.unobserve();return this.dependencies=[]},b.prototype.update=function(a){var b;return null==a&&(a={}),this.model=this.observer.target,null!=(b=this.binder.update)?b.call(this,a):void 0},b}(),a.ComponentBinding=function(b){function c(b,c,d){var g,h,i,j,k;for(this.view=b,this.el=c,this.type=d,this.unbind=e(this.unbind,this),this.bind=e(this.bind,this),this.update=e(this.update,this),this.locals=e(this.locals,this),this.component=a.components[this.type],this.attributes={},this.inflections={},j=this.el.attributes||[],h=0,i=j.length;i>h;h++)g=j[h],k=g.name,f.call(this.component.attributes,k)>=0?this.attributes[g.name]=g.value:this.inflections[g.name]=g.value}return i(c,b),c.prototype.sync=function(){},c.prototype.locals=function(a){var b,c,d,e,f,g,h,i,j;null==a&&(a=this.view.models),f={},i=this.inflections;for(c in i)for(b=i[c],j=b.split("."),g=0,h=j.length;h>g;g++)e=j[g],f[c]=(f[c]||a)[e];for(c in a)d=a[c],null==f[c]&&(f[c]=d);return f},c.prototype.update=function(a){var b;return null!=(b=this.componentView)?b.update(this.locals(a)):void 0},c.prototype.bind=function(){var b,c;return null!=this.componentView?null!=(c=this.componentView)?c.bind():void 0:(b=this.component.build.call(this.attributes),(this.componentView=new a.View(b,this.locals(),this.view.options)).bind(),this.el.parentNode.replaceChild(b,this.el))},c.prototype.unbind=function(){var a;return null!=(a=this.componentView)?a.unbind():void 0},c}(a.Binding),a.TextBinding=function(a){function b(a,b,c,d,f){this.view=a,this.el=b,this.type=c,this.keypath=d,this.options=null!=f?f:{},this.sync=e(this.sync,this),this.formatters=this.options.formatters||[],this.dependencies=[]}return i(b,a),b.prototype.binder={routine:function(a,b){return a.data=null!=b?b:""}},b.prototype.sync=function(){return b.__super__.sync.apply(this,arguments)},b}(a.Binding),a.KeypathParser=function(){function a(){}return a.parse=function(a,b,c){var d,e,g,h,i,j;for(h=[],e={"interface":c,path:""},g=i=0,j=a.length;j>i;g=i+=1)d=a.charAt(g),f.call(b,d)>=0?(h.push(e),e={"interface":d,path:""}):e.path+=d;return h.push(e),h},a}(),a.TextTemplateParser=function(){function a(){}return a.types={text:0,binding:1},a.parse=function(a,b){var c,d,e,f,g,h,i;for(h=[],f=a.length,c=0,d=0;f>d;){if(c=a.indexOf(b[0],d),0>c){h.push({type:this.types.text,value:a.slice(d)});break}if(c>0&&c>d&&h.push({type:this.types.text,value:a.slice(d,c)}),d=c+b[0].length,c=a.indexOf(b[1],d),0>c){g=a.slice(d-b[1].length),e=h[h.length-1],(null!=e?e.type:void 0)===this.types.text?e.value+=g:h.push({type:this.types.text,value:g});break}i=a.slice(d,c).trim(),h.push({type:this.types.binding,value:i}),d=c+b[1].length}return h},a}(),a.Observer=function(){function b(a,b,c,d){this.view=a,this.model=b,this.keypath=c,this.callback=d,this.unobserve=e(this.unobserve,this),this.realize=e(this.realize,this),this.value=e(this.value,this),this.publish=e(this.publish,this),this.read=e(this.read,this),this.set=e(this.set,this),this.adapter=e(this.adapter,this),this.update=e(this.update,this),this.initialize=e(this.initialize,this),this.parse=e(this.parse,this),this.parse(),this.initialize()}return b.prototype.parse=function(){var b,c,d,e,g,h;return b=function(){var a,b;a=this.view.adapters,b=[];for(c in a)g=a[c],b.push(c);return b}.call(this),h=this.keypath[0],f.call(b,h)>=0?(e=this.keypath[0],d=this.keypath.substr(1)):(e=this.view.config.rootInterface,d=this.keypath),this.tokens=a.KeypathParser.parse(d,b,e),this.key=this.tokens.pop()},b.prototype.initialize=function(){return this.objectPath=[],this.target=this.realize(),null!=this.target?this.set(!0,this.key,this.target,this.callback):void 0},b.prototype.update=function(){var a,b;return(a=this.realize())!==this.target&&(null!=this.target&&this.set(!1,this.key,this.target,this.callback),null!=a&&this.set(!0,this.key,a,this.callback),b=this.value(),this.target=a,this.value()!==b)?this.callback():void 0},b.prototype.adapter=function(a){return this.view.adapters[a["interface"]]},b.prototype.set=function(a,b,c,d){var e;return e=a?"subscribe":"unsubscribe",this.adapter(b)[e](c,b.path,d)},b.prototype.read=function(a,b){return this.adapter(a).read(b,a.path)},b.prototype.publish=function(a){return null!=this.target?this.adapter(this.key).publish(this.target,this.key.path,a):void 0},b.prototype.value=function(){return null!=this.target?this.read(this.key,this.target):void 0},b.prototype.realize=function(){var a,b,c,d,e,f,g,h;for(a=this.model,e=null,h=this.tokens,b=f=0,g=h.length;g>f;b=++f)d=h[b],null!=a?(null!=this.objectPath[b]?a!==(c=this.objectPath[b])&&(this.set(!1,d,c,this.update),this.set(!0,d,a,this.update),this.objectPath[b]=a):(this.set(!0,d,a,this.update),this.objectPath[b]=a),a=this.read(d,a)):(null==e&&(e=b),(c=this.objectPath[b])&&this.set(!1,d,c,this.update));return null!=e&&this.objectPath.splice(e),a},b.prototype.unobserve=function(){var a,b,c,d,e,f,g;for(f=this.tokens,g=[],a=d=0,e=f.length;e>d;a=++d)c=f[a],(b=this.objectPath[a])?g.push(this.set(!1,c,b,this.update)):g.push(void 0);return g},b}(),a.binders.text=function(a,b){return null!=a.textContent?a.textContent=null!=b?b:"":a.innerText=null!=b?b:""},a.binders.html=function(a,b){return a.innerHTML=null!=b?b:""},a.binders.show=function(a,b){return a.style.display=b?"":"none"},a.binders.hide=function(a,b){return a.style.display=b?"none":""},a.binders.enabled=function(a,b){return a.disabled=!b},a.binders.disabled=function(a,b){return a.disabled=!!b},a.binders.checked={publishes:!0,bind:function(b){return a.Util.bindEvent(b,"change",this.publish)},unbind:function(b){return a.Util.unbindEvent(b,"change",this.publish)},routine:function(a,b){var c;return a.checked="radio"===a.type?(null!=(c=a.value)?c.toString():void 0)===(null!=b?b.toString():void 0):!!b}},a.binders.unchecked={publishes:!0,bind:function(b){return a.Util.bindEvent(b,"change",this.publish)},unbind:function(b){return a.Util.unbindEvent(b,"change",this.publish)},routine:function(a,b){var c;return a.checked="radio"===a.type?(null!=(c=a.value)?c.toString():void 0)!==(null!=b?b.toString():void 0):!b}},a.binders.value={publishes:!0,bind:function(b){return a.Util.bindEvent(b,"change",this.publish)},unbind:function(b){return a.Util.unbindEvent(b,"change",this.publish)},routine:function(a,b){var c,d,e,g,h,i,j;if(null!=window.jQuery){if(a=jQuery(a),(null!=b?b.toString():void 0)!==(null!=(g=a.val())?g.toString():void 0))return a.val(null!=b?b:"")}else if("select-multiple"===a.type){if(null!=b){for(j=[],d=0,e=a.length;e>d;d++)c=a[d],j.push(c.selected=(h=c.value,f.call(b,h)>=0));return j}}else if((null!=b?b.toString():void 0)!==(null!=(i=a.value)?i.toString():void 0))return a.value=null!=b?b:""}},a.binders["if"]={block:!0,bind:function(a){var b,c;return null==this.marker?(b=[this.view.config.prefix,this.type].join("-").replace("--","-"),c=a.getAttribute(b),this.marker=document.createComment(" rivets: "+this.type+" "+c+" "),a.removeAttribute(b),a.parentNode.insertBefore(this.marker,a),a.parentNode.removeChild(a)):void 0},unbind:function(){var a;return null!=(a=this.nested)?a.unbind():void 0},routine:function(b,c){var d,e,f,g,h;if(!!c==(null==this.nested)){if(c){f={},h=this.view.models;for(d in h)e=h[d],f[d]=e;return g={binders:this.view.options.binders,formatters:this.view.options.formatters,adapters:this.view.options.adapters,config:this.view.options.config},(this.nested=new a.View(b,f,g)).bind(),this.marker.parentNode.insertBefore(b,this.marker.nextSibling)}return b.parentNode.removeChild(b),this.nested.unbind(),delete this.nested}},update:function(a){var b;return null!=(b=this.nested)?b.update(a):void 0}},a.binders.unless={block:!0,bind:function(b){return a.binders["if"].bind.call(this,b)},unbind:function(){return a.binders["if"].unbind.call(this)},routine:function(b,c){return a.binders["if"].routine.call(this,b,!c)},update:function(b){return a.binders["if"].update.call(this,b)}},a.binders["on-*"]={"function":!0,unbind:function(b){return this.handler?a.Util.unbindEvent(b,this.args[0],this.handler):void 0},routine:function(b,c){return this.handler&&a.Util.unbindEvent(b,this.args[0],this.handler),a.Util.bindEvent(b,this.args[0],this.handler=this.eventHandler(c))}},a.binders["each-*"]={block:!0,bind:function(a){var b;return null==this.marker?(b=[this.view.config.prefix,this.type].join("-").replace("--","-"),this.marker=document.createComment(" rivets: "+this.type+" "),this.iterated=[],a.removeAttribute(b),a.parentNode.insertBefore(this.marker,a),a.parentNode.removeChild(a)):void 0},unbind:function(){var a,b,c,d,e;if(null!=this.iterated){for(d=this.iterated,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.unbind());return e}},routine:function(b,c){var d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A;if(k=this.args[0],c=c||[],this.iterated.length>c.length)for(w=Array(this.iterated.length-c.length),q=0,t=w.length;t>q;q++)f=w[q],p=this.iterated.pop(),p.unbind(),this.marker.parentNode.removeChild(p.els[0]);for(g=r=0,u=c.length;u>r;g=++r)if(j=c[g],e={index:g},e[k]=j,null==this.iterated[g]){x=this.view.models;for(i in x)j=x[i],null==e[i]&&(e[i]=j);m=this.iterated.length?this.iterated[this.iterated.length-1].els[0]:this.marker,l={binders:this.view.options.binders,formatters:this.view.options.formatters,adapters:this.view.options.adapters,config:{}},y=this.view.options.config;for(h in y)o=y[h],l.config[h]=o;l.config.preloadData=!0,n=b.cloneNode(!0),p=new a.View(n,e,l),p.bind(),this.iterated.push(p),this.marker.parentNode.insertBefore(n,m.nextSibling)}else this.iterated[g].models[k]!==j&&this.iterated[g].update(e);if("OPTION"===b.nodeName){for(z=this.view.bindings,A=[],s=0,v=z.length;v>s;s++)d=z[s],d.el===this.marker.parentNode&&"value"===d.type?A.push(d.sync()):A.push(void 0);return A}},update:function(a){var b,c,d,e,f,g,h,i;b={};for(c in a)d=a[c],c!==this.args[0]&&(b[c]=d);for(h=this.iterated,i=[],f=0,g=h.length;g>f;f++)e=h[f],i.push(e.update(b));return i}},a.binders["class-*"]=function(a,b){var c;return c=" "+a.className+" ",!b==(-1!==c.indexOf(" "+this.args[0]+" "))?a.className=b?""+a.className+" "+this.args[0]:c.replace(" "+this.args[0]+" "," ").trim():void 0},a.binders["*"]=function(a,b){return null!=b?a.setAttribute(this.type,b):a.removeAttribute(this.type)},a.adapters["."]={id:"_rv",counter:0,weakmap:{},weakReference:function(a){var b;return null==a[this.id]&&(b=this.counter++,this.weakmap[b]={callbacks:{}},Object.defineProperty(a,this.id,{value:b})),this.weakmap[a[this.id]]},stubFunction:function(a,b){var c,d,e;return d=a[b],c=this.weakReference(a),e=this.weakmap,a[b]=function(){var b,f,g,h,i,j,k,l,m,n;h=d.apply(a,arguments),k=c.pointers;for(g in k)for(f=k[g],n=null!=(l=null!=(m=e[g])?m.callbacks[f]:void 0)?l:[],i=0,j=n.length;j>i;i++)b=n[i],b();return h}},observeMutations:function(a,b,c){var d,e,g,h,i,j;if(Array.isArray(a)){if(g=this.weakReference(a),null==g.pointers)for(g.pointers={},e=["push","pop","shift","unshift","sort","reverse","splice"],i=0,j=e.length;j>i;i++)d=e[i],this.stubFunction(a,d);if(null==(h=g.pointers)[b]&&(h[b]=[]),f.call(g.pointers[b],c)<0)return g.pointers[b].push(c)}},unobserveMutations:function(a,b,c){var d,e;return Array.isArray(a&&null!=a[this.id])&&(d=null!=(e=this.weakReference(a).pointers)?e[b]:void 0)?d.splice(d.indexOf(c),1):void 0},subscribe:function(a,b,c){var d,e,g=this;return d=this.weakReference(a).callbacks,null==d[b]&&(d[b]=[],e=a[b],Object.defineProperty(a,b,{enumerable:!0,get:function(){return e},set:function(f){var h,i,j;if(f!==e){for(e=f,j=d[b],h=0,i=j.length;i>h;h++)c=j[h],c();return g.observeMutations(f,a[g.id],b)}}})),f.call(d[b],c)<0&&d[b].push(c),this.observeMutations(a[b],a[this.id],b)},unsubscribe:function(a,b,c){var d;return d=this.weakmap[a[this.id]].callbacks[b],d.splice(d.indexOf(c),1),this.unobserveMutations(a[b],a[this.id],b)},read:function(a,b){return a[b]},publish:function(a,b,c){return a[b]=c}},a.factory=function(b){return b._=a,b.binders=a.binders,b.components=a.components,b.formatters=a.formatters,b.adapters=a.adapters,b.config=a.config,b.configure=function(b){var c,d;null==b&&(b={});for(c in b)d=b[c],a.config[c]=d},b.bind=function(b,c,d){var e;return null==c&&(c={}),null==d&&(d={}),e=new a.View(b,c,d),e.bind(),e}},"object"==typeof exports?a.factory(exports):"function"==typeof define&&define.amd?define(["exports"],function(b){return a.factory(this.rivets=b),b}):a.factory(this.rivets={})}).call(this); \ No newline at end of file diff --git a/ajax/libs/rivets/0.6.8/rivets.js b/ajax/libs/rivets/0.6.8/rivets.js new file mode 100644 index 000000000..23caf2f02 --- /dev/null +++ b/ajax/libs/rivets/0.6.8/rivets.js @@ -0,0 +1,1292 @@ +// Rivets.js +// version: 0.6.8 +// author: Michael Richards +// license: MIT +(function() { + var Rivets, bindMethod, unbindMethod, _ref, + __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, + __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; }, + __slice = [].slice, + __hasProp = {}.hasOwnProperty, + __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; + + Rivets = { + binders: {}, + components: {}, + formatters: {}, + adapters: {}, + config: { + prefix: 'rv', + templateDelimiters: ['{', '}'], + rootInterface: '.', + preloadData: true, + handler: function(context, ev, binding) { + return this.call(context, ev, binding.view.models); + } + } + }; + + if ('jQuery' in window) { + _ref = 'on' in jQuery.prototype ? ['on', 'off'] : ['bind', 'unbind'], bindMethod = _ref[0], unbindMethod = _ref[1]; + Rivets.Util = { + bindEvent: function(el, event, handler) { + return jQuery(el)[bindMethod](event, handler); + }, + unbindEvent: function(el, event, handler) { + return jQuery(el)[unbindMethod](event, handler); + }, + getInputValue: function(el) { + var $el; + $el = jQuery(el); + if ($el.attr('type') === 'checkbox') { + return $el.is(':checked'); + } else { + return $el.val(); + } + } + }; + } else { + Rivets.Util = { + bindEvent: (function() { + if ('addEventListener' in window) { + return function(el, event, handler) { + return el.addEventListener(event, handler, false); + }; + } + return function(el, event, handler) { + return el.attachEvent('on' + event, handler); + }; + })(), + unbindEvent: (function() { + if ('removeEventListener' in window) { + return function(el, event, handler) { + return el.removeEventListener(event, handler, false); + }; + } + return function(el, event, handler) { + return el.detachEvent('on' + event, handler); + }; + })(), + getInputValue: function(el) { + var o, _i, _len, _results; + if (el.type === 'checkbox') { + return el.checked; + } else if (el.type === 'select-multiple') { + _results = []; + for (_i = 0, _len = el.length; _i < _len; _i++) { + o = el[_i]; + if (o.selected) { + _results.push(o.value); + } + } + return _results; + } else { + return el.value; + } + } + }; + } + + Rivets.View = (function() { + function View(els, models, options) { + var k, option, v, _base, _i, _len, _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.componentRegExp = __bind(this.componentRegExp, this); + this.bindingRegExp = __bind(this.bindingRegExp, this); + if (!(this.els.jquery || this.els instanceof Array)) { + this.els = [this.els]; + } + _ref1 = ['config', 'binders', 'formatters', 'adapters']; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + option = _ref1[_i]; + this[option] = {}; + if (this.options[option]) { + _ref2 = this.options[option]; + for (k in _ref2) { + v = _ref2[k]; + this[option][k] = v; + } + } + _ref3 = Rivets[option]; + for (k in _ref3) { + v = _ref3[k]; + if ((_base = this[option])[k] == null) { + _base[k] = v; + } + } + } + this.build(); + } + + View.prototype.bindingRegExp = function() { + return new RegExp("^" + this.config.prefix + "-"); + }; + + View.prototype.componentRegExp = function() { + return new RegExp("^" + (this.config.prefix.toUpperCase()) + "-"); + }; + + View.prototype.build = function() { + var bindingRegExp, buildBinding, componentRegExp, el, parse, skipNodes, _i, _len, _ref1, + _this = this; + this.bindings = []; + skipNodes = []; + bindingRegExp = this.bindingRegExp(); + componentRegExp = this.componentRegExp(); + buildBinding = function(binding, node, type, declaration) { + var context, ctx, dependencies, keypath, options, pipe, pipes; + options = {}; + pipes = (function() { + var _i, _len, _ref1, _results; + _ref1 = declaration.split('|'); + _results = []; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + pipe = _ref1[_i]; + _results.push(pipe.trim()); + } + return _results; + })(); + context = (function() { + var _i, _len, _ref1, _results; + _ref1 = pipes.shift().split('<'); + _results = []; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + ctx = _ref1[_i]; + _results.push(ctx.trim()); + } + return _results; + })(); + keypath = context.shift(); + options.formatters = pipes; + if (dependencies = context.shift()) { + options.dependencies = dependencies.split(/\s+/); + } + return _this.bindings.push(new Rivets[binding](_this, node, type, keypath, options)); + }; + parse = function(node) { + var attribute, attributes, binder, childNode, delimiters, identifier, n, parser, regexp, text, token, tokens, type, value, _i, _j, _k, _l, _len, _len1, _len2, _len3, _len4, _m, _ref1, _ref2, _ref3, _ref4, _ref5, _results; + if (__indexOf.call(skipNodes, node) < 0) { + if (node.nodeType === 3) { + 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)) { + for (_i = 0, _len = tokens.length; _i < _len; _i++) { + token = tokens[_i]; + text = document.createTextNode(token.value); + node.parentNode.insertBefore(text, node); + if (token.type === 1) { + buildBinding('TextBinding', text, null, token.value); + } + } + node.parentNode.removeChild(node); + } + } + } + } else if (componentRegExp.test(node.tagName)) { + type = node.tagName.replace(componentRegExp, '').toLowerCase(); + _this.bindings.push(new Rivets.ComponentBinding(_this, node, type)); + } else if (node.attributes != null) { + _ref1 = node.attributes; + for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { + attribute = _ref1[_j]; + if (bindingRegExp.test(attribute.name)) { + type = attribute.name.replace(bindingRegExp, ''); + if (!(binder = _this.binders[type])) { + _ref2 = _this.binders; + for (identifier in _ref2) { + value = _ref2[identifier]; + if (identifier !== '*' && identifier.indexOf('*') !== -1) { + regexp = new RegExp("^" + (identifier.replace('*', '.+')) + "$"); + if (regexp.test(type)) { + binder = value; + } + } + } + } + binder || (binder = _this.binders['*']); + if (binder.block) { + _ref3 = node.childNodes; + for (_k = 0, _len2 = _ref3.length; _k < _len2; _k++) { + n = _ref3[_k]; + skipNodes.push(n); + } + attributes = [attribute]; + } + } + } + _ref4 = attributes || node.attributes; + for (_l = 0, _len3 = _ref4.length; _l < _len3; _l++) { + attribute = _ref4[_l]; + if (bindingRegExp.test(attribute.name)) { + type = attribute.name.replace(bindingRegExp, ''); + buildBinding('Binding', node, type, attribute.value); + } + } + } + _ref5 = (function() { + var _len4, _n, _ref5, _results1; + _ref5 = node.childNodes; + _results1 = []; + for (_n = 0, _len4 = _ref5.length; _n < _len4; _n++) { + n = _ref5[_n]; + _results1.push(n); + } + return _results1; + })(); + _results = []; + for (_m = 0, _len4 = _ref5.length; _m < _len4; _m++) { + childNode = _ref5[_m]; + _results.push(parse(childNode)); + } + return _results; + } + }; + _ref1 = this.els; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + el = _ref1[_i]; + parse(el); + } + }; + + View.prototype.select = function(fn) { + var binding, _i, _len, _ref1, _results; + _ref1 = this.bindings; + _results = []; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + binding = _ref1[_i]; + if (fn(binding)) { + _results.push(binding); + } + } + return _results; + }; + + View.prototype.bind = function() { + var binding, _i, _len, _ref1, _results; + _ref1 = this.bindings; + _results = []; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + binding = _ref1[_i]; + _results.push(binding.bind()); + } + return _results; + }; + + View.prototype.unbind = function() { + var binding, _i, _len, _ref1, _results; + _ref1 = this.bindings; + _results = []; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + binding = _ref1[_i]; + _results.push(binding.unbind()); + } + return _results; + }; + + View.prototype.sync = function() { + var binding, _i, _len, _ref1, _results; + _ref1 = this.bindings; + _results = []; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + binding = _ref1[_i]; + _results.push(binding.sync()); + } + return _results; + }; + + View.prototype.publish = function() { + var binding, _i, _len, _ref1, _results; + _ref1 = this.select(function(b) { + return b.binder.publishes; + }); + _results = []; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + binding = _ref1[_i]; + _results.push(binding.publish()); + } + return _results; + }; + + View.prototype.update = function(models) { + var binding, key, model, _i, _len, _ref1, _results; + if (models == null) { + models = {}; + } + for (key in models) { + model = models[key]; + this.models[key] = model; + } + _ref1 = this.bindings; + _results = []; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + binding = _ref1[_i]; + _results.push(binding.update(models)); + } + return _results; + }; + + return View; + + })(); + + Rivets.Binding = (function() { + function Binding(view, el, type, keypath, options) { + this.view = view; + this.el = el; + this.type = type; + 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); + this.setBinder = __bind(this.setBinder, this); + this.formatters = this.options.formatters || []; + this.dependencies = []; + this.model = void 0; + this.setBinder(); + } + + Binding.prototype.setBinder = function() { + var identifier, regexp, value, _ref1; + if (!(this.binder = this.view.binders[this.type])) { + _ref1 = this.view.binders; + for (identifier in _ref1) { + value = _ref1[identifier]; + if (identifier !== '*' && identifier.indexOf('*') !== -1) { + regexp = new RegExp("^" + (identifier.replace('*', '.+')) + "$"); + if (regexp.test(this.type)) { + this.binder = value; + this.args = new RegExp("^" + (identifier.replace('*', '(.+)')) + "$").exec(this.type); + this.args.shift(); + } + } + } + } + this.binder || (this.binder = this.view.binders['*']); + if (this.binder instanceof Function) { + return this.binder = { + routine: this.binder + }; + } + }; + + Binding.prototype.formattedValue = function(value) { + var args, formatter, id, _i, _len, _ref1; + _ref1 = this.formatters; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + formatter = _ref1[_i]; + args = formatter.split(/\s+/); + id = args.shift(); + formatter = 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 _ref1; + value = value instanceof Function && !this.binder["function"] ? this.formattedValue(value.call(this.model)) : this.formattedValue(value); + return (_ref1 = this.binder.routine) != null ? _ref1.call(this, this.el, value) : void 0; + }; + + Binding.prototype.sync = function() { + var dependency, observer, _i, _j, _len, _len1, _ref1, _ref2, _ref3; + if (this.model !== this.observer.target) { + _ref1 = this.dependencies; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + observer = _ref1[_i]; + observer.unobserve(); + } + this.dependencies = []; + if (((this.model = this.observer.target) != null) && ((_ref2 = this.options.dependencies) != null ? _ref2.length : void 0)) { + _ref3 = this.options.dependencies; + for (_j = 0, _len1 = _ref3.length; _j < _len1; _j++) { + dependency = _ref3[_j]; + observer = new Rivets.Observer(this.view, this.model, dependency, this.sync); + this.dependencies.push(observer); + } + } + } + return this.set(this.observer.value()); + }; + + Binding.prototype.publish = function() { + var args, formatter, id, value, _i, _len, _ref1, _ref2, _ref3; + value = Rivets.Util.getInputValue(this.el); + _ref1 = this.formatters.slice(0).reverse(); + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + formatter = _ref1[_i]; + args = formatter.split(/\s+/); + id = args.shift(); + if ((_ref2 = this.view.formatters[id]) != null ? _ref2.publish : void 0) { + value = (_ref3 = this.view.formatters[id]).publish.apply(_ref3, [value].concat(__slice.call(args))); + } + } + return this.observer.publish(value); + }; + + Binding.prototype.bind = function() { + var dependency, observer, _i, _len, _ref1, _ref2, _ref3; + if ((_ref1 = this.binder.bind) != null) { + _ref1.call(this, this.el); + } + this.observer = new Rivets.Observer(this.view, this.view.models, this.keypath, this.sync); + this.model = this.observer.target; + if ((this.model != null) && ((_ref2 = this.options.dependencies) != null ? _ref2.length : void 0)) { + _ref3 = this.options.dependencies; + for (_i = 0, _len = _ref3.length; _i < _len; _i++) { + dependency = _ref3[_i]; + observer = new Rivets.Observer(this.view, this.model, dependency, this.sync); + this.dependencies.push(observer); + } + } + if (this.view.config.preloadData) { + return this.sync(); + } + }; + + Binding.prototype.unbind = function() { + var observer, _i, _len, _ref1, _ref2; + if ((_ref1 = this.binder.unbind) != null) { + _ref1.call(this, this.el); + } + this.observer.unobserve(); + _ref2 = this.dependencies; + for (_i = 0, _len = _ref2.length; _i < _len; _i++) { + observer = _ref2[_i]; + observer.unobserve(); + } + return this.dependencies = []; + }; + + Binding.prototype.update = function(models) { + var _ref1; + if (models == null) { + models = {}; + } + this.model = this.observer.target; + return (_ref1 = this.binder.update) != null ? _ref1.call(this, models) : void 0; + }; + + return Binding; + + })(); + + Rivets.ComponentBinding = (function(_super) { + __extends(ComponentBinding, _super); + + function ComponentBinding(view, el, type) { + var attribute, _i, _len, _ref1, _ref2; + this.view = view; + this.el = el; + this.type = type; + this.unbind = __bind(this.unbind, this); + this.bind = __bind(this.bind, this); + this.update = __bind(this.update, this); + this.locals = __bind(this.locals, this); + this.component = Rivets.components[this.type]; + this.attributes = {}; + this.inflections = {}; + _ref1 = this.el.attributes || []; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + attribute = _ref1[_i]; + if (_ref2 = attribute.name, __indexOf.call(this.component.attributes, _ref2) >= 0) { + this.attributes[attribute.name] = attribute.value; + } else { + this.inflections[attribute.name] = attribute.value; + } + } + } + + ComponentBinding.prototype.sync = function() {}; + + ComponentBinding.prototype.locals = function(models) { + var inverse, key, model, path, result, _i, _len, _ref1, _ref2; + if (models == null) { + models = this.view.models; + } + result = {}; + _ref1 = this.inflections; + for (key in _ref1) { + inverse = _ref1[key]; + _ref2 = inverse.split('.'); + for (_i = 0, _len = _ref2.length; _i < _len; _i++) { + path = _ref2[_i]; + result[key] = (result[key] || models)[path]; + } + } + for (key in models) { + model = models[key]; + if (result[key] == null) { + result[key] = model; + } + } + return result; + }; + + ComponentBinding.prototype.update = function(models) { + var _ref1; + return (_ref1 = this.componentView) != null ? _ref1.update(this.locals(models)) : void 0; + }; + + ComponentBinding.prototype.bind = function() { + var el, _ref1; + if (this.componentView != null) { + return (_ref1 = this.componentView) != null ? _ref1.bind() : void 0; + } else { + el = this.component.build.call(this.attributes); + (this.componentView = new Rivets.View(el, this.locals(), this.view.options)).bind(); + return this.el.parentNode.replaceChild(el, this.el); + } + }; + + ComponentBinding.prototype.unbind = function() { + var _ref1; + return (_ref1 = this.componentView) != null ? _ref1.unbind() : void 0; + }; + + return ComponentBinding; + + })(Rivets.Binding); + + Rivets.TextBinding = (function(_super) { + __extends(TextBinding, _super); + + function TextBinding(view, el, type, keypath, options) { + this.view = view; + this.el = el; + this.type = type; + this.keypath = keypath; + this.options = options != null ? options : {}; + this.sync = __bind(this.sync, this); + this.formatters = this.options.formatters || []; + this.dependencies = []; + } + + TextBinding.prototype.binder = { + routine: function(node, value) { + return node.data = value != null ? value : ''; + } + }; + + TextBinding.prototype.sync = function() { + return TextBinding.__super__.sync.apply(this, arguments); + }; + + return TextBinding; + + })(Rivets.Binding); + + Rivets.KeypathParser = (function() { + function KeypathParser() {} + + KeypathParser.parse = function(keypath, interfaces, root) { + var char, current, index, tokens, _i, _ref1; + tokens = []; + current = { + "interface": root, + path: '' + }; + for (index = _i = 0, _ref1 = keypath.length; _i < _ref1; index = _i += 1) { + char = keypath.charAt(index); + if (__indexOf.call(interfaces, char) >= 0) { + tokens.push(current); + current = { + "interface": char, + path: '' + }; + } else { + current.path += char; + } + } + tokens.push(current); + return tokens; + }; + + return KeypathParser; + + })(); + + 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 + delimiters[0].length; + index = template.indexOf(delimiters[1], lastIndex); + if (index < 0) { + substring = template.slice(lastIndex - delimiters[1].length); + 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 + delimiters[1].length; + } + } + return tokens; + }; + + return TextTemplateParser; + + })(); + + Rivets.Observer = (function() { + function Observer(view, model, keypath, callback) { + this.view = view; + this.model = model; + this.keypath = keypath; + this.callback = callback; + this.unobserve = __bind(this.unobserve, this); + this.realize = __bind(this.realize, this); + this.value = __bind(this.value, this); + this.publish = __bind(this.publish, this); + this.read = __bind(this.read, this); + this.set = __bind(this.set, this); + this.adapter = __bind(this.adapter, this); + this.update = __bind(this.update, this); + this.initialize = __bind(this.initialize, this); + this.parse = __bind(this.parse, this); + this.parse(); + this.initialize(); + } + + Observer.prototype.parse = function() { + var interfaces, k, path, root, v, _ref1; + interfaces = (function() { + var _ref1, _results; + _ref1 = this.view.adapters; + _results = []; + for (k in _ref1) { + v = _ref1[k]; + _results.push(k); + } + return _results; + }).call(this); + if (_ref1 = this.keypath[0], __indexOf.call(interfaces, _ref1) >= 0) { + root = this.keypath[0]; + path = this.keypath.substr(1); + } else { + root = this.view.config.rootInterface; + path = this.keypath; + } + this.tokens = Rivets.KeypathParser.parse(path, interfaces, root); + return this.key = this.tokens.pop(); + }; + + Observer.prototype.initialize = function() { + this.objectPath = []; + this.target = this.realize(); + if (this.target != null) { + return this.set(true, this.key, this.target, this.callback); + } + }; + + Observer.prototype.update = function() { + var next, oldValue; + if ((next = this.realize()) !== this.target) { + if (this.target != null) { + this.set(false, this.key, this.target, this.callback); + } + if (next != null) { + this.set(true, this.key, next, this.callback); + } + oldValue = this.value(); + this.target = next; + if (this.value() !== oldValue) { + return this.callback(); + } + } + }; + + Observer.prototype.adapter = function(key) { + return this.view.adapters[key["interface"]]; + }; + + Observer.prototype.set = function(active, key, obj, callback) { + var action; + action = active ? 'subscribe' : 'unsubscribe'; + return this.adapter(key)[action](obj, key.path, callback); + }; + + Observer.prototype.read = function(key, obj) { + return this.adapter(key).read(obj, key.path); + }; + + Observer.prototype.publish = function(value) { + if (this.target != null) { + return this.adapter(this.key).publish(this.target, this.key.path, value); + } + }; + + Observer.prototype.value = function() { + if (this.target != null) { + return this.read(this.key, this.target); + } + }; + + Observer.prototype.realize = function() { + var current, index, prev, token, unreached, _i, _len, _ref1; + current = this.model; + unreached = null; + _ref1 = this.tokens; + for (index = _i = 0, _len = _ref1.length; _i < _len; index = ++_i) { + token = _ref1[index]; + if (current != null) { + if (this.objectPath[index] != null) { + if (current !== (prev = this.objectPath[index])) { + this.set(false, token, prev, this.update); + this.set(true, token, current, this.update); + this.objectPath[index] = current; + } + } else { + this.set(true, token, current, this.update); + this.objectPath[index] = current; + } + current = this.read(token, current); + } else { + if (unreached == null) { + unreached = index; + } + if (prev = this.objectPath[index]) { + this.set(false, token, prev, this.update); + } + } + } + if (unreached != null) { + this.objectPath.splice(unreached); + } + return current; + }; + + Observer.prototype.unobserve = function() { + var index, obj, token, _i, _len, _ref1; + _ref1 = this.tokens; + for (index = _i = 0, _len = _ref1.length; _i < _len; index = ++_i) { + token = _ref1[index]; + if (obj = this.objectPath[index]) { + this.set(false, token, obj, this.update); + } + } + if (this.target != null) { + return this.set(false, this.key, this.target, this.callback); + } + }; + + return Observer; + + })(); + + Rivets.binders.text = function(el, value) { + if (el.textContent != null) { + return el.textContent = value != null ? value : ''; + } else { + return el.innerText = value != null ? value : ''; + } + }; + + Rivets.binders.html = function(el, value) { + return el.innerHTML = value != null ? value : ''; + }; + + Rivets.binders.show = function(el, value) { + return el.style.display = value ? '' : 'none'; + }; + + Rivets.binders.hide = function(el, value) { + return el.style.display = value ? 'none' : ''; + }; + + Rivets.binders.enabled = function(el, value) { + return el.disabled = !value; + }; + + Rivets.binders.disabled = function(el, value) { + return el.disabled = !!value; + }; + + Rivets.binders.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 _ref1; + if (el.type === 'radio') { + return el.checked = ((_ref1 = el.value) != null ? _ref1.toString() : void 0) === (value != null ? value.toString() : void 0); + } else { + return el.checked = !!value; + } + } + }; + + Rivets.binders.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 _ref1; + if (el.type === 'radio') { + return el.checked = ((_ref1 = el.value) != null ? _ref1.toString() : void 0) !== (value != null ? value.toString() : void 0); + } else { + return el.checked = !value; + } + } + }; + + Rivets.binders.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, _ref1, _ref2, _ref3, _results; + if (window.jQuery != null) { + el = jQuery(el); + if ((value != null ? value.toString() : void 0) !== ((_ref1 = el.val()) != null ? _ref1.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 = (_ref2 = o.value, __indexOf.call(value, _ref2) >= 0)); + } + return _results; + } + } else if ((value != null ? value.toString() : void 0) !== ((_ref3 = el.value) != null ? _ref3.toString() : void 0)) { + return el.value = value != null ? value : ''; + } + } + } + }; + + Rivets.binders["if"] = { + block: true, + bind: function(el) { + var attr, declaration; + if (this.marker == null) { + attr = [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 _ref1; + return (_ref1 = this.nested) != null ? _ref1.unbind() : void 0; + }, + routine: function(el, value) { + var key, model, models, options, _ref1; + if (!!value === (this.nested == null)) { + if (value) { + models = {}; + _ref1 = this.view.models; + for (key in _ref1) { + model = _ref1[key]; + models[key] = model; + } + options = { + binders: this.view.options.binders, + formatters: this.view.options.formatters, + adapters: this.view.options.adapters, + 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) { + var _ref1; + return (_ref1 = this.nested) != null ? _ref1.update(models) : void 0; + } + }; + + Rivets.binders.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); + } + }; + + Rivets.binders['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)); + } + }; + + Rivets.binders['each-*'] = { + block: true, + bind: function(el) { + var attr; + if (this.marker == null) { + attr = [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, _ref1, _results; + if (this.iterated != null) { + _ref1 = this.iterated; + _results = []; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + view = _ref1[_i]; + _results.push(view.unbind()); + } + return _results; + } + }, + routine: function(el, collection) { + var binding, data, i, index, k, key, model, modelName, options, previous, template, v, view, _i, _j, _k, _len, _len1, _len2, _ref1, _ref2, _ref3, _ref4, _results; + modelName = this.args[0]; + collection = collection || []; + if (this.iterated.length > collection.length) { + _ref1 = Array(this.iterated.length - collection.length); + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + i = _ref1[_i]; + view = this.iterated.pop(); + view.unbind(); + this.marker.parentNode.removeChild(view.els[0]); + } + } + for (index = _j = 0, _len1 = collection.length; _j < _len1; index = ++_j) { + model = collection[index]; + data = { + index: index + }; + data[modelName] = model; + if (this.iterated[index] == null) { + _ref2 = this.view.models; + for (key in _ref2) { + model = _ref2[key]; + if (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, + adapters: this.view.options.adapters, + 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); + this.marker.parentNode.insertBefore(template, previous.nextSibling); + } else if (this.iterated[index].models[modelName] !== model) { + this.iterated[index].update(data); + } + } + if (el.nodeName === 'OPTION') { + _ref4 = this.view.bindings; + _results = []; + for (_k = 0, _len2 = _ref4.length; _k < _len2; _k++) { + binding = _ref4[_k]; + if (binding.el === this.marker.parentNode && binding.type === 'value') { + _results.push(binding.sync()); + } else { + _results.push(void 0); + } + } + return _results; + } + }, + update: function(models) { + var data, key, model, view, _i, _len, _ref1, _results; + data = {}; + for (key in models) { + model = models[key]; + if (key !== this.args[0]) { + data[key] = model; + } + } + _ref1 = this.iterated; + _results = []; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + view = _ref1[_i]; + _results.push(view.update(data)); + } + return _results; + } + }; + + Rivets.binders['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(); + } + }; + + Rivets.binders['*'] = function(el, value) { + if (value != null) { + return el.setAttribute(this.type, value); + } else { + return el.removeAttribute(this.type); + } + }; + + Rivets.adapters['.'] = { + id: '_rv', + counter: 0, + weakmap: {}, + weakReference: function(obj) { + var id; + if (obj[this.id] == null) { + id = this.counter++; + this.weakmap[id] = { + callbacks: {} + }; + Object.defineProperty(obj, this.id, { + value: id + }); + } + return this.weakmap[obj[this.id]]; + }, + stubFunction: function(obj, fn) { + var map, original, weakmap; + original = obj[fn]; + map = this.weakReference(obj); + weakmap = this.weakmap; + return obj[fn] = function() { + var callback, k, r, response, _i, _len, _ref1, _ref2, _ref3, _ref4; + response = original.apply(obj, arguments); + _ref1 = map.pointers; + for (r in _ref1) { + k = _ref1[r]; + _ref4 = (_ref2 = (_ref3 = weakmap[r]) != null ? _ref3.callbacks[k] : void 0) != null ? _ref2 : []; + for (_i = 0, _len = _ref4.length; _i < _len; _i++) { + callback = _ref4[_i]; + callback(); + } + } + return response; + }; + }, + observeMutations: function(obj, ref, keypath) { + var fn, functions, map, _base, _i, _len; + if (Array.isArray(obj)) { + map = this.weakReference(obj); + if (map.pointers == null) { + map.pointers = {}; + functions = ['push', 'pop', 'shift', 'unshift', 'sort', 'reverse', 'splice']; + for (_i = 0, _len = functions.length; _i < _len; _i++) { + fn = functions[_i]; + this.stubFunction(obj, fn); + } + } + if ((_base = map.pointers)[ref] == null) { + _base[ref] = []; + } + if (__indexOf.call(map.pointers[ref], keypath) < 0) { + return map.pointers[ref].push(keypath); + } + } + }, + unobserveMutations: function(obj, ref, keypath) { + var keypaths, _ref1; + if (Array.isArray(obj && (obj[this.id] != null))) { + if (keypaths = (_ref1 = this.weakReference(obj).pointers) != null ? _ref1[ref] : void 0) { + return keypaths.splice(keypaths.indexOf(keypath), 1); + } + } + }, + subscribe: function(obj, keypath, callback) { + var callbacks, value, + _this = this; + callbacks = this.weakReference(obj).callbacks; + if (callbacks[keypath] == null) { + callbacks[keypath] = []; + value = obj[keypath]; + Object.defineProperty(obj, keypath, { + enumerable: true, + get: function() { + return value; + }, + set: function(newValue) { + var _i, _len, _ref1; + if (newValue !== value) { + value = newValue; + _ref1 = callbacks[keypath]; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + callback = _ref1[_i]; + callback(); + } + return _this.observeMutations(newValue, obj[_this.id], keypath); + } + } + }); + } + if (__indexOf.call(callbacks[keypath], callback) < 0) { + callbacks[keypath].push(callback); + } + return this.observeMutations(obj[keypath], obj[this.id], keypath); + }, + unsubscribe: function(obj, keypath, callback) { + var callbacks; + callbacks = this.weakmap[obj[this.id]].callbacks[keypath]; + callbacks.splice(callbacks.indexOf(callback), 1); + return this.unobserveMutations(obj[keypath], obj[this.id], keypath); + }, + read: function(obj, keypath) { + return obj[keypath]; + }, + publish: function(obj, keypath, value) { + return obj[keypath] = value; + } + }; + + Rivets.factory = function(exports) { + exports._ = Rivets; + exports.binders = Rivets.binders; + exports.components = Rivets.components; + exports.formatters = Rivets.formatters; + exports.adapters = Rivets.adapters; + 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); diff --git a/ajax/libs/rivets/0.6.8/rivets.min.js b/ajax/libs/rivets/0.6.8/rivets.min.js new file mode 100644 index 000000000..16213ff97 --- /dev/null +++ b/ajax/libs/rivets/0.6.8/rivets.min.js @@ -0,0 +1,5 @@ +// Rivets.js +// version: 0.6.8 +// author: Michael Richards +// license: MIT +(function(){var a,b,c,d,e=function(a,b){return function(){return a.apply(b,arguments)}},f=[].indexOf||function(a){for(var b=0,c=this.length;c>b;b++)if(b in this&&this[b]===a)return b;return-1},g=[].slice,h={}.hasOwnProperty,i=function(a,b){function c(){this.constructor=a}for(var d in b)h.call(b,d)&&(a[d]=b[d]);return c.prototype=b.prototype,a.prototype=new c,a.__super__=b.prototype,a};a={binders:{},components:{},formatters:{},adapters:{},config:{prefix:"rv",templateDelimiters:["{","}"],rootInterface:".",preloadData:!0,handler:function(a,b,c){return this.call(a,b,c.view.models)}}},"jQuery"in window?(d="on"in jQuery.prototype?["on","off"]:["bind","unbind"],b=d[0],c=d[1],a.Util={bindEvent:function(a,c,d){return jQuery(a)[b](c,d)},unbindEvent:function(a,b,d){return jQuery(a)[c](b,d)},getInputValue:function(a){var b;return b=jQuery(a),"checkbox"===b.attr("type")?b.is(":checked"):b.val()}}):a.Util={bindEvent:function(){return"addEventListener"in window?function(a,b,c){return a.addEventListener(b,c,!1)}:function(a,b,c){return a.attachEvent("on"+b,c)}}(),unbindEvent:function(){return"removeEventListener"in window?function(a,b,c){return a.removeEventListener(b,c,!1)}:function(a,b,c){return a.detachEvent("on"+b,c)}}(),getInputValue:function(a){var b,c,d,e;if("checkbox"===a.type)return a.checked;if("select-multiple"===a.type){for(e=[],c=0,d=a.length;d>c;c++)b=a[c],b.selected&&e.push(b.value);return e}return a.value}},a.View=function(){function b(b,c,d){var f,g,h,i,j,k,l,m,n;for(this.els=b,this.models=c,this.options=null!=d?d:{},this.update=e(this.update,this),this.publish=e(this.publish,this),this.sync=e(this.sync,this),this.unbind=e(this.unbind,this),this.bind=e(this.bind,this),this.select=e(this.select,this),this.build=e(this.build,this),this.componentRegExp=e(this.componentRegExp,this),this.bindingRegExp=e(this.bindingRegExp,this),this.els.jquery||this.els instanceof Array||(this.els=[this.els]),l=["config","binders","formatters","adapters"],j=0,k=l.length;k>j;j++){if(g=l[j],this[g]={},this.options[g]){m=this.options[g];for(f in m)h=m[f],this[g][f]=h}n=a[g];for(f in n)h=n[f],null==(i=this[g])[f]&&(i[f]=h)}this.build()}return b.prototype.bindingRegExp=function(){return new RegExp("^"+this.config.prefix+"-")},b.prototype.componentRegExp=function(){return new RegExp("^"+this.config.prefix.toUpperCase()+"-")},b.prototype.build=function(){var b,c,d,e,g,h,i,j,k,l=this;for(this.bindings=[],h=[],b=this.bindingRegExp(),d=this.componentRegExp(),c=function(b,c,d,e){var f,g,h,i,j,k,m;return j={},m=function(){var a,b,c,d;for(c=e.split("|"),d=[],a=0,b=c.length;b>a;a++)k=c[a],d.push(k.trim());return d}(),f=function(){var a,b,c,d;for(c=m.shift().split("<"),d=[],a=0,b=c.length;b>a;a++)g=c[a],d.push(g.trim());return d}(),i=f.shift(),j.formatters=m,(h=f.shift())&&(j.dependencies=h.split(/\s+/)),l.bindings.push(new a[b](l,c,d,i,j))},g=function(e){var i,j,k,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I,J,K,L,M;if(f.call(h,e)<0){if(3===e.nodeType){if(q=a.TextTemplateParser,(n=l.config.templateDelimiters)&&(u=q.parse(e.data,n)).length&&(1!==u.length||u[0].type!==q.types.text)){for(x=0,B=u.length;B>x;x++)t=u[x],s=document.createTextNode(t.value),e.parentNode.insertBefore(s,e),1===t.type&&c("TextBinding",s,null,t.value);e.parentNode.removeChild(e)}}else if(d.test(e.tagName))v=e.tagName.replace(d,"").toLowerCase(),l.bindings.push(new a.ComponentBinding(l,e,v));else if(null!=e.attributes){for(H=e.attributes,y=0,C=H.length;C>y;y++)if(i=H[y],b.test(i.name)){if(v=i.name.replace(b,""),!(k=l.binders[v])){I=l.binders;for(o in I)w=I[o],"*"!==o&&-1!==o.indexOf("*")&&(r=new RegExp("^"+o.replace("*",".+")+"$"),r.test(v)&&(k=w))}if(k||(k=l.binders["*"]),k.block){for(J=e.childNodes,z=0,D=J.length;D>z;z++)p=J[z],h.push(p);j=[i]}}for(K=j||e.attributes,A=0,E=K.length;E>A;A++)i=K[A],b.test(i.name)&&(v=i.name.replace(b,""),c("Binding",e,v,i.value))}for(L=function(){var a,b,c,d;for(c=e.childNodes,d=[],b=0,a=c.length;a>b;b++)p=c[b],d.push(p);return d}(),M=[],G=0,F=L.length;F>G;G++)m=L[G],M.push(g(m));return M}},k=this.els,i=0,j=k.length;j>i;i++)e=k[i],g(e)},b.prototype.select=function(a){var b,c,d,e,f;for(e=this.bindings,f=[],c=0,d=e.length;d>c;c++)b=e[c],a(b)&&f.push(b);return f},b.prototype.bind=function(){var a,b,c,d,e;for(d=this.bindings,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.bind());return e},b.prototype.unbind=function(){var a,b,c,d,e;for(d=this.bindings,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.unbind());return e},b.prototype.sync=function(){var a,b,c,d,e;for(d=this.bindings,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.sync());return e},b.prototype.publish=function(){var a,b,c,d,e;for(d=this.select(function(a){return a.binder.publishes}),e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.publish());return e},b.prototype.update=function(a){var b,c,d,e,f,g,h;null==a&&(a={});for(c in a)d=a[c],this.models[c]=d;for(g=this.bindings,h=[],e=0,f=g.length;f>e;e++)b=g[e],h.push(b.update(a));return h},b}(),a.Binding=function(){function b(a,b,c,d,f){this.view=a,this.el=b,this.type=c,this.keypath=d,this.options=null!=f?f:{},this.update=e(this.update,this),this.unbind=e(this.unbind,this),this.bind=e(this.bind,this),this.publish=e(this.publish,this),this.sync=e(this.sync,this),this.set=e(this.set,this),this.eventHandler=e(this.eventHandler,this),this.formattedValue=e(this.formattedValue,this),this.setBinder=e(this.setBinder,this),this.formatters=this.options.formatters||[],this.dependencies=[],this.model=void 0,this.setBinder()}return b.prototype.setBinder=function(){var a,b,c,d;if(!(this.binder=this.view.binders[this.type])){d=this.view.binders;for(a in d)c=d[a],"*"!==a&&-1!==a.indexOf("*")&&(b=new RegExp("^"+a.replace("*",".+")+"$"),b.test(this.type)&&(this.binder=c,this.args=new RegExp("^"+a.replace("*","(.+)")+"$").exec(this.type),this.args.shift()))}return this.binder||(this.binder=this.view.binders["*"]),this.binder instanceof Function?this.binder={routine:this.binder}:void 0},b.prototype.formattedValue=function(a){var b,c,d,e,f,h;for(h=this.formatters,e=0,f=h.length;f>e;e++)c=h[e],b=c.split(/\s+/),d=b.shift(),c=this.view.formatters[d],(null!=c?c.read:void 0)instanceof Function?a=c.read.apply(c,[a].concat(g.call(b))):c instanceof Function&&(a=c.apply(null,[a].concat(g.call(b))));return a},b.prototype.eventHandler=function(a){var b,c;return c=(b=this).view.config.handler,function(d){return c.call(a,this,d,b)}},b.prototype.set=function(a){var b;return a=a instanceof Function&&!this.binder["function"]?this.formattedValue(a.call(this.model)):this.formattedValue(a),null!=(b=this.binder.routine)?b.call(this,this.el,a):void 0},b.prototype.sync=function(){var b,c,d,e,f,g,h,i,j;if(this.model!==this.observer.target){for(h=this.dependencies,d=0,f=h.length;f>d;d++)c=h[d],c.unobserve();if(this.dependencies=[],null!=(this.model=this.observer.target)&&(null!=(i=this.options.dependencies)?i.length:void 0))for(j=this.options.dependencies,e=0,g=j.length;g>e;e++)b=j[e],c=new a.Observer(this.view,this.model,b,this.sync),this.dependencies.push(c)}return this.set(this.observer.value())},b.prototype.publish=function(){var b,c,d,e,f,h,i,j,k;for(e=a.Util.getInputValue(this.el),i=this.formatters.slice(0).reverse(),f=0,h=i.length;h>f;f++)c=i[f],b=c.split(/\s+/),d=b.shift(),(null!=(j=this.view.formatters[d])?j.publish:void 0)&&(e=(k=this.view.formatters[d]).publish.apply(k,[e].concat(g.call(b))));return this.observer.publish(e)},b.prototype.bind=function(){var b,c,d,e,f,g,h;if(null!=(f=this.binder.bind)&&f.call(this,this.el),this.observer=new a.Observer(this.view,this.view.models,this.keypath,this.sync),this.model=this.observer.target,null!=this.model&&(null!=(g=this.options.dependencies)?g.length:void 0))for(h=this.options.dependencies,d=0,e=h.length;e>d;d++)b=h[d],c=new a.Observer(this.view,this.model,b,this.sync),this.dependencies.push(c);return this.view.config.preloadData?this.sync():void 0},b.prototype.unbind=function(){var a,b,c,d,e;for(null!=(d=this.binder.unbind)&&d.call(this,this.el),this.observer.unobserve(),e=this.dependencies,b=0,c=e.length;c>b;b++)a=e[b],a.unobserve();return this.dependencies=[]},b.prototype.update=function(a){var b;return null==a&&(a={}),this.model=this.observer.target,null!=(b=this.binder.update)?b.call(this,a):void 0},b}(),a.ComponentBinding=function(b){function c(b,c,d){var g,h,i,j,k;for(this.view=b,this.el=c,this.type=d,this.unbind=e(this.unbind,this),this.bind=e(this.bind,this),this.update=e(this.update,this),this.locals=e(this.locals,this),this.component=a.components[this.type],this.attributes={},this.inflections={},j=this.el.attributes||[],h=0,i=j.length;i>h;h++)g=j[h],k=g.name,f.call(this.component.attributes,k)>=0?this.attributes[g.name]=g.value:this.inflections[g.name]=g.value}return i(c,b),c.prototype.sync=function(){},c.prototype.locals=function(a){var b,c,d,e,f,g,h,i,j;null==a&&(a=this.view.models),f={},i=this.inflections;for(c in i)for(b=i[c],j=b.split("."),g=0,h=j.length;h>g;g++)e=j[g],f[c]=(f[c]||a)[e];for(c in a)d=a[c],null==f[c]&&(f[c]=d);return f},c.prototype.update=function(a){var b;return null!=(b=this.componentView)?b.update(this.locals(a)):void 0},c.prototype.bind=function(){var b,c;return null!=this.componentView?null!=(c=this.componentView)?c.bind():void 0:(b=this.component.build.call(this.attributes),(this.componentView=new a.View(b,this.locals(),this.view.options)).bind(),this.el.parentNode.replaceChild(b,this.el))},c.prototype.unbind=function(){var a;return null!=(a=this.componentView)?a.unbind():void 0},c}(a.Binding),a.TextBinding=function(a){function b(a,b,c,d,f){this.view=a,this.el=b,this.type=c,this.keypath=d,this.options=null!=f?f:{},this.sync=e(this.sync,this),this.formatters=this.options.formatters||[],this.dependencies=[]}return i(b,a),b.prototype.binder={routine:function(a,b){return a.data=null!=b?b:""}},b.prototype.sync=function(){return b.__super__.sync.apply(this,arguments)},b}(a.Binding),a.KeypathParser=function(){function a(){}return a.parse=function(a,b,c){var d,e,g,h,i,j;for(h=[],e={"interface":c,path:""},g=i=0,j=a.length;j>i;g=i+=1)d=a.charAt(g),f.call(b,d)>=0?(h.push(e),e={"interface":d,path:""}):e.path+=d;return h.push(e),h},a}(),a.TextTemplateParser=function(){function a(){}return a.types={text:0,binding:1},a.parse=function(a,b){var c,d,e,f,g,h,i;for(h=[],f=a.length,c=0,d=0;f>d;){if(c=a.indexOf(b[0],d),0>c){h.push({type:this.types.text,value:a.slice(d)});break}if(c>0&&c>d&&h.push({type:this.types.text,value:a.slice(d,c)}),d=c+b[0].length,c=a.indexOf(b[1],d),0>c){g=a.slice(d-b[1].length),e=h[h.length-1],(null!=e?e.type:void 0)===this.types.text?e.value+=g:h.push({type:this.types.text,value:g});break}i=a.slice(d,c).trim(),h.push({type:this.types.binding,value:i}),d=c+b[1].length}return h},a}(),a.Observer=function(){function b(a,b,c,d){this.view=a,this.model=b,this.keypath=c,this.callback=d,this.unobserve=e(this.unobserve,this),this.realize=e(this.realize,this),this.value=e(this.value,this),this.publish=e(this.publish,this),this.read=e(this.read,this),this.set=e(this.set,this),this.adapter=e(this.adapter,this),this.update=e(this.update,this),this.initialize=e(this.initialize,this),this.parse=e(this.parse,this),this.parse(),this.initialize()}return b.prototype.parse=function(){var b,c,d,e,g,h;return b=function(){var a,b;a=this.view.adapters,b=[];for(c in a)g=a[c],b.push(c);return b}.call(this),h=this.keypath[0],f.call(b,h)>=0?(e=this.keypath[0],d=this.keypath.substr(1)):(e=this.view.config.rootInterface,d=this.keypath),this.tokens=a.KeypathParser.parse(d,b,e),this.key=this.tokens.pop()},b.prototype.initialize=function(){return this.objectPath=[],this.target=this.realize(),null!=this.target?this.set(!0,this.key,this.target,this.callback):void 0},b.prototype.update=function(){var a,b;return(a=this.realize())!==this.target&&(null!=this.target&&this.set(!1,this.key,this.target,this.callback),null!=a&&this.set(!0,this.key,a,this.callback),b=this.value(),this.target=a,this.value()!==b)?this.callback():void 0},b.prototype.adapter=function(a){return this.view.adapters[a["interface"]]},b.prototype.set=function(a,b,c,d){var e;return e=a?"subscribe":"unsubscribe",this.adapter(b)[e](c,b.path,d)},b.prototype.read=function(a,b){return this.adapter(a).read(b,a.path)},b.prototype.publish=function(a){return null!=this.target?this.adapter(this.key).publish(this.target,this.key.path,a):void 0},b.prototype.value=function(){return null!=this.target?this.read(this.key,this.target):void 0},b.prototype.realize=function(){var a,b,c,d,e,f,g,h;for(a=this.model,e=null,h=this.tokens,b=f=0,g=h.length;g>f;b=++f)d=h[b],null!=a?(null!=this.objectPath[b]?a!==(c=this.objectPath[b])&&(this.set(!1,d,c,this.update),this.set(!0,d,a,this.update),this.objectPath[b]=a):(this.set(!0,d,a,this.update),this.objectPath[b]=a),a=this.read(d,a)):(null==e&&(e=b),(c=this.objectPath[b])&&this.set(!1,d,c,this.update));return null!=e&&this.objectPath.splice(e),a},b.prototype.unobserve=function(){var a,b,c,d,e,f;for(f=this.tokens,a=d=0,e=f.length;e>d;a=++d)c=f[a],(b=this.objectPath[a])&&this.set(!1,c,b,this.update);return null!=this.target?this.set(!1,this.key,this.target,this.callback):void 0},b}(),a.binders.text=function(a,b){return null!=a.textContent?a.textContent=null!=b?b:"":a.innerText=null!=b?b:""},a.binders.html=function(a,b){return a.innerHTML=null!=b?b:""},a.binders.show=function(a,b){return a.style.display=b?"":"none"},a.binders.hide=function(a,b){return a.style.display=b?"none":""},a.binders.enabled=function(a,b){return a.disabled=!b},a.binders.disabled=function(a,b){return a.disabled=!!b},a.binders.checked={publishes:!0,bind:function(b){return a.Util.bindEvent(b,"change",this.publish)},unbind:function(b){return a.Util.unbindEvent(b,"change",this.publish)},routine:function(a,b){var c;return a.checked="radio"===a.type?(null!=(c=a.value)?c.toString():void 0)===(null!=b?b.toString():void 0):!!b}},a.binders.unchecked={publishes:!0,bind:function(b){return a.Util.bindEvent(b,"change",this.publish)},unbind:function(b){return a.Util.unbindEvent(b,"change",this.publish)},routine:function(a,b){var c;return a.checked="radio"===a.type?(null!=(c=a.value)?c.toString():void 0)!==(null!=b?b.toString():void 0):!b}},a.binders.value={publishes:!0,bind:function(b){return a.Util.bindEvent(b,"change",this.publish)},unbind:function(b){return a.Util.unbindEvent(b,"change",this.publish)},routine:function(a,b){var c,d,e,g,h,i,j;if(null!=window.jQuery){if(a=jQuery(a),(null!=b?b.toString():void 0)!==(null!=(g=a.val())?g.toString():void 0))return a.val(null!=b?b:"")}else if("select-multiple"===a.type){if(null!=b){for(j=[],d=0,e=a.length;e>d;d++)c=a[d],j.push(c.selected=(h=c.value,f.call(b,h)>=0));return j}}else if((null!=b?b.toString():void 0)!==(null!=(i=a.value)?i.toString():void 0))return a.value=null!=b?b:""}},a.binders["if"]={block:!0,bind:function(a){var b,c;return null==this.marker?(b=[this.view.config.prefix,this.type].join("-").replace("--","-"),c=a.getAttribute(b),this.marker=document.createComment(" rivets: "+this.type+" "+c+" "),a.removeAttribute(b),a.parentNode.insertBefore(this.marker,a),a.parentNode.removeChild(a)):void 0},unbind:function(){var a;return null!=(a=this.nested)?a.unbind():void 0},routine:function(b,c){var d,e,f,g,h;if(!!c==(null==this.nested)){if(c){f={},h=this.view.models;for(d in h)e=h[d],f[d]=e;return g={binders:this.view.options.binders,formatters:this.view.options.formatters,adapters:this.view.options.adapters,config:this.view.options.config},(this.nested=new a.View(b,f,g)).bind(),this.marker.parentNode.insertBefore(b,this.marker.nextSibling)}return b.parentNode.removeChild(b),this.nested.unbind(),delete this.nested}},update:function(a){var b;return null!=(b=this.nested)?b.update(a):void 0}},a.binders.unless={block:!0,bind:function(b){return a.binders["if"].bind.call(this,b)},unbind:function(){return a.binders["if"].unbind.call(this)},routine:function(b,c){return a.binders["if"].routine.call(this,b,!c)},update:function(b){return a.binders["if"].update.call(this,b)}},a.binders["on-*"]={"function":!0,unbind:function(b){return this.handler?a.Util.unbindEvent(b,this.args[0],this.handler):void 0},routine:function(b,c){return this.handler&&a.Util.unbindEvent(b,this.args[0],this.handler),a.Util.bindEvent(b,this.args[0],this.handler=this.eventHandler(c))}},a.binders["each-*"]={block:!0,bind:function(a){var b;return null==this.marker?(b=[this.view.config.prefix,this.type].join("-").replace("--","-"),this.marker=document.createComment(" rivets: "+this.type+" "),this.iterated=[],a.removeAttribute(b),a.parentNode.insertBefore(this.marker,a),a.parentNode.removeChild(a)):void 0},unbind:function(){var a,b,c,d,e;if(null!=this.iterated){for(d=this.iterated,e=[],b=0,c=d.length;c>b;b++)a=d[b],e.push(a.unbind());return e}},routine:function(b,c){var d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,A;if(k=this.args[0],c=c||[],this.iterated.length>c.length)for(w=Array(this.iterated.length-c.length),q=0,t=w.length;t>q;q++)f=w[q],p=this.iterated.pop(),p.unbind(),this.marker.parentNode.removeChild(p.els[0]);for(g=r=0,u=c.length;u>r;g=++r)if(j=c[g],e={index:g},e[k]=j,null==this.iterated[g]){x=this.view.models;for(i in x)j=x[i],null==e[i]&&(e[i]=j);m=this.iterated.length?this.iterated[this.iterated.length-1].els[0]:this.marker,l={binders:this.view.options.binders,formatters:this.view.options.formatters,adapters:this.view.options.adapters,config:{}},y=this.view.options.config;for(h in y)o=y[h],l.config[h]=o;l.config.preloadData=!0,n=b.cloneNode(!0),p=new a.View(n,e,l),p.bind(),this.iterated.push(p),this.marker.parentNode.insertBefore(n,m.nextSibling)}else this.iterated[g].models[k]!==j&&this.iterated[g].update(e);if("OPTION"===b.nodeName){for(z=this.view.bindings,A=[],s=0,v=z.length;v>s;s++)d=z[s],d.el===this.marker.parentNode&&"value"===d.type?A.push(d.sync()):A.push(void 0);return A}},update:function(a){var b,c,d,e,f,g,h,i;b={};for(c in a)d=a[c],c!==this.args[0]&&(b[c]=d);for(h=this.iterated,i=[],f=0,g=h.length;g>f;f++)e=h[f],i.push(e.update(b));return i}},a.binders["class-*"]=function(a,b){var c;return c=" "+a.className+" ",!b==(-1!==c.indexOf(" "+this.args[0]+" "))?a.className=b?""+a.className+" "+this.args[0]:c.replace(" "+this.args[0]+" "," ").trim():void 0},a.binders["*"]=function(a,b){return null!=b?a.setAttribute(this.type,b):a.removeAttribute(this.type)},a.adapters["."]={id:"_rv",counter:0,weakmap:{},weakReference:function(a){var b;return null==a[this.id]&&(b=this.counter++,this.weakmap[b]={callbacks:{}},Object.defineProperty(a,this.id,{value:b})),this.weakmap[a[this.id]]},stubFunction:function(a,b){var c,d,e;return d=a[b],c=this.weakReference(a),e=this.weakmap,a[b]=function(){var b,f,g,h,i,j,k,l,m,n;h=d.apply(a,arguments),k=c.pointers;for(g in k)for(f=k[g],n=null!=(l=null!=(m=e[g])?m.callbacks[f]:void 0)?l:[],i=0,j=n.length;j>i;i++)b=n[i],b();return h}},observeMutations:function(a,b,c){var d,e,g,h,i,j;if(Array.isArray(a)){if(g=this.weakReference(a),null==g.pointers)for(g.pointers={},e=["push","pop","shift","unshift","sort","reverse","splice"],i=0,j=e.length;j>i;i++)d=e[i],this.stubFunction(a,d);if(null==(h=g.pointers)[b]&&(h[b]=[]),f.call(g.pointers[b],c)<0)return g.pointers[b].push(c)}},unobserveMutations:function(a,b,c){var d,e;return Array.isArray(a&&null!=a[this.id])&&(d=null!=(e=this.weakReference(a).pointers)?e[b]:void 0)?d.splice(d.indexOf(c),1):void 0},subscribe:function(a,b,c){var d,e,g=this;return d=this.weakReference(a).callbacks,null==d[b]&&(d[b]=[],e=a[b],Object.defineProperty(a,b,{enumerable:!0,get:function(){return e},set:function(f){var h,i,j;if(f!==e){for(e=f,j=d[b],h=0,i=j.length;i>h;h++)c=j[h],c();return g.observeMutations(f,a[g.id],b)}}})),f.call(d[b],c)<0&&d[b].push(c),this.observeMutations(a[b],a[this.id],b)},unsubscribe:function(a,b,c){var d;return d=this.weakmap[a[this.id]].callbacks[b],d.splice(d.indexOf(c),1),this.unobserveMutations(a[b],a[this.id],b)},read:function(a,b){return a[b]},publish:function(a,b,c){return a[b]=c}},a.factory=function(b){return b._=a,b.binders=a.binders,b.components=a.components,b.formatters=a.formatters,b.adapters=a.adapters,b.config=a.config,b.configure=function(b){var c,d;null==b&&(b={});for(c in b)d=b[c],a.config[c]=d},b.bind=function(b,c,d){var e;return null==c&&(c={}),null==d&&(d={}),e=new a.View(b,c,d),e.bind(),e}},"object"==typeof exports?a.factory(exports):"function"==typeof define&&define.amd?define(["exports"],function(b){return a.factory(this.rivets=b),b}):a.factory(this.rivets={})}).call(this); \ No newline at end of file diff --git a/ajax/libs/rivets/0.6.9/rivets.js b/ajax/libs/rivets/0.6.9/rivets.js new file mode 100644 index 000000000..702f169a7 --- /dev/null +++ b/ajax/libs/rivets/0.6.9/rivets.js @@ -0,0 +1,1316 @@ +// Rivets.js +// version: 0.6.9 +// author: Michael Richards +// license: MIT +(function() { + var Rivets, bindMethod, unbindMethod, _ref, + __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; }, + __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, + __slice = [].slice, + __hasProp = {}.hasOwnProperty, + __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }; + + Rivets = { + binders: {}, + components: {}, + formatters: {}, + adapters: {}, + config: { + prefix: 'rv', + templateDelimiters: ['{', '}'], + rootInterface: '.', + preloadData: true, + handler: function(context, ev, binding) { + return this.call(context, ev, binding.view.models); + } + } + }; + + if ('jQuery' in window) { + _ref = 'on' in jQuery.prototype ? ['on', 'off'] : ['bind', 'unbind'], bindMethod = _ref[0], unbindMethod = _ref[1]; + Rivets.Util = { + bindEvent: function(el, event, handler) { + return jQuery(el)[bindMethod](event, handler); + }, + unbindEvent: function(el, event, handler) { + return jQuery(el)[unbindMethod](event, handler); + }, + getInputValue: function(el) { + var $el; + $el = jQuery(el); + if ($el.attr('type') === 'checkbox') { + return $el.is(':checked'); + } else { + return $el.val(); + } + } + }; + } else { + Rivets.Util = { + bindEvent: (function() { + if ('addEventListener' in window) { + return function(el, event, handler) { + return el.addEventListener(event, handler, false); + }; + } + return function(el, event, handler) { + return el.attachEvent('on' + event, handler); + }; + })(), + unbindEvent: (function() { + if ('removeEventListener' in window) { + return function(el, event, handler) { + return el.removeEventListener(event, handler, false); + }; + } + return function(el, event, handler) { + return el.detachEvent('on' + event, handler); + }; + })(), + getInputValue: function(el) { + var o, _i, _len, _results; + if (el.type === 'checkbox') { + return el.checked; + } else if (el.type === 'select-multiple') { + _results = []; + for (_i = 0, _len = el.length; _i < _len; _i++) { + o = el[_i]; + if (o.selected) { + _results.push(o.value); + } + } + return _results; + } else { + return el.value; + } + } + }; + } + + Rivets.KeypathParser = (function() { + function KeypathParser() {} + + KeypathParser.parse = function(keypath, interfaces, root) { + var char, current, index, tokens, _i, _ref1; + tokens = []; + current = { + "interface": root, + path: '' + }; + for (index = _i = 0, _ref1 = keypath.length; _i < _ref1; index = _i += 1) { + char = keypath.charAt(index); + if (__indexOf.call(interfaces, char) >= 0) { + tokens.push(current); + current = { + "interface": char, + path: '' + }; + } else { + current.path += char; + } + } + tokens.push(current); + return tokens; + }; + + return KeypathParser; + + })(); + + 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 + delimiters[0].length; + index = template.indexOf(delimiters[1], lastIndex); + if (index < 0) { + substring = template.slice(lastIndex - delimiters[1].length); + 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 + delimiters[1].length; + } + } + return tokens; + }; + + return TextTemplateParser; + + })(); + + Rivets.Observer = (function() { + function Observer(view, model, keypath, callback) { + this.view = view; + this.model = model; + this.keypath = keypath; + this.callback = callback; + this.unobserve = __bind(this.unobserve, this); + this.realize = __bind(this.realize, this); + this.value = __bind(this.value, this); + this.publish = __bind(this.publish, this); + this.read = __bind(this.read, this); + this.set = __bind(this.set, this); + this.adapter = __bind(this.adapter, this); + this.update = __bind(this.update, this); + this.initialize = __bind(this.initialize, this); + this.parse = __bind(this.parse, this); + this.parse(); + this.initialize(); + } + + Observer.prototype.parse = function() { + var interfaces, k, path, root, v, _ref1; + interfaces = (function() { + var _ref1, _results; + _ref1 = this.view.adapters; + _results = []; + for (k in _ref1) { + v = _ref1[k]; + if (v) { + _results.push(k); + } + } + return _results; + }).call(this); + if (_ref1 = this.keypath[0], __indexOf.call(interfaces, _ref1) >= 0) { + root = this.keypath[0]; + path = this.keypath.substr(1); + } else { + root = this.view.config.rootInterface; + path = this.keypath; + } + this.tokens = Rivets.KeypathParser.parse(path, interfaces, root); + return this.key = this.tokens.pop(); + }; + + Observer.prototype.initialize = function() { + this.objectPath = []; + this.target = this.realize(); + if (this.target != null) { + return this.set(true, this.key, this.target, this.callback); + } + }; + + Observer.prototype.update = function() { + var next, oldValue; + if ((next = this.realize()) !== this.target) { + if (this.target != null) { + this.set(false, this.key, this.target, this.callback); + } + if (next != null) { + this.set(true, this.key, next, this.callback); + } + oldValue = this.value(); + this.target = next; + if (this.value() !== oldValue) { + return this.callback(); + } + } + }; + + Observer.prototype.adapter = function(key) { + return this.view.adapters[key["interface"]]; + }; + + Observer.prototype.set = function(active, key, obj, callback) { + var action; + action = active ? 'subscribe' : 'unsubscribe'; + return this.adapter(key)[action](obj, key.path, callback); + }; + + Observer.prototype.read = function(key, obj) { + return this.adapter(key).read(obj, key.path); + }; + + Observer.prototype.publish = function(value) { + if (this.target != null) { + return this.adapter(this.key).publish(this.target, this.key.path, value); + } + }; + + Observer.prototype.value = function() { + if (this.target != null) { + return this.read(this.key, this.target); + } + }; + + Observer.prototype.realize = function() { + var current, index, prev, token, unreached, _i, _len, _ref1; + current = this.model; + unreached = null; + _ref1 = this.tokens; + for (index = _i = 0, _len = _ref1.length; _i < _len; index = ++_i) { + token = _ref1[index]; + if (current != null) { + if (this.objectPath[index] != null) { + if (current !== (prev = this.objectPath[index])) { + this.set(false, token, prev, this.update); + this.set(true, token, current, this.update); + this.objectPath[index] = current; + } + } else { + this.set(true, token, current, this.update); + this.objectPath[index] = current; + } + current = this.read(token, current); + } else { + if (unreached == null) { + unreached = index; + } + if (prev = this.objectPath[index]) { + this.set(false, token, prev, this.update); + } + } + } + if (unreached != null) { + this.objectPath.splice(unreached); + } + return current; + }; + + Observer.prototype.unobserve = function() { + var index, obj, token, _i, _len, _ref1; + _ref1 = this.tokens; + for (index = _i = 0, _len = _ref1.length; _i < _len; index = ++_i) { + token = _ref1[index]; + if (obj = this.objectPath[index]) { + this.set(false, token, obj, this.update); + } + } + if (this.target != null) { + return this.set(false, this.key, this.target, this.callback); + } + }; + + return Observer; + + })(); + + Rivets.View = (function() { + function View(els, models, options) { + var k, option, v, _base, _i, _len, _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.componentRegExp = __bind(this.componentRegExp, this); + this.bindingRegExp = __bind(this.bindingRegExp, this); + if (!(this.els.jquery || this.els instanceof Array)) { + this.els = [this.els]; + } + _ref1 = ['config', 'binders', 'formatters', 'adapters']; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + option = _ref1[_i]; + this[option] = {}; + if (this.options[option]) { + _ref2 = this.options[option]; + for (k in _ref2) { + v = _ref2[k]; + this[option][k] = v; + } + } + _ref3 = Rivets[option]; + for (k in _ref3) { + v = _ref3[k]; + if ((_base = this[option])[k] == null) { + _base[k] = v; + } + } + } + this.build(); + } + + View.prototype.bindingRegExp = function() { + return new RegExp("^" + this.config.prefix + "-"); + }; + + View.prototype.componentRegExp = function() { + return new RegExp("^" + (this.config.prefix.toUpperCase()) + "-"); + }; + + View.prototype.build = function() { + var bindingRegExp, buildBinding, componentRegExp, el, parse, skipNodes, _i, _len, _ref1; + this.bindings = []; + skipNodes = []; + bindingRegExp = this.bindingRegExp(); + componentRegExp = this.componentRegExp(); + buildBinding = (function(_this) { + return function(binding, node, type, declaration) { + var context, ctx, dependencies, keypath, options, pipe, pipes; + options = {}; + pipes = (function() { + var _i, _len, _ref1, _results; + _ref1 = declaration.split('|'); + _results = []; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + pipe = _ref1[_i]; + _results.push(pipe.trim()); + } + return _results; + })(); + context = (function() { + var _i, _len, _ref1, _results; + _ref1 = pipes.shift().split('<'); + _results = []; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + ctx = _ref1[_i]; + _results.push(ctx.trim()); + } + return _results; + })(); + keypath = context.shift(); + options.formatters = pipes; + if (dependencies = context.shift()) { + options.dependencies = dependencies.split(/\s+/); + } + return _this.bindings.push(new Rivets[binding](_this, node, type, keypath, options)); + }; + })(this); + parse = (function(_this) { + return function(node) { + var attribute, attributes, binder, childNode, delimiters, identifier, n, parser, regexp, text, token, tokens, type, value, _i, _j, _k, _l, _len, _len1, _len2, _len3, _len4, _m, _ref1, _ref2, _ref3, _ref4, _ref5, _results; + if (__indexOf.call(skipNodes, node) < 0) { + if (node.nodeType === 3) { + 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)) { + for (_i = 0, _len = tokens.length; _i < _len; _i++) { + token = tokens[_i]; + text = document.createTextNode(token.value); + node.parentNode.insertBefore(text, node); + if (token.type === 1) { + buildBinding('TextBinding', text, null, token.value); + } + } + node.parentNode.removeChild(node); + } + } + } + } else if (componentRegExp.test(node.tagName)) { + type = node.tagName.replace(componentRegExp, '').toLowerCase(); + _this.bindings.push(new Rivets.ComponentBinding(_this, node, type)); + } else if (node.attributes != null) { + _ref1 = node.attributes; + for (_j = 0, _len1 = _ref1.length; _j < _len1; _j++) { + attribute = _ref1[_j]; + if (bindingRegExp.test(attribute.name)) { + type = attribute.name.replace(bindingRegExp, ''); + if (!(binder = _this.binders[type])) { + _ref2 = _this.binders; + for (identifier in _ref2) { + value = _ref2[identifier]; + if (identifier !== '*' && identifier.indexOf('*') !== -1) { + regexp = new RegExp("^" + (identifier.replace('*', '.+')) + "$"); + if (regexp.test(type)) { + binder = value; + } + } + } + } + binder || (binder = _this.binders['*']); + if (binder.block) { + _ref3 = node.childNodes; + for (_k = 0, _len2 = _ref3.length; _k < _len2; _k++) { + n = _ref3[_k]; + skipNodes.push(n); + } + attributes = [attribute]; + } + } + } + _ref4 = attributes || node.attributes; + for (_l = 0, _len3 = _ref4.length; _l < _len3; _l++) { + attribute = _ref4[_l]; + if (bindingRegExp.test(attribute.name)) { + type = attribute.name.replace(bindingRegExp, ''); + buildBinding('Binding', node, type, attribute.value); + } + } + } + _ref5 = (function() { + var _len4, _n, _ref5, _results1; + _ref5 = node.childNodes; + _results1 = []; + for (_n = 0, _len4 = _ref5.length; _n < _len4; _n++) { + n = _ref5[_n]; + _results1.push(n); + } + return _results1; + })(); + _results = []; + for (_m = 0, _len4 = _ref5.length; _m < _len4; _m++) { + childNode = _ref5[_m]; + _results.push(parse(childNode)); + } + return _results; + } + }; + })(this); + _ref1 = this.els; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + el = _ref1[_i]; + parse(el); + } + }; + + View.prototype.select = function(fn) { + var binding, _i, _len, _ref1, _results; + _ref1 = this.bindings; + _results = []; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + binding = _ref1[_i]; + if (fn(binding)) { + _results.push(binding); + } + } + return _results; + }; + + View.prototype.bind = function() { + var binding, _i, _len, _ref1, _results; + _ref1 = this.bindings; + _results = []; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + binding = _ref1[_i]; + _results.push(binding.bind()); + } + return _results; + }; + + View.prototype.unbind = function() { + var binding, _i, _len, _ref1, _results; + _ref1 = this.bindings; + _results = []; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + binding = _ref1[_i]; + _results.push(binding.unbind()); + } + return _results; + }; + + View.prototype.sync = function() { + var binding, _i, _len, _ref1, _results; + _ref1 = this.bindings; + _results = []; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + binding = _ref1[_i]; + _results.push(binding.sync()); + } + return _results; + }; + + View.prototype.publish = function() { + var binding, _i, _len, _ref1, _results; + _ref1 = this.select(function(b) { + return b.binder.publishes; + }); + _results = []; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + binding = _ref1[_i]; + _results.push(binding.publish()); + } + return _results; + }; + + View.prototype.update = function(models) { + var binding, key, model, _i, _len, _ref1, _results; + if (models == null) { + models = {}; + } + for (key in models) { + model = models[key]; + this.models[key] = model; + } + _ref1 = this.bindings; + _results = []; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + binding = _ref1[_i]; + _results.push(binding.update(models)); + } + return _results; + }; + + return View; + + })(); + + Rivets.Binding = (function() { + function Binding(view, el, type, keypath, options) { + this.view = view; + this.el = el; + this.type = type; + 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); + this.setBinder = __bind(this.setBinder, this); + this.formatters = this.options.formatters || []; + this.dependencies = []; + this.model = void 0; + this.setBinder(); + } + + Binding.prototype.setBinder = function() { + var identifier, regexp, value, _ref1; + if (!(this.binder = this.view.binders[this.type])) { + _ref1 = this.view.binders; + for (identifier in _ref1) { + value = _ref1[identifier]; + if (identifier !== '*' && identifier.indexOf('*') !== -1) { + regexp = new RegExp("^" + (identifier.replace('*', '.+')) + "$"); + if (regexp.test(this.type)) { + this.binder = value; + this.args = new RegExp("^" + (identifier.replace('*', '(.+)')) + "$").exec(this.type); + this.args.shift(); + } + } + } + } + this.binder || (this.binder = this.view.binders['*']); + if (this.binder instanceof Function) { + return this.binder = { + routine: this.binder + }; + } + }; + + Binding.prototype.formattedValue = function(value) { + var args, formatter, id, _i, _len, _ref1; + _ref1 = this.formatters; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + formatter = _ref1[_i]; + args = formatter.split(/\s+/); + id = args.shift(); + formatter = 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 _ref1; + value = value instanceof Function && !this.binder["function"] ? this.formattedValue(value.call(this.model)) : this.formattedValue(value); + return (_ref1 = this.binder.routine) != null ? _ref1.call(this, this.el, value) : void 0; + }; + + Binding.prototype.sync = function() { + var dependency, observer, _i, _j, _len, _len1, _ref1, _ref2, _ref3; + if (this.model !== this.observer.target) { + _ref1 = this.dependencies; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + observer = _ref1[_i]; + observer.unobserve(); + } + this.dependencies = []; + if (((this.model = this.observer.target) != null) && ((_ref2 = this.options.dependencies) != null ? _ref2.length : void 0)) { + _ref3 = this.options.dependencies; + for (_j = 0, _len1 = _ref3.length; _j < _len1; _j++) { + dependency = _ref3[_j]; + observer = new Rivets.Observer(this.view, this.model, dependency, this.sync); + this.dependencies.push(observer); + } + } + } + return this.set(this.observer.value()); + }; + + Binding.prototype.publish = function() { + var args, formatter, id, value, _i, _len, _ref1, _ref2, _ref3; + value = Rivets.Util.getInputValue(this.el); + _ref1 = this.formatters.slice(0).reverse(); + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + formatter = _ref1[_i]; + args = formatter.split(/\s+/); + id = args.shift(); + if ((_ref2 = this.view.formatters[id]) != null ? _ref2.publish : void 0) { + value = (_ref3 = this.view.formatters[id]).publish.apply(_ref3, [value].concat(__slice.call(args))); + } + } + return this.observer.publish(value); + }; + + Binding.prototype.bind = function() { + var dependency, observer, _i, _len, _ref1, _ref2, _ref3; + if ((_ref1 = this.binder.bind) != null) { + _ref1.call(this, this.el); + } + this.observer = new Rivets.Observer(this.view, this.view.models, this.keypath, this.sync); + this.model = this.observer.target; + if ((this.model != null) && ((_ref2 = this.options.dependencies) != null ? _ref2.length : void 0)) { + _ref3 = this.options.dependencies; + for (_i = 0, _len = _ref3.length; _i < _len; _i++) { + dependency = _ref3[_i]; + observer = new Rivets.Observer(this.view, this.model, dependency, this.sync); + this.dependencies.push(observer); + } + } + if (this.view.config.preloadData) { + return this.sync(); + } + }; + + Binding.prototype.unbind = function() { + var observer, _i, _len, _ref1, _ref2; + if ((_ref1 = this.binder.unbind) != null) { + _ref1.call(this, this.el); + } + this.observer.unobserve(); + _ref2 = this.dependencies; + for (_i = 0, _len = _ref2.length; _i < _len; _i++) { + observer = _ref2[_i]; + observer.unobserve(); + } + return this.dependencies = []; + }; + + Binding.prototype.update = function(models) { + var _ref1; + if (models == null) { + models = {}; + } + this.model = this.observer.target; + this.unbind(); + if ((_ref1 = this.binder.update) != null) { + _ref1.call(this, models); + } + return this.bind(); + }; + + return Binding; + + })(); + + Rivets.ComponentBinding = (function(_super) { + __extends(ComponentBinding, _super); + + function ComponentBinding(view, el, type) { + var attribute, _i, _len, _ref1, _ref2; + this.view = view; + this.el = el; + this.type = type; + this.unbind = __bind(this.unbind, this); + this.bind = __bind(this.bind, this); + this.update = __bind(this.update, this); + this.locals = __bind(this.locals, this); + this.component = Rivets.components[this.type]; + this.attributes = {}; + this.inflections = {}; + _ref1 = this.el.attributes || []; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + attribute = _ref1[_i]; + if (_ref2 = attribute.name, __indexOf.call(this.component.attributes, _ref2) >= 0) { + this.attributes[attribute.name] = attribute.value; + } else { + this.inflections[attribute.name] = attribute.value; + } + } + } + + ComponentBinding.prototype.sync = function() {}; + + ComponentBinding.prototype.locals = function(models) { + var inverse, key, model, path, result, _i, _len, _ref1, _ref2; + if (models == null) { + models = this.view.models; + } + result = {}; + _ref1 = this.inflections; + for (key in _ref1) { + inverse = _ref1[key]; + _ref2 = inverse.split('.'); + for (_i = 0, _len = _ref2.length; _i < _len; _i++) { + path = _ref2[_i]; + result[key] = (result[key] || models)[path]; + } + } + for (key in models) { + model = models[key]; + if (result[key] == null) { + result[key] = model; + } + } + return result; + }; + + ComponentBinding.prototype.update = function(models) { + var _ref1; + return (_ref1 = this.componentView) != null ? _ref1.update(this.locals(models)) : void 0; + }; + + ComponentBinding.prototype.bind = function() { + var el, _ref1; + if (this.componentView != null) { + return (_ref1 = this.componentView) != null ? _ref1.bind() : void 0; + } else { + el = this.component.build.call(this.attributes); + (this.componentView = new Rivets.View(el, this.locals(), this.view.options)).bind(); + return this.el.parentNode.replaceChild(el, this.el); + } + }; + + ComponentBinding.prototype.unbind = function() { + var _ref1; + return (_ref1 = this.componentView) != null ? _ref1.unbind() : void 0; + }; + + return ComponentBinding; + + })(Rivets.Binding); + + Rivets.TextBinding = (function(_super) { + __extends(TextBinding, _super); + + function TextBinding(view, el, type, keypath, options) { + this.view = view; + this.el = el; + this.type = type; + this.keypath = keypath; + this.options = options != null ? options : {}; + this.sync = __bind(this.sync, this); + this.formatters = this.options.formatters || []; + this.dependencies = []; + } + + TextBinding.prototype.binder = { + routine: function(node, value) { + return node.data = value != null ? value : ''; + } + }; + + TextBinding.prototype.sync = function() { + return TextBinding.__super__.sync.apply(this, arguments); + }; + + return TextBinding; + + })(Rivets.Binding); + + Rivets.adapters['.'] = { + id: '_rv', + counter: 0, + weakmap: {}, + weakReference: function(obj) { + var id; + if (!obj.hasOwnProperty(this.id)) { + id = this.counter++; + this.weakmap[id] = { + callbacks: {} + }; + Object.defineProperty(obj, this.id, { + value: id + }); + } + return this.weakmap[obj[this.id]]; + }, + stubFunction: function(obj, fn) { + var map, original, weakmap; + original = obj[fn]; + map = this.weakReference(obj); + weakmap = this.weakmap; + return obj[fn] = function() { + var callback, k, r, response, _i, _len, _ref1, _ref2, _ref3, _ref4; + response = original.apply(obj, arguments); + _ref1 = map.pointers; + for (r in _ref1) { + k = _ref1[r]; + _ref4 = (_ref2 = (_ref3 = weakmap[r]) != null ? _ref3.callbacks[k] : void 0) != null ? _ref2 : []; + for (_i = 0, _len = _ref4.length; _i < _len; _i++) { + callback = _ref4[_i]; + callback(); + } + } + return response; + }; + }, + observeMutations: function(obj, ref, keypath) { + var fn, functions, map, _base, _i, _len; + if (Array.isArray(obj)) { + map = this.weakReference(obj); + if (map.pointers == null) { + map.pointers = {}; + functions = ['push', 'pop', 'shift', 'unshift', 'sort', 'reverse', 'splice']; + for (_i = 0, _len = functions.length; _i < _len; _i++) { + fn = functions[_i]; + this.stubFunction(obj, fn); + } + } + if ((_base = map.pointers)[ref] == null) { + _base[ref] = []; + } + if (__indexOf.call(map.pointers[ref], keypath) < 0) { + return map.pointers[ref].push(keypath); + } + } + }, + unobserveMutations: function(obj, ref, keypath) { + var idx, keypaths, _ref1; + if (Array.isArray(obj && (obj[this.id] != null))) { + if (keypaths = (_ref1 = this.weakReference(obj).pointers) != null ? _ref1[ref] : void 0) { + idx = keypaths.indexOf(keypath); + if (idx >= 0) { + return keypaths.splice(idx, 1); + } + } + } + }, + subscribe: function(obj, keypath, callback) { + var callbacks, value; + callbacks = this.weakReference(obj).callbacks; + if (callbacks[keypath] == null) { + callbacks[keypath] = []; + value = obj[keypath]; + Object.defineProperty(obj, keypath, { + enumerable: true, + get: function() { + return value; + }, + set: (function(_this) { + return function(newValue) { + var _i, _len, _ref1; + if (newValue !== value) { + value = newValue; + _ref1 = callbacks[keypath].slice(); + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + callback = _ref1[_i]; + if (__indexOf.call(callbacks[keypath], callback) >= 0) { + callback(); + } + } + return _this.observeMutations(newValue, obj[_this.id], keypath); + } + }; + })(this) + }); + } + if (__indexOf.call(callbacks[keypath], callback) < 0) { + callbacks[keypath].push(callback); + } + return this.observeMutations(obj[keypath], obj[this.id], keypath); + }, + unsubscribe: function(obj, keypath, callback) { + var callbacks, idx; + callbacks = this.weakmap[obj[this.id]].callbacks[keypath]; + idx = callbacks.indexOf(callback); + if (idx >= 0) { + callbacks.splice(idx, 1); + } + return this.unobserveMutations(obj[keypath], obj[this.id], keypath); + }, + read: function(obj, keypath) { + return obj[keypath]; + }, + publish: function(obj, keypath, value) { + return obj[keypath] = value; + } + }; + + Rivets.binders.text = function(el, value) { + if (el.textContent != null) { + return el.textContent = value != null ? value : ''; + } else { + return el.innerText = value != null ? value : ''; + } + }; + + Rivets.binders.html = function(el, value) { + return el.innerHTML = value != null ? value : ''; + }; + + Rivets.binders.show = function(el, value) { + return el.style.display = value ? '' : 'none'; + }; + + Rivets.binders.hide = function(el, value) { + return el.style.display = value ? 'none' : ''; + }; + + Rivets.binders.enabled = function(el, value) { + return el.disabled = !value; + }; + + Rivets.binders.disabled = function(el, value) { + return el.disabled = !!value; + }; + + Rivets.binders.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 _ref1; + if (el.type === 'radio') { + return el.checked = ((_ref1 = el.value) != null ? _ref1.toString() : void 0) === (value != null ? value.toString() : void 0); + } else { + return el.checked = !!value; + } + } + }; + + Rivets.binders.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 _ref1; + if (el.type === 'radio') { + return el.checked = ((_ref1 = el.value) != null ? _ref1.toString() : void 0) !== (value != null ? value.toString() : void 0); + } else { + return el.checked = !value; + } + } + }; + + Rivets.binders.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, _ref1, _ref2, _ref3, _results; + if (window.jQuery != null) { + el = jQuery(el); + if ((value != null ? value.toString() : void 0) !== ((_ref1 = el.val()) != null ? _ref1.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 = (_ref2 = o.value, __indexOf.call(value, _ref2) >= 0)); + } + return _results; + } + } else if ((value != null ? value.toString() : void 0) !== ((_ref3 = el.value) != null ? _ref3.toString() : void 0)) { + return el.value = value != null ? value : ''; + } + } + } + }; + + Rivets.binders["if"] = { + block: true, + bind: function(el) { + var attr, declaration; + if (this.marker == null) { + attr = [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 _ref1; + return (_ref1 = this.nested) != null ? _ref1.unbind() : void 0; + }, + routine: function(el, value) { + var key, model, models, options, _ref1; + if (!!value === (this.nested == null)) { + if (value) { + models = {}; + _ref1 = this.view.models; + for (key in _ref1) { + model = _ref1[key]; + models[key] = model; + } + options = { + binders: this.view.options.binders, + formatters: this.view.options.formatters, + adapters: this.view.options.adapters, + 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) { + var _ref1; + return (_ref1 = this.nested) != null ? _ref1.update(models) : void 0; + } + }; + + Rivets.binders.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); + } + }; + + Rivets.binders['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)); + } + }; + + Rivets.binders['each-*'] = { + block: true, + bind: function(el) { + var attr, view, _i, _len, _ref1; + if (this.marker == null) { + attr = [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); + el.parentNode.removeChild(el); + } else { + _ref1 = this.iterated; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + view = _ref1[_i]; + view.bind(); + } + } + }, + unbind: function(el) { + var view, _i, _len, _ref1, _results; + if (this.iterated != null) { + _ref1 = this.iterated; + _results = []; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + view = _ref1[_i]; + _results.push(view.unbind()); + } + return _results; + } + }, + routine: function(el, collection) { + var binding, data, i, index, k, key, model, modelName, options, previous, template, v, view, _i, _j, _k, _len, _len1, _len2, _ref1, _ref2, _ref3, _ref4, _results; + modelName = this.args[0]; + collection = collection || []; + if (this.iterated.length > collection.length) { + _ref1 = Array(this.iterated.length - collection.length); + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + i = _ref1[_i]; + view = this.iterated.pop(); + view.unbind(); + this.marker.parentNode.removeChild(view.els[0]); + } + } + for (index = _j = 0, _len1 = collection.length; _j < _len1; index = ++_j) { + model = collection[index]; + data = { + index: index + }; + data[modelName] = model; + if (this.iterated[index] == null) { + _ref2 = this.view.models; + for (key in _ref2) { + model = _ref2[key]; + if (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, + adapters: this.view.options.adapters, + 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); + this.marker.parentNode.insertBefore(template, previous.nextSibling); + } else if (this.iterated[index].models[modelName] !== model) { + this.iterated[index].update(data); + } + } + if (el.nodeName === 'OPTION') { + _ref4 = this.view.bindings; + _results = []; + for (_k = 0, _len2 = _ref4.length; _k < _len2; _k++) { + binding = _ref4[_k]; + if (binding.el === this.marker.parentNode && binding.type === 'value') { + _results.push(binding.sync()); + } else { + _results.push(void 0); + } + } + return _results; + } + }, + update: function(models) { + var data, key, model, view, _i, _len, _ref1, _results; + data = {}; + for (key in models) { + model = models[key]; + if (key !== this.args[0]) { + data[key] = model; + } + } + _ref1 = this.iterated; + _results = []; + for (_i = 0, _len = _ref1.length; _i < _len; _i++) { + view = _ref1[_i]; + _results.push(view.update(data)); + } + return _results; + } + }; + + Rivets.binders['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(); + } + }; + + Rivets.binders['*'] = function(el, value) { + if (value != null) { + return el.setAttribute(this.type, value); + } else { + return el.removeAttribute(this.type); + } + }; + + Rivets.factory = function(exports) { + exports._ = Rivets; + exports.binders = Rivets.binders; + exports.components = Rivets.components; + exports.formatters = Rivets.formatters; + exports.adapters = Rivets.adapters; + 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); diff --git a/ajax/libs/rivets/0.6.9/rivets.min.js b/ajax/libs/rivets/0.6.9/rivets.min.js new file mode 100644 index 000000000..ae10cc26d --- /dev/null +++ b/ajax/libs/rivets/0.6.9/rivets.min.js @@ -0,0 +1,5 @@ +// Rivets.js +// version: 0.6.9 +// author: Michael Richards +// license: MIT +(function(){var t,e,i,n,s=[].indexOf||function(t){for(var e=0,i=this.length;i>e;e++)if(e in this&&this[e]===t)return e;return-1},r=function(t,e){return function(){return t.apply(e,arguments)}},o=[].slice,h={}.hasOwnProperty,u=function(t,e){function i(){this.constructor=t}for(var n in e)h.call(e,n)&&(t[n]=e[n]);return i.prototype=e.prototype,t.prototype=new i,t.__super__=e.prototype,t};t={binders:{},components:{},formatters:{},adapters:{},config:{prefix:"rv",templateDelimiters:["{","}"],rootInterface:".",preloadData:!0,handler:function(t,e,i){return this.call(t,e,i.view.models)}}},"jQuery"in window?(n="on"in jQuery.prototype?["on","off"]:["bind","unbind"],e=n[0],i=n[1],t.Util={bindEvent:function(t,i,n){return jQuery(t)[e](i,n)},unbindEvent:function(t,e,n){return jQuery(t)[i](e,n)},getInputValue:function(t){var e;return e=jQuery(t),"checkbox"===e.attr("type")?e.is(":checked"):e.val()}}):t.Util={bindEvent:function(){return"addEventListener"in window?function(t,e,i){return t.addEventListener(e,i,!1)}:function(t,e,i){return t.attachEvent("on"+e,i)}}(),unbindEvent:function(){return"removeEventListener"in window?function(t,e,i){return t.removeEventListener(e,i,!1)}:function(t,e,i){return t.detachEvent("on"+e,i)}}(),getInputValue:function(t){var e,i,n,s;if("checkbox"===t.type)return t.checked;if("select-multiple"===t.type){for(s=[],i=0,n=t.length;n>i;i++)e=t[i],e.selected&&s.push(e.value);return s}return t.value}},t.KeypathParser=function(){function t(){}return t.parse=function(t,e,i){var n,r,o,h,u,l;for(h=[],r={"interface":i,path:""},o=u=0,l=t.length;l>u;o=u+=1)n=t.charAt(o),s.call(e,n)>=0?(h.push(r),r={"interface":n,path:""}):r.path+=n;return h.push(r),h},t}(),t.TextTemplateParser=function(){function t(){}return t.types={text:0,binding:1},t.parse=function(t,e){var i,n,s,r,o,h,u;for(h=[],r=t.length,i=0,n=0;r>n;){if(i=t.indexOf(e[0],n),0>i){h.push({type:this.types.text,value:t.slice(n)});break}if(i>0&&i>n&&h.push({type:this.types.text,value:t.slice(n,i)}),n=i+e[0].length,i=t.indexOf(e[1],n),0>i){o=t.slice(n-e[1].length),s=h[h.length-1],(null!=s?s.type:void 0)===this.types.text?s.value+=o:h.push({type:this.types.text,value:o});break}u=t.slice(n,i).trim(),h.push({type:this.types.binding,value:u}),n=i+e[1].length}return h},t}(),t.Observer=function(){function e(t,e,i,n){this.view=t,this.model=e,this.keypath=i,this.callback=n,this.unobserve=r(this.unobserve,this),this.realize=r(this.realize,this),this.value=r(this.value,this),this.publish=r(this.publish,this),this.read=r(this.read,this),this.set=r(this.set,this),this.adapter=r(this.adapter,this),this.update=r(this.update,this),this.initialize=r(this.initialize,this),this.parse=r(this.parse,this),this.parse(),this.initialize()}return e.prototype.parse=function(){var e,i,n,r,o,h;return e=function(){var t,e;t=this.view.adapters,e=[];for(i in t)o=t[i],o&&e.push(i);return e}.call(this),h=this.keypath[0],s.call(e,h)>=0?(r=this.keypath[0],n=this.keypath.substr(1)):(r=this.view.config.rootInterface,n=this.keypath),this.tokens=t.KeypathParser.parse(n,e,r),this.key=this.tokens.pop()},e.prototype.initialize=function(){return this.objectPath=[],this.target=this.realize(),null!=this.target?this.set(!0,this.key,this.target,this.callback):void 0},e.prototype.update=function(){var t,e;return(t=this.realize())!==this.target&&(null!=this.target&&this.set(!1,this.key,this.target,this.callback),null!=t&&this.set(!0,this.key,t,this.callback),e=this.value(),this.target=t,this.value()!==e)?this.callback():void 0},e.prototype.adapter=function(t){return this.view.adapters[t["interface"]]},e.prototype.set=function(t,e,i,n){var s;return s=t?"subscribe":"unsubscribe",this.adapter(e)[s](i,e.path,n)},e.prototype.read=function(t,e){return this.adapter(t).read(e,t.path)},e.prototype.publish=function(t){return null!=this.target?this.adapter(this.key).publish(this.target,this.key.path,t):void 0},e.prototype.value=function(){return null!=this.target?this.read(this.key,this.target):void 0},e.prototype.realize=function(){var t,e,i,n,s,r,o,h;for(t=this.model,s=null,h=this.tokens,e=r=0,o=h.length;o>r;e=++r)n=h[e],null!=t?(null!=this.objectPath[e]?t!==(i=this.objectPath[e])&&(this.set(!1,n,i,this.update),this.set(!0,n,t,this.update),this.objectPath[e]=t):(this.set(!0,n,t,this.update),this.objectPath[e]=t),t=this.read(n,t)):(null==s&&(s=e),(i=this.objectPath[e])&&this.set(!1,n,i,this.update));return null!=s&&this.objectPath.splice(s),t},e.prototype.unobserve=function(){var t,e,i,n,s,r;for(r=this.tokens,t=n=0,s=r.length;s>n;t=++n)i=r[t],(e=this.objectPath[t])&&this.set(!1,i,e,this.update);return null!=this.target?this.set(!1,this.key,this.target,this.callback):void 0},e}(),t.View=function(){function e(e,i,n){var s,o,h,u,l,a,d,p,c;for(this.els=e,this.models=i,this.options=null!=n?n:{},this.update=r(this.update,this),this.publish=r(this.publish,this),this.sync=r(this.sync,this),this.unbind=r(this.unbind,this),this.bind=r(this.bind,this),this.select=r(this.select,this),this.build=r(this.build,this),this.componentRegExp=r(this.componentRegExp,this),this.bindingRegExp=r(this.bindingRegExp,this),this.els.jquery||this.els instanceof Array||(this.els=[this.els]),d=["config","binders","formatters","adapters"],l=0,a=d.length;a>l;l++){if(o=d[l],this[o]={},this.options[o]){p=this.options[o];for(s in p)h=p[s],this[o][s]=h}c=t[o];for(s in c)h=c[s],null==(u=this[o])[s]&&(u[s]=h)}this.build()}return e.prototype.bindingRegExp=function(){return new RegExp("^"+this.config.prefix+"-")},e.prototype.componentRegExp=function(){return new RegExp("^"+this.config.prefix.toUpperCase()+"-")},e.prototype.build=function(){var e,i,n,r,o,h,u,l,a;for(this.bindings=[],h=[],e=this.bindingRegExp(),n=this.componentRegExp(),i=function(e){return function(i,n,s,r){var o,h,u,l,a,d,p;return a={},p=function(){var t,e,i,n;for(i=r.split("|"),n=[],t=0,e=i.length;e>t;t++)d=i[t],n.push(d.trim());return n}(),o=function(){var t,e,i,n;for(i=p.shift().split("<"),n=[],t=0,e=i.length;e>t;t++)h=i[t],n.push(h.trim());return n}(),l=o.shift(),a.formatters=p,(u=o.shift())&&(a.dependencies=u.split(/\s+/)),e.bindings.push(new t[i](e,n,s,l,a))}}(this),o=function(r){return function(u){var l,a,d,p,c,f,b,v,g,y,m,w,k,x,E,N,j,O,R,B,P,V,C,U,A,T,S,_,z,Q;if(s.call(h,u)<0){if(3===u.nodeType){if(v=t.TextTemplateParser,(c=r.config.templateDelimiters)&&(w=v.parse(u.data,c)).length&&(1!==w.length||w[0].type!==v.types.text)){for(E=0,R=w.length;R>E;E++)m=w[E],y=document.createTextNode(m.value),u.parentNode.insertBefore(y,u),1===m.type&&i("TextBinding",y,null,m.value);u.parentNode.removeChild(u)}}else if(n.test(u.tagName))k=u.tagName.replace(n,"").toLowerCase(),r.bindings.push(new t.ComponentBinding(r,u,k));else if(null!=u.attributes){for(A=u.attributes,N=0,B=A.length;B>N;N++)if(l=A[N],e.test(l.name)){if(k=l.name.replace(e,""),!(d=r.binders[k])){T=r.binders;for(f in T)x=T[f],"*"!==f&&-1!==f.indexOf("*")&&(g=new RegExp("^"+f.replace("*",".+")+"$"),g.test(k)&&(d=x))}if(d||(d=r.binders["*"]),d.block){for(S=u.childNodes,j=0,P=S.length;P>j;j++)b=S[j],h.push(b);a=[l]}}for(_=a||u.attributes,O=0,V=_.length;V>O;O++)l=_[O],e.test(l.name)&&(k=l.name.replace(e,""),i("Binding",u,k,l.value))}for(z=function(){var t,e,i,n;for(i=u.childNodes,n=[],e=0,t=i.length;t>e;e++)b=i[e],n.push(b);return n}(),Q=[],U=0,C=z.length;C>U;U++)p=z[U],Q.push(o(p));return Q}}}(this),a=this.els,u=0,l=a.length;l>u;u++)r=a[u],o(r)},e.prototype.select=function(t){var e,i,n,s,r;for(s=this.bindings,r=[],i=0,n=s.length;n>i;i++)e=s[i],t(e)&&r.push(e);return r},e.prototype.bind=function(){var t,e,i,n,s;for(n=this.bindings,s=[],e=0,i=n.length;i>e;e++)t=n[e],s.push(t.bind());return s},e.prototype.unbind=function(){var t,e,i,n,s;for(n=this.bindings,s=[],e=0,i=n.length;i>e;e++)t=n[e],s.push(t.unbind());return s},e.prototype.sync=function(){var t,e,i,n,s;for(n=this.bindings,s=[],e=0,i=n.length;i>e;e++)t=n[e],s.push(t.sync());return s},e.prototype.publish=function(){var t,e,i,n,s;for(n=this.select(function(t){return t.binder.publishes}),s=[],e=0,i=n.length;i>e;e++)t=n[e],s.push(t.publish());return s},e.prototype.update=function(t){var e,i,n,s,r,o,h;null==t&&(t={});for(i in t)n=t[i],this.models[i]=n;for(o=this.bindings,h=[],s=0,r=o.length;r>s;s++)e=o[s],h.push(e.update(t));return h},e}(),t.Binding=function(){function e(t,e,i,n,s){this.view=t,this.el=e,this.type=i,this.keypath=n,this.options=null!=s?s:{},this.update=r(this.update,this),this.unbind=r(this.unbind,this),this.bind=r(this.bind,this),this.publish=r(this.publish,this),this.sync=r(this.sync,this),this.set=r(this.set,this),this.eventHandler=r(this.eventHandler,this),this.formattedValue=r(this.formattedValue,this),this.setBinder=r(this.setBinder,this),this.formatters=this.options.formatters||[],this.dependencies=[],this.model=void 0,this.setBinder()}return e.prototype.setBinder=function(){var t,e,i,n;if(!(this.binder=this.view.binders[this.type])){n=this.view.binders;for(t in n)i=n[t],"*"!==t&&-1!==t.indexOf("*")&&(e=new RegExp("^"+t.replace("*",".+")+"$"),e.test(this.type)&&(this.binder=i,this.args=new RegExp("^"+t.replace("*","(.+)")+"$").exec(this.type),this.args.shift()))}return this.binder||(this.binder=this.view.binders["*"]),this.binder instanceof Function?this.binder={routine:this.binder}:void 0},e.prototype.formattedValue=function(t){var e,i,n,s,r,h;for(h=this.formatters,s=0,r=h.length;r>s;s++)i=h[s],e=i.split(/\s+/),n=e.shift(),i=this.view.formatters[n],(null!=i?i.read:void 0)instanceof Function?t=i.read.apply(i,[t].concat(o.call(e))):i instanceof Function&&(t=i.apply(null,[t].concat(o.call(e))));return t},e.prototype.eventHandler=function(t){var e,i;return i=(e=this).view.config.handler,function(n){return i.call(t,this,n,e)}},e.prototype.set=function(t){var e;return t=this.formattedValue(t instanceof Function&&!this.binder["function"]?t.call(this.model):t),null!=(e=this.binder.routine)?e.call(this,this.el,t):void 0},e.prototype.sync=function(){var e,i,n,s,r,o,h,u,l;if(this.model!==this.observer.target){for(h=this.dependencies,n=0,r=h.length;r>n;n++)i=h[n],i.unobserve();if(this.dependencies=[],null!=(this.model=this.observer.target)&&(null!=(u=this.options.dependencies)?u.length:void 0))for(l=this.options.dependencies,s=0,o=l.length;o>s;s++)e=l[s],i=new t.Observer(this.view,this.model,e,this.sync),this.dependencies.push(i)}return this.set(this.observer.value())},e.prototype.publish=function(){var e,i,n,s,r,h,u,l,a;for(s=t.Util.getInputValue(this.el),u=this.formatters.slice(0).reverse(),r=0,h=u.length;h>r;r++)i=u[r],e=i.split(/\s+/),n=e.shift(),(null!=(l=this.view.formatters[n])?l.publish:void 0)&&(s=(a=this.view.formatters[n]).publish.apply(a,[s].concat(o.call(e))));return this.observer.publish(s)},e.prototype.bind=function(){var e,i,n,s,r,o,h;if(null!=(r=this.binder.bind)&&r.call(this,this.el),this.observer=new t.Observer(this.view,this.view.models,this.keypath,this.sync),this.model=this.observer.target,null!=this.model&&(null!=(o=this.options.dependencies)?o.length:void 0))for(h=this.options.dependencies,n=0,s=h.length;s>n;n++)e=h[n],i=new t.Observer(this.view,this.model,e,this.sync),this.dependencies.push(i);return this.view.config.preloadData?this.sync():void 0},e.prototype.unbind=function(){var t,e,i,n,s;for(null!=(n=this.binder.unbind)&&n.call(this,this.el),this.observer.unobserve(),s=this.dependencies,e=0,i=s.length;i>e;e++)t=s[e],t.unobserve();return this.dependencies=[]},e.prototype.update=function(t){var e;return null==t&&(t={}),this.model=this.observer.target,this.unbind(),null!=(e=this.binder.update)&&e.call(this,t),this.bind()},e}(),t.ComponentBinding=function(e){function i(e,i,n){var o,h,u,l,a;for(this.view=e,this.el=i,this.type=n,this.unbind=r(this.unbind,this),this.bind=r(this.bind,this),this.update=r(this.update,this),this.locals=r(this.locals,this),this.component=t.components[this.type],this.attributes={},this.inflections={},l=this.el.attributes||[],h=0,u=l.length;u>h;h++)o=l[h],a=o.name,s.call(this.component.attributes,a)>=0?this.attributes[o.name]=o.value:this.inflections[o.name]=o.value}return u(i,e),i.prototype.sync=function(){},i.prototype.locals=function(t){var e,i,n,s,r,o,h,u,l;null==t&&(t=this.view.models),r={},u=this.inflections;for(i in u)for(e=u[i],l=e.split("."),o=0,h=l.length;h>o;o++)s=l[o],r[i]=(r[i]||t)[s];for(i in t)n=t[i],null==r[i]&&(r[i]=n);return r},i.prototype.update=function(t){var e;return null!=(e=this.componentView)?e.update(this.locals(t)):void 0},i.prototype.bind=function(){var e,i;return null!=this.componentView?null!=(i=this.componentView)?i.bind():void 0:(e=this.component.build.call(this.attributes),(this.componentView=new t.View(e,this.locals(),this.view.options)).bind(),this.el.parentNode.replaceChild(e,this.el))},i.prototype.unbind=function(){var t;return null!=(t=this.componentView)?t.unbind():void 0},i}(t.Binding),t.TextBinding=function(t){function e(t,e,i,n,s){this.view=t,this.el=e,this.type=i,this.keypath=n,this.options=null!=s?s:{},this.sync=r(this.sync,this),this.formatters=this.options.formatters||[],this.dependencies=[]}return u(e,t),e.prototype.binder={routine:function(t,e){return t.data=null!=e?e:""}},e.prototype.sync=function(){return e.__super__.sync.apply(this,arguments)},e}(t.Binding),t.adapters["."]={id:"_rv",counter:0,weakmap:{},weakReference:function(t){var e;return t.hasOwnProperty(this.id)||(e=this.counter++,this.weakmap[e]={callbacks:{}},Object.defineProperty(t,this.id,{value:e})),this.weakmap[t[this.id]]},stubFunction:function(t,e){var i,n,s;return n=t[e],i=this.weakReference(t),s=this.weakmap,t[e]=function(){var e,r,o,h,u,l,a,d,p,c;h=n.apply(t,arguments),a=i.pointers;for(o in a)for(r=a[o],c=null!=(d=null!=(p=s[o])?p.callbacks[r]:void 0)?d:[],u=0,l=c.length;l>u;u++)(e=c[u])();return h}},observeMutations:function(t,e,i){var n,r,o,h,u,l;if(Array.isArray(t)){if(o=this.weakReference(t),null==o.pointers)for(o.pointers={},r=["push","pop","shift","unshift","sort","reverse","splice"],u=0,l=r.length;l>u;u++)n=r[u],this.stubFunction(t,n);if(null==(h=o.pointers)[e]&&(h[e]=[]),s.call(o.pointers[e],i)<0)return o.pointers[e].push(i)}},unobserveMutations:function(t,e,i){var n,s,r;return Array.isArray(t&&null!=t[this.id])&&(s=null!=(r=this.weakReference(t).pointers)?r[e]:void 0)&&(n=s.indexOf(i),n>=0)?s.splice(n,1):void 0},subscribe:function(t,e,i){var n,r;return n=this.weakReference(t).callbacks,null==n[e]&&(n[e]=[],r=t[e],Object.defineProperty(t,e,{enumerable:!0,get:function(){return r},set:function(o){return function(h){var u,l,a;if(h!==r){for(r=h,a=n[e].slice(),u=0,l=a.length;l>u;u++)i=a[u],s.call(n[e],i)>=0&&i();return o.observeMutations(h,t[o.id],e)}}}(this)})),s.call(n[e],i)<0&&n[e].push(i),this.observeMutations(t[e],t[this.id],e)},unsubscribe:function(t,e,i){var n,s;return n=this.weakmap[t[this.id]].callbacks[e],s=n.indexOf(i),s>=0&&n.splice(s,1),this.unobserveMutations(t[e],t[this.id],e)},read:function(t,e){return t[e]},publish:function(t,e,i){return t[e]=i}},t.binders.text=function(t,e){return null!=t.textContent?t.textContent=null!=e?e:"":t.innerText=null!=e?e:""},t.binders.html=function(t,e){return t.innerHTML=null!=e?e:""},t.binders.show=function(t,e){return t.style.display=e?"":"none"},t.binders.hide=function(t,e){return t.style.display=e?"none":""},t.binders.enabled=function(t,e){return t.disabled=!e},t.binders.disabled=function(t,e){return t.disabled=!!e},t.binders.checked={publishes:!0,bind:function(e){return t.Util.bindEvent(e,"change",this.publish)},unbind:function(e){return t.Util.unbindEvent(e,"change",this.publish)},routine:function(t,e){var i;return t.checked="radio"===t.type?(null!=(i=t.value)?i.toString():void 0)===(null!=e?e.toString():void 0):!!e}},t.binders.unchecked={publishes:!0,bind:function(e){return t.Util.bindEvent(e,"change",this.publish)},unbind:function(e){return t.Util.unbindEvent(e,"change",this.publish)},routine:function(t,e){var i;return t.checked="radio"===t.type?(null!=(i=t.value)?i.toString():void 0)!==(null!=e?e.toString():void 0):!e}},t.binders.value={publishes:!0,bind:function(e){return t.Util.bindEvent(e,"change",this.publish)},unbind:function(e){return t.Util.unbindEvent(e,"change",this.publish)},routine:function(t,e){var i,n,r,o,h,u,l;if(null!=window.jQuery){if(t=jQuery(t),(null!=e?e.toString():void 0)!==(null!=(o=t.val())?o.toString():void 0))return t.val(null!=e?e:"")}else if("select-multiple"===t.type){if(null!=e){for(l=[],n=0,r=t.length;r>n;n++)i=t[n],l.push(i.selected=(h=i.value,s.call(e,h)>=0));return l}}else if((null!=e?e.toString():void 0)!==(null!=(u=t.value)?u.toString():void 0))return t.value=null!=e?e:""}},t.binders["if"]={block:!0,bind:function(t){var e,i;return null==this.marker?(e=[this.view.config.prefix,this.type].join("-").replace("--","-"),i=t.getAttribute(e),this.marker=document.createComment(" rivets: "+this.type+" "+i+" "),t.removeAttribute(e),t.parentNode.insertBefore(this.marker,t),t.parentNode.removeChild(t)):void 0},unbind:function(){var t;return null!=(t=this.nested)?t.unbind():void 0},routine:function(e,i){var n,s,r,o,h;if(!!i==(null==this.nested)){if(i){r={},h=this.view.models;for(n in h)s=h[n],r[n]=s;return o={binders:this.view.options.binders,formatters:this.view.options.formatters,adapters:this.view.options.adapters,config:this.view.options.config},(this.nested=new t.View(e,r,o)).bind(),this.marker.parentNode.insertBefore(e,this.marker.nextSibling)}return e.parentNode.removeChild(e),this.nested.unbind(),delete this.nested}},update:function(t){var e;return null!=(e=this.nested)?e.update(t):void 0}},t.binders.unless={block:!0,bind:function(e){return t.binders["if"].bind.call(this,e)},unbind:function(){return t.binders["if"].unbind.call(this)},routine:function(e,i){return t.binders["if"].routine.call(this,e,!i)},update:function(e){return t.binders["if"].update.call(this,e)}},t.binders["on-*"]={"function":!0,unbind:function(e){return this.handler?t.Util.unbindEvent(e,this.args[0],this.handler):void 0},routine:function(e,i){return this.handler&&t.Util.unbindEvent(e,this.args[0],this.handler),t.Util.bindEvent(e,this.args[0],this.handler=this.eventHandler(i))}},t.binders["each-*"]={block:!0,bind:function(t){var e,i,n,s,r;if(null==this.marker)e=[this.view.config.prefix,this.type].join("-").replace("--","-"),this.marker=document.createComment(" rivets: "+this.type+" "),this.iterated=[],t.removeAttribute(e),t.parentNode.insertBefore(this.marker,t),t.parentNode.removeChild(t);else for(r=this.iterated,n=0,s=r.length;s>n;n++)i=r[n],i.bind()},unbind:function(){var t,e,i,n,s;if(null!=this.iterated){for(n=this.iterated,s=[],e=0,i=n.length;i>e;e++)t=n[e],s.push(t.unbind());return s}},routine:function(e,i){var n,s,r,o,h,u,l,a,d,p,c,f,b,v,g,y,m,w,k,x,E,N,j,O;if(a=this.args[0],i=i||[],this.iterated.length>i.length)for(x=Array(this.iterated.length-i.length),v=0,m=x.length;m>v;v++)r=x[v],b=this.iterated.pop(),b.unbind(),this.marker.parentNode.removeChild(b.els[0]);for(o=g=0,w=i.length;w>g;o=++g)if(l=i[o],s={index:o},s[a]=l,null==this.iterated[o]){E=this.view.models;for(u in E)l=E[u],null==s[u]&&(s[u]=l);p=this.iterated.length?this.iterated[this.iterated.length-1].els[0]:this.marker,d={binders:this.view.options.binders,formatters:this.view.options.formatters,adapters:this.view.options.adapters,config:{}},N=this.view.options.config;for(h in N)f=N[h],d.config[h]=f;d.config.preloadData=!0,c=e.cloneNode(!0),b=new t.View(c,s,d),b.bind(),this.iterated.push(b),this.marker.parentNode.insertBefore(c,p.nextSibling)}else this.iterated[o].models[a]!==l&&this.iterated[o].update(s);if("OPTION"===e.nodeName){for(j=this.view.bindings,O=[],y=0,k=j.length;k>y;y++)n=j[y],O.push(n.el===this.marker.parentNode&&"value"===n.type?n.sync():void 0);return O}},update:function(t){var e,i,n,s,r,o,h,u;e={};for(i in t)n=t[i],i!==this.args[0]&&(e[i]=n);for(h=this.iterated,u=[],r=0,o=h.length;o>r;r++)s=h[r],u.push(s.update(e));return u}},t.binders["class-*"]=function(t,e){var i;return i=" "+t.className+" ",!e==(-1!==i.indexOf(" "+this.args[0]+" "))?t.className=e?""+t.className+" "+this.args[0]:i.replace(" "+this.args[0]+" "," ").trim():void 0},t.binders["*"]=function(t,e){return null!=e?t.setAttribute(this.type,e):t.removeAttribute(this.type)},t.factory=function(e){return e._=t,e.binders=t.binders,e.components=t.components,e.formatters=t.formatters,e.adapters=t.adapters,e.config=t.config,e.configure=function(e){var i,n;null==e&&(e={});for(i in e)n=e[i],t.config[i]=n},e.bind=function(e,i,n){var s;return null==i&&(i={}),null==n&&(n={}),s=new t.View(e,i,n),s.bind(),s}},"object"==typeof exports?t.factory(exports):"function"==typeof define&&define.amd?define(["exports"],function(e){return t.factory(this.rivets=e),e}):t.factory(this.rivets={})}).call(this); \ No newline at end of file