source: main/trunk/greenstone3/web/interfaces/oran/js/annotator/pkg/annotator.store.min.js@ 24771

Last change on this file since 24771 was 24771, checked in by papitha, 13 years ago

Changes to JS

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