Mercurial > hg > OKFNAnnotator
annotate annotator_files/lib/plugin/store.js @ 4:6979313586cf
new version of annotator.
author | casties |
---|---|
date | Mon, 27 Aug 2012 19:05:38 +0200 |
parents | 6356e78ccf5c |
children |
rev | line source |
---|---|
4 | 1 // Generated by CoffeeScript 1.3.3 |
3
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
2 var __bind = function(fn, me){ return function(){ return fn.apply(me, arguments); }; }, |
4 | 3 __hasProp = {}.hasOwnProperty, |
4 __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; }, | |
5 __indexOf = [].indexOf || function(item) { for (var i = 0, l = this.length; i < l; i++) { if (i in this && this[i] === item) return i; } return -1; }; | |
3
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
6 |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
7 Annotator.Plugin.Store = (function(_super) { |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
8 |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
9 __extends(Store, _super); |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
10 |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
11 Store.prototype.events = { |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
12 'annotationCreated': 'annotationCreated', |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
13 'annotationDeleted': 'annotationDeleted', |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
14 'annotationUpdated': 'annotationUpdated' |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
15 }; |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
16 |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
17 Store.prototype.options = { |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
18 prefix: '/store', |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
19 autoFetch: true, |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
20 annotationData: {}, |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
21 loadFromSearch: false, |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
22 urls: { |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
23 create: '/annotations', |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
24 read: '/annotations/:id', |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
25 update: '/annotations/:id', |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
26 destroy: '/annotations/:id', |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
27 search: '/search' |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
28 } |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
29 }; |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
30 |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
31 function Store(element, options) { |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
32 this._onError = __bind(this._onError, this); |
4 | 33 |
3
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
34 this._onLoadAnnotationsFromSearch = __bind(this._onLoadAnnotationsFromSearch, this); |
4 | 35 |
3
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
36 this._onLoadAnnotations = __bind(this._onLoadAnnotations, this); |
4 | 37 |
38 this._getAnnotations = __bind(this._getAnnotations, this); | |
39 Store.__super__.constructor.apply(this, arguments); | |
3
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
40 this.annotations = []; |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
41 } |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
42 |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
43 Store.prototype.pluginInit = function() { |
4 | 44 if (!Annotator.supported()) { |
45 return; | |
46 } | |
3
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
47 if (this.annotator.plugins.Auth) { |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
48 return this.annotator.plugins.Auth.withToken(this._getAnnotations); |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
49 } else { |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
50 return this._getAnnotations(); |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
51 } |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
52 }; |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
53 |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
54 Store.prototype._getAnnotations = function() { |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
55 if (this.options.loadFromSearch) { |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
56 return this.loadAnnotationsFromSearch(this.options.loadFromSearch); |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
57 } else { |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
58 return this.loadAnnotations(); |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
59 } |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
60 }; |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
61 |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
62 Store.prototype.annotationCreated = function(annotation) { |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
63 var _this = this; |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
64 if (__indexOf.call(this.annotations, annotation) < 0) { |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
65 this.registerAnnotation(annotation); |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
66 return this._apiRequest('create', annotation, function(data) { |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
67 if (!(data.id != null)) { |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
68 console.warn(Annotator._t("Warning: No ID returned from server for annotation "), annotation); |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
69 } |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
70 return _this.updateAnnotation(annotation, data); |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
71 }); |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
72 } else { |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
73 return this.updateAnnotation(annotation, {}); |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
74 } |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
75 }; |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
76 |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
77 Store.prototype.annotationUpdated = function(annotation) { |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
78 var _this = this; |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
79 if (__indexOf.call(this.annotations, annotation) >= 0) { |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
80 return this._apiRequest('update', annotation, (function(data) { |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
81 return _this.updateAnnotation(annotation, data); |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
82 })); |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
83 } |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
84 }; |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
85 |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
86 Store.prototype.annotationDeleted = function(annotation) { |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
87 var _this = this; |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
88 if (__indexOf.call(this.annotations, annotation) >= 0) { |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
89 return this._apiRequest('destroy', annotation, (function() { |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
90 return _this.unregisterAnnotation(annotation); |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
91 })); |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
92 } |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
93 }; |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
94 |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
95 Store.prototype.registerAnnotation = function(annotation) { |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
96 return this.annotations.push(annotation); |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
97 }; |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
98 |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
99 Store.prototype.unregisterAnnotation = function(annotation) { |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
100 return this.annotations.splice(this.annotations.indexOf(annotation), 1); |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
101 }; |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
102 |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
103 Store.prototype.updateAnnotation = function(annotation, data) { |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
104 if (__indexOf.call(this.annotations, annotation) < 0) { |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
105 console.error(Annotator._t("Trying to update unregistered annotation!")); |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
106 } else { |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
107 $.extend(annotation, data); |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
108 } |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
109 return $(annotation.highlights).data('annotation', annotation); |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
110 }; |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
111 |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
112 Store.prototype.loadAnnotations = function() { |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
113 return this._apiRequest('read', null, this._onLoadAnnotations); |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
114 }; |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
115 |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
116 Store.prototype._onLoadAnnotations = function(data) { |
4 | 117 if (data == null) { |
118 data = []; | |
119 } | |
3
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
120 this.annotations = data; |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
121 return this.annotator.loadAnnotations(data.slice()); |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
122 }; |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
123 |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
124 Store.prototype.loadAnnotationsFromSearch = function(searchOptions) { |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
125 return this._apiRequest('search', searchOptions, this._onLoadAnnotationsFromSearch); |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
126 }; |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
127 |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
128 Store.prototype._onLoadAnnotationsFromSearch = function(data) { |
4 | 129 if (data == null) { |
130 data = {}; | |
131 } | |
3
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
132 return this._onLoadAnnotations(data.rows || []); |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
133 }; |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
134 |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
135 Store.prototype.dumpAnnotations = function() { |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
136 var ann, _i, _len, _ref, _results; |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
137 _ref = this.annotations; |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
138 _results = []; |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
139 for (_i = 0, _len = _ref.length; _i < _len; _i++) { |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
140 ann = _ref[_i]; |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
141 _results.push(JSON.parse(this._dataFor(ann))); |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
142 } |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
143 return _results; |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
144 }; |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
145 |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
146 Store.prototype._apiRequest = function(action, obj, onSuccess) { |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
147 var id, options, request, url; |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
148 id = obj && obj.id; |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
149 url = this._urlFor(action, id); |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
150 options = this._apiRequestOptions(action, obj, onSuccess); |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
151 request = $.ajax(url, options); |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
152 request._id = id; |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
153 request._action = action; |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
154 return request; |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
155 }; |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
156 |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
157 Store.prototype._apiRequestOptions = function(action, obj, onSuccess) { |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
158 var opts; |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
159 opts = { |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
160 type: this._methodFor(action), |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
161 headers: this.element.data('annotator:headers'), |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
162 dataType: "json", |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
163 success: onSuccess || function() {}, |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
164 error: this._onError |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
165 }; |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
166 if (action === "search") { |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
167 opts = $.extend(opts, { |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
168 data: obj |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
169 }); |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
170 } else { |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
171 opts = $.extend(opts, { |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
172 data: obj && this._dataFor(obj), |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
173 contentType: "application/json; charset=utf-8" |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
174 }); |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
175 } |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
176 return opts; |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
177 }; |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
178 |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
179 Store.prototype._urlFor = function(action, id) { |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
180 var replaceWith, url; |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
181 replaceWith = id != null ? '/' + id : ''; |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
182 url = this.options.prefix || '/'; |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
183 url += this.options.urls[action]; |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
184 url = url.replace(/\/:id/, replaceWith); |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
185 return url; |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
186 }; |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
187 |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
188 Store.prototype._methodFor = function(action) { |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
189 var table; |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
190 table = { |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
191 'create': 'POST', |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
192 'read': 'GET', |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
193 'update': 'PUT', |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
194 'destroy': 'DELETE', |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
195 'search': 'GET' |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
196 }; |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
197 return table[action]; |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
198 }; |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
199 |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
200 Store.prototype._dataFor = function(annotation) { |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
201 var data, highlights; |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
202 highlights = annotation.highlights; |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
203 delete annotation.highlights; |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
204 $.extend(annotation, this.options.annotationData); |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
205 data = JSON.stringify(annotation); |
4 | 206 if (highlights) { |
207 annotation.highlights = highlights; | |
208 } | |
3
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
209 return data; |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
210 }; |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
211 |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
212 Store.prototype._onError = function(xhr) { |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
213 var action, message; |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
214 action = xhr._action; |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
215 message = Annotator._t("Sorry we could not ") + action + Annotator._t(" this annotation"); |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
216 if (xhr._action === 'search') { |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
217 message = Annotator._t("Sorry we could not search the store for annotations"); |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
218 } else if (xhr._action === 'read' && !xhr._id) { |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
219 message = Annotator._t("Sorry we could not ") + action + Annotator._t(" the annotations from the store"); |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
220 } |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
221 switch (xhr.status) { |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
222 case 401: |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
223 message = Annotator._t("Sorry you are not allowed to ") + action + Annotator._t(" this annotation"); |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
224 break; |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
225 case 404: |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
226 message = Annotator._t("Sorry we could not connect to the annotations store"); |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
227 break; |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
228 case 500: |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
229 message = Annotator._t("Sorry something went wrong with the annotation store"); |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
230 } |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
231 Annotator.showNotification(message, Annotator.Notification.ERROR); |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
232 return console.error(Annotator._t("API request failed:") + (" '" + xhr.status + "'")); |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
233 }; |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
234 |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
235 return Store; |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
236 |
6356e78ccf5c
new version contains Annotator JS files to be used with FilesystemSite.
casties
parents:
diff
changeset
|
237 })(Annotator.Plugin); |