@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 w,t,E,F,N,O,P,Q,R,S,T,U,V,W,X,Y,Z,aa,ba,ca,da,ea,k,G,fa,v,y,q,z,ga,f,ha,H,I,A,ia,m,ja,J,ka,l,la,B,C,ma,K,D,L,x,M,u;return q={},l={silent:!1,liveProps:!0,dispatchEvents:!1,updateEvenIfSame:!1,updateOnBind:!0,mutateInherited:!1,trackArrayChildren:!1,simpleSelector:!1,promiseTransforms:!1,placeholder:["{{","}}"]},ga=function(){var a;return a=document.createEvent("Event"),a.initEvent("change",!0,!1),a._sb=!0,a},y="push pop shift unshift splice reverse sort".split(" "),ha={},ia=function(){return"sb_"+Math.floor(1e12*(1+Math.random())).toString(16)},m=function(){return Object.create(null)},J=function(a){return function(b,c){return k(b,c,a)}},ja=function(a){return function(b,c,d){return k(0,null,a).ofEvent(b,d,c)}},v=function(a,b){return-1!==a.indexOf(b)},f={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 t},iSO:function(a){return f.iF(a)||f.iA(a)},iB:function(a){return a instanceof w},isI:function(a){return f.iO(a)&&f.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||f.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}},D=function(a,b){var c,d;for(c in b)d=b[c],f.iD(l[c])&&(a.options[c]=d);a.mPL()},A=function(a,b){var c,d,e,h;for(h=Object.keys(b),c=0,e=h.length;c<e;c++)d=h[c],a[d]=b[d]},z={get:function(a,b,c,d){return b?q[a._sb_ID]:d&&a[0]._sb_map&&(b=q[a[0]._sb_map[c]],b.gB)?b.gB:a._sb_map&&a._sb_map[c]?q[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.selc,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})))}},I=/[.*+?^${}()|[\]\\]/g,B=C=null,L=function(){var a,b,c;c=l.placeholder[0].replace(I,"\\$&"),a=l.placeholder[1].replace(I,"\\$&"),b="[^"+a+"]+",B=new RegExp(c+"("+b+")"+a,"g"),C=new RegExp(""+c+b+a,"g")},L(),fa=function(a,b,c){var d,e,h,f,p;for(p="",h=e=0,f=a.length;e<f;h=++e)d=a[h],p+=d,c[h]&&(p+=b[c[h]]);return p},G=function(a,b,c){null==a[c]&&(a[c]=[]),a[c].push(b)},K=function(a,b){var c,d,e,h,f,p,n,k,r,m,l;for(c=Array.prototype.slice.call(a.childNodes),d=0,f=c.length;d<f;d++)if(r=c[d],3!==r.nodeType)K(r,b);else if(r.textContent.match(C))if(e=r.textContent,l=e.split(B),3===l.length&&""===l[0]+l[2])G(b,r,l[1]);else{for(n=document.createDocumentFragment(),e=h=0,p=l.length;h<p;e=++h)m=l[e],k=n.appendChild(document.createTextNode(m)),e%2&&G(b,k,m);r.parentNode.replaceChild(n,r)}},x=function(a){throw Error("SimplyBind: "+(H[a]||a))},u=function(a,b){var c,d;l.silent||(c=ka(b),d=H[a],console.warn("SimplyBind: "+(d+("\n\n"+c))))},M=function(a){x("Invalid argument/s ("+a+")",!0)},ka=function(a){return(Error().stack||"").split("\n").slice(a+3).join("\n")},H={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"},k=function(a,b,c){return(a||0===a)&&(f.iS(a)||f.iN(a)||f.iF(a)||a instanceof Array)||f.iBI(a)||x("erIP"),!f.iO(a)||a instanceof Array?new t(null,0,null,a,c,b):new t(a._,1)},Object.defineProperties(k,{version:{value:"1.6.0"},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){f.iD(l[a])&&(l[a]=b,"placeholder"===a&&L())},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&&(f.iO(a)||f.iF(a))&&(f.isI(a)&&!a._sb_ID&&a[0]&&f.doM(a[0])&&(a=a[0]),e=a._sb_map,a._sb_ID&&q[a._sb_ID].rAS(b),e))for(d in e)c=e[d],q[c].rAS(b)},w=function(a,b,c){return A(this,c),this.type=b,this.object=a,this.ID=ia(),this.subs=[],this.sM=m(),this.pM=m(),this.sP=m(),this.mPH=m(),this.tfms=m(),this.cnds=m(),this.atEV=[],"Event"!==this.type&&"Func"!==this.type||(this.options.updateOnBind=!1,this.options.updateEvenIfSame=!0),this.mC&&(this.cH=m(),this.object.forEach(function(a){return function(b){var c;c=a.cH[b.value]=k("checked").of(b)._,c.aS(a).aTF(a.ID,function(){return c}),c.gB=a}}(this))),"Event"===this.type||"Func"===this.type&&this.pI||(this.value=this.VLo=a=this.fDV(),"ObjectProp"!==this.type||f.iD(a)||(this.object[this.prop]=a),this.placeholder&&!this.pVL&&this.sPH(),this.mPL()),this.aEV(),this.object instanceof Array&&"Array"!==this.type&&(this.aBI=a=z.get(this.object,!0))&&a.options.trackArrayChildren&&!v(a.tC,this.prop)&&(a.tC.push(this.prop),k(this.prop).of(this.object).to(a.uSL)),q[this.ID]=this},w.prototype={mPL:function(a){var b,c,d;this.options.liveProps&&(b=this,"ObjectProp"===this.type?(d=Object.getOwnPropertyDescriptor(this.object,this.prop)||ha,(a=(a=a||!this.isL&&(d.configurable||this.options.mutateInherited))&&this.object.constructor!==CSSStyleDeclaration)&&(this.isL=!0,Object.defineProperty(this.object,this.prop,{configurable:!0,enumerable:d.enumerable,get:function(){return b.value},set:d.set?function(a){d.set(a),b.sV(a)}:function(a){b.sV(a)}}))):"Array"===this.type&&(this.isL||(this.isL=!0,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}})})),this.options.trackArrayChildren&&!this.tC&&(this.tC=[],this.uSL=function(){return b.uAS(b)},c={updateOnBind:!1},this.value.forEach(function(a,d){return b.tC.push(""+d),k(d,c).of(b.value).to(b.uSL)}))))},aS:function(a){var b,c,d;if(a.isMulti)for(c=a.bindings,a=0,b=c.length;a<b;a++)d=c[a],this.aS(d);else this.sM[a.ID]||(this.subs.push(a),this.sM[a.ID]=a,a.pM[this.ID]=this),this.placeholder?this.mPH[a.ID]=this.placeholder:this.mPH[a.ID]&&delete this.mPH[a.ID],a.placeholder&&(this.sP[a.ID]=a.placeholder);return this},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.sP[a.ID],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 q[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.remove);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.prop,{value:this.value,writable:!0}),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.prop)||"";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.prop]}},sV:function(a,b,c,d){var e,h,g,p,n,l,k;if(null==c&&(c=this),e=b?this.pVL[b]:this.value,this.tfS&&(a=this.tfS(a)),(a!==e||this.options.updateEvenIfSame)&&"Array"!==this.type){if(b){if(this.pVL[b]=a,this.txN&&(k=this.txN[b]))for(g=0,p=k.length;g<p;g++)l=k[g],l[this.prop]=a;a=fa(this.pCT,this.pVL,this.pIM)}switch(this.type){case"ObjectProp":this.isL||b&&this.doM&&"textContent"===this.prop||(this.object[this.prop]=a);break;case"Func":e=this.VLp,"Array"===c.type&&a===c.value&&(a=a.slice()),this.VLp=a,a=this.object(a,e);break;case"Event":d||(this.iE=!0,this.eE(a),this.iE=!1);break;case"DOMValue":this.object.value!==a&&(this.object.value=a,this.eCE());break;case"DOMRadio":if(this.mC)if(h=f.iB(a)?a:this.cH[a])for(n in a=h.object.value,b=this.cH)e=b[n],e.sV(e.ID===h.ID,null,c);else a=e;else a=!!a,this.object.checked!==a&&(this.object.checked=a),a&&this.eCE();break;case"DOMCheckbox":if(this.mC){for(n=!f.iB(a),a=[].concat(a),e=b=0,d=a.length;b<d;e=++b)g=a[e],a[e]=f.iB(g)?g:this.cH[g];d=[],g=this.cH;for(h in g)e=g[h],b=n?v(a,e):e.value,e.sV(b,null,c),b&&d.push(h);a=d}else a=!!a,this.object.checked!==a&&(this.object.checked=a,this.eCE());break;case"DOMAttr":this.object.setAttribute(this.prop,a)}this.value=a,this.uAS(c)}},uAS:function(a){var b,c,d,e;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(d=this.subs,b=0,c=d.length;b<c;b++)e=d[b],this.uS(e,a)}},uS:function(a,b){var c,d,e,h;b===a||b!==this&&b.sM[a.ID]||(c=this.mPH[a.ID],e=this.sP[a.ID],c=c?this.pVL[c]:this.value,h=e?a.pVL[e]:a.value,d=this.hasTransforms?this.apT(a,e,c,h):c,this.hCs&&!this.cC(a,e,c,h))||(this.options.promiseTransforms&&d&&f.iF(d.then)?d.then(function(c){a.sV(c,e,b)}):a.sV(d,e,b))},pTF:function(a,b){var c,d,e;if(f.iF(a)){for(c=0,d=b.length;c<d;c++)e=b[c],e=e._||e,e.isMulti?this.pTF(a,e.bindings):(this.aTF(e.ID,a),this.pM[e.ID]&&e.aTF(this.ID,a),(this.options.updateOnBind||"Func"===this.type)&&this.uS(e,this));return!0}return u("erFN",2)},apT:function(a,b,c,d){return this.tfms[a.ID]?this.tfms[a.ID](c,d):c},aTF:function(a,b){this.hasTransforms=!0,this.tfms[a]=b},pCN:function(a,b){var c,d,e;if(f.iF(a)){for(c=0,d=b.length;c<d;c++)e=b[c],e=e._||e,e.isMulti?this.pCN(a,e.bindings):(this.aCND(e.ID,a),this.pM[e.ID]&&e.aCND(this.ID,a));return!0}return u("erFN",2)},cC:function(a,b,c,d){return!this.cnds[a.ID]||this.cnds[a.ID](c,d)},aCND:function(a,b){this.hCs=!0,this.cnds[a]=b},sPH:function(){var a;this.pVL=m(),this.pIM=m(),this.pCT=[],f.iS(this.VLo)&&(this.pCT=this.VLo.split(C),a=0,this.value=this.VLo.replace(B,function(b){return function(c,d){return b.pIM[a++]=d,b.pVL[d]=d}}(this))),this.doM&&"textContent"===this.prop&&K(this.object,this.txN=m())},aPI:function(a){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)},eCE:function(){this.options.dispatchEvents&&this.object.dispatchEvent(ga())},aEV:function(){this.evN?this.rEVE(this.evN,this.cEM.in):"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){v(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&&f.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.out||"dispatchEvent",this.doM&&f.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.prop]:this.fDV(),this.sV(a,null,null,!0))},t=function(a,b,c,d,e,h){var g,p,n,k;switch(c&&A(this,c),this.sG=b||0,null==this.pX&&(this.pX=[]),e&&(this.pI=e),this.sG){case 0:this.opts=h||(h={}),this.options={};for(k in l)this.options[k]=null!=h[k]?h[k]:l[k];if(f.iSO(d))return this.iSO=!0,new t(null,1,this,d);f.iN(d)&&(d=d.toString()),this.selc=this.prop=d,this.options.simpleSelector||(v(this.selc,":")&&(d=this.prop.split(":"),this.desc=d[0],this.prop=d[1]),v(this.selc,".")&&(d=this.prop.split("."),this.prop=d[0],this.placeholder=d.slice(1).join(".")),this.selc=this.prop);break;case 1:if(!a){if(b=(a=d!==window&&f.isI(d))?d[0]:d){if((this.doM=f.doM(b))&&("checked"===this.prop?(n=b&&f.doR(b),g=!n&&b&&f.doC(b)):"value"===this.prop&&(p=f.doI(b)),a&&"multi"!==this.desc))if(1===d.length)d=d[0];else{if((n||g)&&!f.eAS(d))return u("erMX",3);n||g?(this.mC=!0,d=[].slice.call(d)):(d=d[0],u("erOD",3))}}else a&&f.eC(d)&&x("emptyList");switch(!1){case!this.hasEventName:g="Event";break;case!this.iSO:g=f.iF(d)?"Func":"Array";break;case!p:g="DOMValue";break;case!n:g="DOMRadio";break;case!g:g="DOMCheckbox";break;case"attr"!==this.desc:g="DOMAttr";break;default:g="ObjectProp"}"multi"===this.desc?(d.length||x("emptyList"),a=new F(d,g,this)):a=this.createBP(d,g)}}return a&&!this._&&(this.sOB(a),e=this.pI)?(e.pX.push(this),e._.aS(this._),e.sG=2,(e._.options.updateOnBind||"Func"===e._.type)&&e._.uS(this._,e._),e):this},E={sOB:function(a){return this._=a,Object.defineProperties(this,{ID:{get:function(){return a.ID}},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){var d;return this.object=a,(d=z.get(a,this.iSO,this.selc,this.mC))?this.patchCachedBP(d):(a=new w(a,b,c||this),z.set(a,this.iSO),a)},patchCachedBP:function(a){return(a.placeholder=this.placeholder)&&!a.pVL&&(a.VLo=a.fDV(),a.sPH()),"ObjectProp"!==a.type||this.prop in this.object||a.mPL(!0),D(a,this.opts),a}},t.prototype=Object.create(E,{of:{get:function(){if(!this.sG)return S}},ofEvent:{get:function(){if(!this.sG)return T}},chainTo:{get:function(){if(this.sG)return O}},set:{get:function(){if(this.sG)return W}},get:{get:function(){if(this.sG)return R}},transformSelf:{get:function(){if(1===this.sG&&"Array"!==this._.type)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 P}},conditionAll:{get:function(){if(2===this.sG)return Q}},bothWays:{get:function(){if(2===this.sG)return N}},unBind:{get:function(){if(2===this.sG)return da}},pollEvery:{get:function(){if(2===this.sG&&"Event"!==this._.type)return U}},stopPolling:{get:function(){if(2===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(){return X}},to:{get:function(){if(1===this.sG)return J(this)}},and:{get:function(){if(2===this.sG)return J(this)}},toEvent:{get:function(){if(1===this.sG)return ja(this)}},update:{get:function(){return this.set}},twoWay:{get:function(){return this.bothWays}},pipe:{get:function(){return this.chainTo}}}),S=function(a){return f.iO(a)||f.iF(a)||M(a),f.iBI(a)&&(a=a.object),new t(this._,1,this,a)},T=function(a,b,c){return a&&f.iS(a)?isNaN(parseInt(this.prop))&&u("erEV",1):M(a),this.hasEventName=!0,this.evN=a,this.selc=this.prop+"#"+this.evN,this.cEM={in:b,out:c},this},O=function(a,b){return k(this.pX[this.pX.length-1]).to(a,b)},W=function(a){return this._.sV(a,this.placeholder),this},R=function(){return this.placeholder?this._.pVL[this.placeholder]:this._.value},ca=function(a){return f.iF(a)?(this._.tfS=a,this._.options.updateOnBind&&(a=this._.isMulti?this.value[0]:this.value,this._.sV(a))):u("erFN",1),this},aa=function(a){return this._.pTF(a,this.pX.slice(-1)),this},ba=function(a){return this._.pTF(a,this.pX),this},P=function(a){return this._.pCN(a,this.pX.slice(-1)),this},Q=function(a){return this._.pCN(a,this.pX),this},N=function(a){var b,c,d;return b=this.pX[this.pX.length-1],d=b._.aS(this._),c=this._.tfms[b.ID],b=this._.cnds[b.ID],(c||a)&&(c=f.iF(a)?a:c)&&!1!==a&&d.aTF(this.ID,c),b&&d.aCND(this.ID,b),this},da=function(a){var b,c,d,e;for(e=this.pX,b=0,c=e.length;b<c;b++)d=e[b],this._.rS(d._,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&&f.iN(a)?this._.thR=a:!1===a&&delete this._.thR,this},X=function(a,b){var c,d,e,f,g;for(c={},c[""+a]=b,e=c,D(this._,e),g=this.pX,c=0,d=g.length;c<d;c++)f=g[c],D(f._,e);return this},F=function(a,b,c){var d,e,f,g;for(A(this,c),this.isMulti=!0,this.type=b,this.bindings=d=[],e=0,f=a.length;e<f;e++)g=a[e],d.push(this.createBP(g,b,c));return Object.defineProperties(this,{ID:{value:d[0].ID},options:{value:d[0].options},tfms:{value:d[0].tfms},cnds:{value:d[0].cnds},uS:{value:function(a){return d.forEach(function(b){return b.uS(a,b)})}},pVL:{get:function(){return d[0].pVL}},value:{get:function(){return d.map(function(a){return a.value})}},thR:{set:function(a){return d.forEach(function(b){return b.thR=a})}},tfS:{set:function(a){return d.forEach(function(b){return b.tfS=a})}}})},ma=F.prototype=Object.create(E),Object.keys(w.prototype).forEach(function(a){return ma[a]=function(b,c){var d,e,f,g;for(g=this.bindings,e=0,f=g.length;e<f;e++)d=g[e],d[a](b,c)}}),window.SimplyBind=k}();