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