@danielkalen/simplybind
Version:
Magically simple, framework-less one-way/two-way data binding for frontend/backend in ~5kb.
2 lines (1 loc) • 17.3 kB
JavaScript
!function(){var y,l,m,z,u,q,t,A,B,M,C,D,E,F,n,G,p,N,H,I,J,g,v,w,K,x,L,h,r;return I=function(a){return a instanceof NodeList||a instanceof HTMLCollection||"undefined"!=typeof jQuery&&null!==jQuery&&a instanceof jQuery},E=function(a){var b;if(1<a.length){if("radio"===a[0].type||"checkbox"===a[0].type){var c,d,e;for(e=[],c=0,d=a.length;c<d;c++)b=a[c],e.push(b);return e}r("erOD")}return a[0]},A=function(){var a;return a=document.createEvent("Event"),a.initEvent("change",!0,!1),a._sb=!0,a},u="push pop shift unshift splice reverse sort".split(" "),F=function(){return"sb_"+Math.floor(1e12*(1+Math.random())).toString(16)},n=function(){return Object.create(null)},H=function(a){return a instanceof l},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 q[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 q[a._sb_ID]}}},D=/[.*+?^${}()|[\]\\]/g,w=v=null,K=function(){var a,b,c;return c=p.placeholderStart.replace(D,"\\$&"),a=p.placeholderEnd.replace(D,"\\$&"),b="[^"+a+"]+",v=new RegExp(c+"("+b+")"+a,"g"),w=new RegExp(""+c+b+a,"g")},z=function(a,b,c){var d,e,f,k,g;for(g="",f=e=0,k=a.length;e<k;f=++e)d=a[f],g+=d,c[f]&&(g+=b[c[f]]);return g},x=function(a,b){var c;throw c=G(),Error("SimplyBind: "+((b?a:C[a])+("\n\nCall Source:\n"+c)))},r=function(a){var b;if(!p.silent)return b=G(),a=C[a],console.log("SimplyBind: "+(a+("\n\nCall Source:\n"+b)))},h=function(a){return x("You can't use/invoke ."+a+"() at this stage",!0)},L=function(a,b){return x("Invalid argument/s ("+b+") passed to ."+a+"()",!0)},G=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",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()",erOD:"You can only pass 1 DOM element to SimplyBind unless it's a collection of radio/checkbox inputs",erEL:"You can't bind an empty element list to anything..."},q={},p={silent:!1,liveProps:!0,dispatchEvents:!1,updateEvenIfUndefined:!1,updateEvenIfSame:!1,updateOnBind:!0,mutateInherited:!1,trackArrayChildren:!1,simpleSelector:!1,promiseTransforms:!1,placeholderStart:"{{",placeholderEnd:"}}"},K(),m=function(a,b,c){return(a||0===a)&&("string"==typeof a||"number"==typeof a||"function"==typeof a||a instanceof Array)||H(a)||x("erIP"),"object"!=typeof a||a instanceof Array?new y(a,b,c):new l(a._,1,a.placeholder)},y=function(a,b,c){var d,e;null==b&&(b={}),this.opts=b,this.options={};for(d in p)this.options[d]=null!=b[d]?b[d]:p[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 l)return e;e=1}else if(a instanceof Array){if(this.sTP("Array"),e=this.sOB("obFLT",a),e instanceof l)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 l(this,3),new l(this,e,this.placeholder)},l=function(a,b,c,d,e,f){var k;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),k=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 l(a,b,k.placeholder,c||k.proxied,d||k.proxies,k.has)}},_:{value:a}}),this},y.prototype={sOB:function(a,b,c){var d,e,f,k;if(H(b)&&(b=b.original),I(b)){if(b=E(b),!b)return this.stage=0,r("erEL");for(k=this.OBs=[].concat(b),b=0,e=k.length;b<e;b++)f=k[b],f.checked&&(this.OB=f);b=this.OB||(this.OB=this.OBs[0]),1<this.OBs.length||delete this.OBs}if(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 l(b,1,this.placeholder)}(this.dom=this.OB.nodeName&&1===this.OB.nodeType)&&(d=this.OBs?this.OBs[0].nodeName.toUpperCase():this.OB.nodeName.toUpperCase(),this.domI="INPUT"===d||"TEXTAREA"===d||"SELECT"===d),"Event"!==this.type&&(d=this.fDV(null,c),"attr"===this.desc&&(d=this.fDV("DOMAttr")),"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=a=t.obFLT.get(this.OB))&&a.options.trackArrayChildren&&-1===a.arrC.indexOf(this.prop)&&(a.arrC.push(this.prop),m(this.prop).of(this.OB).to(a.uS))},sEV:function(a,b,c){if(isNaN(parseInt(this.prop))&&r("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 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},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},DES:function(){var a,b,c,d;if(delete q[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=u.length;a<b;a++)c=u[a],delete this.OB[c];else"Func"===this.type&&delete this.OB._sb_ID},sID:function(){return this.ID=F(),q[this.ID]=this},sTP:function(a){if(!this.type&&(this.type=a||"ObjectProp",this.dom)){if("value"===this.prop&&this.domI)return this.type="DOMValue";if("textContent"===this.prop)return this.type="DOMText";if("attr"===this.desc)return this.type="DOMAttr"}},sPH:function(){var a;if(this.pVL=n(),this.pIM=n(),this.pCT=[],"string"==typeof this.VLo&&(this.pCT=this.VLo.split(w),a=0,this.value=this.VLo.replace(v,function(b){return function(c,d){return b.pIM[a++]=d,b.pVL[d]=d}}(this))),this.dom&&"textContent"===this.prop)return this.sTNP()},aEV:function(){var a,b;return"Event"===this.type?this.rEVE(this.eveN,this.cEM.in):"DOMValue"!==this.type&&"DOMText"!==this.type||(a=this,b=function(b){if(!b._sb)return a.sV(b.target.value)},!this.domI)?void 0:"radio"===this.OB.type||"checkbox"===this.OB.type?this.OBs.forEach(function(a){return a.addEventListener("change",b,!1)}):(this.OB.addEventListener("input",b,!1),this.OB.addEventListener("change",b,!1))},sTNP:function(a){var b,c,d,e,f,k,g,h,l,m;for(null==a&&(a=this.OB),l=a.childNodes,c=0,f=l.length;c<f;c++)k=l[c],3!==k.nodeType?this.sTNP(k):(g=F(),h=k.textContent,b=!1,m=n(),e=n(),a=h.split(w),d=0,h.replace(v,function(a){return function(c,f){return b=!0,e[d++]=f,m[f]=a.pVL[f]}}(this)),b||(m=null),Object.defineProperty(k,"_sb_ID",{configurable:!0,value:g}),t.txN[g]=[h,a,m,e])},gTNV:function(a,b,c){var d;return!!(a=t.txN[a._sb_ID])&&((d=a[2])?(d[b]=c,z(a[1],a[2],a[3])):a[0])},fDV:function(a,b){switch(null==a&&(a=this.type),a){case"Func":if(!b)return this.OB();break;case"Array":return this.OB;case"DOMAttr":return this.OB.getAttribute(this.prop)||"";default:return this.OB[this.prop]}},aTNR:function(a,b,c){var d,e,f,k;if(this.placeholder)for(k=a.childNodes,a=0,d=k.length;a<d;a++)f=k[a],3!==f.nodeType?this.aTNR(f,b,c):(e=this.gTNV(f,b,c))&&(f.textContent=e);else a.textContent=this.value},sV:function(a,b,c,d){var e,f,k,g;if(null==c&&(c=this),e=b?this.pVL[b]:this.value,this.tfS&&(a=this.tfS(a)),e=a!==e||this.options.updateEvenIfSame,k="undefined"!=typeof a||this.options.updateEvenIfUndefined,(e&&k||this.isAsync)&&"Array"!==this.type){switch(b?(this.pVL[b]=f=a,this.value=a=z(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);break;case"DOMValue":if(this.OBs)for(g=this.OBs,f=0,e=g.length;f<e;f++)k=g[f],b=k.checked=k.value===a,this.options.dispatchEvents&&b&&k.dispatchEvent(A());else this.OB.value!==this.value&&(this.OB.value=this.value,this.options.dispatchEvents&&this.OB.dispatchEvent(A()));break;case"DOMAttr":this.OB.setAttribute(this.prop,this.value);break;case"DOMText":this.aTNR(this.OB,b,f)}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,l;if(this.options.liveProps)if(b=this,"ObjectProp"===this.type){if(f=Object.getOwnPropertyDescriptor(this.OB,this.prop),(l=(l=this.options.mutateInherited||f&&f.configurable)&&this.OB.constructor!==CSSStyleDeclaration)&&!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,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.arrC))return this.arrC=[],this.uS=function(){return b.uAD()},c={updateOnBind:!1},this.value.forEach(function(a,d){return b.arrC.push(""+d),m(d,c).of(b.value).to(b.uS)})},iEM:function(a,b,c){var d;return d=this.OB,(this.dom&&"undefined"!=typeof jQuery&&null!==jQuery&&"on"===b||"off"===b)&&(d=jQuery(this.OB)),d[b]||(b=c),this.eveH||(this.eveH=N.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||"dispatchEvent",this.dom&&"undefined"!=typeof jQuery&&null!==jQuery&&"trigger"===b&&(c=jQuery(this.OB)),c[b]||(b="dispatchEvent"),"dispatchEvent"===b?(this.eveO||(this.eveO=document.createEvent("Event"),this.eveO.initEvent(this.eveN,!0,!0)),this.eveO.boundData=a,c[b](this.eveO)):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||"addEventListener","addEventListener")},urEVE:function(a,b){var c;if(c=this.atEV.indexOf(a),-1!==c)return this.atEV.splice(c,1),this.iEM(a,b||"removeEventListener","removeEventListener")},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}},N=function(){var a;return a="Event"===this.type?arguments[this.prop]:this.fDV(),this.sV(a,null,this.eveC,!0)},l.prototype={of:function(a){var b,c;return 0!==this.stage&&2!==this.stage&&h(g[0]),"object"!=typeof a&&"function"!=typeof a&&L(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),J(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||L(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=B(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=B(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=m(0,d).ofEvent(a,c,b),this.new(2)},chainTo:function(a,b){return 3!==this.stage&&h(g[5]),m(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?r("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!=p[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}},l.prototype.update=l.prototype.set,l.prototype.twoWay=l.prototype.bothWays,l.prototype.pipe=l.prototype.chainTo,g=Object.keys(l.prototype),B=function(a,b,c){return null==c&&(c={}),a.proxied=b=m(b,c,!0),0!==b.stage&&(a._.aD(b,a),J(a._,b._)),b},J=function(a,b){if(a.options.updateOnBind||"Func"===a.type)return a.uD(b)},Object.defineProperties(m,{version:{value:"1.1.2"},options:{get:function(){var a,b,c;a={};for(b in p)c=p[b],a[b]=c;return a}}}),m.setOption=function(a,b){if(null!=p[a]&&(p[a]=b,-1!==a.indexOf("place")))return K()},m.setOptions=function(a){var b,c;for(b in a)c=a[b],m.setOption(b,c)},m.unBindAll=function(a,b){var c,d,e;if(a&&("object"==typeof a||"function"==typeof a)&&(I(a)&&(a=E(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)},window.SimplyBind=m}();