@danielkalen/simplybind
Version:
Magically simple, framework-less one-way/two-way data binding for frontend/backend in ~5kb.
2 lines (1 loc) • 14.2 kB
JavaScript
!function(){var t,p,A,u,J,K,L,M,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,aa,k,ba,q,v,n,w,g,ca,B,C,x,da,m,ea,D,fa,l,ga,E,F,G,y,H,z,I,r;return n={},l={silent:!1,liveProps:!0,dispatchEvents:!1,updateEvenIfSame:!1,updateOnBind:!0,mutateInherited:!1,trackArrayChildren:!1,simpleSelector:!1,promiseTransforms:!1,placeholder:["{{","}}"]},v="push pop shift unshift splice reverse sort".split(" "),ca={},da=function(){return"sb_"+Math.floor(1e12*(1+Math.random())).toString(16)},m=function(){return Object.create(null)},D=function(a,b){return function(c,d){return k(c,d,a,b)}},ea=function(a,b){return function(c,d,e){return k(0,null,a,b).ofEvent(c,e,d)}},q=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 p},isSimpleObject:function(a){return g.iF(a)||g.iA(a)}},y=function(a,b){var c,d;for(c in b)d=b[c],g.iD(l[c])&&(a.options[c]=d);a.mPL()},x=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]},w={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.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})))}},C=/[.*+?^${}()|[\]\\]/g,E=F=null,H=function(){var a,b,c;c=l.placeholder[0].replace(C,"\\$&"),a=l.placeholder[1].replace(C,"\\$&"),b="[^"+a+"]+",E=new RegExp(c+"("+b+")"+a,"g"),F=new RegExp(""+c+b+a,"g")},H(),ba=function(a,b,c){var d,e,f,h,g;for(g="",e=f=0,h=a.length;f<h;e=++f)d=a[e],g+=d,c[e]&&(g+=b[c[e]]);return g},z=function(a){throw Error("SimplyBind: "+(B[a]||a))},r=function(a,b){var c,d;l.silent||(c=fa(b),d=B[a],console.warn("SimplyBind: "+(d+("\n\n"+c))))},I=function(a){z("Invalid argument/s ("+a+")",!0)},fa=function(a){return(Error().stack||"").split("\n").slice(a+3).join("\n")},B={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"},k=function(a,b,c,d){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 p(null,0,a,b,c,d):a.sC(1)},Object.defineProperties(k,{version:{value:"1.7.1-multiBPs"},options:{get:function(){var a,b,c;a={};for(b in l)c=l[b],a[b]=c;return a}}}),k.setOption=function(a,b){g.iD(l[a])&&(l[a]=b,"placeholder"===a&&H())},k.setOptions=function(a){var b,c;for(b in a)c=a[b],k.setOption(b,c)},k.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)},t=function(a,b,c){return x(this,c),this.type=b,this.object=a,this.ID=da(),this.subs=[],this.sO=m(),this.sM=m(),this.pM=m(),this.sP=m(),this.mPH=m(),this.tfms=m(),this.cnds=m(),this.atEV=[],"Event"!==this.type&&"Func"!==this.type||(this.options.updateOnBind=!1,this.options.updateEvenIfSame=!0),"Event"===this.type||"Func"===this.type&&this.parentInterface||(this.value=this.VLo=a=this.fDV(),"ObjectProp"!==this.type||g.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=w.get(this.object,!0))&&a.options.trackArrayChildren&&!q(a.tC,this.prop)&&(a.tC.push(this.prop),k(this.prop).of(this.object).to(a.uSL)),n[this.ID]=this},t.prototype={mPL:function(a){var b,c,d;this.options.liveProps&&(b=this,"ObjectProp"===this.type?(d=Object.getOwnPropertyDescriptor(this.object,this.prop)||ca,(a=a||!this.isL&&(d.configurable||this.options.mutateInherited))&&(this.isL=!0,Object.defineProperty(this.object,this.prop,{configurable:!0,enumerable:d.enumerable,get:function(){return b.value},set:d.set?function(a){d.set(a),b.sV(a)}:function(a){b.sV(a)}}))):"Array"===this.type&&(this.isL||(this.isL=!0,v.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}})})),this.options.trackArrayChildren&&!this.tC&&(this.tC=[],this.uSL=function(){return b.uAS(b)},c={updateOnBind:!1},this.value.forEach(function(a,d){return b.tC.push(""+d),k(d,c).of(b.value).to(b.uSL)}))))},aS: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.aS(f);else this.sM[a.ID]||(this.subs.unshift(a),this.sM[a.ID]=a,b&&(this.sO[a.ID]=a),a.pM[this.ID]=this),this.placeholder?this.mPH[a.ID]=this.placeholder:this.mPH[a.ID]&&delete this.mPH[a.ID],a.placeholder&&(this.sP[a.ID]=a.placeholder);return this},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.sP[a.ID],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.remove);delete this.object._sb_map}else if("Array"===this.type)for(delete this.object._sb_ID,a=0,b=v.length;a<b;a++)c=v[a],delete this.object[c];else"Func"!==this.type&&("ObjectProp"===this.type&&Object.defineProperty(this.object,this.prop,{value:this.value,writable:!0}),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.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=ba(this.pCT,this.pVL,this.pIM)),this.type){case"ObjectProp":this.isL||(this.object[this.prop]=a);break;case"Func":e=this.VLp,"Array"===c.type&&a===c.value&&(a=a.slice()),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.uAS(c)}},uAS:function(a){var b,c,d;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--;)d=b[c],this.uS(d,a),this.sO[d.ID]&&this.rS(d,a)}},uS:function(a,b){var c,d,e,f;b===a||b!==this&&b.sM[a.ID]||(c=this.mPH[a.ID],e=this.sP[a.ID],c=c?this.pVL[c]:this.value,f=e?a.pVL[e]:a.value,d=this.hasTransforms?this.apT(a,e,c,f):c,this.hCs&&!this.cC(a,e,c,f))||(this.options.promiseTransforms&&d&&g.iF(d.then)?d.then(function(c){a.sV(c,e,b)}):a.sV(d,e,b))},pTF:function(a,b){var c,d,e;if(g.iF(a)){for(c=0,d=b.length;c<d;c++)e=b[c],e=e._||e,e.isMulti?this.pTF(a,e.bindings):(this.aTF(e.ID,a),this.pM[e.ID]&&e.aTF(this.ID,a),(this.options.updateOnBind||"Func"===this.type)&&this.uS(e,this));return!0}return r("erFN",2)},apT:function(a,b,c,d){return this.tfms[a.ID]?this.tfms[a.ID](c,d):c},aTF:function(a,b){this.hasTransforms=!0,this.tfms[a]=b},ss:function(a,b){"Array"!==this.type&&(this.tfS=a,b&&this.sV(this.value))},pCN:function(a,b){var c,d,e;if(g.iF(a)){for(c=0,d=b.length;c<d;c++)e=b[c],e=e._||e,e.isMulti?this.pCN(a,e.bindings):(this.aCND(e.ID,a),this.pM[e.ID]&&e.aCND(this.ID,a));return!0}return r("erFN",2)},cC:function(a,b,c,d){return!this.cnds[a.ID]||this.cnds[a.ID](c,d)},aCND:function(a,b){this.hCs=!0,this.cnds[a]=b},sPH:function(){var a;this.pVL=m(),this.pIM=m(),this.pCT=[],g.iS(this.VLo)&&(this.pCT=this.VLo.split(F),a=0,this.value=this.VLo.replace(E,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.in)},rEVE:function(a,b){q(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=ga.bind(this)),"function"==typeof d[b]&&d[b](a,this.evH)},eE:function(a){var b,c;c=this.object,b=this.cEM.out||"emit",c[b]||(b="emit"),c[b](this.evN,a)}},ga=function(){var a;this.iE||(a="Event"===this.type?arguments[this.prop]:this.fDV(),this.sV(a,null,null,!0))},p=function(a,b,c,d,e,f){var h;if(a&&x(this,a),this.sG=b||0,null==this.subs&&(this.subs=[]),f&&(this.iSi=f),e&&(this.parentInterface=e),0===this.sG){this.opts=d||(d={}),this.options={};for(h in l)this.options[h]=null!=d[h]?d[h]:l[h];if(g.isSimpleObject(c))return this.sG=1,this.sS(c,!0);g.iN(c)&&(c=c.toString()),this.selc=this.prop=c,this.options.simpleSelector||(q(this.selc,":")&&(a=this.prop.split(":"),this.desc=a[0],this.prop=a[1]),q(this.selc,".")&&(a=this.prop.split("."),this.prop=a[0],this.placeholder=a.slice(1).join(".")),this.selc=this.prop)}return this},A={sC:function(a){return new p(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=w.get(a,d,this.selc,this.mC))?this.patchCachedBP(e):(a=new t(a,b,c),w.set(a,d),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),y(a,this.opts),this.options=a.options,a},sS:function(a,b){var c,d,e,f,h;switch(!1){case!b:c=g.iF(a)?"Func":"Array";break;case!this.evN:c="Event";break;default:c="ObjectProp"}if("multi"===this.desc?(a.length||z("emptyList"),this.dM(new u(this,a,c))):this.dM(this.createBP(a,c,this,b)),f=this.parentInterface){if(this.iSi)f._.addBP(this._);else if(f.sG=2,f.subs.push(this),f._.aS(this._,f.uO),f.uO)delete f.uO;else if(f.options.updateOnBind||"Func"===f._.type)if(this._.isMulti)for(h=this._.bindings,d=0,e=h.length;d<e;d++)c=h[d],f._.uS(c,f._);else f._.uS(this._,f._);return f}return this}},p.prototype=Object.create(A,{of:{get:function(){if(!this.sG)return O}},ofEvent:{get:function(){if(!this.sG)return P}},chainTo:{get:function(){if(this.sG)return K}},set:{get:function(){if(this.sG)return S}},get:{get:function(){if(this.sG)return N}},transformSelf:{get:function(){if(1===this.sG)return Y}},transform:{get:function(){if(2===this.sG)return W}},transformAll:{get:function(){if(2===this.sG)return X}},condition:{get:function(){if(2===this.sG)return L}},conditionAll:{get:function(){if(2===this.sG)return M}},bothWays:{get:function(){if(2===this.sG)return J}},unBind:{get:function(){if(2===this.sG)return Z}},pollEvery:{get:function(){if(2===this.sG)return Q}},stopPolling:{get:function(){if(2===this.sG)return U}},updateSubsOnEvent:{get:function(){if(2===this.sG)return aa}},removeEvent:{get:function(){if(2===this.sG)return R}},throttle:{get:function(){if(this.sG)return V}},setOption:{get:function(){return T}},to:{get:function(){if(1===this.sG)return D(this)}},toEvent:{get:function(){if(1===this.sG)return ea(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 u(b),b._.addBP(a)),D(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)||I(a),g.iBI(a)&&(a=a.object),this.sG=1,this.sS(a)},P=function(a,b,c){return a&&g.iS(a)?isNaN(parseInt(this.prop))&&r("erEV",1):I(a),this.evN=a,this.selc=this.prop+"#"+this.evN,this.cEM={in:b,out:c},this},K=function(a,b){return k(this.subs[this.subs.length-1]).to(a,b)},S=function(a){return this._.sV(a,this.placeholder),this},N=function(){return!this.placeholder||this._.isMulti?this._.value:this._.pVL[this.placeholder]},Y=function(a){return g.iF(a)?this._.ss(a,this.options.updateOnBind):r("erFN",1),this},W=function(a){return this._.pTF(a,this.subs.slice(-1)),this},X=function(a){return this._.pTF(a,this.subs),this},L=function(a){return this._.pCN(a,this.subs.slice(-1)),this},M=function(a){return this._.pCN(a,this.subs),this},J=function(a){var b,c,d,e,f,h,k;for(k=this.subs[this.subs.length-1]._.aS(this._),c=this._.isMulti?this._.bindings:[this._],d=0,e=c.length;d<e;d++)b=c[d],h=b.tfms[k.ID],f=b.cnds[k.ID],(h||a)&&(h=g.iF(a)?a:h)&&!1!==a&&k.aTF(b.ID,h),f&&k.aCND(b.ID,f);return this},Z=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},Q=function(a){return this._.aPI(a),this},U=function(){return this._.rPI(),this},aa=function(a,b){return this._.rEVE(a,b),this},R=function(a,b){return this._.urEVE(a,b),this},V=function(a){return a&&g.iN(a)?this._.thR=a:!1===a&&delete this._.thR,this},T=function(a,b){var c,d,e,f,h;for(c={},c[""+a]=b,e=c,y(this._,e),f=this.subs,c=0,d=f.length;c<d;c++)h=f[c],y(h._,e);return this},u=function(a,b,c){var d,e,f;if(x(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,{value:{get:function(){return d.map(function(a){return a.placeholder?a.pVL[a.placeholder]: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})}},uS:{value:function(a){return d.forEach(function(b){b.uS(a,b)})}}})},G=u.prototype=Object.create(A),Object.keys(t.prototype).forEach(function(a){return G[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)}}),G.addBP=function(a,b){this.bindings.push(b?this.createBP(a,b,this.In):a)},module.exports=k}();