@danielkalen/simplybind
Version:
Magically simple, framework-less one-way/two-way data binding for frontend/backend in ~5kb.
2 lines (1 loc) • 13.9 kB
JavaScript
!function(){var t,p,m,k,L,u,q,v,z,A,M,B,C,D,N,n,E,l,O,w,P,F,G,f,H,I,x,J,y,K,g,r;u="push pop shift unshift splice reverse sort".split(" "),N=function(){return"sb_"+Math.floor(1e12*(1+Math.random())).toString(16)},n=function(){return Object.create(null)},z={isDefined:function(a){return"undefined"!=typeof a},isI:function(a){return"object"==typeof a&&"number"==typeof a.length},isBP:function(a){return a instanceof t},bi:function(a){return a instanceof p}},x=function(a,b,c){var d,e;for(d in b)e=b[d],null!=l[d]&&(a.options[d]=e);if(c)return a.mPL()},D=function(a,b){var c,d,e,h,f;for(f=Object.keys(b),h=[],c=0,e=f.length;c<e;c++)d=f[c],h.push(a[d]=b[d]);return h},v={txN:{},object:{set:function(a){var b,c;return c=a.selc||a.prop,a.object._sb_map?a.object._sb_map[c]=a.ID:(b={},b[c]=a.ID,Object.defineProperty(a.object,"_sb_map",{configurable:!0,value:b}))},get:function(a,b,c){if(a._sb_map&&a._sb_map[b])return q[a._sb_map[b]]}},sOJ:{set:function(a){return Object.defineProperty(a.object,"_sb_ID",{configurable:!0,value:a.ID})},get:function(a){return q[a._sb_ID]}}},C=/[.*+?^${}()|[\]\\]/g,I=H=null,J=function(){var a,b,c;return c=l.placeholderStart.replace(C,"\\$&"),a=l.placeholderEnd.replace(C,"\\$&"),b="[^"+a+"]+",H=new RegExp(c+"("+b+")"+a,"g"),I=new RegExp(""+c+b+a,"g")},L=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},y=function(a,b){var c;throw c=E(),Error("SimplyBind: "+((b?a:B[a])+("\n\nCall Source:\n"+c)))},r=function(a){var b;if(!l.silent)return b=E(),a=B[a],console.log("SimplyBind: "+(a+("\n\nCall Source:\n"+b)))},g=function(a){return y("You can't use/invoke ."+a+"() at this stage",!0)},K=function(a,b){return y("Invalid argument/s ("+b+") passed to ."+a+"()",!0)},E=function(){return Error().stack.split("\n").slice(3,10).map(function(a){return a.replace(M,function(a,c,d){return"@ "+d})}).join("\n")},M=/[@(?:\wat\s)](.*)\/(.+)$/,Error.prototype.stack="\n\n\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()"},q={},l={silent:!1,liveProps:!0,dispatchEvents:!1,updateEvenIfUndefined:!1,updateEvenIfSame:!1,updateOnBind:!0,mutateInherited:!1,trackArrayChildren:!1,simpleSelector:!1,promiseTransforms:!1,placeholderStart:"{{",placeholderEnd:"}}"},J(),k=function(a,b,c){return(a||0===a)&&("string"==typeof a||"number"==typeof a||"function"==typeof a||a instanceof Array)||z.bi(a)||y("erIP"),"object"!=typeof a||a instanceof Array?new p(null,0,null,a,c,b):new p(a._,1)},t=function(a,b,c){return D(this,b),this.type=a,this.ID=N(),this.value=this.VLo=null,this.deps=[],this.depsMap={1:n(),2:n()},this.dPH=n(),this.mPH=n(),this.tfms=n(),this.cnds=n(),this.atEV=[],"Event"!==this.type&&(a=this.fDV(null,c),"undefined"==typeof a&&"ObjectProp"===this.type&&(this.object[this.prop]=a=null),this.value=this.VLo=a,this.placeholder&&!this.pVL&&this.sPH()),this.mPL(),this.aEV(),this.object instanceof Array&&"Array"!==this.type&&(this.aBI=a=v.sOJ.get(this.object))&&a.options.trackArrayChildren&&-1===a.tC.indexOf(this.prop)&&(a.tC.push(this.prop),k(this.prop).of(this.object).to(a.uS)),q[this.ID]=this},p=function(a,b,c,d,e,h){var f;switch(c&&D(this,c),this.stage=b||0,null==this.pX&&(this.pX=[]),null==this.sT&&(this.sT={}),this.stage){case 0:this.opts=h||(h={}),this.options={};for(f in l)this.options[f]=null!=h[f]?h[f]:l[f];"function"==typeof d?(a=this.sOB(d,"Func",!0,e),this.stage=1):d instanceof Array?(a=this.sOB(d,"Array",!0),this.stage=1):("number"==typeof d&&(d=d.toString()),this.selc=this.prop=d,this.options.simpleSelector||(-1!==this.selc.indexOf(":")&&(d=this.prop.split(":"),this.desc=d[0],this.prop=d[1]),-1!==this.selc.indexOf(".")&&(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:b="Event";break a;default:b="ObjectProp"}a=this.sOB(d,b)}}return Object.defineProperties(this,{_:{value:a},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]}},new:{value:function(b,c){return new p(a,b,this,c,e)}}}),this},t.prototype={mPL:function(a){var b,c,d,e,h,f;if(this.options.liveProps)if(b=this,"ObjectProp"===this.type){if(d=Object.getOwnPropertyDescriptor(this.object,this.prop)||{},(f=this.options.mutateInherited||d&&d.configurable)&&!this.live||a)return this.live=!0,null!=(e=d.get)&&e.bind(this.object),null!=(h=d.set)&&h.bind(this.object),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:null!=d?d.enumerable:void 0,get:a,set:e})}else if("Array"===this.type&&(this.live||(this.live=!0,u.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),k(d,c).of(b.value).to(b.uS)})},aD:function(a,b){return this.depsMap[1][a.ID]||(this.depsMap[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.depsMap[2][a.ID]=a:a.depsMap[1][this.ID]&&(a.aD(this,!0),this.aD(a,!0)),this},rD:function(a,b){if(this.depsMap[1][a.ID]&&(this.deps.splice(this.deps.indexOf(a),1),delete this.depsMap[1][a.ID],delete this.dPH[a.ID]),b)return a.rD(this),delete this.depsMap[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.depsMap[2]).length)return this.DES()},DES:function(){var a,b,c,d;if(delete q[this.ID],"ObjectProp"===this.type||"DOMText"===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=u.length;a<b;a++)c=u[a],delete this.object[c];else"Func"===this.type&&delete this.object._sb_ID},fDV:function(a,b){switch(null==a&&(a=this.type),!1){case"Func"!==a:if(!b)return this.object();break;case"Array"!==a:return this.object;default:return this.object[this.prop]}},sV:function(a,b,c,d){var e,f;if(null==c&&(c=this),e=b?this.pVL[b]:this.value,this.tfS&&(a=this.tfS(a)),e=a!==e||this.options.updateEvenIfSame,f="undefined"!=typeof a||this.options.updateEvenIfUndefined,(e&&f||this.evN)&&"Array"!==this.type){switch(b?(this.pVL[b]=a,this.value=a=L(this.pCT,this.pVL,this.pIM)):this.value=a,this.type){case"ObjectProp":this.live||(this.object[this.prop]=a);break;case"Func":b=this.VLp,this.VLp=a,this.value=this.object(a,b);break;case"Event":d||(this.evC=c,this.eE(a),this.evC=null)}d&&this.evC||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;if(c=null===this.value&&"Event"===this.type,!(b&&(b===a||b!==this&&b.depsMap[1][a.ID])||c)&&(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=c,f=this.apT(a,d,c,e),this.cC(a,d,c,e)))return this.options.promiseTransforms&&null!=f&&"function"==typeof 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("function"!=typeof a)return r("erFN");for(c=0,d=b.length;c<d;c++)e=b[c],f=e.ID,f+=e.placeholder?"."+e.placeholder:"",this.aTF(f,a),e=e._,this.depsMap[2][e.ID]&&e.aTF(this.ID,a),(this.options.updateOnBind||"Func"===this.type)&&this.uD(e);return!0},apT:function(a,b,c,d){return this.hTfs?(a=a.ID,b&&(a+="."+b),this.tfms[a]?this.tfms[a](c,d):c):c},aTF:function(a,b){return this.hTfs=!0,this.tfms[a]=b},pCN:function(a,b){var c,d,e,f;if("function"!=typeof a)return r("erFN");for(c=0,d=b.length;c<d;c++)e=b[c],f=e.ID,f+=e.placeholder?"."+e.placeholder:"",this.aCND(f,a),e=e._,this.depsMap[2][e.ID]&&e.aCND(this.ID,a);return!0},cC:function(a,b,c,d){return!this.hCnds||(a=a.ID,b&&(a+="."+b),!this.cnds[a]||this.cnds[a](c,d))},aCND:function(a,b){return this.hCnds=!0,this.cnds[a]=b},sPH:function(){var a;if(this.pVL=n(),this.pIM=n(),this.pCT=[],"string"==typeof this.VLo)return this.pCT=this.VLo.split(I),a=0,this.value=this.VLo.replace(H,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(-1===this.atEV.indexOf(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=O.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)}},O=function(){var a;return a="Event"===this.type?arguments[this.prop]:this.fDV(),this.sV(a,null,this.evC,!0)},p.prototype=Object.create({sOB:function(a,b,c,d){var e;return this.object=a,c=c?"sOJ":"object",(e=v[c].get(a,this.selc,this.mC))?((e.placeholder=this.placeholder)&&!e.pVL&&(e.VLo=e.fDV(),e.sPH()),"ObjectProp"!==e.type||this.prop in a||e.mPL(!0),x(e,this.opts,!0),e):(a=new t(b,this,d),v[c].set(a),a)}}),m={of:function(a){var b;return 0!==this.stage&&2!==this.stage&&g(f[0]),"object"!=typeof a&&"function"!=typeof a&&K(f[0],a),z.bi(a)&&(a=a.object),2===this.stage&&(this.pX[this.pX.length-1]=b=this.lpX.of(a),this.sT.hIB=!0,this.sT.hTf=!1,this._.aD(b._),F(this._,b._)),this.new(this.stage+1,a)},ofEvent:function(a,b,c){return 0!==this.stage||this.sT.hEN?g(f[1]):a&&"string"==typeof a?isNaN(parseInt(this.prop))&&r("erEV"):K(f[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.stage||this.sT.hIB)&&g(f[2]),this.pX.push(c=A(this,a,b)),0===c.stage?c=2:(c=3,this.sT.hIB=!0),this.new(c)},and:function(a,b){var c;return 3===this.stage&&this.sT.hIB&&!this.sT.hasMultiTransform||g(f[3]),this.pX.push(c=A(this,a,b)),0===c.stage?c=2:(c=3,this.sT.hTf=!1),this.new(c)},toEvent:function(a,b,c,d){return 1!==this.stage&&g(f[4]),this.pX.push(k(0,d).ofEvent(a,c,b)),this.new(2)},chainTo:function(a,b){return 3!==this.stage&&g(f[5]),k(this.lpX).to(a,b)},set:function(a){return 0!==this.stage&&2!==this.stage||g(f[6]),this._.sV(a,this.placeholder),this},get:function(){return 0!==this.stage&&2!==this.stage||g(f[7]),this.placeholder?this._.pVL[this.placeholder]:this._.value},transformSelf:function(a){return(1!==this.stage||1===this.stage&&"Array"===this._.type)&&g(f[8]),"function"!=typeof a?r("erFN"):(this._.tfS=a,this._.options.updateOnBind&&this.set(this.value)),this},transform:function(a){return(3!==this.stage||this.sT.hTf||this.sT.hasMultiTransform)&&g(f[9]),this.sT.hTf=this._.pTF(a,this.pX.slice(-1))||!1,this.new(3)},transformAll:function(a){return(3!==this.stage||this.sT.hTf||this.sT.hasMultiTransform)&&g(f[10]),this.sT.hasMultiTransform=this._.pTF(a,this.pX)||!1,this.new(3)},condition:function(a){return 3!==this.stage&&g(f[11]),this._.pCN(a,this.pX.slice(-1)),this.new(3)},conditionAll:function(a){return 3!==this.stage&&g(f[12]),this._.pCN(a,this.pX),this.new(3)},bothWays:function(a){var b,c,d,e;return(3!==this.stage||this.sT.hasMultiTransform)&&g(f[13]),e=this.lpX,b=e._.aD(this._,!0),d=this._.tfms[e.ID],c=this._.cnds[e.ID],(d||a)&&(d="function"==typeof a?a:d)&&!1!==a&&b.aTF(this.ID,d),c&&b.aCND(this.ID,c),this._.aD(e._,!0),this},unBind:function(a){var b,c,d,e;for(3!==this.stage&&g(f[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.stage&&"Event"!==this._.type||g(f[15]),this._.aPI(a),this},stopPolling:function(){return 3!==this.stage&&g(f[16]),this._.rPI(),this},updateDepsOnEvent:function(a,b){return 3!==this.stage&&g(f[17]),this._.rEVE(a,b),this},removeEvent:function(a,b){return 3!==this.stage&&g(f[18]),this._.urEVE(a,b),this},throttle:function(a){return 1!==this.stage&&3!==this.stage&&g(f[19]),"number"==typeof a&&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,x(this._,e,!0),g=this.pX,c=0,d=g.length;c<d;c++)f=g[c],x(f._,e,!0);return this}},m.update=m.set,m.twoWay=m.bothWays,m.pipe=m.chainTo,f=Object.keys(m),w=0;for(P=f.length;w<P;w++)G=f[w],p.prototype[G]=m[G];return A=function(a,b,c){return null==c&&(c={}),b=k(b,c,!0),0!==b.stage&&(a._.aD(b._,a),F(a._,b._)),b},F=function(a,b){if(a.options.updateOnBind||"Func"===a.type)return a.uD(b)},Object.defineProperties(k,{version:{value:"1.3.2"},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){if(null!=l[a]&&(l[a]=b,-1!==a.indexOf("place")))return J()},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&&("object"==typeof a||"function"==typeof a)&&(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)},module.exports=k}();