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