@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 A,m,n,B,v,r,u,C,D,O,E,F,G,H,p,I,q,P,J,K,L,h,w,x,y,M,z,N,k,t;return K=function(a){return a instanceof NodeList||a instanceof HTMLCollection||"undefined"!=typeof jQuery&&null!==jQuery&&a instanceof jQuery},G=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}t("erOD")}return a[0]},C=function(){var a;return a=document.createEvent("Event"),a.initEvent("change",!0,!1),a._sb=!0,a},v="push pop shift unshift splice reverse sort".split(" "),H=function(){return"sb_"+Math.floor(1e12*(1+Math.random())).toString(16)},p=function(){return Object.create(null)},J=function(a){return a instanceof m},y=function(a,b,c){var d,e;for(d in b)e=b[d],null!=q[d]&&(a.options[d]=e);if(c)return a.mPL()},u={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){if(a._sb_map&&a._sb_map[b])return r[a._sb_map[b]]}},simpleObject:{set:function(a){return Object.defineProperty(a.object,"_sb_ID",{configurable:!0,value:a.ID})},get:function(a){return r[a._sb_ID]}}},F=/[.*+?^${}()|[\]\\]/g,x=w=null,M=function(){var a,b,c;return c=q.placeholderStart.replace(F,"\\$&"),a=q.placeholderEnd.replace(F,"\\$&"),b="[^"+a+"]+",w=new RegExp(c+"("+b+")"+a,"g"),x=new RegExp(""+c+b+a,"g")},B=function(a,b,c){var d,e,f,g,l;for(l="",f=e=0,g=a.length;e<g;f=++e)d=a[f],l+=d,c[f]&&(l+=b[c[f]]);return l},z=function(a,b){var c;throw c=I(),Error("SimplyBind: "+((b?a:E[a])+("\n\nCall Source:\n"+c)))},t=function(a){var b;if(!q.silent)return b=I(),a=E[a],console.log("SimplyBind: "+(a+("\n\nCall Source:\n"+b)))},k=function(a){return z("You can't use/invoke ."+a+"() at this stage",!0)},N=function(a,b){return z("Invalid argument/s ("+b+") passed to ."+a+"()",!0)},I=function(){return Error().stack.split("\n").slice(3,10).map(function(a){return a.replace(O,function(a,c,d){return"@ "+d})}).join("\n")},O=/[@(?:\wat\s)](.*)\/(.+)$/,Error.prototype.stack="\n\n\n",E={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..."},r={},q={silent:!1,liveProps:!0,dispatchEvents:!1,updateEvenIfUndefined:!1,updateEvenIfSame:!1,updateOnBind:!0,mutateInherited:!1,trackArrayChildren:!1,simpleSelector:!1,promiseTransforms:!1,placeholderStart:"{{",placeholderEnd:"}}"},M(),n=function(a,b,c){return(a||0===a)&&("string"==typeof a||"number"==typeof a||"function"==typeof a||a instanceof Array)||J(a)||z("erIP"),"object"!=typeof a||a instanceof Array?new m(null,0,null,a,c,b):new m(a._,1)},A=function(a,b,c,d,e,f,g,l){var h,k;for(h in g)k=g[h],this[h]=k;return this.options=f,this.type=a,this.ID=H(),this.prop=c,this.selc=b,this.object=d,this.objects=e,this.value=this.VLo=null,this.deps=[],this.depsMap={1:p(),2:p()},this.dPH=p(),this.mPH=p(),this.tfms=p(),this.cnds=p(),this.atEV=[],"Event"!==this.type&&(a=this.fDV(null,l),"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=u.simpleObject.get(this.object))&&a.options.trackArrayChildren&&-1===a.tC.indexOf(this.prop)&&(a.tC.push(this.prop),n(this.prop).of(this.object).to(a.uS)),r[this.ID]=this},m=function(a,b,c,d,e,f){var g,l,h;c=c||{};for(g in c)h=c[g],this[g]=h;switch(this.stage=b||0,null==this.pX&&(this.pX=[]),null==this.sT&&(this.sT={}),this.stage){case 0:this.opts=f||(f={}),this.options={};for(g in q)this.options[g]=null!=f[g]?f[g]:q[g];"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(":")&&(l=this.prop.split(":"),this.desc=l[0],this.prop=l[1]),-1!==this.selc.indexOf(".")&&(l=this.prop.split("."),this.prop=l[0],this.placeholder=l.slice(1).join(".")),this.selc=this.prop));break;case 1:if(!a){if(K(d)){if(d=G(d),!d)return t("erEL");for(g=this.objects=[].concat(d),b=0,d=g.length;b<d;b++)f=g[b],f.checked&&(l=f);d=l||(l=this.objects[0]),1<this.objects.length||delete this.objects}l=d,(this.dom=l.nodeName&&1===l.nodeType)&&(b=this.objects?this.objects[0].nodeName.toUpperCase():l.nodeName.toUpperCase(),this.domI="INPUT"===b||"TEXTAREA"===b||"SELECT"===b),b=function(){switch(!1){case!this.sT.hEN:return"Event";case!(this.domI&&"value"===this.prop):return"DOMValue";case!(this.dom&&"textContent"===this.prop):return"DOMText";case"attr"!==this.desc:return"DOMAttr";default:return"ObjectProp"}}.call(this),a=this.sOB(l,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 m(a,b,this.gST(),c,e)}}}),this},A.prototype={mPL:function(a){var b,c,d,e,f,g,l,h;if(this.options.liveProps)if(b=this,"ObjectProp"===this.type){if(f=Object.getOwnPropertyDescriptor(this.object,this.prop),(h=(h=this.options.mutateInherited||f&&f.configurable)&&this.object.constructor!==CSSStyleDeclaration)&&!this.live||a)return this.live=!0,d=(null!=f&&null!=(g=f.get)?g.bind(this.object):void 0)||function(){return b.value},e=(null!=f&&null!=(l=f.set)?l.bind(this.object):void 0)||function(){},Object.defineProperty(this.object,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,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.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),n(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 r[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=v.length;a<b;a++)c=v[a],delete this.object[c];else"Func"===this.type&&delete this.object._sb_ID},fDV:function(a,b){switch(null==a&&(a=this.type),a){case"Func":if(!b)return this.object();break;case"Array":return this.object;case"DOMAttr":return this.object.getAttribute(this.prop)||"";default:return this.object[this.prop]}},sV:function(a,b,c,d){var e,f,g,l;if(null==c&&(c=this),e=b?this.pVL[b]:this.value,this.tfS&&(a=this.tfS(a)),e=a!==e||this.options.updateEvenIfSame,g="undefined"!=typeof a||this.options.updateEvenIfUndefined,(e&&g||this.isEvent)&&"Array"!==this.type){switch(b?(this.pVL[b]=f=a,this.value=a=B(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.eEVE(a),this.evC=null);break;case"DOMValue":if(this.objects)for(l=this.objects,f=0,e=l.length;f<e;f++)g=l[f],b=g.checked=g.value===a,this.options.dispatchEvents&&b&&g.dispatchEvent(C());else this.object.value!==this.value&&(this.object.value=this.value,this.options.dispatchEvents&&this.object.dispatchEvent(C()));break;case"DOMAttr":this.object.setAttribute(this.prop,this.value);break;case"DOMText":this.aTNR(this.object,b,f)}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 t("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 t("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=p(),this.pIM=p(),this.pCT=[],"string"==typeof this.VLo&&(this.pCT=this.VLo.split(x),a=0,this.value=this.VLo.replace(w,function(b){return function(c,d){return b.pIM[a++]=d,b.pVL[d]=d}}(this))),this.dom&&"textContent"===this.prop)return this.sTNP()},sTNP:function(a){var b,c,d,e,f,g,h,k,m,n;for(null==a&&(a=this.object),m=a.childNodes,c=0,f=m.length;c<f;c++)g=m[c],3!==g.nodeType?this.sTNP(g):(h=H(),k=g.textContent,b=!1,n=p(),e=p(),a=k.split(x),d=0,k.replace(w,function(a){return function(c,f){return b=!0,e[d++]=f,n[f]=a.pVL[f]}}(this)),b||(n=null),Object.defineProperty(g,"_sb_ID",{configurable:!0,value:h}),u.txN[h]=[k,a,n,e])},gTNV:function(a,b,c){var d;return!!(a=u.txN[a._sb_ID])&&((d=a[2])?(d[b]=c,B(a[1],a[2],a[3])):a[0])},aTNR:function(a,b,c){var d,e,f,g;if(this.placeholder)for(g=a.childNodes,a=0,d=g.length;a<d;a++)f=g[a],3!==f.nodeType?this.aTNR(f,b,c):(e=this.gTNV(f,b,c))&&(f.textContent=e);else a.textContent=this.value},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(){var a,b;return"Event"===this.type?this.rEVE(this.evN,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.object.type||"checkbox"===this.object.type?this.objects.forEach(function(a){return a.addEventListener("change",b,!1)}):(this.object.addEventListener("input",b,!1),this.object.addEventListener("change",b,!1))},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")},iEM:function(a,b,c){var d;return d=this.object,(this.dom&&"undefined"!=typeof jQuery&&null!==jQuery&&"on"===b||"off"===b)&&(d=jQuery(this.object)),d[b]||(b=c),this.evH||(this.evH=P.bind(this)),"function"==typeof d[b]?d[b](a,this.evH):void 0},eEVE:function(a){var b,c;return c=this.object,b=this.cEM.out||"dispatchEvent",this.dom&&"undefined"!=typeof jQuery&&null!==jQuery&&"trigger"===b&&(c=jQuery(this.object)),c[b]||(b="dispatchEvent"),"dispatchEvent"===b?(this.evO||(this.evO=document.createEvent("Event"),this.evO.initEvent(this.evN,!0,!0)),this.evO.boundData=a,c[b](this.evO)):c[b](this.evN,a)}},P=function(){var a;return a="Event"===this.type?arguments[this.prop]:this.fDV(),this.sV(a,null,this.evC,!0)},m.prototype={of:function(a){var b;return 0!==this.stage&&2!==this.stage&&k(h[0]),"object"!=typeof a&&"function"!=typeof a&&N(h[0],a),J(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._),L(this._,b._)),this.new(this.stage+1,a)},ofEvent:function(a,b,c){return 0!==this.stage||this.sT.hEN?k(h[1]):a&&"string"==typeof a?isNaN(parseInt(this.prop))&&t("erEV"):N(h[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)&&k(h[2]),this.pX.push(c=D(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||k(h[3]),this.pX.push(c=D(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&&k(h[4]),this.pX.push(n(0,d).ofEvent(a,c,b)),this.new(2)},chainTo:function(a,b){return 3!==this.stage&&k(h[5]),n(this.lpX).to(a,b)},set:function(a){return 0!==this.stage&&2!==this.stage||k(h[6]),this._.sV(a,this.placeholder),this},get:function(){return 0!==this.stage&&2!==this.stage||k(h[7]),this.placeholder?this._.pVL[this.placeholder]:this._.value},transformSelf:function(a){return(1!==this.stage||1===this.stage&&"Array"===this._.type)&&k(h[8]),"function"!=typeof a?t("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)&&k(h[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)&&k(h[10]),this.sT.hasMultiTransform=this._.pTF(a,this.pX)||!1,this.new(3)},condition:function(a){return 3!==this.stage&&k(h[11]),this._.pCN(a,this.pX.slice(-1)),this.new(3)},conditionAll:function(a){return 3!==this.stage&&k(h[12]),this._.pCN(a,this.pX),this.new(3)},bothWays:function(a){var b,c,d,e;return(3!==this.stage||this.sT.hasMultiTransform)&&k(h[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&&k(h[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||k(h[15]),this._.aPI(a),this},stopPolling:function(){return 3!==this.stage&&k(h[16]),this._.rPI(),this},updateDepsOnEvent:function(a,b){return 3!==this.stage&&k(h[17]),this._.rEVE(a,b),this},removeEvent:function(a,b){return 3!==this.stage&&k(h[18]),this._.urEVE(a,b),this},throttle:function(a){return 1!==this.stage&&3!==this.stage&&k(h[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,y(this._,e,!0),g=this.pX,c=0,d=g.length;c<d;c++)f=g[c],y(f._,e,!0);return this}},m.prototype.update=m.prototype.set,m.prototype.twoWay=m.prototype.bothWays,m.prototype.pipe=m.prototype.chainTo,h=Object.keys(m.prototype),D=function(a,b,c){return null==c&&(c={}),b=n(b,c,!0),0!==b.stage&&(a._.aD(b._,a),L(a._,b._)),b},L=function(a,b){if(a.options.updateOnBind||"Func"===a.type)return a.uD(b)},Object.defineProperties(m.prototype,{sOB:{value:function(a,b,c,d){var e;return this.object=a,c=c?"simpleObject":"object",(e=u[c].get(a,this.selc))?((e.placeholder=this.placeholder)&&!e.pVL&&(e.VLo=e.fDV(),e.sPH()),"ObjectProp"!==e.type||this.prop in a||e.mPL(!0),y(e,this.options,!0),e):(a=new A(b,this.selc,this.prop,this.object,this.objects,this.options,{dom:this.dom,domI:this.domI,placeholder:this.placeholder,evN:this.evN,cEM:this.cEM,isEvent:this.sT.hEN},d),u[c].set(a),a)}},gST:{value:function(){return{options:this.options,selc:this.selc,object:this.object,prop:this.prop,desc:this.desc,placeholder:this.placeholder,evN:this.evN,cEM:this.cEM,proxied:this.proxied,pX:this.pX,sT:this.sT}}}}),Object.defineProperties(n,{version:{value:"1.2.1"},options:{get:function(){var a,b,c;a={};for(b in q)c=q[b],a[b]=c;return a}}}),n.setOption=function(a,b){if(null!=q[a]&&(q[a]=b,-1!==a.indexOf("place")))return M()},n.setOptions=function(a){var b,c;for(b in a)c=a[b],n.setOption(b,c)},n.unBindAll=function(a,b){var c,d,e;if(a&&("object"==typeof a||"function"==typeof a)&&(K(a)&&(a=G(a)),e=a._sb_map,a._sb_ID&&r[a._sb_ID].rAD(b),e))for(d in e)c=e[d],r[c].rAD(b)},window.SimplyBind=n}();