rivets.min.js
9.36 KB
// rivets.js
// version: 0.4.5
// author: Michael Richards
// license: MIT
(function(){var e,t,n,r,i,s,o,u,a,f,l,c,h,p=function(e,t){return function(){return e.apply(t,arguments)}},d=[].slice,v=[].indexOf||function(e){for(var t=0,n=this.length;t<n;t++)if(t in this&&this[t]===e)return t;return-1};e={},String.prototype.trim||(String.prototype.trim=function(){return this.replace(/^\s+|\s+$/g,"")}),u=function(t){var n,r,i,s,o,u;if(!(r=e.binders[t])){r=e.binders["*"],u=e.binders;for(i in u)o=u[i],i!=="*"&&i.indexOf("*")!==-1&&(s=new RegExp("^"+i.replace("*","(.+)")+"$"),s.test(t)&&(r=o,n=s.exec(t),n.shift()))}return r instanceof Function&&(r={routine:r}),[r,n]},e.Binding=function(){function t(e,t,n,r,i){var s;this.el=e,this.type=t,this.model=n,this.keypath=r,this.unbind=p(this.unbind,this),this.bind=p(this.bind,this),this.publish=p(this.publish,this),this.sync=p(this.sync,this),this.set=p(this.set,this),this.formattedValue=p(this.formattedValue,this),this.options=i||(i={}),s=i.binder?[i.binder,i.args]:u(t),this.binder=s[0],this.args=s[1],this.formatters=i.formatters||[]}return t.prototype.formattedValue=function(t){var n,r,i,s,o,u,a,f;o=this.model,f=this.formatters;for(u=0,a=f.length;u<a;u++)r=f[u],n=r.split(/\s+/),i=n.shift(),s=e.config.adapter.read(o,i),r=s instanceof Function?s:e.formatters[i],(r!=null?r.read:void 0)instanceof Function?t=r.read.apply(r,[t].concat(d.call(n))):r instanceof Function&&(t=r.apply(null,[t].concat(d.call(n))));return t},t.prototype.set=function(e){var t,n;return t=this.binder,e=this.formattedValue(e instanceof Function&&!t["function"]?e.call(this.model,this.options.bindContext):e),(n=t.routine)!=null?n.call(this,this.el,e):void 0},t.prototype.sync=function(){var t,n;return t=this.keypath,n=this.model,this.set(this.options.bypass?n[t]:e.config.adapter.read(n,t))},t.prototype.publish=function(){var t,n,r,i,s,o,u,f;if(this.binder.tokenizes)return;s=a(this.el),f=this.formatters.slice(0).reverse();for(o=0,u=f.length;o<u;o++){r=f[o],t=r.split(/\s+/),i=t.shift(),n=e.formatters[i];if(n!=null?n.publish:void 0)s=n.publish.apply(n,[s].concat(d.call(t)))}return e.config.adapter.publish(this.model,this.keypath,s)},t.prototype.bind=function(){var t,n=this;return(t=this.binder.bind)!=null&&t.call(this,this.el),this.options.bypass?this.sync():(this.keypath&&!this.binder.tokenizes&&e.config.adapter.subscribe(this.model,this.keypath,this.sync),e.config.preloadData&&this.sync()),l(this,function(t,r){return e.config.adapter.subscribe(t,r,n.sync)})},t.prototype.unbind=function(){var t,n=this;return(t=this.binder.unbind)!=null&&t.call(this,this.el),!this.options.bypass&&!!this.binder.tokenizes&&this.keypath&&e.config.adapter.unsubscribe(this.model,this.keypath,this.sync),l(this,function(t,r){return e.config.adapter.unsubscribe(t,r,n.sync)})},t}(),l=function(t,n){var r,i,s,o,u,a,f,l;if((a=t.options.dependencies)!=null?a.length:void 0){f=t.options.dependencies,l=[];for(o=0,u=f.length;o<u;o++)r=f[o],/^\./.test(r)?(s=t.model,i=r.substr(1)):(r=r.split("."),s=e.config.adapter.read(t.view.models(r.shift())),i=r.join(".")),l.push(n(s,i));return l}},o=/(.*?)\{\{([^{}]+)\}\}/,i=function(e,t,n){return t[n]=null,{routine:function(r,i){return t[n]=i,e.sync()}}},s=function(t,n,r,a,f){var l,c,h,p,d,v,m,g,y,b,w,E,S,x,T,N,C,k,L,A,O,M;if(o.test(f)){p=new e.Binding(n,r,a),O=[],L=[];while(f&&o.test(f))b=o.exec(f),f=f.substring(b[0].length),b[1]&&(O[O.length]=b[1]),L[L.length]=k=s(t,null,"*",a,b[2]),k.binder=i(p,O,O.length);return f&&(O[O.length]=f),l=p.bind,A=p.unbind,p.sync=function(){return p.set(O.join(""))},p.publish=function(){},p.bind=function(){var e,t,n,r;l(),r=[];for(t=0,n=L.length;t<n;t++)e=L[t],r.push(e.bind());return r},p.unbind=function(){var e,t,n,r;A(),r=[];for(t=0,n=L.length;t<n;t++)e=L[t],r.push(e.unbind());return r},p}N=function(){var e,t,n,r;n=f.split("|"),r=[];for(e=0,t=n.length;e<t;e++)T=n[e],r.push(T.trim());return r}(),d=function(){var e,t,n,r;n=N.shift().split("<"),r=[];for(e=0,t=n.length;e<t;e++)v=n[e],r.push(v.trim());return r}(),x=d.shift(),C=x.split(/\.|:/),E={formatters:N,bypass:x.indexOf(":")!==-1,bindContext:a},S=e.config.adapter.parsingSupport,M=u(r),c=M[0],E.args=M[1],h=c.tokenizes,E.binder=c,g=S||h?C[0]:C.shift(),w=g||!h?e.config.adapter.read(a,g):a,y=C.join(".");if(w||h){if(m=d.shift())E.dependencies=m.split(/\s+/);p=new e.Binding(n,r,S?a:w,y,E),p.view=t}return p},e.View=function(){function t(e,t){this.models=t,this.publish=p(this.publish,this),this.sync=p(this.sync,this),this.unbind=p(this.unbind,this),this.bind=p(this.bind,this),this.select=p(this.select,this),this.build=p(this.build,this),this.bindingRegExp=p(this.bindingRegExp,this),this.els=e.jquery||e instanceof Array?e:[e],this.build()}return t.prototype.bindingRegExp=function(){var t;return t=e.config.prefix,t?new RegExp("^data-"+t+"-"):/^data-/},t.prototype.build=function(){var t,n,r,i,o,u,a,f,l,c,h,p,d=this;n=this.bindings=[],u=[],t=this.bindingRegExp(),o=function(r){var i,o,a,f,l,c,h,p,m,g,y,b,w,E,S,x,T,N,C;if(v.call(u,r)<0){x=r.attributes;for(g=0,w=x.length;g<w;g++){i=x[g];if(t.test(i.name)){p=i.name.replace(t,"");if(!(a=e.binders[p])){T=e.binders;for(l in T)m=T[l],l!=="*"&&l.indexOf("*")!==-1&&(h=new RegExp("^"+l.replace("*",".+")+"$"),h.test(p)&&(a=m))}a||(a=e.binders["*"]);if(a.block){N=r.getElementsByTagName("*");for(y=0,E=N.length;y<E;y++)c=N[y],u.push(c);o=[i]}}}C=o||r.attributes;for(b=0,S=C.length;b<S;b++)i=C[b],t.test(i.name)&&(p=i.name.replace(t,""),f=s(d,r,p,d.models,i.value),f&&n.push(f));o&&(o=null)}},h=this.els;for(a=0,l=h.length;a<l;a++){r=h[a],r.attributes!=null&&o(r),p=r.getElementsByTagName("*");for(f=0,c=p.length;f<c;f++)i=p[f],i.attributes!=null&&o(i)}},t.prototype.select=function(e){var t,n,r,i,s;i=this.bindings,s=[];for(n=0,r=i.length;n<r;n++)t=i[n],e(t)&&s.push(t);return s},t.prototype.bind=function(){return this.bindings.map(function(e){return e.bind()})},t.prototype.unbind=function(){return this.bindings.map(function(e){return e.unbind()})},t.prototype.sync=function(){return this.bindings.map(function(e){return e.sync()})},t.prototype.publish=function(){return this.select(function(e){return e.binder.publishes}).map(function(e){return e.publish()})},t}(),t=function(e,t,n,r,i){var s;return s=function(e){return n.call(r,e,i)},window.jQuery!=null?(e=jQuery(e),e.on!=null?e.on(t,s):e.bind(t,s)):window.addEventListener!=null?e.addEventListener(t,s,!1):(t="on"+t,e.attachEvent(t,s)),s},h=function(e,t,n){return window.jQuery!=null?(e=jQuery(e),e.off!=null?e.off(t,n):e.unbind(t,n)):window.removeEventListener?e.removeEventListener(t,n,!1):(t="on"+t,e.detachEvent(t,n))},a=function(e){var t,n,r,i;switch(e.type){case"checkbox":return e.checked;case"select-multiple":i=[];for(n=0,r=e.length;n<r;n++)t=e[n],t.selected&&i.push(t.value);return i;default:return e.value}},f=function(t,n){var r,i,s,o,u,a,f,l;if(e.config.adapter.iterate)return e.config.adapter.iterate(t,n);if(t instanceof Array){f=[];for(r=u=0,a=t.length;u<a;r=++u)i=t[r],f.push(n(i,r));return f}l=[];for(o in t)s=t[o],l.push(n(s,o));return l},n=function(t){return e.config.adapter.convertToModel?e.config.adapter.convertToModel(t):t},r=function(e){return{publishes:!0,bind:function(e){return this.currentListener=t(e,"change",this.publish)},unbind:function(e){return h(e,"change",this.currentListener)},routine:e}},e.binders={enabled:function(e,t){return e.disabled=!t},disabled:function(e,t){return e.disabled=!!t},checked:r(function(e,t){return e.checked=e.type==="radio"?e.value===t:!!t}),unchecked:r(function(e,t){return e.checked=e.type==="radio"?e.value!==t:!t}),show:function(e,t){return e.style.display=t?"":"none"},hide:function(e,t){return e.style.display=t?"none":""},html:function(e,t){return e.innerHTML=t!=null?t:""},value:r(function(e,t){var n,r,i,s,o;if(e.type!=="select-multiple")return e.value=t!=null?t:"";if(t!=null){o=[];for(r=0,i=e.length;r<i;r++)n=e[r],o.push(n.selected=(s=n.value,v.call(t,s)>=0));return o}}),text:function(e,t){var n;return n=t!=null?t:"",e.innerText!=null?e.innerText=n:e.textContent=n},"on-*":{"function":!0,routine:function(e,n){var r,i;return i=this.args[0],r=this.currentListener,r&&h(e,i,r),this.currentListener=t(e,i,n,this.model,this.options.bindContext)}},"each-*":{block:!0,bind:function(e,t){return e.removeAttribute(["data",c.config.prefix,this.type].join("-").replace("--","-"))},routine:function(e,t){var r,i,s,o,u,a,l,h,p,d,v=this;i=this.iterated;if(i!=null)for(a=0,h=i.length;a<h;a++){u=i[a],u.unbind(),d=u.els;for(l=0,p=d.length;l<p;l++)r=d[l],r.parentNode.removeChild(r)}else s=this.marker=document.createComment(" rivets: "+this.type+" "),o=e.parentNode,o.insertBefore(s,e),o.removeChild(e);this.iterated=i=[];if(t)return s=this.marker,f(t,function(t,r){var o,u,a,l;return o={},f(v.view.models,function(e,t){return o[t]=e}),o[v.args[0]]=t,o[""+v.args[0]+"_index"]=o.rivets_index=r,o=n(o),u=e.cloneNode(!0),a=i.length>0?i[i.length-1].els[0]:s,s.parentNode.insertBefore(u,(l=a.nextSibling)!=null?l:null),i.push(c.bind(u,o))})}},"class-*":function(e,t){var n;n=" "+e.className+" ";if(!t==(n.indexOf(" "+this.args[0]+" ")!==-1))return e.className=t?""+e.className+" "+this.args[0]:n.replace(" "+this.args[0]+" "," ").trim()},"*":function(e,t){return t?e.setAttribute(this.type,t):e.removeAttribute(this.type)}},e.config={preloadData:!0},e.formatters={},c={binders:e.binders,formatters:e.formatters,config:e.config,configure:function(t){var n,r;t||(t={});for(n in t)r=t[n],e.config[n]=r},bind:function(t,n){var r;return n||(n={}),r=new e.View(t,n),r.bind(),r}},typeof module!="undefined"&&module!==null?module.exports=c:this.rivets=c}).call(this);