UNPKG

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