comparison webapp/src/main/webapp/jquery/jquery.digilib.annotations.js @ 1086:782593171adb

comments for annotations plugin.
author robcast
date Mon, 04 Jun 2012 11:09:41 +0200
parents 1d3e8f853b9c
children ebdde1bbb026
comparison
equal deleted inserted replaced
1085:53c61e3e64c7 1086:782593171adb
1 /** 1 /**
2 digilib plugin for annotations. 2 digilib plugin for annotations.
3 3
4 currently only point-like (like marks). 4 currently only point-like annotations (like marks).
5
6 Annotations are stored on a Annotator http://annotateit.org compatible server.
5 7
6 */ 8 */
7 9
8 (function($) { 10 (function($) {
9 11
70 digilib.fn.redisplay(data); 72 digilib.fn.redisplay(data);
71 } 73 }
72 }, 74 },
73 }; 75 };
74 76
77 /**
78 * create a new annotation object
79 */
75 var newAnnotation = function(mpos, text, id, uri, user) { 80 var newAnnotation = function(mpos, text, id, uri, user) {
76 var annot = { 81 var annot = {
77 pos : mpos, 82 pos : mpos,
78 text : text, 83 text : text,
79 id : id, 84 id : id,
88 */ 93 */
89 var getAnnotationPageUrl = function(data) { 94 var getAnnotationPageUrl = function(data) {
90 var url = data.settings.digilibBaseUrl + '/jquery/digilib.html?'; 95 var url = data.settings.digilibBaseUrl + '/jquery/digilib.html?';
91 url += digilib.fn.getParamString(data.settings, ['fn', 'pn'], digilib.defaults); 96 url += digilib.fn.getParamString(data.settings, ['fn', 'pn'], digilib.defaults);
92 return url; 97 return url;
93 } 98 };
99
94 /** 100 /**
95 * add a mark-annotation where clicked. 101 * add a mark-annotation where clicked.
96 *
97 */ 102 */
98 var setAnnotationMark = function(data) { 103 var setAnnotationMark = function(data) {
99 var $scaler = data.$scaler; 104 var $scaler = data.$scaler;
100 // unbind other handler TODO: do we need to do this? 105 // unbind other handler TODO: do we need to do this?
101 $scaler.off(".dlZoomDrag"); 106 $scaler.off(".dlZoomDrag");
103 $scaler.one('mousedown.dlSetAnnotationMark', function(evt) { 108 $scaler.one('mousedown.dlSetAnnotationMark', function(evt) {
104 // event handler adding a new mark 109 // event handler adding a new mark
105 console.log("setAnnotationMark at=", evt); 110 console.log("setAnnotationMark at=", evt);
106 var mpos = geom.position(evt); 111 var mpos = geom.position(evt);
107 var pos = data.imgTrafo.invtransform(mpos); 112 var pos = data.imgTrafo.invtransform(mpos);
113 // Annotation text entered in JS-prompt
108 var text = window.prompt("Annotation text:"); 114 var text = window.prompt("Annotation text:");
109 if (text == null) 115 if (text == null)
110 return false; 116 return false;
111 var annotation = newAnnotation(pos, text); 117 var annotation = newAnnotation(pos, text);
112 storeAnnotation(data, annotation); 118 storeAnnotation(data, annotation);
116 }); 122 });
117 }; 123 };
118 124
119 /** 125 /**
120 * place annotations on the image 126 * place annotations on the image
121 *
122 */ 127 */
123 var renderAnnotations = function(data) { 128 var renderAnnotations = function(data) {
124 if (data.annotations == null || data.$img == null || data.imgTrafo == null) 129 if (data.annotations == null || data.$img == null || data.imgTrafo == null)
125 return; 130 return;
126 var cssPrefix = data.settings.cssPrefix; 131 var cssPrefix = data.settings.cssPrefix;
143 mpos.adjustDiv($annotation); 148 mpos.adjustDiv($annotation);
144 } 149 }
145 } 150 }
146 }; 151 };
147 152
153 /**
154 * Get an authentication token from the token server.
155 *
156 * Stores the token and loads annotations on success.
157 */
148 var loadAnnotationToken = function(data) { 158 var loadAnnotationToken = function(data) {
149 var settings = data.settings; 159 var settings = data.settings;
150 var url = settings.annotationTokenUrl; 160 var url = settings.annotationTokenUrl;
151 // TODO: better error handling 161 // TODO: better error handling
152 $.get(url, function(authToken, authStatus) { 162 $.get(url, function(authToken, authStatus) {
154 data.annotationToken = authToken; 164 data.annotationToken = authToken;
155 loadAnnotations(data); 165 loadAnnotations(data);
156 }); 166 });
157 }; 167 };
158 168
169 /**
170 * loads all annotations for this url from the annotation server.
171 */
159 var loadAnnotations = function(data) { 172 var loadAnnotations = function(data) {
160 var settings = data.settings; 173 var settings = data.settings;
174 // we use the search API
161 var url = settings.annotationServerUrl + '/search'; 175 var url = settings.annotationServerUrl + '/search';
162 var pageUrl = getAnnotationPageUrl(data); 176 var pageUrl = getAnnotationPageUrl(data);
163 // send authentication token in header 177 // send authentication token in header
164 headers = { 178 headers = {
165 'x-annotator-auth-token' : data.annotationToken 179 'x-annotator-auth-token' : data.annotationToken
166 }; 180 };
167 // get only annotations with this url 181 // get only 20 annotations with this url
168 var query = { 182 var query = {
169 limit : 20, 183 limit : 20,
170 uri : pageUrl 184 uri : pageUrl
171 }; 185 };
172 $.ajax(url, { 186 $.ajax(url, {
180 renderAnnotations(data); 194 renderAnnotations(data);
181 } 195 }
182 }); 196 });
183 }; 197 };
184 198
199 /**
200 * parse all JSON-annotations in annotationData.rows and put in data.annotations
201 */
185 var parseAnnotations = function(data, annotationData) { 202 var parseAnnotations = function(data, annotationData) {
186 var annotations = []; 203 var annotations = [];
187 for (var i = 0; i < annotationData.rows.length; ++i) { 204 for (var i = 0; i < annotationData.rows.length; ++i) {
188 var ann = annotationData.rows[i]; 205 var ann = annotationData.rows[i];
189 var annot = parseAnnotation(ann) 206 var annot = parseAnnotation(ann)
192 } 209 }
193 } 210 }
194 data.annotations = annotations; 211 data.annotations = annotations;
195 }; 212 };
196 213
214 /**
215 * Parse a JSON-annotation.
216 *
217 * Returns an annotation object.
218 */
197 var parseAnnotation = function(ann) { 219 var parseAnnotation = function(ann) {
198 // TODO: check validity of annotation data 220 // TODO: check validity of annotation data
199 if (ann.areas != null && ann.areas.length > 0) { 221 if (ann.areas != null && ann.areas.length > 0) {
200 var area = ann.areas[0]; 222 var area = ann.areas[0];
223 // currently only point annotations
201 var pos = geom.position(area.x, area.y); 224 var pos = geom.position(area.x, area.y);
202 return newAnnotation(pos, ann.text, ann.id, ann.uri, ann.user); 225 return newAnnotation(pos, ann.text, ann.id, ann.uri, ann.user);
203 } 226 }
204 return null; 227 return null;
205 }; 228 };
206 229
230 /**
231 * Store an annotation on the annotation server.
232 */
207 var storeAnnotation = function(data, annotation) { 233 var storeAnnotation = function(data, annotation) {
208 console.debug("storeAnnotation:", annotation); 234 console.debug("storeAnnotation:", annotation);
209 var settings = data.settings; 235 var settings = data.settings;
210 var url = settings.annotationServerUrl + '/annotations'; 236 var url = settings.annotationServerUrl + '/annotations';
211 var pageUrl = getAnnotationPageUrl(data); 237 var pageUrl = getAnnotationPageUrl(data);
236 // TODO: we have to add the returned data to the real annotation! 262 // TODO: we have to add the returned data to the real annotation!
237 //renderAnnotations(data); 263 //renderAnnotations(data);
238 } 264 }
239 }); 265 });
240 266
241 } 267 };
242 /** install additional buttons */ 268
269 /**
270 * install additional buttons
271 */
243 var installButtons = function(data) { 272 var installButtons = function(data) {
244 var settings = data.settings; 273 var settings = data.settings;
245 var mode = settings.interactionMode; 274 var mode = settings.interactionMode;
246 var buttonSettings = settings.buttonSettings[mode]; 275 var buttonSettings = settings.buttonSettings[mode];
247 // configure buttons through digilib "annotationSet" option 276 // configure buttons through digilib "annotationSet" option
251 buttonSettings.annotationSet = buttonSet; 280 buttonSettings.annotationSet = buttonSet;
252 buttonSettings.buttonSets.push('annotationSet'); 281 buttonSettings.buttonSets.push('annotationSet');
253 } 282 }
254 }; 283 };
255 284
256 /** plugin installation called by digilib on plugin object. */ 285 /**
286 * plugin installation. called by digilib on plugin object.
287 */
257 var install = function(plugin) { 288 var install = function(plugin) {
258 digilib = plugin; 289 digilib = plugin;
259 console.debug('installing annotations plugin. digilib:', digilib); 290 console.debug('installing annotations plugin. digilib:', digilib);
260 // import geometry classes 291 // import geometry classes
261 geom = digilib.fn.geometry; 292 geom = digilib.fn.geometry;
278 // install event handler 309 // install event handler
279 $data.bind('setup', handleSetup); 310 $data.bind('setup', handleSetup);
280 $data.bind('update', handleUpdate); 311 $data.bind('update', handleUpdate);
281 }; 312 };
282 313
314 /**
315 * setup loads all annotations.
316 */
283 var handleSetup = function(evt) { 317 var handleSetup = function(evt) {
284 console.debug("annotations: handleSetup"); 318 console.debug("annotations: handleSetup");
285 var data = this; 319 var data = this;
286 // load annotations from server 320 // load annotations from server
287 loadAnnotationToken(data); 321 loadAnnotationToken(data);
288 }; 322 };
289 323
324 /**
325 * update renders all annotations.
326 */
290 var handleUpdate = function(evt) { 327 var handleUpdate = function(evt) {
291 console.debug("annotations: handleUpdate"); 328 console.debug("annotations: handleUpdate");
292 var data = this; 329 var data = this;
293 if (data.marks != null) { 330 if (data.marks != null) {
294 renderAnnotations(data); 331 renderAnnotations(data);