-
Notifications
You must be signed in to change notification settings - Fork 0
/
octobinder.min.js
1 lines (1 loc) · 4.91 KB
/
octobinder.min.js
1
angular.module("OctoBinder",[]).factory("obBinder",["$parse","$q","obBinderTypes","obModelWriter","obObserver",function(a,b,c,d,e){function f(a,b,c,d){if(d=d||{},!c)throw new Error("protocol is required");if(!a)throw new Error("scope is required");if(!b)throw new Error("model is required");if(d.key&&"string"!=typeof d.key)throw new Error("key must be a string");this.protocol=c,this.scope=a,this.model=b,this.query=d.query,this.type=d.type,this.key=d.key,this.ignoreNModelChanges=0,this.ignoreNProtocolChanges=0,this.bindModel(this.type,a,b),this.protocol.subscribe(this)}return f.prototype.bindModel=function(a,b,d){switch(a){case c.COLLECTION:case c.OBJECT:this.observer=e.observeCollection(this,b[d],this.onModelChange)}},f.prototype.unbind=function(){this.observer.close()},f.prototype.onModelChange=function(a){for(var c=0,d={changes:a},e=b.defer(),f=0;f<a.length;f++)if(a.name)c++;else if(a[f].removed&&a[f].removed.length&&(c+=a[f].removed.length),a[f].addedCount&&(c+=a[f].addedCount),a[f].changed)for(var g in a[f].changed)a[f].changed.hasOwnProperty(g)&&c++;return d.changes.length?this.ignoreNModelChanges?(this.ignoreNModelChanges-=c,e.resolve()):c&&this.protocol.processChanges(this,d).then(function(){e.resolve()}):e.resolve(),e.promise},f.prototype.onProtocolChange=function(a){var c={changes:a},e=b.defer();if(a.length){if(this.ignoreNProtocolChanges){for(var f=[],g=0;g<a.length;g++)a[g].force&&f.push(a[g]),this.ignoreNProtocolChanges--;f.length?c.changes=f:e.resolve()}d.processChanges(this,c).then(function(){e.resolve()})}else e.resolve();return e.promise},f.prototype.val=function(){var b=a(this.model);return b(this.scope)},function(){var a=Object.create(f.prototype);return f.apply(a,arguments),a}}]),angular.module("OctoBinder").factory("obBinderTypes",[function(){return{COLLECTION:"collection",OBJECT:"object",BOOLEAN:"boolean",STRING:"string",NUMBER:"number",BINARY:"binary",BINARY_STREAM:"binaryStream"}}]),function(){var a=function(){};a.prototype.addChange=function(a){if(!a.type)throw new Error("Change must contain a type");this.changes.push(a)},a.prototype.updateObject=function(a){this.object=a,angular.forEach(this.changes,function(b,c,d){d[c].object=a})},angular.module("OctoBinder").factory("obDelta",function(){return function(b){var c=Object.create(a.prototype);return a.call(c),c.changes=[],b&&c.addChange(b),c}})}(),angular.module("OctoBinder").service("obModelWriter",["$parse","$q","obBinderTypes","obSyncEvents",function(a,b,c){var e=this;this.applyArrayChange=function(a,b){var c=a.scope[a.model];if(b.added){var d=b.added.shift();for(c.splice(b.index,b.removed?b.removed.length:0,d);next=b.added.shift();)b.index++,c.splice(b.index,0,next)}else c.splice(b.index,b.removed?b.removed.length:0);a.ignoreNModelChanges+=(b.removed&&b.removed.length||0)+b.addedCount,a.scope[a.model]=c,a.scope.$$phase||a.scope.$apply()},this.applyObjectChange=function(a,b){function c(b,c){var d,e=a.scope[a.model];return angular.forEach(e,function(a){d||(a[b]===c?d=a:"undefined"==typeof a[b]&&(d=a))}),d}if(a.key){var d=c(a.key,b.object[a.key]);if(!d)throw new Error("Could not find object with key"+b.object[a.key]);switch(b.type){case"update":d[b.name]!==b.object[b.name]&&a.ignoreNModelChanges++,d[b.name]=b.object[b.name];break;case"delete":a.ignoreNModelChanges++,delete d[b.name];break;case"new":d[b.name]!==b.object[b.name]&&a.ignoreNModelChanges++,d[b.name]=b.object[b.name]}a.scope.$$phase||a.scope.$apply()}},this.processChanges=function(a,d){for(var f=b.defer(),g=[],h=0;h<d.changes.length;h++)switch(a.type){case c.COLLECTION:"number"==typeof d.changes[h].index?g.push(function(){e.applyArrayChange(a,d.changes[h])}(h)):"string"==typeof change.name&&(this.applyObjectChange(a,d.changes[h]),g.push(function(){e.applyObjectChange(a,d.changes[h])}(h)))}return b.all(g).then(function(){f.resolve()}),f.promise}}]),angular.module("OctoBinder").factory("obArrayChange",function(){return function(a,b,c){return{addedCount:a,removed:b,index:c}}}).factory("obOldObject",function(){return function(a){var b=angular.copy(a.object);return b[a.name]=a.oldValue,b}}).service("obObserver",["obArrayChange","obOldObject","$q",function(a,b){this.observeObjectInCollection=function(c,d,e,f){function g(e,g,h,i){function l(c){var e=b(c),f=d.indexOf(c.object);k.push(a(1,[e],f))}var j=[{added:e,removed:g,changed:h,index:i(c.key)}],k=[];c.key?f.call(c,j):(angular.forEach(j,l),f.call(c,k))}this.observers[e]=new ObjectObserver(e,g)},this.observers={},this.observeCollection=function(a,b,c){function f(e){d.observeObjectInCollection(a,b,e,c)}function g(b){angular.forEach(b,h),c.call(a,b)}function h(a){for(var c=a.index,e=a.addedCount+a.index;e>c;)f(b[c]),c++;a.removed.length&&angular.forEach(a.removed,function(a){d.observers[a].close()})}var e,d=this;return angular.forEach(b,f),e=new ArrayObserver(b,g)}}]),angular.module("OctoBinder").value("obSyncEvents",{NEW:"new",UPDATED:"update",DELETED:"deleted",RECONFIGURED:"reconfigured",READ:"read",MOVE:"move",NONE:"none",INIT:"init",UNKNOWN:"unknown"});