1083
|
1 /**
|
|
2 digilib plugin for annotations.
|
|
3
|
1086
|
4 currently only point-like annotations (like marks).
|
|
5
|
|
6 Annotations are stored on a Annotator http://annotateit.org compatible server.
|
1083
|
7
|
|
8 */
|
|
9
|
|
10 (function($) {
|
|
11
|
|
12 // affine geometry
|
|
13 var geom;
|
|
14 // plugin object with digilib data
|
|
15 var digilib;
|
|
16
|
|
17 var FULL_AREA;
|
|
18
|
|
19 var buttons = {
|
|
20 annotations : {
|
|
21 onclick : "toggleAnnotations",
|
|
22 tooltip : "show or hide annotations",
|
|
23 icon : "annotations.png"
|
|
24 },
|
|
25 annotationmark : {
|
|
26 onclick : "setAnnotationMark",
|
|
27 tooltip : "create an annotation for a point",
|
|
28 icon : "annotation-mark.png"
|
|
29 }
|
|
30 };
|
|
31
|
|
32 var defaults = {
|
|
33 // are annotations active?
|
|
34 'isAnnotationsVisible' : true,
|
|
35 // buttonset of this plugin
|
|
36 'annotationSet' : ['annotations', 'annotationmark', 'lessoptions'],
|
|
37 // URL of annotation server
|
|
38 'annotationServerUrl' : 'http://virtuoso.mpiwg-berlin.mpg.de:8080/AnnotationManager/annotator',
|
|
39 // URL of authentication token server
|
|
40 'annotationTokenUrl' : 'http://localhost:8080/test/annotator/token?user=anonymous',
|
|
41 // annotation user name
|
|
42 'annotationUser' : 'anonymous'
|
|
43 };
|
|
44
|
|
45 var actions = {
|
|
46 /**
|
|
47 * show/hide annotations
|
|
48 */
|
|
49 toggleAnnotations : function(data) {
|
|
50 var show = !data.settings.isAnnotationsVisible;
|
|
51 data.settings.isAnnotationsVisible = show;
|
1097
|
52 digilib.fn.highlightButtons(data, 'annotations', show);
|
1083
|
53 renderAnnotations(data);
|
|
54 },
|
|
55
|
|
56 /**
|
|
57 * set a mark-annotation by clicking (or giving a position and a text)
|
|
58 *
|
|
59 * @param data
|
|
60 * @param mpos
|
|
61 * @param text
|
|
62 */
|
|
63 setAnnotationMark : function(data, mpos, text) {
|
|
64 if (mpos == null) {
|
|
65 // interactive
|
|
66 setAnnotationMark(data);
|
|
67 } else {
|
|
68 // use position and text
|
|
69 var annotation = newAnnotation(mpos, text);
|
|
70 storeAnnotation(data, annotation);
|
|
71 data.annotations.push(annotation);
|
|
72 digilib.fn.redisplay(data);
|
|
73 }
|
|
74 },
|
|
75 };
|
|
76
|
1086
|
77 /**
|
|
78 * create a new annotation object
|
|
79 */
|
1083
|
80 var newAnnotation = function(mpos, text, id, uri, user) {
|
|
81 var annot = {
|
|
82 pos : mpos,
|
|
83 text : text,
|
|
84 id : id,
|
|
85 uri : uri,
|
|
86 user : user
|
|
87 };
|
|
88 return annot;
|
|
89 };
|
|
90
|
|
91 /**
|
|
92 * returns an annotatable url to this digilib image
|
|
93 */
|
|
94 var getAnnotationPageUrl = function(data) {
|
|
95 var url = data.settings.digilibBaseUrl + '/jquery/digilib.html?';
|
|
96 url += digilib.fn.getParamString(data.settings, ['fn', 'pn'], digilib.defaults);
|
|
97 return url;
|
1086
|
98 };
|
|
99
|
1083
|
100 /**
|
|
101 * add a mark-annotation where clicked.
|
|
102 */
|
|
103 var setAnnotationMark = function(data) {
|
|
104 var $scaler = data.$scaler;
|
|
105 // unbind other handler TODO: do we need to do this?
|
|
106 $scaler.off(".dlZoomDrag");
|
|
107 // start event capturing
|
|
108 $scaler.one('mousedown.dlSetAnnotationMark', function(evt) {
|
|
109 // event handler adding a new mark
|
|
110 console.log("setAnnotationMark at=", evt);
|
|
111 var mpos = geom.position(evt);
|
|
112 var pos = data.imgTrafo.invtransform(mpos);
|
1086
|
113 // Annotation text entered in JS-prompt
|
1083
|
114 var text = window.prompt("Annotation text:");
|
|
115 if (text == null)
|
|
116 return false;
|
|
117 var annotation = newAnnotation(pos, text);
|
|
118 storeAnnotation(data, annotation);
|
|
119 data.annotations.push(annotation);
|
|
120 digilib.fn.redisplay(data);
|
|
121 return false;
|
|
122 });
|
|
123 };
|
|
124
|
|
125 /**
|
|
126 * place annotations on the image
|
|
127 */
|
|
128 var renderAnnotations = function(data) {
|
|
129 if (data.annotations == null || data.$img == null || data.imgTrafo == null)
|
|
130 return;
|
|
131 var cssPrefix = data.settings.cssPrefix;
|
|
132 var $elem = data.$elem;
|
|
133 var annotations = data.annotations;
|
|
134 console.debug("renderAnnotations: annotations=" + annotations);
|
|
135 // clear annotations
|
|
136 $elem.find('div.' + cssPrefix + 'annotationmark').remove();
|
1097
|
137 if (!data.settings.isAnnotationsVisible) return;
|
1083
|
138 for (var i = 0; i < annotations.length; i++) {
|
|
139 var annotation = annotations[i];
|
|
140 if (data.zoomArea.containsPosition(annotation.pos)) {
|
|
141 var mpos = data.imgTrafo.transform(annotation.pos);
|
|
142 console.debug("renderannotations: pos=", mpos);
|
|
143 // create annotation
|
|
144 var html = '<div class="' + cssPrefix + 'annotationmark ' + cssPrefix + 'overlay">' + (i + 1) + '</div>';
|
|
145 var $annotation = $(html);
|
|
146 // set text as tooltip TODO: have real popup with editing
|
|
147 $annotation.attr('title', "Annotation: " + annotation.text);
|
|
148 $elem.append($annotation);
|
|
149 mpos.adjustDiv($annotation);
|
|
150 }
|
|
151 }
|
|
152 };
|
|
153
|
1086
|
154 /**
|
|
155 * Get an authentication token from the token server.
|
|
156 *
|
|
157 * Stores the token and loads annotations on success.
|
|
158 */
|
1083
|
159 var loadAnnotationToken = function(data) {
|
|
160 var settings = data.settings;
|
|
161 var url = settings.annotationTokenUrl;
|
|
162 // TODO: better error handling
|
|
163 $.get(url, function(authToken, authStatus) {
|
|
164 console.debug("got auth token data=", authToken);
|
|
165 data.annotationToken = authToken;
|
|
166 loadAnnotations(data);
|
|
167 });
|
|
168 };
|
|
169
|
1086
|
170 /**
|
|
171 * loads all annotations for this url from the annotation server.
|
|
172 */
|
1083
|
173 var loadAnnotations = function(data) {
|
|
174 var settings = data.settings;
|
1086
|
175 // we use the search API
|
1083
|
176 var url = settings.annotationServerUrl + '/search';
|
1084
|
177 var pageUrl = getAnnotationPageUrl(data);
|
1083
|
178 // send authentication token in header
|
|
179 headers = {
|
|
180 'x-annotator-auth-token' : data.annotationToken
|
|
181 };
|
1086
|
182 // get only 20 annotations with this url
|
1083
|
183 var query = {
|
|
184 limit : 20,
|
|
185 uri : pageUrl
|
|
186 };
|
|
187 $.ajax(url, {
|
|
188 dataType : 'json',
|
|
189 data : query,
|
|
190 headers : headers,
|
|
191 success : function(annotData, annotStatus) {
|
|
192 console.debug("got annotation data=", annotData);
|
|
193 data.annotationData = annotData;
|
|
194 parseAnnotations(data, annotData);
|
|
195 renderAnnotations(data);
|
|
196 }
|
|
197 });
|
|
198 };
|
|
199
|
1086
|
200 /**
|
|
201 * parse all JSON-annotations in annotationData.rows and put in data.annotations
|
|
202 */
|
1083
|
203 var parseAnnotations = function(data, annotationData) {
|
|
204 var annotations = [];
|
|
205 for (var i = 0; i < annotationData.rows.length; ++i) {
|
|
206 var ann = annotationData.rows[i];
|
|
207 var annot = parseAnnotation(ann)
|
|
208 if (annot != null) {
|
|
209 annotations.push(annot);
|
|
210 }
|
|
211 }
|
|
212 data.annotations = annotations;
|
|
213 };
|
|
214
|
1086
|
215 /**
|
|
216 * Parse a JSON-annotation.
|
|
217 *
|
|
218 * Returns an annotation object.
|
|
219 */
|
1083
|
220 var parseAnnotation = function(ann) {
|
|
221 // TODO: check validity of annotation data
|
1084
|
222 if (ann.areas != null && ann.areas.length > 0) {
|
|
223 var area = ann.areas[0];
|
1086
|
224 // currently only point annotations
|
1084
|
225 var pos = geom.position(area.x, area.y);
|
1083
|
226 return newAnnotation(pos, ann.text, ann.id, ann.uri, ann.user);
|
|
227 }
|
|
228 return null;
|
|
229 };
|
|
230
|
1086
|
231 /**
|
|
232 * Store an annotation on the annotation server.
|
|
233 */
|
1083
|
234 var storeAnnotation = function(data, annotation) {
|
|
235 console.debug("storeAnnotation:", annotation);
|
|
236 var settings = data.settings;
|
|
237 var url = settings.annotationServerUrl + '/annotations';
|
|
238 var pageUrl = getAnnotationPageUrl(data);
|
|
239 // send authentication token in header
|
|
240 headers = {
|
|
241 'x-annotator-auth-token' : data.annotationToken
|
|
242 };
|
|
243 // create annotation object to send
|
|
244 var annotData = {
|
1084
|
245 areas : [{
|
|
246 x : annotation.pos.x,
|
|
247 y : annotation.pos.y
|
|
248 }],
|
1083
|
249 text : annotation.text,
|
|
250 uri : pageUrl,
|
|
251 user : settings.annotationUser
|
|
252 };
|
|
253 var dataString = JSON.stringify(annotData);
|
|
254 $.ajax(url, {
|
|
255 type : 'POST',
|
|
256 dataType : 'json',
|
|
257 contentType : 'application/json',
|
|
258 data : dataString,
|
|
259 headers : headers,
|
|
260 success : function(annotData, annotStatus) {
|
1084
|
261 console.debug("sent annotation data, got=", annotData, " status=" + annotStatus);
|
1083
|
262 var annot = parseAnnotation(annotData);
|
|
263 // TODO: we have to add the returned data to the real annotation!
|
|
264 //renderAnnotations(data);
|
|
265 }
|
|
266 });
|
|
267
|
1086
|
268 };
|
|
269
|
|
270 /**
|
|
271 * install additional buttons
|
|
272 */
|
1083
|
273 var installButtons = function(data) {
|
|
274 var settings = data.settings;
|
|
275 var mode = settings.interactionMode;
|
|
276 var buttonSettings = settings.buttonSettings[mode];
|
|
277 // configure buttons through digilib "annotationSet" option
|
|
278 var buttonSet = settings.annotationSet || annotationSet;
|
|
279 // set annotationSet to [] or '' for no buttons (when showing annotations only)
|
|
280 if (buttonSet.length && buttonSet.length > 0) {
|
|
281 buttonSettings.annotationSet = buttonSet;
|
|
282 buttonSettings.buttonSets.push('annotationSet');
|
|
283 }
|
|
284 };
|
|
285
|
1086
|
286 /**
|
|
287 * plugin installation. called by digilib on plugin object.
|
|
288 */
|
1083
|
289 var install = function(plugin) {
|
|
290 digilib = plugin;
|
|
291 console.debug('installing annotations plugin. digilib:', digilib);
|
|
292 // import geometry classes
|
|
293 geom = digilib.fn.geometry;
|
|
294 FULL_AREA = geom.rectangle(0, 0, 1, 1);
|
|
295 // add defaults, actins, buttons
|
|
296 $.extend(digilib.defaults, defaults);
|
|
297 $.extend(digilib.actions, actions);
|
|
298 $.extend(digilib.buttons, buttons);
|
|
299 };
|
|
300
|
|
301 /** plugin initialization */
|
|
302 var init = function(data) {
|
|
303 console.debug('initialising annotations plugin. data:', data);
|
|
304 var $data = $(data);
|
|
305 // set up
|
|
306 data.annotations = [];
|
|
307 if (digilib.plugins.buttons != null) {
|
|
308 installButtons(data);
|
|
309 }
|
|
310 // install event handler
|
|
311 $data.bind('setup', handleSetup);
|
|
312 $data.bind('update', handleUpdate);
|
|
313 };
|
|
314
|
1086
|
315 /**
|
|
316 * setup loads all annotations.
|
|
317 */
|
1083
|
318 var handleSetup = function(evt) {
|
|
319 console.debug("annotations: handleSetup");
|
|
320 var data = this;
|
|
321 // load annotations from server
|
|
322 loadAnnotationToken(data);
|
|
323 };
|
|
324
|
1086
|
325 /**
|
|
326 * update renders all annotations.
|
|
327 */
|
1083
|
328 var handleUpdate = function(evt) {
|
|
329 console.debug("annotations: handleUpdate");
|
|
330 var data = this;
|
|
331 if (data.marks != null) {
|
|
332 renderAnnotations(data);
|
|
333 }
|
|
334 };
|
|
335
|
|
336 // plugin object with name and init
|
|
337 // shared objects filled by digilib on registration
|
|
338 var plugin = {
|
|
339 name : 'annotations',
|
|
340 install : install,
|
|
341 init : init,
|
|
342 buttons : {},
|
|
343 actions : {},
|
|
344 fn : {},
|
|
345 plugins : {}
|
|
346 };
|
|
347
|
|
348 if ($.fn.digilib == null) {
|
|
349 $.error("jquery.digilib.annotations must be loaded after jquery.digilib!");
|
|
350 } else {
|
|
351 $.fn.digilib('plugin', plugin);
|
|
352 }
|
|
353 })(jQuery);
|