(function(){var__bind=function(fn,me){returnfunction(){returnfn.apply(me,arguments)}},__hasProp={}.hasOwnProperty,__extends=function(child,parent){for(varkeyinparent){if(__hasProp.call(parent,key))child[key]=parent[key]}functionctor(){this.constructor=child}ctor.prototype=parent.prototype;child.prototype=newctor;child.__super__=parent.prototype;returnchild},__indexOf=[].indexOf||function(item){for(vari=0,l=this.length;i<l;i++){if(iinthis&&this[i]===item)returni}return-1};Annotator.Plugin.Store=function(_super){__extends(Store,_super);Store.prototype.events={annotationCreated:"annotationCreated",annotationDeleted:"annotationDeleted",annotationUpdated:"annotationUpdated"};Store.prototype.options={annotationData:{},emulateHTTP:false,loadFromSearch:false,prefix:"/store",urls:{create:"/annotations",read:"/annotations/:id",update:"/annotations/:id",destroy:"/annotations/:id",search:"/search"}};functionStore(element,options){this._onError=__bind(this._onError,this);this._onLoadAnnotationsFromSearch=__bind(this._onLoadAnnotationsFromSearch,this);this._onLoadAnnotations=__bind(this._onLoadAnnotations,this);this._getAnnotations=__bind(this._getAnnotations,this);Store.__super__.constructor.apply(this,arguments);this.annotations=[]}Store.prototype.pluginInit=function(){if(!Annotator.supported()){return}if(this.annotator.plugins.Auth){returnthis.annotator.plugins.Auth.withToken(this._getAnnotations)}else{returnthis._getAnnotations()}};Store.prototype._getAnnotations=function(){if(this.options.loadFromSearch){returnthis.loadAnnotationsFromSearch(this.options.loadFromSearch)}else{returnthis.loadAnnotations()}};Store.prototype.annotationCreated=function(annotation){var_this=this;if(__indexOf.call(this.annotations,annotation)<0){this.registerAnnotation(annotation);returnthis._apiRequest("create",annotation,function(data){if(data.id==null){console.warn(Annotator._t("Warning: No ID returned from server for annotation "),annotation)}return_this.updateAnnotation(annotation,data)})}else{returnthis.updateAnnotation(annotation,{})}};Store.prototype.annotationUpdated=function(annotation){var_this=this;if(__indexOf.call(this.annotations,annotation)>=0){returnthis._apiRequest("update",annotation,function(data){return_this.updateAnnotation(annotation,data)})}};Store.prototype.annotationDeleted=function(annotation){var_this=this;if(__indexOf.call(this.annotations,annotation)>=0){returnthis._apiRequest("destroy",annotation,function(){return_this.unregisterAnnotation(annotation)})}};Store.prototype.registerAnnotation=function(annotation){returnthis.annotations.push(annotation)};Store.prototype.unregisterAnnotation=function(annotation){returnthis.annotations.splice(this.annotations.indexOf(annotation),1)};Store.prototype.updateAnnotation=function(annotation,data){if(__indexOf.call(this.annotations,annotation)<0){console.error(Annotator._t("Trying to update unregistered annotation!"))}else{$.extend(annotation,data)}return$(annotation.highlights).data("annotation",annotation)};Store.prototype.loadAnnotations=function(){returnthis._apiRequest("read",null,this._onLoadAnnotations)};Store.prototype._onLoadAnnotations=function(data){if(data==null){data=[]}this.annotations=this.annotations.concat(data);returnthis.annotator.loadAnnotations(data.slice())};Store.prototype.loadAnnotationsFromSearch=function(searchOptions){returnthis._apiRequest("search",searchOptions,this._onLoadAnnotationsFromSearch)};Store.prototype._onLoadAnnotationsFromSearch=function(data){if(data==null){data={}}returnthis._onLoadAnnotations(data.rows||[])};Store.prototype.dumpAnnotations=function(){varann,_i,_len,_ref,_results;_ref=this.annotations;_results=[];for(_i=0,_len=_ref.length;_i<_len;_i++){ann=_ref[_i];_results.push(JSON.parse(this._dataFor(ann)))}return_results};Store.prototype._apiRequest=function(action,obj,onSuccess){varid,options,request,url;id=obj&&obj.id;url=this._urlFor(action,id);options=this._apiRequestOptions(action,obj,onSuccess);request=$.ajax(url,options);request._id=id;request._action=action;returnrequest};Store.prototype._apiRequestOptions=function(action,obj,onSuccess){vardata,method,opts;method=this._methodFor(action);opts={type:method,headers:this.element.data("annotator:headers"),dataType:"json",success:onSuccess||function(){},error:this._onError};if(this.options.emulateHTTP&&(method==="PUT"||method==="DELETE")){opts.headers=$.extend(opts.headers,{"X-HTTP-Method-Override":method});opts.type="POST"}if(action==="search"){opts=$.extend(opts,{data:obj});returnopts}data=obj&&this._dataFor(obj);if(this.options.emulateJSON){opts.data={json:data};if(this.options.emulateHTTP){opts.data._method=method}returnopts}opts=$.extend(opts,{data:data,contentType:"application/json; charset=utf-8"});returnopts};Store.prototype._urlFor=function(action,id){varurl;url=this.options.prefix!=null?this.options.prefix:"";url+=this.options.urls[action];url=url.replace(/\/:id/,id!=null?"/"+id:"");url=url.replace(/:id/,id!=null?id:"");returnurl};Store.prototype._methodFor=function(action){vartable;table={create:"POST",read:"GET",update:"PUT",destroy:"DELETE",search:"GET"};returntable[action]};Store.prototype._dataFor=function(annotation){vardata,highlights;highlights=annotation.highlights;deleteannotation.highlights;$.extend(annotation,this.options.annotationData);data=JSON.stringify(annotation);if(highlights){annotation.highlights=highlights}returndata};Store.prototype._onError=function(xhr){varaction,message;action=xhr._action;message=Annotator._t("Sorry we could not ")+action+Annotator._t(" this annotation");if(xhr._action==="search"){message=Annotator._t("Sorry we could not search the store for annotations")}elseif(xhr._action==="read"&&!xhr._id){message=Annotator._t("Sorry we could not ")+action+Annotator._t(" the annotations from the store")}switch(xhr.status){case401:message=Annotator._t("Sorry you are not allowed to ")+action+Annotator._t(" this annotation");break;case404:message=Annotator._t("Sorry we could not connect to the annotations store");break;case500:message=Annotator._t("Sorry something went wrong with the annotation store")}Annotator.showNotification(message,Annotator.Notification.ERROR);returnconsole.error(Annotator._t("API request failed:")+(" '"+xhr.status+"'"))};returnStore}(Annotator.Plugin)}).call(this);