@danielkalen/simplybind
Version:
Magically simple, framework-less one-way/two-way data binding for frontend/backend in ~5kb.
2 lines (1 loc) • 17.4 kB
JavaScript
!function(){var t,u,F,x,P,Q,R,S,T,U,V,W,X,Y,Z,aa,ba,ca,da,ea,G,m,H,fa,y,n,z,A,B,g,ga,ha,v,ia,I,J,C,ja,l,K,ka,la,ma,D,E,L,M,N,O,p,q,w,na,r;return G={updateOnBind:!1},ha=0,y="push pop shift unshift splice reverse sort".split(" "),ia={},n={},L=["{{","}}"],p=Object.create({silent:!1,trackArrayChildren:!1},{placeholder:{get:function(){return L},set:function(a){g.iA(a)&&2===a.length&&(L=a,O())}}}),v={simpleSelector:!1,promiseTransforms:!1,dispatchEvents:!1,updateEvenIfSame:!1,updateOnBind:!0},A=null,B=function(){var a;return A||(a=A=document.createEvent("Event"),a.initEvent("change",!0,!1),a._sb=!0),A},ma=function(){},ja=function(){return""+ ++ha},l=function(){return Object.create(null)},K=function(a,b){return function(c,d,e){return m(c,d,e,a,b)}},q=function(a,b){return-1!==a.indexOf(b)},g={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 u},isSimpleObject:function(a){return g.iF(a)||g.iA(a)},iB:function(a){return a instanceof t},isI:function(a){return g.iO(a)&&g.iN(a.length)},doM:function(a){return a.nodeName&&1===a.nodeType},doI:function(a){return a=a.nodeName.toUpperCase(),"INPUT"===a||"TEXTAREA"===a||"SELECT"===a},doR:function(a){return"radio"===a.type},doC:function(a){return"checkbox"===a.type},eC:function(a){return a instanceof NodeList||a instanceof HTMLCollection||g.iD(jQuery)&&a instanceof jQuery},eAS:function(a){var b;return b=a[0].type,[].filter.call(a,function(a){return a.type===b}).length===a.length}},ga=function(a){var b,c;b=l();for(c in a)b[c]=a[c];return b},C=function(a,b){var c,d,e,f;for(f=Object.keys(b),c=0,e=f.length;c<e;c++)d=f[c],a[d]=b[d]},z={get:function(a,b,c,d){return b?n[a._sb_ID]:d&&a[0]._sb_map&&(b=n[a[0]._sb_map[c]],b.gB)?b.gB: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.se,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})))}},J=/[.*+?^${}()|[\]\\]/g,D=E=null,O=function(){var a,b,c;c=p.placeholder[0].replace(J,"\\$&"),a=p.placeholder[1].replace(J,"\\$&"),b="[^"+a+"]+",D=new RegExp(c+"("+b+")"+a,"g"),E=new RegExp(""+c+b+a,"g")},O(),fa=function(a,b,c){var d,e,f,h,g;for(g="",e=f=0,h=a.length;f<h;e=++f)d=a[e],g+=d,c[e]&&(g+=b[c[e]]);return g},H=function(a,b,c){null==a[c]&&(a[c]=[]),a[c].push(b)},N=function(a,b){var c,d,e,f,h,g,m,q,l,p,n;for(c=Array.prototype.slice.call(a.childNodes),e=0,h=c.length;e<h;e++)if(l=c[e],3!==l.nodeType)N(l,b);else if(l.textContent.match(E))if(n=l.textContent.split(D),3===n.length&&""===n[0]+n[2])H(b,l,n[1]);else{for(m=document.createDocumentFragment(),d=f=0,g=n.length;f<g;d=++f)p=n[d],q=m.appendChild(document.createTextNode(p)),d%2&&H(b,q,p);l.parentNode.replaceChild(m,l)}},w=function(a){throw Error("SimplyBind: "+(I[a]||a))},r=function(a,b){var c,d;p.silent||(c=ka(b),d=I[a],console.warn("SimplyBind: "+(d+("\n\n"+c))))},na=function(a){w("Invalid argument/s ("+a+")",!0)},ka=function(a){return(Error().stack||"").split("\n").slice(a+3).join("\n")},I={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",erOD:"You can only pass a single DOM element to a binding",erMX:"'checked' of Mixed list of element cannot be bound"},m=function(a,b,c,d,e){return(a||0===a)&&(g.iS(a)||g.iN(a)||g.iF(a)||a instanceof Array)||g.iBI(a)||w("erIP"),!g.iO(a)||a instanceof Array?new u(null,0,a,b,d,e,c):d?a.aP(d):a.sC(1)},m.version="1.11.0",m.settings=p,m.defaultOptions=v,m.unBindAll=function(a,b){var c,d,e;if(a&&(g.iO(a)||g.iF(a))&&(g.isI(a)&&!a._sb_ID&&a[0]&&g.doM(a[0])&&(a=a[0]),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)},t=function(a,b,c){return C(this,c),this.oD=this.so?this.options:v,this.type=b,this.object=a,this.ID=ja(),this.subs=[],this.sM=l(),this.pM=l(),this.atEV=[],"Array"!==this.type&&"Proxy"!==this.type||(this.sV=ma),this.mC&&(this.cH=l(),this.object.forEach(function(a){return function(b){var c;c=a.cH[b.value]=m("checked").of(b)._,c.aS(a,G),c.sM[a.ID].tF=function(){return c},c.gB=a}}(this))),"Event"===this.type||"Func"===this.type&&this.parentInterface||("Pholder"===this.type?(b=this.de&&"multi"!==this.de?this.de+":"+this.pr:this.pr,a=this.pB=m(b).of(a)._,a.sPH(),this.value=a.pVL[this.Ph],a.txN&&(this.txN=a.txN[this.Ph])):(this.value=a=this.fDV(),"ObjectProp"!==this.type||g.iD(a)||(this.object[this.pr]=a),this.cL())),this.aEV(),this.object instanceof Array&&"Array"!==this.type&&(this.aBI=a=z.get(this.object,!0))&&a.tC&&!q(a.tC,this.pr)&&(a.tC.push(this.pr),m(this.pr).of(this.object).to(a.uSL)),n[this.ID]=this},t.prototype={cL:function(a){var b,c,d,e,f;switch(b=this,this.type){case"ObjectProp":e=Object.getOwnPropertyDescriptor(this.object,this.pr)||ia,(a=(a=a||e.configurable)&&this.object.constructor!==CSSStyleDeclaration)&&(this.isL=!0,Object.defineProperty(this.object,this.pr,{configurable:!0,enumerable:e.enumerable,get:function(){return b.value},set:e.set?function(a){e.set(a),b.sV(a)}:function(a){b.sV(a)}}));break;case"Array":y.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}})}),p.trackArrayChildren&&!this.tC&&(this.tC=[],this.uSL=function(){return b.uAS(b)},this.value.forEach(function(a,c){return b.tC.push(""+c),m(c,G).of(b.value).to(b.uSL)}));break;case"Proxy":d=this.oR=this.value,c=this.object,this.value={result:null,args:null},g.iF(d)&&(f=[].slice,this.object[this.pr]=function(){var a;return a=f.call(arguments),b.value.args=a=b.tfS?b.tfS(a):a,b.value.result=a=d.apply(c,a),b.uAS(b),a})}},aS:function(a,b,c){var d,e,f,h;if(a.isMulti)for(f=a.bindings,a=0,e=f.length;a<e;a++)h=f[a],this.aS(h,b,c);else this.sM[a.ID]?d=!0:(a.pM[this.ID]=this,this.subs.unshift(a),e=this.sM[a.ID]=l(),e.uO=c,e.opts=ga(b),"Event"!==this.type&&"Proxy"!==this.type||(e.opts.updateEvenIfSame=!0),e.VR="Func"===a.type?"ps":"value");return d},rS:function(a,b){var c,d,e,f;if(a.isMulti)for(e=a.bindings,c=0,d=e.length;c<d;c++)f=e[c],this.rS(f,b);else this.sM[a.ID]&&(this.subs.splice(this.subs.indexOf(a),1),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.listen);delete this.object._sb_map}else if("Array"===this.type)for(delete this.object._sb_ID,a=0,b=y.length;a<b;a++)c=y[a],delete this.object[c];else"Func"!==this.type&&("ObjectProp"===this.type?Object.defineProperty(this.object,this.pr,{value:this.value,writable:!0}):"Proxy"===this.type&&(this.object[this.pr]=this.oR),delete this.object._sb_map),delete this.object._sb_ID},fDV:function(){var a,b,c,d,e;switch(e=this.type,!1){case"Func"!==e:return this.object();case"Array"!==e:return this.object;case"DOMAttr"!==e:return this.object.getAttribute(this.pr)||"";case!this.mC:d=[],c=this.cH;for(b in c)if(a=c[b],a.object.checked){if("DOMRadio"===e)return b;d.push(b)}return d;default:return this.object[this.pr]}},sV:function(a,b,c){var d,e,f,h,k;switch(b||(b=this),this.tfS&&(a=this.tfS(a)),this.type){case"ObjectProp":this.isL||a===this.value||c&&this.doM&&"textContent"===this.pr||(this.object[this.pr]=a);break;case"Pholder":if(f=this.pB,f.pVL[this.Ph]=a,d=fa(f.pCT,f.pVL,f.pIM),this.txN&&a!==this.value)for(h=this.txN,e=0,c=h.length;e<c;e++)k=h[e],k.textContent=a;f.sV(d,b,!0);break;case"Func":d=this.ps,"Array"===b.type&&a===b.value&&(a=a.slice()),this.ps=a,a=this.object(a,d);break;case"Event":c||(this.iE=!0,this.eE(a),this.iE=!1);break;case"DOMValue":a===this.value&&a===this.object.value||(this.object.value=a,p.dispatchEvents&&this.object.dispatchEvent(B()));break;case"DOMRadio":if(this.mC)if(f=g.iB(a)?a:this.cH[a])for(e in a=f.object.value,d=this.cH)c=d[e],c.sV(c.ID===f.ID,b);else a=this.value;else{if(a=!!a,a===this.value)return;this.object.checked!==a&&(this.object.checked=a),a&&p.dispatchEvents&&this.object.dispatchEvent(B())}break;case"DOMCheckbox":if(this.mC){for(e=!g.iB(a),a=[].concat(a),c=f=0,h=a.length;f<h;c=++f)k=a[c],a[c]=g.iB(k)?k:this.cH[k];h=[],k=this.cH;for(d in k)c=k[d],f=e?q(a,c):c.value,c.sV(f,b),f&&h.push(d);a=h}else{if(a=!!a,a===this.value)return;this.object.checked!==a&&(this.object.checked=a,p.dispatchEvents&&this.object.dispatchEvent(B()))}break;case"DOMAttr":this.object.setAttribute(this.pr,a)}this.value=a,this.uAS(b)},uAS:function(a){var b,c;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(c=(b=this.subs).length;c--;)this.uS(b[c],a)}},uS:function(a,b){var c,d,e,f;b===a||b!==this&&b.sM[a.ID]||(c=this.sM[a.ID],d=this.value,e=a[c.VR],d=(f=c.tF)?f(d,e,a.object):d,d===e&&!c.opts.updateEvenIfSame||c.cN&&!c.cN(d,e,a.object)||(c.opts.promiseTransforms&&d&&g.iF(d.then)?d.then(function(c){a.sV(c,b)}):a.sV(d,b),c.uO&&this.rS(a)))},aM:function(a,b,c,d){var e,f,h,k;if(g.iF(c)){for(e=0,f=b.length;e<f;e++)h=b[e],k=h._||h,k.isMulti?this.aM(a,k.bindings,c,d):(h=this.sM[k.ID],h[a]=c,d=d&&!h.uO,this.pM[k.ID]&&(k.sM[this.ID][a]=c),!d&&"Func"!==this.type||"tF"!==a||this.uS(k,this));return!0}return r("erFN",2)},ss:function(a,b){this.tfS=a,b&&this.sV(this.value)},sPH:function(){var a;this.pVL||(this.pVL=l(),this.pIM=l(),this.pCT=[],g.iS(this.value)&&(this.pCT=this.value.split(E),a=0,this.value=this.value.replace(D,function(b){return function(c,d){return b.pIM[a++]=d,b.pVL[d]=d}}(this))),this.doM&&"textContent"===this.pr&&N(this.object,this.txN=l()))},aPI:function(a){if("Event"!==this.type)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},aUV:function(a,b){this.object.addEventListener(a,function(a){return function(c){c._sb||a.sV(c.target[b])}}(this),!1)},aEV:function(){this.evN?this.rEVE(this.evN,this.cEM.listen):"DOMValue"===this.type?(this.aUV("input","value"),this.aUV("change","value")):this.mC||"DOMRadio"!==this.type&&"DOMCheckbox"!==this.type||this.aUV("change","checked")},rEVE:function(a,b){q(this.atEV,a)||(this.atEV.push(a),this.iEM(a,b||"addEventListener","addEventListener"))},urEVE:function(a,b){var c;c=this.atEV.indexOf(a),-1!==c&&(this.atEV.splice(c,1),this.iEM(a,b||"removeEventListener","removeEventListener"))},iEM:function(a,b,c){var d;d=this.object,(this.doM&&g.iD(jQuery)&&"on"===b||"off"===b)&&(d=jQuery(this.object)),d[b]||(b=c),this.evH||(this.evH=la.bind(this)),"function"==typeof d[b]&&d[b](a,this.evH)},eE:function(a){var b,c;return c=this.object,b=this.cEM.emit||"dispatchEvent",this.doM&&g.iD(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.bindingData=a,c[b](this.evO)):void c[b](this.evN,a)}},la=function(){var a;this.iE||(a="Event"===this.type?arguments[this.pr]:this.fDV(),this.sV(a,null,!0))},u=function(a,b,c,d,e,f,h){var k;if(a&&C(this,a),this.sG=b||0,null==this.subs&&(this.subs=[]),h&&(this.so=h),f&&(this.iSi=f),e&&(this.parentInterface=e),0===this.sG){this.oP=d||(d={}),this.options={};for(k in v)this.options[k]=null!=d[k]?d[k]:v[k];if(g.isSimpleObject(c))return this.sG=1,this.sS(c,!0);g.iN(c)&&(c=c.toString()),this.se=this.pr=c,this.options.simpleSelector||(q(c,":")&&(a=c.split(":"),this.de=a[0],this.pr=a[1]),q(c,".")&&(a=this.pr.split("."),this.pr=a[0],this.Ph=a.slice(1).join(".")),"event"===this.de&&(q(c,"#")?(a=this.pr.split("#"),this.evN=a[0],this.pr=a[1]):(this.evN=this.pr,this.pr=0),isNaN(parseInt(this.pr))&&r("erEV",1),this.cEM={listen:this.oP.listenMethod,emit:this.oP.emitMethod}))}return this},F={sC:function(a){return new u(this,a)},dM:function(a){return this._=a,Object.defineProperties(this,{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,d){var e;return this.object=a,(e=z.get(a,d,this.se,this.mC))?this.patchCachedBP(e):(a=new t(a,b,c),z.set(a,d),a)},patchCachedBP:function(a){var b,c,d,e;if("ObjectProp"!==a.type||this.pr in this.object||a.cL(!0),this.so)for(c in d=this.oP)e=d[c],a.oD[c]=e;c=a.oD;for(b in c)e=c[b],this.options[b]=g.iD(this.oP[b])?this.oP[b]:e;return a},sS:function(a,b){var c,d,e,f,h;if(h=(f=a!==window&&g.isI(a)&&!a.nodeType)?a[0]:a){if((this.doM=g.doM(h))&&("checked"===this.pr?(e=h&&g.doR(h),c=!e&&h&&g.doC(h)):"value"===this.pr&&(d=g.doI(h)),f&&"multi"!==this.de))if(1===a.length)a=a[0];else{if((e||c)&&!g.eAS(a))return r("erMX",3);e||c?(this.mC=!0,a=[].slice.call(a)):(a=a[0],r("erOD",3))}}else f&&g.eC(a)&&w("emptyList");switch(!1){case!b:c=g.iF(a)?"Func":"Array";break;case!this.Ph:c="Pholder";break;case"event"!==this.de:c="Event";break;case"func"!==this.de:c="Proxy";break;case!d:c="DOMValue";break;case!e:c="DOMRadio";break;case!c:c="DOMCheckbox";break;case"attr"!==this.de:c="DOMAttr";break;default:c="ObjectProp"}return"multi"===this.de?(a.length||w("emptyList"),this.dM(new x(this,a,c))):this.dM(this.createBP(a,c,this,b)),q(this._.type,"Event")||q(this._.type,"Proxy")?this.options.updateOnBind=!1:q(this._.type,"Func")&&(this.options.updateOnBind=!0),this.parentInterface?this.aP(this.parentInterface):this},aP:function(a){var b,c,d,e;if(this.iSi)a._.addBP(this._);else if(a.sG=2,a.subs.push(this),b=a._.aS(this._,a.options,a.uO),a.uO)delete a.uO;else if(a.options.updateOnBind&&!b)if(this._.isMulti)for(e=this._.bindings,c=0,d=e.length;c<d;c++)b=e[c],a._.uS(b,a._);else a._.uS(this._,a._);return a}},u.prototype=Object.create(F,{of:{get:function(){if(!this.sG)return T}},chainTo:{get:function(){if(this.sG)return Q}},set:{get:function(){if(this.sG)return W}},transformSelf:{get:function(){if(1===this.sG)return ca}},transform:{get:function(){if(2===this.sG)return aa}},transformAll:{get:function(){if(2===this.sG)return ba}},condition:{get:function(){if(2===this.sG)return R}},conditionAll:{get:function(){if(2===this.sG)return S}},bothWays:{get:function(){if(2===this.sG)return P}},unBind:{get:function(){if(2===this.sG)return da}},pollEvery:{get:function(){if(this.sG)return U}},stopPolling:{get:function(){if(this.sG)return Y}},updateSubsOnEvent:{get:function(){if(2===this.sG)return ea}},removeEvent:{get:function(){if(2===this.sG)return V}},throttle:{get:function(){if(this.sG)return Z}},setOption:{get:function(){if(2===this.sG)return X}},to:{get:function(){if(1===this.sG)return K(this)}},and:{get:function(){var a,b;return b=this.sC(1),2===this.sG?b:1===this.sG?(b._.isMulti||(a=b._,b._=new x(b),b._.addBP(a)),K(b,!0)):void 0}},once:{get:function(){var a;if(1===this.sG)return a=this.sC(1),a.uO=!0,a}},update:{get:function(){return this.set}},twoWay:{get:function(){return this.bothWays}},pipe:{get:function(){return this.chainTo}}}),T=function(a){return g.iO(a)||g.iF(a)||na(a),g.iBI(a)&&(a=a.object),this.sG=1,this.sS(a)},Q=function(a,b,c){return m(this.subs[this.subs.length-1]).to(a,b,c)},W=function(a){return this._.sV(a),this},ca=function(a){return g.iF(a)?this._.ss(a,this.options.updateOnBind):r("erFN",1),this},aa=function(a){return this._.aM("tF",this.subs.slice(-1),a,this.options.updateOnBind),this},ba=function(a){return this._.aM("tF",this.subs,a,this.options.updateOnBind),this},R=function(a){return this._.aM("cN",this.subs.slice(-1),a),this},S=function(a){return this._.aM("cN",this.subs,a),this},P=function(a){var b,c,d,e,f,h;for(d=this.subs[this.subs.length-1],h=d._,c=this._.isMulti?this._.bindings:[this._],h.aS(this._,d.options),d=0,e=c.length;d<e;d++)b=c[d],f=b.sM[h.ID].tF,b=b.sM[h.ID].cN,(f||a)&&(f=g.iF(a)?a:f)&&!1!==a&&(h.sM[this._.ID].tF=f),b&&(h.sM[this._.ID].cN=b);return this},da=function(a){var b,c,d,e;for(d=this.subs,b=0,c=d.length;b<c;b++)e=d[b],this._.rS(e._,a);return this},U=function(a){return this._.aPI(a),this},Y=function(){return this._.rPI(),this},ea=function(a,b){return this._.rEVE(a,b),this},V=function(a,b){return this._.urEVE(a,b),this},Z=function(a){return a&&g.iN(a)?this._.thR=a:!1===a&&delete this._.thR,this},X=function(a,b){return this._.sM[this.subs[this.subs.length-1]._.ID].opts[a]=b,this},x=function(a,b,c){var d,e,f;if(C(this,this.In=a),this.isMulti=!0,this.bindings=d=[],b)for(a=0,e=b.length;a<e;a++)f=b[a],this.addBP(f,c);return Object.defineProperties(this,{type:{get:function(){return d.map(function(a){return a.type})}},value:{get:function(){return d.map(function(a){return a.value})}},thR:{set:function(a){return d.forEach(function(b){return b.thR=a})}}})},M=x.prototype=Object.create(F),Object.keys(t.prototype).forEach(function(a){return M[a]=function(b,c,d,e){var f,g,k,l;for(l=this.bindings,g=0,k=l.length;g<k;g++)f=l[g],"uS"===a&&(c=f),f[a](b,c,d,e)}}),M.addBP=function(a,b){this.bindings.push(b?this.createBP(a,b,this.In):a)},window.SimplyBind=m}();