@danielkalen/simplybind
Version:
Magically simple, framework-less one-way/two-way data binding for frontend/backend in ~5kb.
2 lines (1 loc) • 18.2 kB
JavaScript
!function(){var r,t,E,w,P,Q,R,S,T,U,V,W,X,Y,Z,aa,ba,ca,da,ea,fa,ga,F,k,G,ha,x,l,y,z,A,g,ia,ja,u,ka,H,I,B,la,n,ma,J,na,oa,pa,C,D,K,L,M,N,m,p,v,O,q;return F={updateOnBind:!1},ja=0,x="push pop shift unshift splice reverse sort".split(" "),ka={},l={},K=["{{","}}"],m=Object.create({silent:!1,trackArrayChildren:!1},{placeholder:{get:function(){return K},set:function(a){g.iA(a)&&2===a.length&&(K=a,N())}}}),u={simpleSelector:!1,promiseTransforms:!1,dispatchEvents:!1,updateEvenIfSame:!1,updateOnBind:!0},z=null,A=function(){var a;return z||(a=z=document.createEvent("Event"),a.initEvent("change",!0,!1),a._sb=!0),z},pa=function(){},la=function(){return""+ ++ja},n=function(){return Object.create(null)},J=function(a,b){return function(c,d,e){return k(c,d,e,a,b)}},ma=function(a,b){return function(c,d,e){return k(0,null,!1,a,b).ofEvent(c,e,d)}},p=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 t},isSimpleObject:function(a){return g.iF(a)||g.iA(a)},iB:function(a){return a instanceof r},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}},ia=function(a){var b,c;b=n();for(c in a)b[c]=a[c];return b},B=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]},y={get:function(a,b,c,d){return b?l[a._sb_ID]:d&&a[0]._sb_map&&(b=l[a[0]._sb_map[c]],b.gB)?b.gB:a._sb_map&&a._sb_map[c]?l[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,C=D=null,N=function(){var a,b,c;c=m.placeholder[0].replace(I,"\\$&"),a=m.placeholder[1].replace(I,"\\$&"),b="[^"+a+"]+",C=new RegExp(c+"("+b+")"+a,"g"),D=new RegExp(""+c+b+a,"g")},N(),ha=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},G=function(a,b,c){null==a[c]&&(a[c]=[]),a[c].push(b)},M=function(a,b){var c,d,e,f,h,g,k,n,m,p,l;for(c=Array.prototype.slice.call(a.childNodes),e=0,h=c.length;e<h;e++)if(m=c[e],3!==m.nodeType)M(m,b);else if(m.textContent.match(D))if(d=m.textContent,l=d.split(C),3===l.length&&""===l[0]+l[2])G(b,m,l[1]);else{for(k=document.createDocumentFragment(),d=f=0,g=l.length;f<g;d=++f)p=l[d],n=k.appendChild(document.createTextNode(p)),d%2&&G(b,n,p);m.parentNode.replaceChild(k,m)}},v=function(a){throw Error("SimplyBind: "+(H[a]||a))},q=function(a,b){var c,d;m.silent||(c=na(b),d=H[a],console.warn("SimplyBind: "+(d+("\n\n"+c))))},O=function(a){v("Invalid argument/s ("+a+")",!0)},na=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,d,e){return(a||0===a)&&(g.iS(a)||g.iN(a)||g.iF(a)||a instanceof Array)||g.iBI(a)||v("erIP"),!g.iO(a)||a instanceof Array?new t(null,0,a,b,d,e,c):d?a.aP(d):a.sC(1)},k.version="1.9.0",k.settings=m,k.defaultOptions=u,k.func=function(a,b){var c;return c=k(a,b),c.iP=!0,c},k.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&&l[a._sb_ID].rAS(b),e))for(d in e)c=e[d],l[c].rAS(b)},r=function(a,b,c){return B(this,c),this.oD=this.so?this.options:u,this.type=b,this.object=a,this.ID=la(),this.subs=[],this.sO=n(),this.pM=n(),this.sP=n(),this.mPH=n(),this.atEV=[],"Array"!==this.type&&"Proxy"!==this.type||(this.sV=pa),this.mC&&(this.cH=n(),this.object.forEach(function(a){return function(b){var c;c=a.cH[b.value]=k("checked").of(b)._,c.aS(a),c.SM(a.ID,"tfms",function(){return c}),c.gB=a}}(this))),"Event"===this.type||"Func"===this.type&&this.parentInterface||(this.value=this.VLo=a=this.fDV(),"ObjectProp"!==this.type||g.iD(a)||(this.object[this.prop]=a),this.placeholder&&!this.pVL&&this.sPH(),this.cL()),this.aEV(),this.object instanceof Array&&"Array"!==this.type&&(this.aBI=a=y.get(this.object,!0))&&a.tC&&!p(a.tC,this.prop)&&(a.tC.push(this.prop),k(this.prop).of(this.object).to(a.uSL)),l[this.ID]=this},r.prototype={cL:function(a){var b,c,d,e,f;switch(b=this,this.type){case"ObjectProp":e=Object.getOwnPropertyDescriptor(this.object,this.prop)||ka,(a=(a=a||e.configurable)&&this.object.constructor!==CSSStyleDeclaration)&&(this.isL=!0,Object.defineProperty(this.object,this.prop,{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":x.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}})}),m.trackArrayChildren&&!this.tC&&(this.tC=[],this.uSL=function(){return b.uAS(b)},this.value.forEach(function(a,c){return b.tC.push(""+c),k(c,F).of(b.value).to(b.uSL)}));break;case"Proxy":d=this.original=this.value,c=this.object,this.value={result:null,args:null},g.iF(d)&&(f=[].slice,this.object[this.prop]=function(){var a;return b.value.args=a=f.call(arguments),b.value.result=a=d.apply(c,a),b.uAS(b),a})}},aS:function(a,b,c){var d,e,f,h,g;if(null==b&&(b=this.oD),a.isMulti)for(h=a.bindings,e=0,f=h.length;e<f;e++)g=h[e],this.aS(g,b,c);else this.sO[a.ID]?d=!this.mPH[a.ID]:(this.subs.unshift(a),this.sO[a.ID]=ia(b),"Func"!==a.type&&"Event"!==a.type||(this.sO[a.ID].updateEvenIfSame=!0),a.pM[this.ID]=this,c&&k("value",F).of(a).to(function(b){return function(){return b.rS(a)}}(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 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.sO[a.ID]&&(this.subs.splice(this.subs.indexOf(a),1),delete this.sP[a.ID],delete this.sO[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 l[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=x.length;a<b;a++)c=x[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,f,h,k,l,n;if(c||(c=this),this.tfS&&(a=this.tfS(a)),b){if(f=this.pVL[b],this.pVL[b]=a,this.txN&&a!==f&&(k=this.txN[b]))for(f=0,h=k.length;f<h;f++)n=k[f],n[this.prop]=a;a=ha(this.pCT,this.pVL,this.pIM)}switch(this.type){case"ObjectProp":this.isL||a===this.value||b&&this.doM&&"textContent"===this.prop||(this.object[this.prop]=a);break;case"Func":f=this.VLp,"Array"===c.type&&a===c.value&&(a=a.slice()),this.VLp=a,a=this.object(a,f);break;case"Event":d||(this.iE=!0,this.eE(a),this.iE=!1);break;case"DOMValue":a===this.value&&a===this.object.value||(this.object.value=a,m.dispatchEvents&&this.object.dispatchEvent(A()));break;case"DOMRadio":if(this.mC)if(d=g.iB(a)?a:this.cH[a])for(l in a=d.object.value,e=this.cH)b=e[l],b.sV(b.ID===d.ID,null,c);else a=this.value;else{if(a=!!a,a===this.value)return;this.object.checked!==a&&(this.object.checked=a),a&&m.dispatchEvents&&this.object.dispatchEvent(A())}break;case"DOMCheckbox":if(this.mC){for(l=!g.iB(a),a=[].concat(a),b=d=0,k=a.length;d<k;b=++d)f=a[b],a[b]=g.iB(f)?f:this.cH[f];k=[],f=this.cH;for(e in f)b=f[e],d=l?p(a,b):b.value,b.sV(d,null,c),d&&k.push(e);a=k}else{if(a=!!a,a===this.value)return;this.object.checked!==a&&(this.object.checked=a,m.dispatchEvents&&this.object.dispatchEvent(A()))}break;case"DOMAttr":this.object.setAttribute(this.prop,a)}this.value=a,this.uAS(c)},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.sO[a.ID]||(e=this.sO[a.ID],d=this.placeholder&&this.mPH[a.ID],f=a.placeholder&&this.sP[a.ID],c=d?this.pVL[d]:this.value,d=f?a.pVL[f]:a.value,c=this.tfms&&this.tfms[a.ID]?this.tfms[a.ID](c,d,a.object):c,c===d&&!e.updateEvenIfSame||this.cnds&&this.cnds[a.ID]&&!this.cnds[a.ID](c,d,a.object)||(e.promiseTransforms&&c&&g.iF(c.then)?c.then(function(c){a.sV(c,f,b)}):a.sV(c,f,b)))},aM:function(a,b,c,d){var e,f,h;if(g.iF(c)){for(e=0,f=b.length;e<f;e++)h=b[e],h=h._||h,h.isMulti?this.aM(a,h.bindings,c):(this.SM(h.ID,a,c),this.pM[h.ID]&&h.SM(this.ID,a,c),!d&&"Func"!==this.type||"tfms"!==a||this.uS(h,this));return!0}return q("erFN",2)},SM:function(a,b,c){null==this[b]&&(this[b]=n()),this[b][a]=c},ss:function(a,b){"Array"!==this.type&&(this.tfS=a,b&&this.sV(this.value))},sPH:function(){var a;this.pVL=n(),this.pIM=n(),this.pCT=[],g.iS(this.VLo)&&(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))),this.doM&&"textContent"===this.prop&&M(this.object,this.txN=n())},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.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){p(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=oa.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&&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)}},oa=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,f,h){var k;if(a&&B(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 u)this.options[k]=null!=d[k]?d[k]:u[k];if(g.isSimpleObject(c))return this.sG=1,this.sS(c,!0);g.iN(c)&&(c=c.toString()),this.selc=this.prop=c,this.options.simpleSelector||(p(this.selc,":")&&(a=this.prop.split(":"),this.desc=a[0],this.prop=a[1]),p(this.selc,".")&&(a=this.prop.split("."),this.prop=a[0],this.placeholder=a.slice(1).join(".")),this.selc=this.prop)}return this},E={sC:function(a){return new t(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=y.get(a,d,this.selc,this.mC))?this.patchCachedBP(e):(a=new r(a,b,c),y.set(a,d),a)},patchCachedBP:function(a){var b,c,d,e;if((a.placeholder=this.placeholder)&&!a.pVL&&(a.VLo=a.fDV(),a.sPH()),"ObjectProp"!==a.type||this.prop 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.prop?(e=h&&g.doR(h),c=!e&&h&&g.doC(h)):"value"===this.prop&&(d=g.doI(h)),f&&"multi"!==this.desc))if(1===a.length)a=a[0];else{if((e||c)&&!g.eAS(a))return q("erMX",3);e||c?(this.mC=!0,a=[].slice.call(a)):(a=a[0],q("erOD",3))}}else f&&g.eC(a)&&v("emptyList");switch(!1){case!b:c=g.iF(a)?"Func":"Array";break;case!this.evN:c="Event";break;case!this.iP:c="Proxy";break;case!d:c="DOMValue";break;case!e:c="DOMRadio";break;case!c:c="DOMCheckbox";break;case"attr"!==this.desc:c="DOMAttr";break;default:c="ObjectProp"}return"multi"===this.desc?(a.length||v("emptyList"),this.dM(new w(this,a,c))):this.dM(this.createBP(a,c,this,b)),p(this._.type,"Event")||p(this._.type,"Proxy")?this.options.updateOnBind=!1:p(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}},t.prototype=Object.create(E,{of:{get:function(){if(!this.sG)return U}},ofEvent:{get:function(){if(!this.sG)return V}},chainTo:{get:function(){if(this.sG)return Q}},set:{get:function(){if(this.sG)return Y}},get:{get:function(){if(this.sG)return T}},transformSelf:{get:function(){if(1===this.sG)return ea}},transform:{get:function(){if(2===this.sG)return ca}},transformAll:{get:function(){if(2===this.sG)return da}},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 fa}},pollEvery:{get:function(){if(2===this.sG)return W}},stopPolling:{get:function(){if(2===this.sG)return aa}},updateSubsOnEvent:{get:function(){if(2===this.sG)return ga}},removeEvent:{get:function(){if(2===this.sG)return X}},throttle:{get:function(){if(this.sG)return ba}},setOption:{get:function(){if(2===this.sG)return Z}},to:{get:function(){if(1===this.sG)return J(this)}},toEvent:{get:function(){if(1===this.sG)return ma(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 w(b),b._.addBP(a)),J(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}}}),U=function(a){return g.iO(a)||g.iF(a)||O(a),g.iBI(a)&&(a=a.object),this.sG=1,this.sS(a)},V=function(a,b,c){return a&&g.iS(a)?isNaN(parseInt(this.prop))&&q("erEV",1):O(a),this.evN=a,this.selc=this.prop+"#"+this.evN,this.cEM={in:b,out:c},this},Q=function(a,b,c){return k(this.subs[this.subs.length-1]).to(a,b,c)},Y=function(a){return this._.sV(a,this.placeholder),this},T=function(){return!this.placeholder||this._.isMulti?this._.value:this._.pVL[this.placeholder]},ea=function(a){return g.iF(a)?this._.ss(a,this.options.updateOnBind):q("erFN",1),this},ca=function(a){return this._.aM("tfms",this.subs.slice(-1),a,this.options.updateOnBind),this},da=function(a){return this._.aM("tfms",this.subs,a,this.options.updateOnBind),this},R=function(a){return this._.aM("cnds",this.subs.slice(-1),a),this},S=function(a){return this._.aM("cnds",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.tfms&&b.tfms[h.ID],b=b.cnds&&b.cnds[h.ID],(f||a)&&(f=g.iF(a)?a:f)&&!1!==a&&h.SM(this._.ID,"tfms",f),b&&h.SM(this._.ID,"cnds",b);return this},fa=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},W=function(a){return this._.aPI(a),this},aa=function(){return this._.rPI(),this},ga=function(a,b){return this._.rEVE(a,b),this},X=function(a,b){return this._.urEVE(a,b),this},ba=function(a){return a&&g.iN(a)?this._.thR=a:!1===a&&delete this._.thR,this},Z=function(a,b){return this._.sO[this.subs[this.subs.length-1]._.ID][a]=b,this},w=function(a,b,c){var d,e,f;if(B(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.placeholder?a.pVL[a.placeholder]:a.value})}},thR:{set:function(a){return d.forEach(function(b){return b.thR=a})}}})},L=w.prototype=Object.create(E),Object.keys(r.prototype).forEach(function(a){return L[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)}}),L.addBP=function(a,b){this.bindings.push(b?this.createBP(a,b,this.In):a)},window.SimplyBind=k}();