/
annotator.store.min.js
1 lines (1 loc) · 3.67 KB
/
annotator.store.min.js
1
(function(){var e;var b=Object.prototype.hasOwnProperty,d=function(i,g){for(var f in g){if(b.call(g,f)){i[f]=g[f]}}function h(){this.constructor=i}h.prototype=g.prototype;i.prototype=new h;i.__super__=g.prototype;return i},a=function(f,g){return function(){return f.apply(g,arguments)}},c=Array.prototype.indexOf||function(h){for(var g=0,f=this.length;g<f;g++){if(this[g]===h){return g}}return -1};e=jQuery;Annotator.Plugins.Store=(function(){d(f,Delegator);f.prototype.events={annotationCreated:"annotationCreated",annotationDeleted:"annotationDeleted",annotationUpdated:"annotationUpdated"};f.prototype.options={prefix:"/store",autoFetch:true,annotationData:{},loadFromSearch:false,urls:{create:"/annotations",read:"/annotations/:id",update:"/annotations/:id",destroy:"/annotations/:id",search:"/search"}};function f(h,g){f.__super__.constructor.apply(this,arguments);this.addEvents();this.annotations=[]}f.prototype.pluginInit=function(){var g,h;h=a(function(){if(this.options.loadFromSearch){return this.loadAnnotationsFromSearch(this.options.loadFromSearch)}else{return this.loadAnnotations()}},this);g=e(this.element).data("annotator:auth");if(g){return g.withToken(h)}else{return h()}};f.prototype.annotationCreated=function(h,g){if(c.call(this.annotations,g)<0){this.registerAnnotation(g);return this._apiRequest("create",g,a(function(i){if(!(i.id!=null)){console.warn("Warning: No ID returned from server for annotation ",g)}return this.updateAnnotation(g,i)},this))}else{return this.updateAnnotation(g,{})}};f.prototype.annotationDeleted=function(h,g){if(c.call(this.annotations,g)>=0){return this._apiRequest("destroy",g,(a(function(){return this.unregisterAnnotation(g)},this)))}};f.prototype.annotationUpdated=function(h,g){if(c.call(this.annotations,g)>=0){return this._apiRequest("update",g,(a(function(){return this.updateAnnotation(g)},this)))}};f.prototype.registerAnnotation=function(g){return this.annotations.push(g)};f.prototype.unregisterAnnotation=function(g){return this.annotations.splice(this.annotations.indexOf(g),1)};f.prototype.updateAnnotation=function(g,h){if(c.call(this.annotations,g)<0){console.error("Trying to update unregistered annotation!")}else{e.extend(g,h)}return e(g.highlights).data("annotation",g)};f.prototype.loadAnnotations=function(){return this._apiRequest("read",null,a(function(g){this.annotations=g.slice();return this.annotator.loadAnnotations(g)},this))};f.prototype.loadAnnotationsFromSearch=function(g){return this._apiRequest("search",g,a(function(h){this.annotations=h.results.slice();return this.annotator.loadAnnotations(h.results)},this))};f.prototype.dumpAnnotations=function(){var k,j,h,i,g;i=this.annotations;g=[];for(j=0,h=i.length;j<h;j++){k=i[j];g.push(JSON.parse(this._dataFor(k)))}return g};f.prototype._apiRequest=function(i,j,l){var k,h,g;k=a(function(q){var p,n,o,m;p=e(this.element).data("annotator:headers");if(p){m=[];for(n in p){o=p[n];m.push(q.setRequestHeader(n,o))}return m}},this);h=function(o,m,n){return console.error("API request failed: '"+m+"'",o)};g={url:this._urlFor(i,j&&j.id),type:this._methodFor(i),beforeSend:k,dataType:"json",success:l||function(){},error:h};if(i==="search"){g=e.extend(g,{data:j})}else{g=e.extend(g,{data:j&&this._dataFor(j),contentType:"application/json; charset=utf-8"})}return e.ajax(g)};f.prototype._urlFor=function(i,j){var g,h;g=j!=null?"/"+j:"";h=this.options.prefix||"/";h+=this.options.urls[i];h=h.replace(/\/:id/,g);return h};f.prototype._methodFor=function(h){var g;g={create:"POST",read:"GET",update:"PUT",destroy:"DELETE",search:"GET"};return g[h]};f.prototype._dataFor=function(g){var h,i;i=g.highlights;delete g.highlights;e.extend(g,this.options.annotationData);h=JSON.stringify(g);g.highlights=i;return h};return f})()}).call(this);