rivets.min.js
8.31 KB
// rivets.js
// version: 0.4.0
// author: Michael Richards
// license: MIT
(function(){var a,b,c,d,e,f=function(a,b){return function(){return a.apply(b,arguments)}},g=[].slice,h=[].indexOf||function(a){for(var b=0,c=this.length;b<c;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 b(b,c,d,e,g){var h,i,j,k;this.el=b,this.type=c,this.model=d,this.keypath=e,this.options=g!=null?g:{},this.unbind=f(this.unbind,this),this.bind=f(this.bind,this),this.publish=f(this.publish,this),this.sync=f(this.sync,this),this.set=f(this.set,this),this.formattedValue=f(this.formattedValue,this);if(!(this.binder=a.binders[c])){k=a.binders;for(h in k)j=k[h],h!=="*"&&h.indexOf("*")!==-1&&(i=new RegExp("^"+h.replace("*",".+")+"$"),i.test(c)&&(this.binder=j,this.args=(new RegExp("^"+h.replace("*","(.+)")+"$")).exec(c),this.args.shift()))}this.binder||(this.binder=a.binders["*"]),this.binder instanceof Function&&(this.binder={routine:this.binder}),this.formatters=this.options.formatters||[]}return b.prototype.formattedValue=function(b){var c,d,e,f,h,i,j,k;i=this.formatters;for(f=0,h=i.length;f<h;f++)d=i[f],c=d.split(/\s+/),e=c.shift(),b=this.model[e]instanceof Function?(j=this.model)[e].apply(j,[b].concat(g.call(c))):a.formatters[e]?(k=a.formatters)[e].apply(k,[b].concat(g.call(c))):void 0;return 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),(b=this.binder.routine)!=null?b.call(this,this.el,a):void 0},b.prototype.sync=function(){return this.set(this.options.bypass?this.model[this.keypath]:a.config.adapter.read(this.model,this.keypath))},b.prototype.publish=function(){return a.config.adapter.publish(this.model,this.keypath,c(this.el))},b.prototype.bind=function(){var b,c,d,e,f,g,h,i,j;this.options.bypass?this.sync():((g=this.binder.bind)!=null&&g.call(this,this.el),a.config.adapter.subscribe(this.model,this.keypath,this.sync),a.config.preloadData&&this.sync());if((h=this.options.dependencies)!=null?h.length:void 0){i=this.options.dependencies,j=[];for(e=0,f=i.length;e<f;e++)b=i[e],/^\./.test(b)?(d=this.model,c=b.substr(1)):(b=b.split("."),d=this.view.models[b.shift()],c=b.join(".")),j.push(a.config.adapter.subscribe(d,c,this.sync));return j}},b.prototype.unbind=function(){var b,c,d,e,f,g,h;this.options.bypass||((e=this.binder.unbind)!=null&&e.call(this,this.el),a.config.adapter.unsubscribe(this.model,this.keypath,this.sync));if((f=this.options.dependencies)!=null?f.length:void 0){g=this.options.dependencies,h=[];for(c=0,d=g.length;c<d;c++)b=g[c],h.push(a.config.adapter.unsubscribe(this.model,b,this.sync));return h}},b}(),a.View=function(){function b(a,b){this.els=a,this.models=b,this.publish=f(this.publish,this),this.sync=f(this.sync,this),this.unbind=f(this.unbind,this),this.bind=f(this.bind,this),this.select=f(this.select,this),this.build=f(this.build,this),this.bindingRegExp=f(this.bindingRegExp,this),this.els.jquery||this.els instanceof Array||(this.els=[this.els]),this.build()}return b.prototype.bindingRegExp=function(){var b;return b=a.config.prefix,b?new RegExp("^data-"+b+"-"):/^data-/},b.prototype.build=function(){var b,c,d,e,f,g,i,j,k,l,m,n=this;this.bindings=[],f=[],b=this.bindingRegExp(),e=function(c){var d,e,g,i,j,k,l,m,o,p,q,r,s,t,u,v,w,x,y,z,A,B,C,D,E,F,G,H,I;if(h.call(f,c)<0){F=c.attributes;for(z=0,C=F.length;z<C;z++){d=F[z];if(b.test(d.name)){x=d.name.replace(b,"");if(!(g=a.binders[x])){G=a.binders;for(m in G)y=G[m],m!=="*"&&m.indexOf("*")!==-1&&(v=new RegExp("^"+m.replace("*",".+")+"$"),v.test(x)&&(g=y))}g||(g=a.binders["*"]);if(g.block){H=c.getElementsByTagName("*");for(A=0,D=H.length;A<D;A++)q=H[A],f.push(q);e=[d]}}}I=e||c.attributes;for(B=0,E=I.length;B<E;B++){d=I[B];if(b.test(d.name)){r={},x=d.name.replace(b,""),u=function(){var a,b,c,e;c=d.value.split("|"),e=[];for(a=0,b=c.length;a<b;a++)t=c[a],e.push(t.trim());return e}(),j=function(){var a,b,c,d;c=u.shift().split("<"),d=[];for(a=0,b=c.length;a<b;a++)k=c[a],d.push(k.trim());return d}(),s=j.shift(),w=s.split(/\.|:/),r.formatters=u,r.bypass=s.indexOf(":")!==-1,w[0]?p=n.models[w.shift()]:(p=n.models,w.shift()),o=w.join(".");if(p){if(l=j.shift())r.dependencies=l.split(/\s+/);i=new a.Binding(c,x,p,o,r),i.view=n,n.bindings.push(i)}}}e&&(e=null)}},l=this.els;for(g=0,j=l.length;g<j;g++){c=l[g],e(c),m=c.getElementsByTagName("*");for(i=0,k=m.length;i<k;i++)d=m[i],e(d)}},b.prototype.select=function(a){var b,c,d,e,f;e=this.bindings,f=[];for(c=0,d=e.length;c<d;c++)b=e[c],a(b)&&f.push(b);return f},b.prototype.bind=function(){var a,b,c,d,e;d=this.bindings,e=[];for(b=0,c=d.length;b<c;b++)a=d[b],e.push(a.bind());return e},b.prototype.unbind=function(){var a,b,c,d,e;d=this.bindings,e=[];for(b=0,c=d.length;b<c;b++)a=d[b],e.push(a.unbind());return e},b.prototype.sync=function(){var a,b,c,d,e;d=this.bindings,e=[];for(b=0,c=d.length;b<c;b++)a=d[b],e.push(a.sync());return e},b.prototype.publish=function(){var a,b,c,d,e;d=this.select(function(a){return a.binder.publishes}),e=[];for(b=0,c=d.length;b<c;b++)a=d[b],e.push(a.publish());return e},b}(),b=function(a,b,c,d){var e;return e=function(a){return c.call(d,a)},window.jQuery!=null?(a=jQuery(a),a.on!=null?a.on(b,e):a.bind(b,e)):window.addEventListener!=null?a.addEventListener(b,e,!1):(b="on"+b,a.attachEvent(b,e)),e},e=function(a,b,c){return window.jQuery!=null?(a=jQuery(a),a.off!=null?a.off(b,c):a.unbind(b,c)):window.removeEventListener?a.removeEventListener(b,c,!1):(b="on"+b,a.detachEvent(b,c))},c=function(a){var b,c,d,e;switch(a.type){case"checkbox":return a.checked;case"select-multiple":e=[];for(c=0,d=a.length;c<d;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(a){return b(a,"change",this.publish)},unbind:function(a){return e(a,"change",this.publish)},routine:function(a,b){return a.type==="radio"?a.checked=a.value===b:a.checked=!!b}},unchecked:{publishes:!0,bind:function(a){return b(a,"change",this.publish)},unbind:function(a){return e(a,"change",this.publish)},routine:function(a,b){return a.type==="radio"?a.checked=a.value!==b:a.checked=!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=b!=null?b:""},value:{publishes:!0,bind:function(a){return b(a,"change",this.publish)},unbind:function(a){return e(a,"change",this.publish)},routine:function(a,b){var c,d,e,f,g;if(a.type!=="select-multiple")return a.value=b!=null?b:"";if(b!=null){g=[];for(d=0,e=a.length;d<e;d++)c=a[d],g.push(c.selected=(f=c.value,h.call(b,f)>=0));return g}}},text:function(a,b){return a.innerText!=null?a.innerText=b!=null?b:"":a.textContent=b!=null?b:""},"on-*":{"function":!0,routine:function(a,c){return this.currentListener&&e(a,this.args[0],this.currentListener),this.currentListener=b(a,this.args[0],c,this.model)}},"each-*":{block:!0,bind:function(a,b){return a.removeAttribute(["data",d.config.prefix,this.type].join("-").replace("--","-"))},routine:function(a,b){var c,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v;if(this.iterated!=null){r=this.iterated;for(l=0,o=r.length;l<o;l++){k=r[l],k.unbind(),s=k.els;for(m=0,p=s.length;m<p;m++)e=s[m],e.parentNode.removeChild(e)}}else this.marker=document.createComment(" rivets: "+this.type+" "),a.parentNode.insertBefore(this.marker,a),a.parentNode.removeChild(a);this.iterated=[],v=[];for(n=0,q=b.length;n<q;n++){f=b[n],c={},t=this.view.models;for(i in t)h=t[i],c[i]=h;c[this.args[0]]=f,g=a.cloneNode(!0),j=this.iterated[this.iterated.length-1]||this.marker,this.marker.parentNode.insertBefore(g,(u=j.nextSibling)!=null?u:null),v.push(this.iterated.push(d.bind(g,c)))}return v}},"class-*":function(a,b){var c;c=" "+a.className+" ";if(!b==(c.indexOf(" "+this.args[0]+" ")!==-1))return a.className=b?""+a.className+" "+this.args[0]:c.replace(" "+this.args[0]+" "," ").trim()},"*":function(a,b){return b?a.setAttribute(this.type,b):a.removeAttribute(this.type)}},a.config={preloadData:!0},a.formatters={},d={binders:a.binders,formatters:a.formatters,config:a.config,configure:function(b){var c,d;b==null&&(b={});for(c in b)d=b[c],a.config[c]=d},bind:function(b,c){var d;return c==null&&(c={}),d=new a.View(b,c),d.bind(),d}},typeof module!="undefined"&&module!==null?module.exports=d:this.rivets=d}).call(this);