@danielkalen/simplybind
Version:
Magically simple, framework-less one-way/two-way data binding for frontend/backend in ~5kb.
2 lines (1 loc) • 16.7 kB
JavaScript
!function(){var u,v,F,z,P,Q,R,S,T,U,V,W,X,Y,Z,aa,ba,n,ca,A,q,G,g,da,w,B,ea,x,r,fa,H,I,ga,C,J,ha,p,t,ia,ja,ka,K,L,M,N,O,la,y,m,D,ma,E;return ea=0,A="push pop shift unshift splice reverse sort".split(" "),fa={},q={},M=["{{","}}"],y=Object.create({silent:!1},{placeholder:{get:function(){return M},set:function(a){g.isArray(a)&&2===a.length&&(M=a,O())}}}),x={delay:!1,throttle:!1,simpleSelector:!1,promiseTransforms:!1,dispatchEvents:!1,sendArrayCopies:!1,updateEvenIfSame:!1,updateOnBind:!0},r=Object.defineProperty,ja=Object.getOwnPropertyDescriptor,la=function(a,b){return this.updateAllSubs(b||this)},ha=function(){return""+ ++ea},p=function(){return Object.create(null)},t=function(a,b){return function(c,d,e){return n(c,d,e,a,b)}},ia=function(a,b){return a.selfUpdater||(a.selfUpdater=new u(function(){return b?a.setValue(a.fetchDirectValue(),a,!0):a.updateAllSubs(a)},"Func",{}))},m=function(a,b){return a&&-1!==a.indexOf(b)},g={isDefined:function(a){return void 0!==a},isArray:function(a){return a instanceof Array},isObject:function(a){return"object"==typeof a&&a},isString:function(a){return"string"==typeof a},isNumber:function(a){return"number"==typeof a},isFunction:function(a){return"function"==typeof a},isBindingInterface:function(a){return a instanceof v}},J=function(a,b,c){var d;return(d=ja(a,b))?(c&&(d.configurable=!0),d):(a=Object.getPrototypeOf(a))?J(a,b,!0):void 0},w=function(a,b,c){var d,e,f,h,k,l;a.origDescriptor||(a.origDescriptor=J(b,a.property)),c?A.forEach(function(c){return r(b,c,{configurable:!0,value:function(){var d;return d=Array.prototype[c].apply(b,arguments),a.updateAllSubs(a),d}})}):"Proxy"===a.type?(e=a.origFn=a.value,a.value={result:null,args:null},g.isFunction(e)&&(l=[].slice,d=f=function(){var c;return c=l.call(arguments),a.value.args=c=a.selfTransform?a.selfTransform(c):c,a.value.result=c=e.apply(b,c),a.updateAllSubs(a),c},r(b,a.property,{configurable:a.isLiveProp=!0,get:function(){return d},set:function(b){g.isFunction(b)?b!==e&&(b!==f&&(e=a.origFn=b),d!==f&&(d=f)):d=b}}))):(c=a.origDescriptor||fa,c.get&&(a.origGetter=c.get.bind(b)),c.set&&(a.origSetter=c.set.bind(b)),(k=c.configurable)&&(k="Array"===a.type,h=!a.origSetter&&!k,r(b,a.property,{configurable:a.isLiveProp=!0,enumerable:c.enumerable,get:a.origGetter||function(){return a.value},set:function(b){a.setValue(b,a,h)}}),k&&w(a,b[a.property],!0)))},B=function(a,b,c){var d,e;if(c){for(e=[],a=0,c=A.length;a<c;a++)d=A[a],e.push(delete b[d]);return e}return c=a.origDescriptor,c.set||c.get||(c.value=a.origFn||a.value),r(b,a.property,c)},da=function(a){var b,c;b=p();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]},G={get:function(a,b,c,d){return b?q[a._sb_ID]:a._sb_map&&a._sb_map[c]?q[a._sb_map[c]]:void 0},set:function(a,b){var c,d;b?r(a.object,"_sb_ID",{configurable:!0,value:a.ID}):(d=a.selector,a.object._sb_map?a.object._sb_map[d]=a.ID:(c={},c[d]=a.ID,r(a.object,"_sb_map",{configurable:!0,value:c})))}},I=/[.*+?^${}()|[\]\\]/g,K=L=null,O=function(){var a,b,c;c=y.placeholder[0].replace(I,"\\$&"),a=y.placeholder[1].replace(I,"\\$&"),b="[^"+a+"]+",K=new RegExp(c+"("+b+")"+a,"g"),L=new RegExp(""+c+b+a,"g")},O(),ca=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},D=function(a){throw Error("SimplyBind: "+(H[a]||a))},E=function(a,b){var c,d;y.silent||(c=ka(b),d=H[a],console.warn("SimplyBind: "+(d+("\n\n"+c))))},ma=function(a){D("Invalid argument/s ("+a+")",!0)},ka=function(a){return(Error().stack||"").split("\n").slice(a+3).join("\n")},H={invalidParamName:"SimplyBind() and .to() only accept a function, an array, a bound object, a string, or a number.",fnOnly:"Only functions are allowed for .transform/.condition/All()",badEventArg:"Invalid argument number in .ofEvent()",emptyList:"Empty collection provided"},n=function(a,b,c,d,e){return(a||0===a)&&(g.isString(a)||g.isNumber(a)||g.isFunction(a)||a instanceof Array)||g.isBindingInterface(a)||D("invalidParamName"),!g.isObject(a)||a instanceof Array?(b=new v(b),b.saveOptions=c,b.isSub=d,b.completeCallback=e,a=g.isFunction(a)?b.setObject(a,!0):b.setProperty(a)):a=e?e(a):a.selfClone(),a},n.version="1.15.3",n.settings=y,n.defaultOptions=x,n.unBindAll=function(a,b){var c,d,e;if(a&&(g.isObject(a)||g.isFunction(a))&&(e=a._sb_map,a._sb_ID&&q[a._sb_ID].removeAllSubs(b),e))for(d in e)c=e[d],q[c].removeAllSubs(b)},u=function(a,b,c){return C(this,c),this.optionsDefault=this.saveOptions?this.options:x,this.type=b,this.object=a,this.ID=ha(),this.subs=[],this.subsMeta=p(),this.pubsMap=p(),this.attachedEvents=[],"Proxy"===this.type&&(this.setValue=la),"Event"===this.type||"Func"===this.type&&this.isSub||("Pholder"===this.type?(b=this.property,a=this.parentBinding=n(b).of(a)._,a.scanForPholders(),this.value=a.pholderValues[this.pholder]):(this.value=a=this.fetchDirectValue(),"ObjectProp"!==this.type||g.isDefined(a)||(this.object[this.property]=a),w(this,this.object))),this.attachEvents(),q[this.ID]=this},u.prototype={addSub:function(a,b,c,d){var e,f,h,g;if(a.isMulti)for(h=a.bindings,a=0,f=h.length;a<f;a++)g=h[a],this.addSub(g,b,c,d);else this.subsMeta[a.ID]?e=!0:(a.pubsMap[this.ID]=this,this.subs.unshift(a),f=this.subsMeta[a.ID]=p(),f.updateOnce=c,f.opts=da(b),(d||"Event"===this.type||"Proxy"===this.type||"Array"===this.type)&&(f.opts.updateEvenIfSame=!0),f.valueRef="Func"===a.type?"valuePassed":"value");return e},removeSub: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.removeSub(f,b);else this.subsMeta[a.ID]&&(this.subs.splice(this.subs.indexOf(a),1),delete this.subsMeta[a.ID],delete a.pubsMap[this.ID]),b&&(a.removeSub(this),delete this.pubsMap[a.ID]);0===this.subs.length&&0===Object.keys(this.pubsMap).length&&this.destroy()},removeAllSubs:function(a){var b,c,d,e;for(d=this.subs.slice(),b=0,c=d.length;b<c;b++)e=d[b],this.removeSub(e,a)},destroy:function(){var a,b,c,d;if(delete q[this.ID],this.removePollInterval(),"Event"===this.type)for(d=this.attachedEvents,b=0,c=d.length;b<c;b++)a=d[b],this.unRegisterEvent(a);else"Func"===this.type&&delete this.object._sb_ID;this.isLiveProp&&this.origDescriptor&&B(this,this.object),"Array"===this.type&&B(this,this.value,!0),this.object._sb_map&&(delete this.object._sb_map[this.selector],0===Object.keys(this.object._sb_map).length&&delete this.object._sb_map)},fetchDirectValue:function(){var a;switch(a=this.type,!1){case"Func"!==a:return this.object();default:return this.object[this.property]}},setValue:function(a,b,c,d){if(b||(b=this),this.selfTransform&&(a=this.selfTransform(a)),!c)switch(this.type){case"ObjectProp":this.isLiveProp?this.origSetter&&this.origSetter(a):a!==this.value&&(this.object[this.property]=a);break;case"Pholder":d=this.parentBinding,d.pholderValues[this.pholder]=a,c=ca(d.pholderContexts,d.pholderValues,d.pholderIndexMap),d.setValue(c,b);break;case"Array":a!==this.value&&(g.isArray(a)||(a=Array.prototype.concat(a)),B(this,this.value,!0),w(this,a=a.slice(),!0),this.origSetter&&this.origSetter(a));break;case"Func":c=this.valuePassed,this.valuePassed=a,a=this.object(a,c);break;case"Event":this.isEmitter=!0,this.emitEvent(a),this.isEmitter=!1}this.value=a,this.updateAllSubs(b)},updateAllSubs:function(a){var b,c;if(c=(b=this.subs).length)for(;c--;)this.updateSub(b[c],a)},updateSub:function(a,b,c){var d,e,f;if(!(b===a||b!==this&&b.subsMeta[a.ID]||(d=this.subsMeta[a.ID],d.disallowList&&d.disallowList[b.ID]))){if(d.opts.throttle){if(c=+new Date,e=c-d.lastUpdate,e<d.opts.throttle)return clearTimeout(d.updateTimer),d.updateTimer=setTimeout(function(c){return function(){return c.updateSub(a,b)}}(this),d.opts.throttle-e);d.lastUpdate=c}else if(d.opts.delay&&!c)return setTimeout(function(c){return function(){return c.updateSub(a,b,!0)}}(this),d.opts.delay);c="Array"===this.type&&d.opts.sendArrayCopies?this.value.slice():this.value,e=a[d.valueRef],c=(f=d.transformFn)?f(c,e,a.object):c,c===e&&!d.opts.updateEvenIfSame||d.conditionFn&&!d.conditionFn(c,e,a.object)||(d.opts.promiseTransforms&&c&&g.isFunction(c.then)?c.then(function(c){a.setValue(c,b)}):a.setValue(c,b),d.updateOnce&&this.removeSub(a))}},addModifierFn:function(a,b,c,d){var e,f,h,k,l;if(g.isFunction(c)){for(f=0,h=b.length;f<h;f++)k=b[f],l=k._||k,l.isMulti?this.addModifierFn(a,l.bindings,c,d):(k=this.subsMeta[l.ID],k[a]=c,d=d&&!k.updateOnce,this.pubsMap[l.ID]&&((e=l.subsMeta[this.ID])[a]||(e[a]=c)),!d&&"Func"!==this.type||"transformFn"!==a||this.updateSub(l,this));return!0}return E("fnOnly",2)},setSelfTransform:function(a,b){this.selfTransform=a,b&&this.setValue(this.value)},addDisallowRule:function(a,b){var c;(null!=(c=this.subsMeta[a.ID]).disallowList?c.disallowList:c.disallowList=p())[b.ID]=1},scanForPholders:function(){var a;this.pholderValues||(this.pholderValues=p(),this.pholderIndexMap=p(),this.pholderContexts=[],g.isString(this.value)&&(this.pholderContexts=this.value.split(L),a=0,this.value=this.value.replace(K,function(b){return function(c,d){return b.pholderIndexMap[a++]=d,b.pholderValues[d]=d}}(this))))},addPollInterval:function(a){if("Event"!==this.type)return this.removePollInterval(),this.pollInterval=setInterval(function(a){return function(){var b;return b=a.fetchDirectValue(),a.setValue(b)}}(this),a)},removePollInterval:function(){return clearInterval(this.pollInterval),this.pollInterval=null},attachEvents:function(){this.eventName&&this.registerEvent(this.eventName)},registerEvent:function(a){this.attachedEvents.push(a),this.eventHandler||(this.eventHandler=ga.bind(this)),this.object[this.eventMethods.listen](a,this.eventHandler)},unRegisterEvent:function(a){this.attachedEvents.splice(this.attachedEvents.indexOf(a),1),this.object[this.eventMethods.remove](a,this.eventHandler)},emitEvent:function(a){this.object[this.eventMethods.emit](this.eventName,a)}},ga=function(){this.isEmitter||this.setValue(arguments[this.property],null,!0)},v=function(a,b){var c;if(b)C(this,b),this.stage=1;else for(c in this.stage=0,this.subs=[],this.optionsPassed=a||(a={}),this.options={},x)this.options[c]=null!=a[c]?a[c]:x[c];return this},F={selfClone:function(){return new v(null,this)},defineMainProps: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})}}})},createBinding:function(a,b,c,d){var e;return this.object=a,(e=G.get(a,d,this.selector,this.isMultiChoice))?this.patchCachedBinding(e):(a=new u(a,b,c),G.set(a,d),a)},patchCachedBinding:function(a){var b,c,d,e;if("ObjectProp"!==a.type||this.property in this.object||w(a,this.object),this.saveOptions)for(c in d=this.optionsPassed)e=d[c],a.optionsDefault[c]=e;c=a.optionsDefault;for(b in c)e=c[b],this.options[b]=g.isDefined(this.optionsPassed[b])?this.optionsPassed[b]:e;return a},setProperty:function(a){var b;return g.isNumber(a)&&(a=a.toString()),this.selector=this.property=a,this.options.simpleSelector||(m(a,":")&&(b=a.split(":"),this.descriptor=b.slice(0,-1).join(":"),this.property=b[b.length-1]),m(a,".")&&(b=this.property.split("."),this.property=b[0],this.pholder=b.slice(1).join(".")),m(this.descriptor,"event")&&(m(a,"#")?(b=this.property.split("#"),this.eventName=b[0],this.property=b[1]):(this.eventName=this.property,this.property=0),isNaN(parseInt(this.property))&&E("badEventArg",1))),this},setObject:function(a,b){var c;switch(this.stage=1,!1){case!b:c="Func";break;case!this.pholder:c="Pholder";break;case!(m(this.descriptor,"array")&&g.isArray(a[this.property])):c="Array";break;case!m(this.descriptor,"event"):c="Event",this.eventMethods={listen:this.optionsPassed.listenMethod,remove:this.optionsPassed.removeMethod,emit:this.optionsPassed.emitMethod},a[this.eventMethods.listen]||(this.eventMethods.listen="on"),a[this.eventMethods.remove]||(this.eventMethods.remove="removeListener"),a[this.eventMethods.emit]||(this.eventMethods.emit="emit");break;case!m(this.descriptor,"func"):c="Proxy";break;default:c="ObjectProp"}return m(this.descriptor,"multi")?(a.length||D("emptyList"),this.defineMainProps(new z(this,a,c))):this.defineMainProps(this.createBinding(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.completeCallback?this.completeCallback(this):this},addToPublisher:function(a){var b,c,d,e;if(a.stage=2,a.subs.push(this),b=a._.addSub(this._,a.options,a.updateOnce),a.updateOnce)delete a.updateOnce;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._.updateSub(b,a._);else a._.updateSub(this._,a._)}},v.prototype=Object.create(F,{of:{get:function(){if(!this.stage)return T}},set:{get:function(){if(this.stage)return V}},chainTo:{get:function(){if(2===this.stage)return Q}},transformSelf:{get:function(){if(1===this.stage)return aa}},transform:{get:function(){if(2===this.stage)return Y}},transformAll:{get:function(){if(2===this.stage)return Z}},condition:{get:function(){if(2===this.stage)return R}},conditionAll:{get:function(){if(2===this.stage)return S}},bothWays:{get:function(){if(2===this.stage)return P}},unBind:{get:function(){if(2===this.stage)return ba}},pollEvery:{get:function(){if(this.stage)return U}},stopPolling:{get:function(){if(this.stage)return X}},setOption:{get:function(){if(2===this.stage)return W}},disallowFrom:{get:function(){var a;if(2===this.stage&&(a=this))return t(!1,function(b){return a._.addDisallowRule(a.subs[a.subs.length-1]._,b._),a})}},updateOn:{get:function(){var a;if(this.stage&&(a=this))return t(!1,function(b){return b._!==a._&&(a._.pubsMap[b._.ID]=b._,b._.addSub(ia(a._,!0),b.options,!1,!0)),a})}},removeUpdater:{get:function(){var a,b;if(this.stage&&(b=this)&&(a=this._.selfUpdater))return t(!1,function(c){c._.subsMeta[a.ID]&&(delete b._.pubsMap[c._.ID],c._.removeSub(a))})}},to:{get:function(){var a;if(1===this.stage&&(a=this))return t(!0,function(b){return b._!==a._&&b.addToPublisher(a),a})}},and:{get:function(){var a,b;return b=this.selfClone(),2===this.stage?b:1===this.stage?(b._.isMulti||(a=b._,b._=b._=new z(b),b._.addBinding(a)),t(!1,function(a){return b._.addBinding(a._),b})):void 0}},once:{get:function(){var a;if(1===this.stage)return a=this.selfClone(),a.updateOnce=!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.isObject(a)||g.isFunction(a)||ma(a),g.isBindingInterface(a)&&(a=a.object),this.stage=1,this.setObject(a)},Q=function(a,b,c){return n(this.subs[this.subs.length-1]).to(a,b,c)},V=function(a){return this._.setValue(a),this},aa=function(a){return g.isFunction(a)?this._.setSelfTransform(a,this.options.updateOnBind):E("fnOnly",1),this},Y=function(a){return this._.addModifierFn("transformFn",this.subs.slice(-1),a,this.options.updateOnBind),this},Z=function(a){return this._.addModifierFn("transformFn",this.subs,a,this.options.updateOnBind),this},R=function(a){return this._.addModifierFn("conditionFn",this.subs.slice(-1),a),this},S=function(a){return this._.addModifierFn("conditionFn",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.addSub(this._,d.options),d=0,e=c.length;d<e;d++)b=c[d],f=b.subsMeta[h.ID].transformFn,b=b.subsMeta[h.ID].conditionFn,(f||a)&&(f=g.isFunction(a)?a:f)&&!1!==a&&(h.subsMeta[this._.ID].transformFn=f),b&&(h.subsMeta[this._.ID].conditionFn=b);return this},ba=function(a){var b,c,d,e;for(d=this.subs,b=0,c=d.length;b<c;b++)e=d[b],this._.removeSub(e._,a);return this},U=function(a){return this._.addPollInterval(a),this},X=function(){return this._.removePollInterval(),this},W=function(a,b){return this._.subsMeta[this.subs[this.subs.length-1]._.ID].opts[a]=b,this},z=function(a,b,c){var d,e,f;if(a.selector=a.selector.slice(6),C(this,this.interface=a),this.isMulti=!0,this.bindings=d=[],b)for(a=0,e=b.length;a<e;a++)f=b[a],this.addBinding(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})}}})},N=z.prototype=Object.create(F),Object.keys(u.prototype).forEach(function(a){return N[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],"updateSub"===a&&(c=f),f[a](b,c,d,e)}}),N.addBinding=function(a,b){this.bindings.push(b?this.createBinding(a,b,this.interface):a)},null!=("undefined"!=typeof module&&null!==module?module.exports:void 0)?module.exports=n:"function"==typeof define&&define.amd?define(["simplybind"],function(){return n}):this.SimplyBind=n}();