UNPKG

@danielkalen/simplybind

Version:

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

2 lines (1 loc) 16.3 kB
!function(){var q,r,E,x,O,P,Q,R,S,T,U,V,W,X,Y,Z,aa,l,ba,y,m,F,g,ca,t,z,da,u,n,ea,G,H,fa,A,I,ga,p,v,ha,ia,ja,J,K,L,M,N,ka,w,h,B,la,C;return da=0,y="push pop shift unshift splice reverse sort".split(" "),ea={},m={},L=["{{","}}"],w=Object.create({silent:!1},{placeholder:{get:function(){return L},set:function(a){g.isArray(a)&&2===a.length&&(L=a,N())}}}),u={delay:!1,throttle:!1,simpleSelector:!1,promiseTransforms:!1,dispatchEvents:!1,sendArrayCopies:!1,updateEvenIfSame:!1,updateOnBind:!0},n=Object.defineProperty,ia=Object.getOwnPropertyDescriptor,ka=function(a,b){return this.updateAllSubs(b||this)},ga=function(){return""+ ++da},p=function(){return Object.create(null)},v=function(a,b){return function(c,d,e){return l(c,d,e,a,b)}},ha=function(a,b){return a.selfUpdater||(a.selfUpdater=new q(function(){return b?a.setValue(a.fetchDirectValue(),a,!0):a.updateAllSubs(a)},"Func",{}))},h=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 r}},I=function(a,b,c){var d;return(d=ia(a,b))?(c&&(d.configurable=!0),d):(a=Object.getPrototypeOf(a))?I(a,b,!0):void 0},t=function(a,b,c){var d,e,f,k,D,h;a.origDescriptor||(a.origDescriptor=I(b,a.property)),c?y.forEach(function(c){return n(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)&&(h=[].slice,d=f=function(){var c;return c=h.call(arguments),a.value.args=c=a.selfTransform?a.selfTransform(c):c,a.value.result=c=e.apply(b,c),a.updateAllSubs(a),c},n(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||ea,c.get&&(a.origGetter=c.get.bind(b)),c.set&&(a.origSetter=c.set.bind(b)),(D=c.configurable)&&(D="Array"===a.type,k=!a.origSetter&&!D,n(b,a.property,{configurable:a.isLiveProp=!0,enumerable:c.enumerable,get:a.origGetter||function(){return a.value},set:function(b){a.setValue(b,a,k)}}),D&&t(a,b[a.property],!0)))},z=function(a,b,c){var d,e;if(c){for(e=[],a=0,c=y.length;a<c;a++)d=y[a],e.push(delete b[d]);return e}return c=a.origDescriptor,c.set||c.get||(c.value=a.origFn||a.value),n(b,a.property,c)},ca=function(a){var b,c;b=p();for(c in a)b[c]=a[c];return b},A=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]},F={get:function(a,b,c,d){return b?m[a._sb_ID]:a._sb_map&&a._sb_map[c]?m[a._sb_map[c]]:void 0},set:function(a,b){var c,d;b?n(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,n(a.object,"_sb_map",{configurable:!0,value:c})))}},H=/[.*+?^${}()|[\]\\]/g,J=K=null,N=function(){var a,b,c;c=w.placeholder[0].replace(H,"\\$&"),a=w.placeholder[1].replace(H,"\\$&"),b="[^"+a+"]+",J=new RegExp(c+"("+b+")"+a,"g"),K=new RegExp(""+c+b+a,"g")},N(),ba=function(a,b,c){var d,e,f,k,g;for(g="",e=f=0,k=a.length;f<k;e=++f)d=a[e],g+=d,c[e]&&(g+=b[c[e]]);return g},B=function(a){throw Error("SimplyBind: "+(G[a]||a))},C=function(a,b){var c,d;w.silent||(c=ja(b),d=G[a],console.warn("SimplyBind: "+(d+("\n\n"+c))))},la=function(a){B("Invalid argument/s ("+a+")",!0)},ja=function(a){return(Error().stack||"").split("\n").slice(a+3).join("\n")},G={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"},l=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)||B("invalidParamName"),!g.isObject(a)||a instanceof Array?(b=new r(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},l.version="1.14.2",l.settings=w,l.defaultOptions=u,l.unBindAll=function(a,b){var c,d,e;if(a&&(g.isObject(a)||g.isFunction(a))&&(e=a._sb_map,a._sb_ID&&m[a._sb_ID].removeAllSubs(b),e))for(d in e)c=e[d],m[c].removeAllSubs(b)},q=function(a,b,c){return A(this,c),this.optionsDefault=this.saveOptions?this.options:u,this.type=b,this.object=a,this.ID=ga(),this.subs=[],this.subsMeta=p(),this.pubsMap=p(),this.attachedEvents=[],"Proxy"===this.type&&(this.setValue=ka),"Event"===this.type||"Func"===this.type&&this.isSub||("Pholder"===this.type?(b=this.property,a=this.parentBinding=l(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),t(this,this.object))),this.attachEvents(),m[this.ID]=this},q.prototype={addSub:function(a,b,c,d){var e,f,k,g;if(a.isMulti)for(k=a.bindings,a=0,f=k.length;a<f;a++)g=k[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=ca(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 m[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,this.customEventMethod.listen);else"Func"===this.type&&delete this.object._sb_ID;this.isLiveProp&&this.origDescriptor&&z(this,this.object),"Array"===this.type&&z(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=ba(d.pholderContexts,d.pholderValues,d.pholderIndexMap),d.setValue(c,b);break;case"Array":a!==this.value&&(g.isArray(a)||(a=Array.prototype.concat(a)),z(this,this.value,!0),t(this,a=a.slice(),!0));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])){if(d=this.subsMeta[a.ID],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,k,h;if(g.isFunction(c)){for(e=0,f=b.length;e<f;e++)k=b[e],h=k._||k,h.isMulti?this.addModifierFn(a,h.bindings,c,d):(k=this.subsMeta[h.ID],k[a]=c,d=d&&!k.updateOnce,this.pubsMap[h.ID]&&(h.subsMeta[this.ID][a]=c),!d&&"Func"!==this.type||"transformFn"!==a||this.updateSub(h,this));return!0}return C("fnOnly",2)},setSelfTransform:function(a,b){this.selfTransform=a,b&&this.setValue(this.value)},scanForPholders:function(){var a;this.pholderValues||(this.pholderValues=p(),this.pholderIndexMap=p(),this.pholderContexts=[],g.isString(this.value)&&(this.pholderContexts=this.value.split(K),a=0,this.value=this.value.replace(J,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,this.customEventMethod.listen)},registerEvent:function(a,b){this.attachedEvents.push(a),this.invokeEventMethod(a,b||"on","on")},unRegisterEvent:function(a,b){this.attachedEvents.splice(this.attachedEvents.indexOf(a),1),this.invokeEventMethod(a,b||"removeListener","removeListener")},invokeEventMethod:function(a,b,c){var d;d=this.object,d[b]||(b=c),this.eventHandler||(this.eventHandler=fa.bind(this)),"function"==typeof d[b]&&d[b](a,this.eventHandler)},emitEvent:function(a){var b,c;c=this.object,b=this.customEventMethod.emit||"emit",c[b]||(b="emit"),c[b](this.eventName,a)}},fa=function(){this.isEmitter||this.setValue(arguments[this.property],null,!0)},r=function(a,b){var c;if(b)A(this,b),this.stage=1;else for(c in this.stage=0,this.subs=[],this.optionsPassed=a||(a={}),this.options={},u)this.options[c]=null!=a[c]?a[c]:u[c];return this},E={selfClone:function(){return new r(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=F.get(a,d,this.selector,this.isMultiChoice))?this.patchCachedBinding(e):(a=new q(a,b,c),F.set(a,d),a)},patchCachedBinding:function(a){var b,c,d,e;if("ObjectProp"!==a.type||this.property in this.object||t(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||(h(a,":")&&(b=a.split(":"),this.descriptor=b.slice(0,-1).join(":"),this.property=b[b.length-1]),h(a,".")&&(b=this.property.split("."),this.property=b[0],this.pholder=b.slice(1).join(".")),h(this.descriptor,"event")&&(h(a,"#")?(b=this.property.split("#"),this.eventName=b[0],this.property=b[1]):(this.eventName=this.property,this.property=0),isNaN(parseInt(this.property))&&C("badEventArg",1),this.customEventMethod={listen:this.optionsPassed.listenMethod,emit:this.optionsPassed.emitMethod})),this},setObject:function(a,b){var c;switch(this.stage=1,!1){case!b:c="Func";break;case!this.pholder:c="Pholder";break;case!h(this.descriptor,"array"):c="Array";break;case!h(this.descriptor,"event"):c="Event";break;case!h(this.descriptor,"func"):c="Proxy";break;default:c="ObjectProp"}return h(this.descriptor,"multi")?(a.length||B("emptyList"),this.defineMainProps(new x(this,a,c))):this.defineMainProps(this.createBinding(a,c,this,b)),h(this._.type,"Event")||h(this._.type,"Proxy")?this.options.updateOnBind=!1:h(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._)}},r.prototype=Object.create(E,{of:{get:function(){if(!this.stage)return S}},set:{get:function(){if(this.stage)return U}},chainTo:{get:function(){if(2===this.stage)return P}},transformSelf:{get:function(){if(1===this.stage)return Z}},transform:{get:function(){if(2===this.stage)return X}},transformAll:{get:function(){if(2===this.stage)return Y}},condition:{get:function(){if(2===this.stage)return Q}},conditionAll:{get:function(){if(2===this.stage)return R}},bothWays:{get:function(){if(2===this.stage)return O}},unBind:{get:function(){if(2===this.stage)return aa}},pollEvery:{get:function(){if(this.stage)return T}},stopPolling:{get:function(){if(this.stage)return W}},setOption:{get:function(){if(2===this.stage)return V}},updateOn:{get:function(){var a;if(this.stage&&(a=this))return v(!1,function(b){return b._!==a._&&(a._.pubsMap[b._.ID]=b._,b._.addSub(ha(a._,!0),b.options,!1,!0)),a})}},removeUpdater:{get:function(){var a,b;if(this.stage&&(b=this)&&(a=this._.selfUpdater))return v(!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 v(!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 x(b),b._.addBinding(a)),v(!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}}}),S=function(a){return g.isObject(a)||g.isFunction(a)||la(a),g.isBindingInterface(a)&&(a=a.object),this.stage=1,this.setObject(a)},P=function(a,b,c){return l(this.subs[this.subs.length-1]).to(a,b,c)},U=function(a){return this._.setValue(a),this},Z=function(a){return g.isFunction(a)?this._.setSelfTransform(a,this.options.updateOnBind):C("fnOnly",1),this},X=function(a){return this._.addModifierFn("transformFn",this.subs.slice(-1),a,this.options.updateOnBind),this},Y=function(a){return this._.addModifierFn("transformFn",this.subs,a,this.options.updateOnBind),this},Q=function(a){return this._.addModifierFn("conditionFn",this.subs.slice(-1),a),this},R=function(a){return this._.addModifierFn("conditionFn",this.subs,a),this},O=function(a){var b,c,d,e,f,k;for(d=this.subs[this.subs.length-1],k=d._,c=this._.isMulti?this._.bindings:[this._],k.addSub(this._,d.options),d=0,e=c.length;d<e;d++)b=c[d],f=b.subsMeta[k.ID].transformFn,b=b.subsMeta[k.ID].conditionFn,(f||a)&&(f=g.isFunction(a)?a:f)&&!1!==a&&(k.subsMeta[this._.ID].transformFn=f),b&&(k.subsMeta[this._.ID].conditionFn=b);return this},aa=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},T=function(a){return this._.addPollInterval(a),this},W=function(){return this._.removePollInterval(),this},V=function(a,b){return this._.subsMeta[this.subs[this.subs.length-1]._.ID].opts[a]=b,this},x=function(a,b,c){var d,e,f;if(a.selector=a.selector.slice(6),A(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})}}})},M=x.prototype=Object.create(E),Object.keys(q.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],"updateSub"===a&&(c=f),f[a](b,c,d,e)}}),M.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=l:"function"==typeof define&&define.amd?define(["simplybind"],function(){return l}):this.SimplyBind=l}();