@danielkalen/simplybind
Version:
Magically simple, framework-less one-way/two-way data binding for frontend/backend in ~5kb.
2 lines (1 loc) • 14.1 kB
JavaScript
!function(){var v,k,l,G,r,p,t,w,H,x,y,I,n,z,m,J,A,B,g,C,D,E,u,F,h,q;return r="push pop shift unshift splice reverse sort".split(" "),I=function(){return"sb_"+Math.floor(1e12*(1+Math.random())).toString(16)},n=function(){return Object.create(null)},A=function(a){return a instanceof k},t={txN:{},object:{set:function(a){var b,c;return b="Event"===a.type?a.prop+"#"+a.eveN:a.prop,a.OB._sb_map?a.OB._sb_map[b]=a.ID:(c={},c[b]=a.ID,Object.defineProperty(a.OB,"_sb_map",{configurable:!0,value:c}))},get:function(a,b){var c;if(c="Event"===b.type?b.prop+"#"+b.eveN:b.prop,b.OB._sb_map&&b.OB._sb_map[c])return p[b.OB._sb_map[c]]}},obFLT:{set:function(a){return Object.defineProperty(a.OB,"_sb_ID",{configurable:!0,value:a.ID})},get:function(a){return p[a._sb_ID]}}},y=/[.*+?^${}()|[\]\\]/g,D=C=null,E=function(){var a,b,c;return c=m.placeholderStart.replace(y,"\\$&"),a=m.placeholderEnd.replace(y,"\\$&"),b="[^"+a+"]+",C=new RegExp(c+"("+b+")"+a,"g"),D=new RegExp(""+c+b+a,"g")},G=function(a,b,c){var d,e,f,g,h;for(h="",f=e=0,g=a.length;e<g;f=++e)d=a[f],h+=d,c[f]&&(h+=b[c[f]]);return h},u=function(a,b){var c;throw c=z(),Error("SimplyBind: "+((b?a:x[a])+("\n\nCall Source:\n"+c)))},q=function(a){var b;if(!m.silent)return b=z(),a=x[a],console.log("SimplyBind: "+(a+("\n\nCall Source:\n"+b)))},h=function(a){return u("You can't use/invoke ."+a+"() at this stage",!0)},F=function(a,b){return u("Invalid argument/s ("+b+") passed to ."+a+"()",!0)},z=function(){return Error().stack.split("\n").slice(3,10).map(function(a){return a.replace(H,function(a,c,d){return"@ "+d})}).join("\n")},H=/[@(?:\wat\s)](.*)\/(.+)$/,Error.prototype.stack="\n\n\n",x={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()"},p={},m={silent:!1,liveProps:!0,dispatchEvents:!1,updateEvenIfUndefined:!1,updateEvenIfSame:!1,updateOnBind:!0,mutateInherited:!1,trackArrayChildren:!1,simpleSelector:!1,promiseTransforms:!1,placeholderStart:"{{",placeholderEnd:"}}"},E(),l=function(a,b,c){return(a||0===a)&&("string"==typeof a||"number"==typeof a||"function"==typeof a||a instanceof Array)||A(a)||u("erIP"),"object"!=typeof a||a instanceof Array?new v(a,b,c):new k(a._,1,a.placeholder)},v=function(a,b,c){var d,e;null==b&&(b={}),this.opts=b,this.options={};for(d in m)this.options[d]=null!=b[d]?b[d]:m[d];if(this.ID=this.type=null,this.selc=this.prop=a,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=[],this.cEM=n(),"function"==typeof a){if(this.sTP("Func"),e=this.sOB("obFLT",a,c),e instanceof k)return e;e=1}else if(a instanceof Array){if(this.sTP("Array"),e=this.sOB("obFLT",a),e instanceof k)return e;e=1}else"number"==typeof this.selc&&(this.selc=this.prop=this.selc.toString()),-1===this.selc.indexOf(":")||this.options.simpleSelector||(a=this.prop.split(":"),this.desc=a[0],this.prop=a[1]),-1===this.selc.indexOf(".")||this.options.simpleSelector||(a=this.prop.split("."),this.prop=a[0],this.placeholder=a.slice(1).join("."));return this.pub=new k(this,3),new k(this,e,this.placeholder)},k=function(a,b,c,d,e,f){var g;return this.placeholder=c,this.stage=b||0,this.proxied=d,this.proxies=e&&e.length?e:d?[d]:[],this.has=f?f:{tf:!1,massTf:!1,initBind:!1,event:!1},this.placeholder&&(a.placeholder=this.placeholder),g=this,Object.defineProperties(this,{ID:{get:function(){return a.ID}},value:{get:function(){return a.value}},original:{get:function(){return a.OBs||a.OB}},dependents:{get:function(){return a.deps.slice().map(function(a){return a.OB})}},new:{value:function(b,c,d){return new k(a,b,g.placeholder,c||g.proxied,d||g.proxies,g.has)}},_:{value:a}}),this},v.prototype={sOB:function(a,b,c){var d;if(A(b)&&(b=b.original),this.OB=b,b=t[a].get(this.OB,this)){this.placeholder&&!b.pVL&&(b.VLo=b.fDV(),b.sPH()),"ObjectProp"!==b.type||this.prop in this.OB||b.mPL(!0),a=this.opts;for(d in a)c=a[d],b.pub.setOption(d,c);return new k(b,1,this.placeholder)}"Event"!==this.type&&(d=this.fDV(null,c),"undefined"==typeof d&&"object"===a&&(this.OB[this.prop]=d=null),this.value=this.VLo=d,this.placeholder&&!this.pVL&&"object"===a&&this.sPH()),this.sID(),this.sTP(),this.mPL(),this.aEV(),t[a].set(this),this.OB instanceof Array&&"Array"!==this.type&&(this.arrB=d=t.obFLT.get(this.OB))&&d.options.trackArrayChildren&&-1===d.arrC.indexOf(this.prop)&&(d.arrC.push(this.prop),l(this.prop).of(this.OB).to(d.uS))},sEV:function(a,b,c){if(isNaN(parseInt(this.prop))&&q("erEV"),this.sTP("Event"),this.isAsync=!0,this.eveN=a,b&&(this.cEM.in=b),c)return this.cEM.out=c},pTF:function(a,b){var c,d,e,f;if("function"!=typeof a)return q("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},pCN:function(a,b){var c,d,e,f;if("function"!=typeof a)return q("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},DES:function(){var a,b,c,d;if(delete p[this.ID],"ObjectProp"===this.type||"DOMText"===this.type)Object.defineProperty(this.OB,this.prop,{value:this.value,writable:!0}),delete this.OB._sb_map,delete this.OB._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.OB._sb_map}else if("Array"===this.type)for(delete this.OB._sb_ID,a=0,b=r.length;a<b;a++)c=r[a],delete this.OB[c];else"Func"===this.type&&delete this.OB._sb_ID},sID:function(){return this.ID=I(),p[this.ID]=this},sTP:function(a){if(!this.type)return this.type=a||"ObjectProp"},sPH:function(){var a;if(this.pVL=n(),this.pIM=n(),this.pCT=[],"string"==typeof this.VLo)return this.pCT=this.VLo.split(D),a=0,this.value=this.VLo.replace(C,function(b){return function(c,d){return b.pIM[a++]=d,b.pVL[d]=d}}(this))},aEV:function(){if("Event"===this.type)return this.rEVE(this.eveN,this.cEM.in)},fDV:function(a,b){switch(null==a&&(a=this.type),a){case"Func":if(!b)return this.OB();break;case"Array":return this.OB;default:return this.OB[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.isAsync)&&"Array"!==this.type){switch(b?(this.pVL[b]=a,this.value=a=G(this.pCT,this.pVL,this.pIM)):this.value=a,this.type){case"ObjectProp":this.live||(this.OB[this.prop]=a);break;case"Func":b=this.valuePassed,this.valuePassed=a,this.value=this.OB(a,b);break;case"Event":d||(this.eveC=c,this.eEVE(a),this.eveC=null)}d&&this.eveC||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)},apT:function(a,b,c,d){return this.hTfms?(a=a.ID,b&&(a+="."+b),this.tfms[a]?this.tfms[a](c,d):c):c},cC:function(a,b,c,d){return!this.hCnds||(a=a.ID,b&&(a+="."+b),!this.cnds[a]||this.cnds[a](c,d))},mPL:function(a){var b,c,d,e,f,g,h,k;if(this.options.liveProps)if(b=this,"ObjectProp"===this.type){if(f=Object.getOwnPropertyDescriptor(this.OB,this.prop),(k=this.options.mutateInherited||f&&f.configurable)&&!this.live||a)return this.live=!0,d=(null!=f&&null!=(g=f.get)?g.bind(this.OB):void 0)||function(){return b.value},e=(null!=f&&null!=(h=f.set)?h.bind(this.OB):void 0)||function(){},Object.defineProperty(this.OB,this.prop,{configurable:!0,enumerable:null!=f?f.enumerable:void 0,get:function(){return d()},set:function(a){return b.sV(a),e(a)}})}else if("Array"===this.type&&(this.live||(this.live=!0,r.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.arrC))return this.arrC=[],this.uS=function(){return b.uAD()},c={updateOnBind:!1},this.value.forEach(function(a,d){return b.arrC.push(""+d),l(d,c).of(b.value).to(b.uS)})},iEM:function(a,b,c){var d;return d=this.OB,d[b]||(b=c),this.eveH||(this.eveH=J.bind(this)),"function"==typeof d[b]?d[b](a,this.eveH):void 0},eEVE:function(a){var b,c;return c=this.OB,b=this.cEM.out||"emit",c[b]||(b="emit"),c[b](this.eveN,a)},aD:function(a,b,c){return this.depsMap[1][a.ID]||(this.depsMap[1][a.ID]=a._,this.deps.push(a._)),b.placeholder?this.mPH[a.ID]=b.placeholder:this.mPH[a.ID]&&delete this.mPH[a.ID],a.placeholder&&(this.dPH[a.ID]=a.placeholder),c?this.depsMap[2][a.ID]=a._:a._.depsMap[1][this.ID]&&(a._.aD(b,a,!0),this.aD(a,b,!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()},aTF:function(a,b){return this.hTfms=!0,this.tfms[a]=b},aCND:function(a,b){return this.hCnds=!0,this.cnds[a]=b},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")},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}},J=function(){var a;return a="Event"===this.type?arguments[this.prop]:this.fDV(),this.sV(a,null,this.eveC,!0)},k.prototype={of:function(a){var b,c;return 0!==this.stage&&2!==this.stage&&h(g[0]),"object"!=typeof a&&"function"!=typeof a&&F(g[0],a),0===this.stage?c=this._.sOB("object",a):(b=this.proxies.indexOf(this.proxied),this.proxied=this.proxied.of(a),this.proxies[b]=this.proxied,this.has.initBind=!0,this.has.tf=!1,this._.aD(this.proxied,this),B(this._,this.proxied._)),c||this.new(this.stage+1)},ofEvent:function(a,b,c){return 0!==this.stage||this.has.event?h(g[1]):a&&"string"==typeof a||F(g[1],a),this._.sEV(a,b,c),this.has.event=!0,this.new(0)},to:function(a,b){var c;return(1!==this.stage||this.has.initBind)&&h(g[2]),this.proxied=w(this,a,b),0===this.proxied.stage?c=2:(c=3,this.has.initBind=!0),this.new(c)},and:function(a,b){var c;return 3===this.stage&&this.has.initBind&&!this.has.massTf||h(g[3]),this.proxied=w(this,a,b),this.proxies=this.proxies.concat(this.proxied),0===this.proxied.stage?c=2:(c=3,this.has.tf=!1),this.new(c)},toEvent:function(a,b,c,d){return 1!==this.stage&&h(g[4]),this.proxied=l(0,d).ofEvent(a,c,b),this.new(2)},chainTo:function(a,b){return 3!==this.stage&&h(g[5]),l(this.proxied).to(a,b)},set:function(a){return 0!==this.stage&&2!==this.stage||h(g[6]),this._.sV(a,this.placeholder),this},get:function(){return 0!==this.stage&&2!==this.stage||h(g[7]),this.placeholder?this._.pVL[this.placeholder]:this._.value},transformSelf:function(a){return(1!==this.stage||1===this.stage&&"Array"===this._.type)&&h(g[8]),"function"!=typeof a?q("erFN"):(this._.tfS=a,this._.options.updateOnBind&&this.set(this.value)),this},transform:function(a){return(3!==this.stage||this.has.tf||this.has.massTf)&&h(g[9]),this.has.tf=this._.pTF(a,[this.proxied])||!1,this.new(3)},transformAll:function(a){return(3!==this.stage||this.has.tf||this.has.massTf)&&h(g[10]),this.has.massTf=this._.pTF(a,this.proxies)||!1,this.new(3)},condition:function(a){return 3!==this.stage&&h(g[11]),this._.pCN(a,[this.proxied]),this.new(3)},conditionAll:function(a){return 3!==this.stage&&h(g[12]),this._.pCN(a,this.proxies),this.new(3)},bothWays:function(a){var b,c,d;return(3!==this.stage||this.has.massTf)&&h(g[13]),b=this.proxied._.aD(this,this.proxied,!0),d=this._.tfms[this.proxied.ID],c=this._.cnds[this.proxied.ID],(d||a)&&(d="function"==typeof a?a:d)&&!1!==a&&b.aTF(this.ID,d),c&&b.aCND(this.ID,c),this._.aD(this.proxied,this,!0),this},unBind:function(a){var b,c,d,e;for(3!==this.stage&&h(g[14]),e=this.proxies,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||h(g[15]),this._.aPI(a),this},stopPolling:function(){return 3!==this.stage&&h(g[16]),this._.rPI(),this},updateDepsOnEvent:function(a,b){return 3!==this.stage&&h(g[17]),this._.rEVE(a,b),this},removeEvent:function(a,b){return 3!==this.stage&&h(g[18]),this._.urEVE(a,b),this},throttle:function(a){return 1!==this.stage&&3!==this.stage&&h(g[19]),"number"==typeof a&&a?this._.thR=a:!1===a&&delete this._.thR,this},setOption:function(a,b){var c,d,e,f;if(null!=m[a])for(this._.options[a]=b,this._.mPL(),f=this.proxies,c=0,d=f.length;c<d;c++)e=f[c],e._.options[a]=b,e._.mPL();return this}},k.prototype.update=k.prototype.set,k.prototype.twoWay=k.prototype.bothWays,k.prototype.pipe=k.prototype.chainTo,g=Object.keys(k.prototype),w=function(a,b,c){return null==c&&(c={}),a.proxied=b=l(b,c,!0),0!==b.stage&&(a._.aD(b,a),B(a._,b._)),b},B=function(a,b){if(a.options.updateOnBind||"Func"===a.type)return a.uD(b)},Object.defineProperties(l,{version:{value:"1.1.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(null!=m[a]&&(m[a]=b,-1!==a.indexOf("place")))return E()},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&&("object"==typeof a||"function"==typeof a)&&(e=a._sb_map,a._sb_ID&&p[a._sb_ID].rAD(b),e))for(d in e)c=e[d],p[c].rAD(b)},module.exports=l}();