UNPKG

@danielkalen/simplybind

Version:

Magically simple, framework-less one-way/two-way data binding for frontend/backend in ~5kb.

2 lines (1 loc) 17.6 kB
!function(){var r,u,G,y,Q,R,S,T,U,V,W,X,Y,Z,aa,ba,ca,l,H,da,z,q,A,B,C,h,ea,fa,v,ga,I,J,ha,D,ia,p,w,K,ja,E,F,L,M,N,O,ka,n,m,x,la,t;return fa=0,z="push pop shift unshift splice reverse sort".split(" "),ga={},q={},L=["{{","}}"],n=Object.create({silent:!1,trackArrayChildren:!1},{placeholder:{get:function(){return L},set:function(a){h.iA(a)&&2===a.length&&(L=a,O())}}}),v={throttle:!1,simpleSelector:!1,promiseTransforms:!1,dispatchEvents:!1,updateEvenIfSame:!1,updateOnBind:!0},B=null,C=function(){var a;return B||(a=B=document.createEvent("Event"),a.initEvent("change",!0,!1),a._sb=!0),B},ka=function(a,b){return this.uAS(b||this)},ia=function(){return""+ ++fa},p=function(){return Object.create(null)},w=function(a,b){return function(c,d,e){return l(c,d,e,a,b)}},K=function(a,b){return a.sU||(a.sU=new r(function(){return b?a.sV(a.fDV(),a,!0):a.uAS(a)},"Func",{}))},m=function(a,b){return a&&-1!==a.indexOf(b)},h={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 h.iF(a)||h.iA(a)},iB:function(a){return a instanceof r},isI:function(a){return h.iO(a)&&h.iN(a.length)},DM:function(a){return a.nodeName&&1===a.nodeType},dI:function(a){return a=a.nodeName,"INPUT"===a||"TEXTAREA"===a||"SELECT"===a},dR:function(a){return"radio"===a.type},dC:function(a){return"checkbox"===a.type},eC:function(a){return a instanceof NodeList||a instanceof HTMLCollection||h.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}},ea=function(a){var b,c;b=p();for(c in a)b[c]=a[c];return b},D=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]},A={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.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,E=F=null,O=function(){var a,b,c;c=n.placeholder[0].replace(J,"\\$&"),a=n.placeholder[1].replace(J,"\\$&"),b="[^"+a+"]+",E=new RegExp(c+"("+b+")"+a,"g"),F=new RegExp(""+c+b+a,"g")},O(),da=function(a,b,c){var d,e,f,g,h;for(h="",e=f=0,g=a.length;f<g;e=++f)d=a[e],h+=d,c[e]&&(h+=b[c[e]]);return h},H=function(a,b,c){null==a[c]&&(a[c]=[]),a[c].push(b)},N=function(a,b){var c,d,e,f,g,h,P,m,l,p,n;for(c=Array.prototype.slice.call(a.childNodes),e=0,g=c.length;e<g;e++)if(l=c[e],3!==l.nodeType)N(l,b);else if(l.textContent.match(F))if(n=l.textContent.split(E),3===n.length&&""===n[0]+n[2])H(b,l,n[1]);else{for(P=document.createDocumentFragment(),d=f=0,h=n.length;f<h;d=++f)p=n[d],m=P.appendChild(document.createTextNode(p)),d%2&&H(b,m,p);l.parentNode.replaceChild(P,l)}},x=function(a){throw Error("SimplyBind: "+(I[a]||a))},t=function(a,b){var c,d;n.silent||(c=ja(b),d=I[a],console.warn("SimplyBind: "+(d+("\n\n"+c))))},la=function(a){x("Invalid argument/s ("+a+")",!0)},ja=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"},l=function(a,b,c,d,e){return(a||0===a)&&(h.iS(a)||h.iN(a)||h.iF(a)||a instanceof Array)||h.iBI(a)||x("erIP"),!h.iO(a)||a instanceof Array?new u(null,0,a,b,c,d,e):e?e(a):a.sC(1)},l.version="1.12.2",l.settings=n,l.defaultOptions=v,l.unBindAll=function(a,b){var c,d,e;if(a&&(h.iO(a)||h.iF(a))&&(h.isI(a)&&!a._sb_ID&&a[0]&&h.DM(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)},r=function(a,b,c){return D(this,c),this.oD=this.so?this.options:v,this.type=b,this.object=a,this.ID=ia(),this.subs=[],this.sM=p(),this.pM=p(),this.atEV=[],"Array"!==this.type&&"Proxy"!==this.type||(this.sV=ka),this.mC&&(this.cH=p(),this.object.forEach(function(a){return function(b){var c;c=a.cH[b.value]=l("checked").of(b)._,c.aS(a),c.sM[a.ID].tF=function(){return c},c.gB=a}}(this))),"Event"===this.type||"Func"===this.type&&this.IS||("Pholder"===this.type?(b=this.de&&!m(this.de,"multi")?this.de+":"+this.pr:this.pr,a=this.pB=l(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||h.iD(a)||(this.object[this.pr]=a),this.cL())),this.aEV(),this.object instanceof Array&&"Array"!==this.type&&(this.aBI=a=A.get(this.object,!0))&&a.tC&&!m(a.tC,this.pr)&&(a.tC.push(this.pr),this.aS(a.sU),this.uS(a.sU,this)),q[this.ID]=this},r.prototype={cL:function(a){var b,c,d,e,f,g,k;switch(b=this,c=this.object,this.type){case"ObjectProp":g=(this.OD=Object.getOwnPropertyDescriptor(c,this.pr))||ga,g.get&&(e=g.get.bind(c)),g.set&&(f=g.set.bind(c)),(a=(a=a||g.configurable)&&this.object.constructor!==CSSStyleDeclaration)&&Object.defineProperty(c,this.pr,{configurable:this.isL=!0,enumerable:g.enumerable,get:e||function(){return b.value},set:f?function(a){f(a),b.sV(a,b,!0)}:function(a){b.sV(a,b,!0)}});break;case"Array":z.forEach(function(a){return Object.defineProperty(c,a,{configurable:!0,value:function(){var d;return d=Array.prototype[a].apply(c,arguments),b.uAS(b),d}})}),n.trackArrayChildren&&!this.tC&&(this.tC=[],c.forEach(function(a,d){return b.tC.push(""+d),l(d).of(c)._.aS(K(b))}));break;case"Proxy":d=this.oR=this.value,this.value={result:null,args:null},h.iF(d)&&(k=[].slice,c[this.pr]=function(){var a;return a=k.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,d){var e,f,g,h;if(a.isMulti)for(g=a.bindings,a=0,f=g.length;a<f;a++)h=g[a],this.aS(h,b,c,d);else this.sM[a.ID]?e=!0:(a.pM[this.ID]=this,this.subs.unshift(a),f=this.sM[a.ID]=p(),f.uO=c,f.opts=ea(b),(d||"Event"===this.type||"Proxy"===this.type)&&(f.opts.updateEvenIfSame=!0),f.VR="Func"===a.type?"ps":"value");return e},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 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.listen);delete this.object._sb_map[this.pr]}else if("Array"===this.type)for(delete this.object._sb_ID,a=0,b=z.length;a<b;a++)c=z[a],delete this.object[c];else"Func"===this.type?delete this.object._sb_ID:("ObjectProp"===this.type&&this.isL?(a=this.OD,a.set||a.get||(a.value=this.value),Object.defineProperty(this.object,this.pr,a)):"Proxy"===this.type&&(this.object[this.pr]=this.oR),this.object._sb_map&&(delete this.object._sb_map[this.pr],0===Object.keys(this.object._sb_map).length&&delete this.object._sb_map))},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,g,k;if(b||(b=this),this.tfS&&(a=this.tfS(a)),!c)switch(this.type){case"ObjectProp":this.isL||a===this.value||(this.object[this.pr]=a);break;case"Pholder":if(f=this.pB,f.pVL[this.Ph]=a,d=da(f.pCT,f.pVL,f.pIM),this.txN&&a!==this.value)for(g=this.txN,e=0,c=g.length;e<c;e++)k=g[e],k.textContent=a;"textContent"!==this.pr&&f.sV(d,b);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":this.iE=!0,this.eE(a),this.iE=!1;break;case"DOMValue":a===this.value&&a===this.object.value||(this.object.value=a,n.dispatchEvents&&this.object.dispatchEvent(C()));break;case"DOMRadio":if(this.mC)if(f=h.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&&n.dispatchEvents&&this.object.dispatchEvent(C())}break;case"DOMCheckbox":if(this.mC){for(e=!h.iB(a),a=[].concat(a),c=f=0,g=a.length;f<g;c=++f)k=a[c],a[c]=h.iB(k)?k:this.cH[k];g=[],k=this.cH;for(d in k)c=k[d],f=e?m(a,c):c.value,c.sV(f,b),f&&g.push(d);a=g}else{if(a=!!a,a===this.value)return;this.object.checked!==a&&(this.object.checked=a,n.dispatchEvents&&this.object.dispatchEvent(C()))}break;case"DOMAttr":this.object.setAttribute(this.pr,a)}this.value=a,this.uAS(b)},uAS:function(a){var b,c;if(c=(b=this.subs).length)for(;c--;)this.uS(b[c],a)},uS:function(a,b){var c,d,e,f;if(!(b===a||b!==this&&b.sM[a.ID])){if(d=this.sM[a.ID],d.opts.throttle){if(c=+new Date,e=c-d.lU,e<d.opts.throttle)return clearTimeout(d.uT),d.uT=setTimeout(function(c){return function(){return c.uS(a,b)}}(this),d.opts.throttle-e);d.lU=c}c=this.value,e=a[d.VR],c=(f=d.tF)?f(c,e,a.object):c,c===e&&!d.opts.updateEvenIfSame||d.cN&&!d.cN(c,e,a.object)||(d.opts.promiseTransforms&&c&&h.iF(c.then)?c.then(function(c){a.sV(c,b)}):a.sV(c,b),d.uO&&this.rS(a))}},aM:function(a,b,c,d){var e,f,g,k;if(h.iF(c)){for(e=0,f=b.length;e<f;e++)g=b[e],k=g._||g,k.isMulti?this.aM(a,k.bindings,c,d):(g=this.sM[k.ID],g[a]=c,d=d&&!g.uO,this.pM[k.ID]&&(k.sM[this.ID][a]=c),!d&&"Func"!==this.type||"tF"!==a||this.uS(k,this));return!0}return t("erFN",2)},ss:function(a,b){this.tfS=a,b&&this.sV(this.value)},sPH:function(){var a;this.pVL||(this.pVL=p(),this.pIM=p(),this.pCT=[],h.iS(this.value)&&(this.pCT=this.value.split(F),a=0,this.value=this.value.replace(E,function(b){return function(c,d){return b.pIM[a++]=d,b.pVL[d]=d}}(this))),this.DM&&"textContent"===this.pr&&N(this.object,this.txN=p()))},aPI:function(a){if("Event"!==this.type)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},aUV:function(a,b){this.object.addEventListener(a,function(a){return function(c){var d;c._sb||((c=a.tfS&&a.dT)&&(d=a.object.selectionStart),a.sV(a.object[b],null,!c),c&&a.object.setSelectionRange(d,d))}}(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){this.atEV.push(a),this.iEM(a,b||"addEventListener","addEventListener")},urEVE:function(a,b){this.atEV.splice(this.atEV.indexOf(a),1),this.iEM(a,b||"removeEventListener","removeEventListener")},iEM:function(a,b,c){var d;d=this.object,(this.DM&&h.iD(jQuery)&&"on"===b||"off"===b)&&(d=jQuery(this.object)),d[b]||(b=c),this.evH||(this.evH=ha.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.DM&&h.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)}},ha=function(){this.iE||this.sV(arguments[this.pr],null,!0)},u=function(a,b,c,d,e,f,g){var k;if(a&&D(this,a),this.sG=b||0,null==this.subs&&(this.subs=[]),e&&(this.so=e),f&&(this.IS=f),g&&(this.cC=g),0===this.sG){this.oP=d||(d={}),this.options={};for(k in v)this.options[k]=null!=d[k]?d[k]:v[k];if(h.isSimpleObject(c))return this.sG=1,this.sS(c,!0);h.iN(c)&&(c=c.toString()),this.se=this.pr=c,this.options.simpleSelector||(m(c,":")&&(a=c.split(":"),this.de=a.slice(0,-1).join(":"),this.pr=a[a.length-1]),m(c,".")&&(a=this.pr.split("."),this.pr=a[0],this.Ph=a.slice(1).join(".")),m(this.de,"event")&&(m(c,"#")?(a=this.pr.split("#"),this.evN=a[0],this.pr=a[1]):(this.evN=this.pr,this.pr=0),isNaN(parseInt(this.pr))&&t("erEV",1),this.cEM={listen:this.oP.listenMethod,emit:this.oP.emitMethod}))}return this},G={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=A.get(a,d,this.se,this.mC))?this.patchCachedBP(e):(a=new r(a,b,c),A.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]=h.iD(this.oP[b])?this.oP[b]:e;return a},sS:function(a,b){var c,d,e,f,g;if(g=(f=a!==window&&h.isI(a)&&!a.nodeType)?a[0]:a){if((this.DM=h.DM(g))&&("checked"===this.pr?(e=g&&h.dR(g),c=!e&&g&&h.dC(g)):"value"===this.pr&&(this.dT=(d=h.dI(g))&&m(g.type,"text")),f&&!m(this.de,"multi")))if(1===a.length)a=a[0];else{if((e||c)&&!h.eAS(a))return t("erMX",3);e||c?(this.mC=!0,a=[].slice.call(a)):(a=a[0],t("erOD",3))}}else f&&h.eC(a)&&x("emptyList");switch(!1){case!b:c=h.iF(a)?"Func":"Array";break;case!this.Ph:c="Pholder";break;case!m(this.de,"event"):c="Event";break;case!m(this.de,"func"):c="Proxy";break;case!d:c="DOMValue";break;case!e:c="DOMRadio";break;case!c:c="DOMCheckbox";break;case!m(this.de,"attr"):c="DOMAttr";break;default:c="ObjectProp"}return m(this.de,"multi")?(a.length||x("emptyList"),this.dM(new y(this,a,c))):this.dM(this.createBP(a,c,this,b)),m(this._.type,"Event")||m(this._.type,"Proxy")?this.options.updateOnBind=!1:m(this._.type,"Func")&&(this.options.updateOnBind=!0),this.cC?this.cC(this):this},aP:function(a){var b,c,d,e;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._)}},u.prototype=Object.create(G,{of:{get:function(){if(!this.sG)return U}},set:{get:function(){if(this.sG)return W}},chainTo:{get:function(){if(2===this.sG)return R}},transformSelf:{get:function(){if(1===this.sG)return ba}},transform:{get:function(){if(2===this.sG)return Z}},transformAll:{get:function(){if(2===this.sG)return aa}},condition:{get:function(){if(2===this.sG)return S}},conditionAll:{get:function(){if(2===this.sG)return T}},bothWays:{get:function(){if(2===this.sG)return Q}},unBind:{get:function(){if(2===this.sG)return ca}},pollEvery:{get:function(){if(this.sG)return V}},stopPolling:{get:function(){if(this.sG)return Y}},setOption:{get:function(){if(2===this.sG)return X}},updateOn:{get:function(){var a;if(this.sG&&(a=this))return w(!1,function(b){return a._.pM[b._.ID]=b._,b._.aS(K(a._,!0),b.options,!1,!0),a})}},removeUpdater:{get:function(){var a,b;if(this.sG&&(b=this)&&(a=this._.sU))return w(!1,function(c){if(c._.sM[a.ID])return delete b._.pM[c._.ID],c._.rS(a)})}},to:{get:function(){var a;if(1===this.sG&&(a=this))return w(!0,function(b){return function(c){return c.aP(a),a}}(this))}},and:{get:function(){var a,b;return b=this.sC(1),2===this.sG?b:1===this.sG?(b._.isMulti||(a=b._,b._=b._=new y(b),b._.addBP(a)),w(!1,function(a){return b._.addBP(a._),b})):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 h.iO(a)||h.iF(a)||la(a),h.iBI(a)&&(a=a.object),this.sG=1,this.sS(a)},R=function(a,b,c){return l(this.subs[this.subs.length-1]).to(a,b,c)},W=function(a){return this._.sV(a),this},ba=function(a){return h.iF(a)?this._.ss(a,this.options.updateOnBind):t("erFN",1),this},Z=function(a){return this._.aM("tF",this.subs.slice(-1),a,this.options.updateOnBind),this},aa=function(a){return this._.aM("tF",this.subs,a,this.options.updateOnBind),this},S=function(a){return this._.aM("cN",this.subs.slice(-1),a),this},T=function(a){return this._.aM("cN",this.subs,a),this},Q=function(a){var b,c,d,e,f,g;for(d=this.subs[this.subs.length-1],g=d._,c=this._.isMulti?this._.bindings:[this._],g.aS(this._,d.options),d=0,e=c.length;d<e;d++)b=c[d],f=b.sM[g.ID].tF,b=b.sM[g.ID].cN,(f||a)&&(f=h.iF(a)?a:f)&&!1!==a&&(g.sM[this._.ID].tF=f),b&&(g.sM[this._.ID].cN=b);return this},ca=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},V=function(a){return this._.aPI(a),this},Y=function(){return this._.rPI(),this},X=function(a,b){return this._.sM[this.subs[this.subs.length-1]._.ID].opts[a]=b,this},y=function(a,b,c){var d,e,f;if(D(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})}}})},M=y.prototype=Object.create(G),Object.keys(r.prototype).forEach(function(a){return M[a]=function(b,c,d,e){var f,g,h,l;for(l=this.bindings,g=0,h=l.length;g<h;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=l}();