UNPKG

@danielkalen/simplybind

Version:

Magically simple, framework-less one-way/two-way data binding for frontend/backend in ~5kb.

2 lines (1 loc) 17.8 kB
!function(){var x,u,G,p,H,l,I,R,w,y,q,z,S,f,J,K,L,A,T,n,U,m,V,B,W,M,N,h,C,D,X,O,E,P,F,Q,k,r;S=function(){var a;return a=document.createEvent("Event"),a.initEvent("change",!0,!1),a._sb=!0,a},y="push pop shift unshift splice reverse sort".split(" "),T=function(){return"sb_"+Math.floor(1e12*(1+Math.random())).toString(16)},n=function(){return Object.create(null)},w=function(a,b){return-1!==a.indexOf(b)},f={iD:function(a){return void 0!==a},iO:function(a){return"object"==typeof a&&a},iS:function(a){return"string"==typeof a},iN:function(a){return"number"==typeof a},iF:function(a){return"function"==typeof a},iBI:function(a){return a instanceof u},iB:function(a){return a instanceof x},isI:function(a){return f.iO(a)&&f.iN(a.length)},doM:function(a){return a.nodeName&&1===a.nodeType},doI:function(a){return a=a.nodeName.toUpperCase(),"INPUT"===a||"TEXTAREA"===a||"SELECT"===a},doR:function(a){return"radio"===a.type},doC:function(a){return"checkbox"===a.type},eC:function(a){return a instanceof NodeList||a instanceof HTMLCollection||f.iD(jQuery)&&a instanceof jQuery},eAS:function(a){var b;return b=a[0].type,[].filter.call(a,function(a){return a.type===b}).length===a.length}},E=function(a,b){var c,d;for(c in b)d=b[c],f.iD(m[c])&&(a.options[c]=d);return a.mPL()},A=function(a,b){var c,d,e,g;for(g=Object.keys(b),c=0,e=g.length;c<e;c++)d=g[c],a[d]=b[d]},z={set:function(a,b){var c,d;return b?Object.defineProperty(a.object,"_sb_ID",{configurable:!0,value:a.ID}):(d=a.selc,a.object._sb_map?a.object._sb_map[d]=a.ID:(c={},c[d]=a.ID,Object.defineProperty(a.object,"_sb_map",{configurable:!0,value:c})))},get:function(a,b,c,d){return b?q[a._sb_ID]:d&&a[0]._sb_map&&(b=q[a[0]._sb_map[c]],b.gB)?b.gB:a._sb_map&&a._sb_map[c]?q[a._sb_map[c]]:void 0}},L=/[.*+?^${}()|[\]\\]/g,C=D=null,P=function(){var a,b,c;return c=m.placeholder[0].replace(L,"\\$&"),a=m.placeholder[1].replace(L,"\\$&"),b="[^"+a+"]+",C=new RegExp(c+"("+b+")"+a,"g"),D=new RegExp(""+c+b+a,"g")},R=function(a,b,c){var d,e,g,v,f;for(f="",g=e=0,v=a.length;e<v;g=++e)d=a[g],f+=d,c[g]&&(f+=b[c[g]]);return f},I=function(a,b,c){null==a[c]&&(a[c]=[]),a[c].push(b)},O=function(a,b){var c,d,e,g,f,h,k,t,l,n,m;for(l=a.childNodes,c=0,g=l.length;c<g;c++)if(t=l[c],3!==t.nodeType)O(t,b);else if(t.textContent.match(D))if(d=t.textContent,m=d.split(C),3===m.length&&""===m[0]+m[2])I(b,t,m[1]);else{for(h=document.createDocumentFragment(),d=e=0,f=m.length;e<f;d=++e)n=m[d],k=h.appendChild(document.createTextNode(n)),d%2&&I(b,k,n);t.parentNode.replaceChild(h,t)}},F=function(a){throw Error("SimplyBind: "+(K[a]||a))},r=function(a,b){var c,d;if(!m.silent)return c=U(b),d=K[a],console.warn("SimplyBind: "+(d+("\n\n"+c)))},k=function(a){return F("You can't use/invoke ."+a+"() at this sG",!0)},Q=function(a,b){return F("Invalid argument/s ("+b+") passed to ."+a+"()",!0)},U=function(a){return(Error().stack||"").split("\n").slice(a+3).join("\n")},K={erIP:"SimplyBind() and .to() only accept a function, an array, a bound object, a string, or a number.",erFN:"Only functions are allowed for .transform/.condition/All()",erEV:"Invalid argument number in .ofEvent()",erOD:"You can only pass a single DOM element to a binding",erMX:"'checked' of Mixed list of element cannot be bound",erEL:"Empty element lists cannot be bound"},q={},m={silent:!1,liveProps:!0,dispatchEvents:!1,updateEvenIfSame:!1,updateOnBind:!0,mutateInherited:!1,trackArrayChildren:!1,simpleSelector:!1,promiseTransforms:!1,placeholder:["{{","}}"]},P(),l=function(a,b,c){return(a||0===a)&&(f.iS(a)||f.iN(a)||f.iF(a)||a instanceof Array)||f.iBI(a)||F("erIP"),!f.iO(a)||a instanceof Array?new u(null,0,null,a,c,b):new u(a._,1)},x=function(a,b,c,d){return A(this,c),this.type=b,this.object=a,this.ID=T(),this.deps=[],this.dM={1:n(),2:n()},this.dPH=n(),this.mPH=n(),this.tfms=n(),this.cnds=n(),this.atEV=[],"Event"!==this.type&&"Func"!==this.type||(this.options.updateOnBind=!1,this.options.updateEvenIfSame=!0),this.mC&&(this.cH=n(),this.object.forEach(function(a){return function(b){var c;return c=a.cH[b.value]=l("checked").of(b)._,c.aD(a).aTF(a.ID,function(){return c}),c.gB=a}}(this))),"Event"===this.type||d||(this.value=this.VLo=a=this.fDV(),"ObjectProp"!==this.type||f.iD(a)||(this.object[this.prop]=a),this.placeholder&&!this.pVL&&this.sPH(),this.mPL()),this.aEV(),this.object instanceof Array&&"Array"!==this.type&&(this.aBI=a=z.get(this.object,!0))&&a.options.trackArrayChildren&&!w(a.tC,this.prop)&&(a.tC.push(this.prop),l(this.prop).of(this.object).to(a.uS)),q[this.ID]=this},u=function(a,b,c,d,e,g){var v,h,k,l;switch(c&&A(this,c),this.sG=b||0,null==this.pX&&(this.pX=[]),null==this.sT&&(this.sT={}),this.sG){case 0:this.opts=g||(g={}),this.options={};for(l in m)this.options[l]=null!=g[l]?g[l]:m[l];f.iF(d)?(this.sG=1,a=this.createBP(d,"Func",!0,e)):d instanceof Array?(this.sG=1,a=this.createBP(d,"Array",!0)):(f.iN(d)&&(d=d.toString()),this.selc=this.prop=d,this.options.simpleSelector||(w(this.selc,":")&&(d=this.prop.split(":"),this.desc=d[0],this.prop=d[1]),w(this.selc,".")&&(d=this.prop.split("."),this.prop=d[0],this.placeholder=d.slice(1).join(".")),this.selc=this.prop));break;case 1:if(!a){if(b=(a=d!==window&&f.isI(d))?d[0]:d){if((this.doM=f.doM(b))&&("checked"===this.prop?(k=b&&f.doR(b),v=!k&&b&&f.doC(b)):"value"===this.prop&&(h=f.doI(b)),a&&"multi"!==this.desc))if(1===d.length)d=d[0];else{if((k||v)&&!f.eAS(d))return r("erMX",3);k||v?(this.mC=!0,d=[].slice.call(d)):(d=d[0],r("erOD",3))}}else a&&f.eC(d)&&r("erEL",3);a=function(){switch(!1){case!this.sT.hEN:return"Event";case!h:return"DOMValue";case!k:return"DOMRadio";case!v:return"DOMCheckbox";case"attr"!==this.desc:return"DOMAttr";default:return"ObjectProp"}}.call(this),a="multi"===this.desc?new H(d,a,this):this.createBP(d,a)}}return this.sOB(a)},x.prototype={mPL:function(a){var b,c,d,e;if(this.options.liveProps)if(b=this,"ObjectProp"===this.type){if(d=Object.getOwnPropertyDescriptor(this.object,this.prop)||{},(e=(e=this.options.mutateInherited||d&&d.configurable)&&this.object.constructor!==CSSStyleDeclaration)&&!this.isL||a)return this.isL=!0,a=d.get?d.get:function(){return b.value},e=d.set?function(a){return b.sV(a),d.set(a)}:function(a){return b.sV(a)},Object.defineProperty(this.object,this.prop,{configurable:!0,enumerable:d.enumerable,get:a,set:e})}else if("Array"===this.type&&(this.isL||(this.isL=!0,y.forEach(function(a){return Object.defineProperty(b.value,a,{configurable:!0,value:function(){var c;return c=Array.prototype[a].apply(b.value,arguments),b.uAD(),c}})})),this.options.trackArrayChildren&&!this.tC))return this.tC=[],this.uS=function(){return b.uAD()},c={updateOnBind:!1},this.value.forEach(function(a,d){return b.tC.push(""+d),l(d,c).of(b.value).to(b.uS)})},aD:function(a,b){var c,d,e,g;if(a.isMulti)for(g=a.bindings,d=0,e=g.length;d<e;d++)c=g[d],this.aD(c);else this.dM[1][a.ID]||(this.dM[1][a.ID]=a,this.deps.push(a)),this.placeholder?this.mPH[a.ID]=this.placeholder:this.mPH[a.ID]&&delete this.mPH[a.ID],a.placeholder&&(this.dPH[a.ID]=a.placeholder),b?this.dM[2][a.ID]=a:a.dM[1][this.ID]&&(a.aD(this,!0),this.aD(a,!0));return this},rD:function(a,b){var c,d,e,g;if(a.isMulti)for(g=a.bindings,d=0,e=g.length;d<e;d++)c=g[d],this.rD(c,b);else this.dM[1][a.ID]&&(this.deps.splice(this.deps.indexOf(a),1),delete this.dM[1][a.ID],delete this.dPH[a.ID]),b&&(a.rD(this),delete this.dM[2][a.ID])},rAD:function(a){var b,c,d,e;for(e=this.deps.slice(),c=0,d=e.length;c<d;c++)b=e[c],this.rD(b,a);if(a||0===Object.keys(this.dM[2]).length)return this.DES()},DES:function(){var a,b,c,d;if(delete q[this.ID],"ObjectProp"===this.type)Object.defineProperty(this.object,this.prop,{value:this.value,writable:!0}),delete this.object._sb_map,delete this.object._sb_ID;else if("Event"===this.type){for(d=this.atEV,b=0,c=d.length;b<c;b++)a=d[b],this.urEVE(a,this.cEM.remove);delete this.object._sb_map}else if("Array"===this.type)for(delete this.object._sb_ID,a=0,b=y.length;a<b;a++)c=y[a],delete this.object[c];else"Func"===this.type&&delete this.object._sb_ID},fDV:function(){var a,b,c,d,e;switch(e=this.type,!1){case"Func"!==e:return this.object();case"Array"!==e:return this.object;case"DOMAttr"!==e:return this.object.getAttribute(this.prop)||"";case!this.mC:d=[],c=this.cH;for(b in c)if(a=c[b],a.object.checked){if("DOMRadio"===e)return b;d.push(b)}return d;default:return this.object[this.prop]}},sV:function(a,b,c,d){var e,g,h,k,l,m,n;if(null==c&&(c=this),e=b?this.pVL[b]:this.value,this.tfS&&(a=this.tfS(a)),(a!==e||this.options.updateEvenIfSame)&&"Array"!==this.type){if(b){if(this.pVL[b]=a,this.txN&&(n=this.txN[b]))for(h=0,k=n.length;h<k;h++)m=n[h],m[this.prop]=a;a=R(this.pCT,this.pVL,this.pIM)}switch(this.type){case"ObjectProp":this.isL||b&&this.doM&&"textContent"===this.prop||(this.object[this.prop]=a);break;case"Func":e=this.VLp,this.VLp=a,a=this.object(a,e);break;case"Event":d||(this.iE=!0,this.eE(a),this.iE=!1);break;case"DOMValue":this.object.value!==a&&(this.object.value=a,this.eCE());break;case"DOMRadio":if(this.mC)if(g=f.iB(a)?a:this.cH[a])for(l in a=g.object.value,b=this.cH)e=b[l],e.sV(e.ID===g.ID,null,c);else a=e;else a=!!a,this.object.checked!==a&&(this.object.checked=a),a&&this.eCE();break;case"DOMCheckbox":if(this.mC){for(l=!f.iB(a),a=[].concat(a),e=b=0,d=a.length;b<d;e=++b)h=a[e],a[e]=f.iB(h)?h:this.cH[h];d=[],h=this.cH;for(g in h)e=h[g],b=l?w(a,e):e.value,e.sV(b,null,c),b&&d.push(g);a=d}else a=!!a,this.object.checked!==a&&(this.object.checked=a,this.eCE());break;case"DOMAttr":this.object.setAttribute(this.prop,a)}this.value=a,this.uAD(c)}},uAD:function(a){var b,c,d,e;if(this.deps.length){if(this.thR){if(+new Date-this.thLU<this.thR)return clearTimeout(this.thT),this.thT=setTimeout(function(b){return function(){return b.uAD(a)}}(this),this.thR);this.thLU=+new Date}for(e=this.deps,c=0,d=e.length;c<d;c++)b=e[c],this.uD(b,a)}},uD:function(a,b){var c,d,e,g;b&&(b===a||b!==this&&b.dM[1][a.ID])||(b||(b=this),c=(c=this.mPH[a.ID])?this.pVL[c]:this.value,e=(d=this.dPH[a.ID])?a.pVL[d]:a.value,g=this.hTfs?this.apT(a,d,c,e):c,(!this.hCs||this.cC(a,d,c,e))&&(this.options.promiseTransforms&&null!=g&&f.iF(g.then)?g.then(function(c){return function(c){return a.sV(c,d,b)}}(this)):a.sV(g,d,b)))},pTF:function(a,b){var c,d,e,g;if(f.iF(a)){for(c=0,d=b.length;c<d;c++)e=b[c],e=e._||e,e.isMulti?this.pTF(a,e.bindings):(g=e.ID,g+=e.placeholder?"."+e.placeholder:"",this.aTF(g,a),this.dM[2][e.ID]&&e.aTF(this.ID,a),(this.options.updateOnBind||"Func"===this.type)&&this.uD(e));return!0}return r("erFN",2)},apT:function(a,b,c,d){return a=a.ID,b&&(a+="."+b),this.tfms[a]?this.tfms[a](c,d):c},aTF:function(a,b){return this.hTfs=!0,this.tfms[a]=b},pCN:function(a,b){var c,d,e,g;if(f.iF(a)){for(c=0,d=b.length;c<d;c++)e=b[c],e=e._||e,e.isMulti?this.pCN(a,e.bindings):(g=e.ID,g+=e.placeholder?"."+e.placeholder:"",this.aCND(g,a),this.dM[2][e.ID]&&e.aCND(this.ID,a));return!0}return r("erFN",2)},cC:function(a,b,c,d){return a=a.ID,b&&(a+="."+b),!this.cnds[a]||this.cnds[a](c,d)},aCND:function(a,b){return this.hCs=!0,this.cnds[a]=b},sPH:function(){var a;if(this.pVL=n(),this.pIM=n(),this.pCT=[],f.iS(this.VLo)&&(this.pCT=this.VLo.split(D),a=0,this.value=this.VLo.replace(C,function(b){return function(c,d){return b.pIM[a++]=d,b.pVL[d]=d}}(this))),this.doM&&"textContent"===this.prop)return O(this.object,this.txN=n())},aPI:function(a){return this.rPI(),this.PI=setInterval(function(a){return function(){var c;return c=a.fDV(),a.sV(c)}}(this),a)},rPI:function(){return clearInterval(this.PI),this.PI=null},aUV:function(a,b){return this.object.addEventListener(a,function(a){return function(d){if(!d._sb)return a.sV(d.target[b])}}(this),!1)},eCE:function(){if(this.options.dispatchEvents)return this.object.dispatchEvent(S())},aEV:function(){return this.evN?this.rEVE(this.evN,this.cEM.in):"DOMValue"===this.type?(this.aUV("input","value"),this.aUV("change","value")):this.mC||"DOMRadio"!==this.type&&"DOMCheckbox"!==this.type?void 0:this.aUV("change","checked")},rEVE:function(a,b){if(!w(this.atEV,a))return this.atEV.push(a),this.iEM(a,b||"addEventListener","addEventListener")},urEVE:function(a,b){var c;if(c=this.atEV.indexOf(a),-1!==c)return this.atEV.splice(c,1),this.iEM(a,b||"removeEventListener","removeEventListener")},iEM:function(a,b,c){var d;return d=this.object,(this.doM&&f.iD(jQuery)&&"on"===b||"off"===b)&&(d=jQuery(this.object)),d[b]||(b=c),this.evH||(this.evH=V.bind(this)),"function"==typeof d[b]?d[b](a,this.evH):void 0},eE:function(a){var b,c;return c=this.object,b=this.cEM.out||"dispatchEvent",this.doM&&f.iD(jQuery)&&"trigger"===b&&(c=jQuery(this.object)),c[b]||(b="dispatchEvent"),"dispatchEvent"===b?(this.evO||(this.evO=document.createEvent("Event"),this.evO.initEvent(this.evN,!0,!0)),this.evO.bindingData=a,c[b](this.evO)):c[b](this.evN,a)}},V=function(){var a;if(!this.iE)return a="Event"===this.type?arguments[this.prop]:this.fDV(),this.sV(a,null,null,!0)},G={new:function(a,b){return new u(this._,a,this,b)},sOB:function(a){return this._=a,Object.defineProperties(this,{ID:{get:function(){return a.ID}},value:{get:function(){return a.value}},original:{get:function(){return a.objects||a.object}},dependents:{get:function(){return a.deps.slice().map(function(a){return a.object})}},lpX:{get:function(){return this.pX[this.pX.length-1]}}})},createBP:function(a,b,c,d,e){var g;return this.object=a,(g=z.get(a,c,this.selc,this.mC))?this.patchCachedBP(g):(a=new x(a,b,e||this,d),z.set(a,c),a)},patchCachedBP:function(a){return(a.placeholder=this.placeholder)&&!a.pVL&&(a.VLo=a.fDV(),a.sPH()),"ObjectProp"!==a.type||this.prop in this.object||a.mPL(!0),E(a,this.opts),a}},u.prototype=Object.create(G),p={of:function(a){var b;return 0!==this.sG&&2!==this.sG&&k(h[0]),f.iO(a)||f.iF(a)||Q(h[0],a),f.iBI(a)&&(a=a.object),2===this.sG&&(this.pX[this.pX.length-1]=b=this.lpX.of(a),this.sT.hIB=!0,this.sT.hTf=!1,this._.aD(b._),M(this._,b._)),this.new(this.sG+1,a)},ofEvent:function(a,b,c){return 0!==this.sG||this.sT.hEN?k(h[1]):a&&f.iS(a)?isNaN(parseInt(this.prop))&&r("erEV",1):Q(h[1],a),this.sT.hEN=!0,this.evN=a,this.selc=this.prop+"#"+this.evN,this.cEM={in:b,out:c},this},to:function(a,b){var c;return(1!==this.sG||this.sT.hIB)&&k(h[2]),this.pX.push(c=J(this,a,b)),0===c.sG?c=2:(c=3,this.sT.hIB=!0),this.new(c)},and:function(a,b){var c;return 3===this.sG&&this.sT.hIB&&!this.sT.hMTf||k(h[3]),this.pX.push(c=J(this,a,b)),0===c.sG?c=2:(c=3,this.sT.hTf=!1),this.new(c)},toEvent:function(a,b,c,d){return 1!==this.sG&&k(h[4]),this.pX.push(l(0,d).ofEvent(a,c,b)),this.new(2)},chainTo:function(a,b){return 3!==this.sG&&k(h[5]),l(this.lpX).to(a,b)},set:function(a){return 0!==this.sG&&2!==this.sG||k(h[6]),this._.sV(a,this.placeholder),this},get:function(){return 0!==this.sG&&2!==this.sG||k(h[7]),this.placeholder?this._.pVL[this.placeholder]:this._.value},transformSelf:function(a){return(1!==this.sG||1===this.sG&&"Array"===this._.type)&&k(h[8]),f.iF(a)?(this._.tfS=a,this._.options.updateOnBind&&(a=this._.isMulti?this.value[0]:this.value,this._.sV(a))):r("erFN",1),this},transform:function(a){return(3!==this.sG||this.sT.hTf||this.sT.hMTf)&&k(h[9]),this.sT.hTf=this._.pTF(a,this.pX.slice(-1))||!1,this.new(3)},transformAll:function(a){return(3!==this.sG||this.sT.hTf||this.sT.hMTf)&&k(h[10]),this.sT.hMTf=this._.pTF(a,this.pX)||!1,this.new(3)},condition:function(a){return 3!==this.sG&&k(h[11]),this._.pCN(a,this.pX.slice(-1)),this.new(3)},conditionAll:function(a){return 3!==this.sG&&k(h[12]),this._.pCN(a,this.pX),this.new(3)},bothWays:function(a){var b,c,d,e;return(3!==this.sG||this.sT.hMTf)&&k(h[13]),d=this.lpX,e=d._.aD(this._,!0),c=this._.tfms[d.ID],b=this._.cnds[d.ID],(c||a)&&(c=f.iF(a)?a:c)&&!1!==a&&e.aTF(this.ID,c),b&&e.aCND(this.ID,b),this._.aD(d._,!0),this},unBind:function(a){var b,c,d,e;for(3!==this.sG&&k(h[14]),e=this.pX,b=0,c=e.length;b<c;b++)d=e[b],this._.rD(d._,a);return this},pollEvery:function(a){return 3===this.sG&&"Event"!==this._.type||k(h[15]),this._.aPI(a),this},stopPolling:function(){return 3!==this.sG&&k(h[16]),this._.rPI(),this},updateDepsOnEvent:function(a,b){return 3!==this.sG&&k(h[17]),this._.rEVE(a,b),this},removeEvent:function(a,b){return 3!==this.sG&&k(h[18]),this._.urEVE(a,b),this},throttle:function(a){return 1!==this.sG&&3!==this.sG&&k(h[19]),a&&f.iN(a)?this._.thR=a:!1===a&&delete this._.thR,this},setOption:function(a,b){var c,d,e,g,f;for(c={},c[""+a]=b,e=c,E(this._,e),f=this.pX,c=0,d=f.length;c<d;c++)g=f[c],E(g._,e);return this}},p.update=p.set,p.twoWay=p.bothWays,p.pipe=p.chainTo,h=Object.keys(p),B=0;for(W=h.length;B<W;B++)N=h[B],u.prototype[N]=p[N];return J=function(a,b,c){return null==c&&(c={}),b=l(b,c,!0),0!==b.sG&&(a._.aD(b._,a),M(a._,b._)),b},M=function(a,b){if(a.options.updateOnBind||"Func"===a.type)return a.uD(b)},H=function(a,b,c){var d,e,g,f;for(A(this,c),this.isMulti=!0,this.type=b,this.bindings=d=[],e=0,g=a.length;e<g;e++)f=a[e],d.push(this.createBP(f,b,null,null,c));return Object.defineProperties(this,{ID:{value:d[0].ID},options:{value:d[0].options},tfms:{value:d[0].tfms},cnds:{value:d[0].cnds},pVL:{get:function(){return d[0].pVL}},value:{get:function(){return d.map(function(a){return a.value})}},thR:{set:function(a){return d.forEach(function(b){return b.thR=a})}},tfS:{set:function(a){return d.forEach(function(b){return b.tfS=a})}}})},X=H.prototype=Object.create(G),Object.keys(x.prototype).forEach(function(a){return X[a]=function(b,c){var d,e,g,f;for(f=this.bindings,e=0,g=f.length;e<g;e++)d=f[e],d[a](b,c)}}),Object.defineProperties(l,{version:{value:"1.5.2"},options:{get:function(){var a,b,c;a={};for(b in m)c=m[b],a[b]=c;return a}}}),l.setOption=function(a,b){if(f.iD(m[a])&&(m[a]=b,"placeholder"===a))return P()},l.setOptions=function(a){var b,c;for(b in a)c=a[b],l.setOption(b,c)},l.unBindAll=function(a,b){var c,d,e;if(a&&(f.iO(a)||f.iF(a))&&(f.isI(a)&&!a._sb_ID&&a[0]&&f.doM(a[0])&&(a=a[0]),e=a._sb_map,a._sb_ID&&q[a._sb_ID].rAD(b),e))for(d in e)c=e[d],q[c].rAD(b)},window.SimplyBind=l}();