@danielkalen/simplybind
Version:
Magically simple, framework-less one-way/two-way data binding for frontend/backend in ~5kb.
2 lines (1 loc) • 13.5 kB
JavaScript
!function(){var u,r,B,v,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,aa,l,ba,w,n,x,g,ca,da,t,ea,C,D,y,fa,p,E,ga,ha,ia,F,G,H,I,J,q,m,z,ja,A;return aa={updateOnBind:!1},da=0,w="push pop shift unshift splice reverse sort".split(" "),ea={},n={},H=["{{","}}"],q=Object.create({silent:!1,trackArrayChildren:!1},{placeholder:{get:function(){return H},set:function(a){g.iA(a)&&2===a.length&&(H=a,J())}}}),t={simpleSelector:!1,promiseTransforms:!1,dispatchEvents:!1,updateEvenIfSame:!1,updateOnBind:!0},ia=function(){},fa=function(){return""+ ++da},p=function(){return Object.create(null)},E=function(a,b){return function(c,d,e){return l(c,d,e,a,b)}},m=function(a,b){return-1!==a.indexOf(b)},g={iD:function(a){return void 0!==a},iA:function(a){return a instanceof Array},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 r},isSimpleObject:function(a){return g.iF(a)||g.iA(a)}},ca=function(a){var b,c;b=p();for(c in a)b[c]=a[c];return b},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={get:function(a,b,c,d){return b?n[a._sb_ID]:a._sb_map&&a._sb_map[c]?n[a._sb_map[c]]:void 0},set:function(a,b){var c,d;b?Object.defineProperty(a.object,"_sb_ID",{configurable:!0,value:a.ID}):(d=a.se,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})))}},D=/[.*+?^${}()|[\]\\]/g,F=G=null,J=function(){var a,b,c;c=q.placeholder[0].replace(D,"\\$&"),a=q.placeholder[1].replace(D,"\\$&"),b="[^"+a+"]+",F=new RegExp(c+"("+b+")"+a,"g"),G=new RegExp(""+c+b+a,"g")},J(),ba=function(a,b,c){var d,e,f,h,k;for(k="",e=f=0,h=a.length;f<h;e=++f)d=a[e],k+=d,c[e]&&(k+=b[c[e]]);return k},z=function(a){throw Error("SimplyBind: "+(C[a]||a))},A=function(a,b){var c,d;q.silent||(c=ga(b),d=C[a],console.warn("SimplyBind: "+(d+("\n\n"+c))))},ja=function(a){z("Invalid argument/s ("+a+")",!0)},ga=function(a){return(Error().stack||"").split("\n").slice(a+3).join("\n")},C={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()",emptyList:"Empty collection provided"},l=function(a,b,c,d,e){return(a||0===a)&&(g.iS(a)||g.iN(a)||g.iF(a)||a instanceof Array)||g.iBI(a)||z("erIP"),!g.iO(a)||a instanceof Array?new r(null,0,a,b,d,e,c):d?a.aP(d):a.sC(1)},l.version="1.11.0",l.settings=q,l.defaultOptions=t,l.unBindAll=function(a,b){var c,d,e;if(a&&(g.iO(a)||g.iF(a))&&(e=a._sb_map,a._sb_ID&&n[a._sb_ID].rAS(b),e))for(d in e)c=e[d],n[c].rAS(b)},u=function(a,b,c){return y(this,c),this.oD=this.so?this.options:t,this.type=b,this.object=a,this.ID=fa(),this.subs=[],this.sM=p(),this.pM=p(),this.atEV=[],"Array"!==this.type&&"Proxy"!==this.type||(this.sV=ia),"Event"===this.type||"Func"===this.type&&this.parentInterface||("Pholder"===this.type?(b=this.pr,a=this.pB=l(b).of(a)._,a.sPH(),this.value=a.pVL[this.Ph]):(this.value=a=this.fDV(),"ObjectProp"!==this.type||g.iD(a)||(this.object[this.pr]=a),this.cL())),this.aEV(),this.object instanceof Array&&"Array"!==this.type&&(this.aBI=a=x.get(this.object,!0))&&a.tC&&!m(a.tC,this.pr)&&(a.tC.push(this.pr),l(this.pr).of(this.object).to(a.uSL)),n[this.ID]=this},u.prototype={cL:function(a){var b,c,d,e,f;switch(b=this,this.type){case"ObjectProp":e=Object.getOwnPropertyDescriptor(this.object,this.pr)||ea,(a=a||e.configurable)&&(this.isL=!0,Object.defineProperty(this.object,this.pr,{configurable:!0,enumerable:e.enumerable,get:function(){return b.value},set:e.set?function(a){e.set(a),b.sV(a)}:function(a){b.sV(a)}}));break;case"Array":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.uAS(b),c}})}),q.trackArrayChildren&&!this.tC&&(this.tC=[],this.uSL=function(){return b.uAS(b)},this.value.forEach(function(a,c){return b.tC.push(""+c),l(c,aa).of(b.value).to(b.uSL)}));break;case"Proxy":d=this.oR=this.value,c=this.object,this.value={result:null,args:null},g.iF(d)&&(f=[].slice,this.object[this.pr]=function(){var a;return a=f.call(arguments),b.value.args=a=b.tfS?b.tfS(a):a,b.value.result=a=d.apply(c,a),b.uAS(b),a})}},aS:function(a,b,c){var d,e,f,h;if(a.isMulti)for(f=a.bindings,a=0,e=f.length;a<e;a++)h=f[a],this.aS(h,b,c);else this.sM[a.ID]?d=!0:(a.pM[this.ID]=this,this.subs.unshift(a),e=this.sM[a.ID]=p(),e.uO=c,e.opts=ca(b),"Event"!==this.type&&"Proxy"!==this.type||(e.opts.updateEvenIfSame=!0),e.VR="Func"===a.type?"ps":"value");return d},rS:function(a,b){var c,d,e,f;if(a.isMulti)for(e=a.bindings,c=0,d=e.length;c<d;c++)f=e[c],this.rS(f,b);else this.sM[a.ID]&&(this.subs.splice(this.subs.indexOf(a),1),delete this.sM[a.ID],delete a.pM[this.ID]),b&&(a.rS(this),delete this.pM[a.ID]);0===this.subs.length&&0===Object.keys(this.pM).length&&this.DES()},rAS:function(a){var b,c,d,e;for(d=this.subs.slice(),b=0,c=d.length;b<c;b++)e=d[b],this.rS(e,a)},DES:function(){var a,b,c,d;if(delete n[this.ID],this.rPI(),"Event"===this.type){for(d=this.atEV,b=0,c=d.length;b<c;b++)a=d[b],this.urEVE(a,this.cEM.listen);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&&("ObjectProp"===this.type?Object.defineProperty(this.object,this.pr,{value:this.value,writable:!0}):"Proxy"===this.type&&(this.object[this.pr]=this.oR),delete this.object._sb_map),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.pr]}},sV:function(a,b,c){var d;switch(b||(b=this),this.tfS&&(a=this.tfS(a)),this.type){case"ObjectProp":this.isL||a===this.value||(this.object[this.pr]=a);break;case"Pholder":d=this.pB,d.pVL[this.Ph]=a,c=ba(d.pCT,d.pVL,d.pIM),d.sV(c,b,!0);break;case"Func":c=this.ps,"Array"===b.type&&a===b.value&&(a=a.slice()),this.ps=a,a=this.object(a,c);break;case"Event":c||(this.iE=!0,this.eE(a),this.iE=!1)}this.value=a,this.uAS(b)},uAS:function(a){var b,c;if(this.subs.length){if(this.thR){if(b=+new Date,c=b-this.thLU,c<this.thR)return clearTimeout(this.thT),this.thT=setTimeout(function(b){return function(){return b.uAS(a)}}(this),this.thR-c);this.thLU=b}for(c=(b=this.subs).length;c--;)this.uS(b[c],a)}},uS:function(a,b){var c,d,e,f;b===a||b!==this&&b.sM[a.ID]||(c=this.sM[a.ID],d=this.value,e=a[c.VR],d=(f=c.tF)?f(d,e,a.object):d,d===e&&!c.opts.updateEvenIfSame||c.cN&&!c.cN(d,e,a.object)||(c.opts.promiseTransforms&&d&&g.iF(d.then)?d.then(function(c){a.sV(c,b)}):a.sV(d,b),c.uO&&this.rS(a)))},aM:function(a,b,c,d){var e,f,h,k;if(g.iF(c)){for(e=0,f=b.length;e<f;e++)h=b[e],k=h._||h,k.isMulti?this.aM(a,k.bindings,c,d):(h=this.sM[k.ID],h[a]=c,d=d&&!h.uO,this.pM[k.ID]&&(k.sM[this.ID][a]=c),!d&&"Func"!==this.type||"tF"!==a||this.uS(k,this));return!0}return A("erFN",2)},ss:function(a,b){this.tfS=a,b&&this.sV(this.value)},sPH:function(){var a;this.pVL||(this.pVL=p(),this.pIM=p(),this.pCT=[],g.iS(this.value)&&(this.pCT=this.value.split(G),a=0,this.value=this.value.replace(F,function(b){return function(c,d){return b.pIM[a++]=d,b.pVL[d]=d}}(this))))},aPI:function(a){if("Event"!==this.type)return this.rPI(),this.PI=setInterval(function(a){return function(){var b;return b=a.fDV(),a.sV(b)}}(this),a)},rPI:function(){return clearInterval(this.PI),this.PI=null},aEV:function(){this.evN&&this.rEVE(this.evN,this.cEM.listen)},rEVE:function(a,b){m(this.atEV,a)||(this.atEV.push(a),this.iEM(a,b||"on","on"))},urEVE:function(a,b){var c;c=this.atEV.indexOf(a),-1!==c&&(this.atEV.splice(c,1),this.iEM(a,b||"removeListener","removeListener"))},iEM:function(a,b,c){var d;d=this.object,d[b]||(b=c),this.evH||(this.evH=ha.bind(this)),"function"==typeof d[b]&&d[b](a,this.evH)},eE:function(a){var b,c;c=this.object,b=this.cEM.emit||"emit",c[b]||(b="emit"),c[b](this.evN,a)}},ha=function(){var a;this.iE||(a="Event"===this.type?arguments[this.pr]:this.fDV(),this.sV(a,null,!0))},r=function(a,b,c,d,e,f,h){var k;if(a&&y(this,a),this.sG=b||0,null==this.subs&&(this.subs=[]),h&&(this.so=h),f&&(this.iSi=f),e&&(this.parentInterface=e),0===this.sG){this.oP=d||(d={}),this.options={};for(k in t)this.options[k]=null!=d[k]?d[k]:t[k];if(g.isSimpleObject(c))return this.sG=1,this.sS(c,!0);g.iN(c)&&(c=c.toString()),this.se=this.pr=c,this.options.simpleSelector||(m(c,":")&&(a=c.split(":"),this.de=a[0],this.pr=a[1]),m(c,".")&&(a=this.pr.split("."),this.pr=a[0],this.Ph=a.slice(1).join(".")),"event"===this.de&&(m(c,"#")?(a=this.pr.split("#"),this.evN=a[0],this.pr=a[1]):(this.evN=this.pr,this.pr=0),isNaN(parseInt(this.pr))&&A("erEV",1),this.cEM={listen:this.oP.listenMethod,emit:this.oP.emitMethod}))}return this},B={sC:function(a){return new r(this,a)},dM:function(a){return this._=a,Object.defineProperties(this,{value:{get:function(){return a.value}},original:{get:function(){return a.objects||a.object}},subscribers:{get:function(){return a.subs.slice().map(function(a){return a.object})}}})},createBP:function(a,b,c,d){var e;return this.object=a,(e=x.get(a,d,this.se,this.mC))?this.patchCachedBP(e):(a=new u(a,b,c),x.set(a,d),a)},patchCachedBP:function(a){var b,c,d,e;if("ObjectProp"!==a.type||this.pr in this.object||a.cL(!0),this.so)for(c in d=this.oP)e=d[c],a.oD[c]=e;c=a.oD;for(b in c)e=c[b],this.options[b]=g.iD(this.oP[b])?this.oP[b]:e;return a},sS:function(a,b){var c;switch(!1){case!b:c=g.iF(a)?"Func":"Array";break;case!this.Ph:c="Pholder";break;case"event"!==this.de:c="Event";break;case"func"!==this.de:c="Proxy";break;default:c="ObjectProp"}return"multi"===this.de?(a.length||z("emptyList"),this.dM(new v(this,a,c))):this.dM(this.createBP(a,c,this,b)),m(this._.type,"Event")||m(this._.type,"Proxy")?this.options.updateOnBind=!1:m(this._.type,"Func")&&(this.options.updateOnBind=!0),this.parentInterface?this.aP(this.parentInterface):this},aP:function(a){var b,c,d,e;if(this.iSi)a._.addBP(this._);else if(a.sG=2,a.subs.push(this),b=a._.aS(this._,a.options,a.uO),a.uO)delete a.uO;else if(a.options.updateOnBind&&!b)if(this._.isMulti)for(e=this._.bindings,c=0,d=e.length;c<d;c++)b=e[c],a._.uS(b,a._);else a._.uS(this._,a._);return a}},r.prototype=Object.create(B,{of:{get:function(){if(!this.sG)return O}},chainTo:{get:function(){if(this.sG)return L}},set:{get:function(){if(this.sG)return R}},transformSelf:{get:function(){if(1===this.sG)return X}},transform:{get:function(){if(2===this.sG)return V}},transformAll:{get:function(){if(2===this.sG)return W}},condition:{get:function(){if(2===this.sG)return M}},conditionAll:{get:function(){if(2===this.sG)return N}},bothWays:{get:function(){if(2===this.sG)return K}},unBind:{get:function(){if(2===this.sG)return Y}},pollEvery:{get:function(){if(this.sG)return P}},stopPolling:{get:function(){if(this.sG)return T}},updateSubsOnEvent:{get:function(){if(2===this.sG)return Z}},removeEvent:{get:function(){if(2===this.sG)return Q}},throttle:{get:function(){if(this.sG)return U}},setOption:{get:function(){if(2===this.sG)return S}},to:{get:function(){if(1===this.sG)return E(this)}},and:{get:function(){var a,b;return b=this.sC(1),2===this.sG?b:1===this.sG?(b._.isMulti||(a=b._,b._=new v(b),b._.addBP(a)),E(b,!0)):void 0}},once:{get:function(){var a;if(1===this.sG)return a=this.sC(1),a.uO=!0,a}},update:{get:function(){return this.set}},twoWay:{get:function(){return this.bothWays}},pipe:{get:function(){return this.chainTo}}}),O=function(a){return g.iO(a)||g.iF(a)||ja(a),g.iBI(a)&&(a=a.object),this.sG=1,this.sS(a)},L=function(a,b,c){return l(this.subs[this.subs.length-1]).to(a,b,c)},R=function(a){return this._.sV(a),this},X=function(a){return g.iF(a)?this._.ss(a,this.options.updateOnBind):A("erFN",1),this},V=function(a){return this._.aM("tF",this.subs.slice(-1),a,this.options.updateOnBind),this},W=function(a){return this._.aM("tF",this.subs,a,this.options.updateOnBind),this},M=function(a){return this._.aM("cN",this.subs.slice(-1),a),this},N=function(a){return this._.aM("cN",this.subs,a),this},K=function(a){var b,c,d,e,f,h;for(d=this.subs[this.subs.length-1],h=d._,c=this._.isMulti?this._.bindings:[this._],h.aS(this._,d.options),d=0,e=c.length;d<e;d++)b=c[d],f=b.sM[h.ID].tF,b=b.sM[h.ID].cN,(f||a)&&(f=g.iF(a)?a:f)&&!1!==a&&(h.sM[this._.ID].tF=f),b&&(h.sM[this._.ID].cN=b);return this},Y=function(a){var b,c,d,e;for(d=this.subs,b=0,c=d.length;b<c;b++)e=d[b],this._.rS(e._,a);return this},P=function(a){return this._.aPI(a),this},T=function(){return this._.rPI(),this},Z=function(a,b){return this._.rEVE(a,b),this},Q=function(a,b){return this._.urEVE(a,b),this},U=function(a){return a&&g.iN(a)?this._.thR=a:!1===a&&delete this._.thR,this},S=function(a,b){return this._.sM[this.subs[this.subs.length-1]._.ID].opts[a]=b,this},v=function(a,b,c){var d,e,f;if(y(this,this.In=a),this.isMulti=!0,this.bindings=d=[],b)for(a=0,e=b.length;a<e;a++)f=b[a],this.addBP(f,c);return Object.defineProperties(this,{type:{get:function(){return d.map(function(a){return a.type})}},value:{get:function(){return d.map(function(a){return a.value})}},thR:{set:function(a){return d.forEach(function(b){return b.thR=a})}}})},I=v.prototype=Object.create(B),Object.keys(u.prototype).forEach(function(a){return I[a]=function(b,c,d,e){var f,h,g,l;for(l=this.bindings,h=0,g=l.length;h<g;h++)f=l[h],"uS"===a&&(c=f),f[a](b,c,d,e)}}),I.addBP=function(a,b){this.bindings.push(b?this.createBP(a,b,this.In):a)},module.exports=l}();