@danielkalen/simplybind
Version:
Magically simple, framework-less one-way/two-way data binding for frontend/backend in ~5kb.
2 lines (1 loc) • 14 kB
JavaScript
!function(){var v,q,C,n,D,l,N,t,w,r,x,h,E,F,G,y,O,p,P,m,Q,z,R,H,I,g,J,K,S,A,L,B,M,k,u;w="push pop shift unshift splice reverse sort".split(" "),O=function(){return"sb_"+Math.floor(1e12*(1+Math.random())).toString(16)},p=function(){return Object.create(null)},t=function(a,b){return-1!==a.indexOf(b)},h={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 q}},A=function(a,b){var c,d;for(c in b)d=b[c],h.iD(m[c])&&(a.options[c]=d);return a.mPL()},y=function(a,b){var c,d,e,f;for(f=Object.keys(b),c=0,e=f.length;c<e;c++)d=f[c],a[d]=b[d]},x={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?r[a._sb_ID]:a._sb_map&&a._sb_map[c]?r[a._sb_map[c]]:void 0}},G=/[.*+?^${}()|[\]\\]/g,J=K=null,L=function(){var a,b,c;return c=m.placeholder[0].replace(G,"\\$&"),a=m.placeholder[1].replace(G,"\\$&"),b="[^"+a+"]+",J=new RegExp(c+"("+b+")"+a,"g"),K=new RegExp(""+c+b+a,"g")},N=function(a,b,c){var d,e,f,T,g;for(g="",f=e=0,T=a.length;e<T;f=++e)d=a[f],g+=d,c[f]&&(g+=b[c[f]]);return g},B=function(a){throw Error("SimplyBind: "+(F[a]||a))},u=function(a,b){var c,d;if(!m.silent)return c=P(b),d=F[a],console.warn("SimplyBind: "+(d+("\n\n"+c)))},k=function(a){return B("You can't use/invoke ."+a+"() at this sG",!0)},M=function(a,b){return B("Invalid argument/s ("+b+") passed to ."+a+"()",!0)},P=function(a){return(Error().stack||"").split("\n").slice(a+3).join("\n")},F={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()"},r={},m={silent:!1,liveProps:!0,dispatchEvents:!1,updateEvenIfSame:!1,updateOnBind:!0,mutateInherited:!1,trackArrayChildren:!1,simpleSelector:!1,promiseTransforms:!1,placeholder:["{{","}}"]},L(),l=function(a,b,c){return(a||0===a)&&(h.iS(a)||h.iN(a)||h.iF(a)||a instanceof Array)||h.iBI(a)||B("erIP"),!h.iO(a)||a instanceof Array?new q(null,0,null,a,c,b):new q(a._,1)},v=function(a,b,c,d){return y(this,c),this.type=b,this.object=a,this.ID=O(),this.deps=[],this.dM={1:p(),2:p()},this.dPH=p(),this.mPH=p(),this.tfms=p(),this.cnds=p(),this.atEV=[],"Event"!==this.type&&"Func"!==this.type||(this.options.updateOnBind=!1,this.options.updateEvenIfSame=!0),"Event"===this.type||d||(this.value=this.VLo=a=this.fDV(),"ObjectProp"!==this.type||h.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=x.get(this.object,!0))&&a.options.trackArrayChildren&&!t(a.tC,this.prop)&&(a.tC.push(this.prop),l(this.prop).of(this.object).to(a.uS)),r[this.ID]=this},q=function(a,b,c,d,e,f){var g;switch(c&&y(this,c),this.sG=b||0,null==this.pX&&(this.pX=[]),null==this.sT&&(this.sT={}),this.sG){case 0:this.opts=f||(f={}),this.options={};for(g in m)this.options[g]=null!=f[g]?f[g]:m[g];h.iF(d)?(this.sG=1,a=this.createBP(d,"Func",!0,e)):d instanceof Array?(this.sG=1,a=this.createBP(d,"Array",!0)):(h.iN(d)&&(d=d.toString()),this.selc=this.prop=d,this.options.simpleSelector||(t(this.selc,":")&&(d=this.prop.split(":"),this.desc=d[0],this.prop=d[1]),t(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){a:switch(!1){case!this.sT.hEN:a="Event";break a;default:a="ObjectProp"}a="multi"===this.desc?new D(d,a,this):this.createBP(d,a)}}return this.sOB(a)},v.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=this.options.mutateInherited||d&&d.configurable)&&!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,w.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,f;if(a.isMulti)for(f=a.bindings,d=0,e=f.length;d<e;d++)c=f[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,f;if(a.isMulti)for(f=a.bindings,d=0,e=f.length;d<e;d++)c=f[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 r[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=w.length;a<b;a++)c=w[a],delete this.object[c];else"Func"===this.type&&delete this.object._sb_ID},fDV:function(){var a;switch(a=this.type,!1){case"Func"!==a:return this.object();case"Array"!==a:return this.object;default:return this.object[this.prop]}},sV:function(a,b,c,d){var e;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){switch(b&&(this.pVL[b]=a,a=N(this.pCT,this.pVL,this.pIM)),this.type){case"ObjectProp":this.isL||(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)}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,f;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,f=this.hTfs?this.apT(a,d,c,e):c,(!this.hCs||this.cC(a,d,c,e))&&(this.options.promiseTransforms&&null!=f&&h.iF(f.then)?f.then(function(c){return function(c){return a.sV(c,d,b)}}(this)):a.sV(f,d,b)))},pTF:function(a,b){var c,d,e,f;if(h.iF(a)){for(c=0,d=b.length;c<d;c++)e=b[c],e=e._||e,e.isMulti?this.pTF(a,e.bindings):(f=e.ID,f+=e.placeholder?"."+e.placeholder:"",this.aTF(f,a),this.dM[2][e.ID]&&e.aTF(this.ID,a),(this.options.updateOnBind||"Func"===this.type)&&this.uD(e));return!0}return u("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,f;if(h.iF(a)){for(c=0,d=b.length;c<d;c++)e=b[c],e=e._||e,e.isMulti?this.pCN(a,e.bindings):(f=e.ID,f+=e.placeholder?"."+e.placeholder:"",this.aCND(f,a),this.dM[2][e.ID]&&e.aCND(this.ID,a));return!0}return u("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=p(),this.pIM=p(),this.pCT=[],h.iS(this.VLo))return this.pCT=this.VLo.split(K),a=0,this.value=this.VLo.replace(J,function(b){return function(c,d){return b.pIM[a++]=d,b.pVL[d]=d}}(this))},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},aEV:function(){if(this.evN)return this.rEVE(this.evN,this.cEM.in)},rEVE:function(a,b){if(!t(this.atEV,a))return this.atEV.push(a),this.iEM(a,b||"on","on")},urEVE:function(a,b){var c;if(c=this.atEV.indexOf(a),-1!==c)return this.atEV.splice(c,1),this.iEM(a,b||"removeListener","removeListener")},iEM:function(a,b,c){var d;return d=this.object,d[b]||(b=c),this.evH||(this.evH=Q.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||"emit",c[b]||(b="emit"),c[b](this.evN,a)}},Q=function(){var a;if(!this.iE)return a="Event"===this.type?arguments[this.prop]:this.fDV(),this.sV(a,null,null,!0)},C={new:function(a,b){return new q(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 f;return this.object=a,(f=x.get(a,c,this.selc,this.mC))?this.patchCachedBP(f):(a=new v(a,b,e||this,d),x.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),A(a,this.opts),a}},q.prototype=Object.create(C),n={of:function(a){var b;return 0!==this.sG&&2!==this.sG&&k(g[0]),h.iO(a)||h.iF(a)||M(g[0],a),h.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._),H(this._,b._)),this.new(this.sG+1,a)},ofEvent:function(a,b,c){return 0!==this.sG||this.sT.hEN?k(g[1]):a&&h.iS(a)?isNaN(parseInt(this.prop))&&u("erEV",1):M(g[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(g[2]),this.pX.push(c=E(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(g[3]),this.pX.push(c=E(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(g[4]),this.pX.push(l(0,d).ofEvent(a,c,b)),this.new(2)},chainTo:function(a,b){return 3!==this.sG&&k(g[5]),l(this.lpX).to(a,b)},set:function(a){return 0!==this.sG&&2!==this.sG||k(g[6]),this._.sV(a,this.placeholder),this},get:function(){return 0!==this.sG&&2!==this.sG||k(g[7]),this.placeholder?this._.pVL[this.placeholder]:this._.value},transformSelf:function(a){return(1!==this.sG||1===this.sG&&"Array"===this._.type)&&k(g[8]),h.iF(a)?(this._.tfS=a,this._.options.updateOnBind&&(a=this._.isMulti?this.value[0]:this.value,this._.sV(a))):u("erFN",1),this},transform:function(a){return(3!==this.sG||this.sT.hTf||this.sT.hMTf)&&k(g[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(g[10]),this.sT.hMTf=this._.pTF(a,this.pX)||!1,this.new(3)},condition:function(a){return 3!==this.sG&&k(g[11]),this._.pCN(a,this.pX.slice(-1)),this.new(3)},conditionAll:function(a){return 3!==this.sG&&k(g[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(g[13]),d=this.lpX,e=d._.aD(this._,!0),c=this._.tfms[d.ID],b=this._.cnds[d.ID],(c||a)&&(c=h.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(g[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(g[15]),this._.aPI(a),this},stopPolling:function(){return 3!==this.sG&&k(g[16]),this._.rPI(),this},updateDepsOnEvent:function(a,b){return 3!==this.sG&&k(g[17]),this._.rEVE(a,b),this},removeEvent:function(a,b){return 3!==this.sG&&k(g[18]),this._.urEVE(a,b),this},throttle:function(a){return 1!==this.sG&&3!==this.sG&&k(g[19]),a&&h.iN(a)?this._.thR=a:!1===a&&delete this._.thR,this},setOption:function(a,b){var c,d,e,f,g;for(c={},c[""+a]=b,e=c,A(this._,e),g=this.pX,c=0,d=g.length;c<d;c++)f=g[c],A(f._,e);return this}},n.update=n.set,n.twoWay=n.bothWays,n.pipe=n.chainTo,g=Object.keys(n),z=0;for(R=g.length;z<R;z++)I=g[z],q.prototype[I]=n[I];return E=function(a,b,c){return null==c&&(c={}),b=l(b,c,!0),0!==b.sG&&(a._.aD(b._,a),H(a._,b._)),b},H=function(a,b){if(a.options.updateOnBind||"Func"===a.type)return a.uD(b)},D=function(a,b,c){var d,e,f,g;for(y(this,c),this.isMulti=!0,this.type=b,this.bindings=d=[],e=0,f=a.length;e<f;e++)g=a[e],d.push(this.createBP(g,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})}}})},S=D.prototype=Object.create(C),Object.keys(v.prototype).forEach(function(a){return S[a]=function(b,c){var d,e,f,g;for(g=this.bindings,e=0,f=g.length;e<f;e++)d=g[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(h.iD(m[a])&&(m[a]=b,"placeholder"===a))return L()},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&&(h.iO(a)||h.iF(a))&&(e=a._sb_map,a._sb_ID&&r[a._sb_ID].rAD(b),e))for(d in e)c=e[d],r[c].rAD(b)},module.exports=l}();