annotate webapp/src/main/webapp/jquery/jquery.digilib.annotations.js @ 1097:ebdde1bbb026

annotation show/hide works now.
author robcast
date Wed, 24 Oct 2012 18:05:47 +0200
parents 782593171adb
children c5ed20cd24ae
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;
1097
ebdde1bbb026 annotation show/hide works now.
robcast
parents: 1086
diff changeset
52 digilib.fn.highlightButtons(data, 'annotations', show);
1083
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();
1097
ebdde1bbb026 annotation show/hide works now.
robcast
parents: 1086
diff changeset
137 if (!data.settings.isAnnotationsVisible) return;
1083
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
138 for (var i = 0; i < annotations.length; i++) {
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
139 var annotation = annotations[i];
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
140 if (data.zoomArea.containsPosition(annotation.pos)) {
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
141 var mpos = data.imgTrafo.transform(annotation.pos);
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
142 console.debug("renderannotations: pos=", mpos);
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
143 // create annotation
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
144 var html = '<div class="' + cssPrefix + 'annotationmark ' + cssPrefix + 'overlay">' + (i + 1) + '</div>';
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
145 var $annotation = $(html);
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
146 // set text as tooltip TODO: have real popup with editing
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
147 $annotation.attr('title', "Annotation: " + annotation.text);
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
148 $elem.append($annotation);
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
149 mpos.adjustDiv($annotation);
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 };
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
153
1086
782593171adb comments for annotations plugin.
robcast
parents: 1084
diff changeset
154 /**
782593171adb comments for annotations plugin.
robcast
parents: 1084
diff changeset
155 * Get an authentication token from the token server.
782593171adb comments for annotations plugin.
robcast
parents: 1084
diff changeset
156 *
782593171adb comments for annotations plugin.
robcast
parents: 1084
diff changeset
157 * Stores the token and loads annotations on success.
782593171adb comments for annotations plugin.
robcast
parents: 1084
diff changeset
158 */
1083
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
159 var loadAnnotationToken = function(data) {
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
160 var settings = data.settings;
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
161 var url = settings.annotationTokenUrl;
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
162 // TODO: better error handling
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
163 $.get(url, function(authToken, authStatus) {
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
164 console.debug("got auth token data=", authToken);
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
165 data.annotationToken = authToken;
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
166 loadAnnotations(data);
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
167 });
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
168 };
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
169
1086
782593171adb comments for annotations plugin.
robcast
parents: 1084
diff changeset
170 /**
782593171adb comments for annotations plugin.
robcast
parents: 1084
diff changeset
171 * loads all annotations for this url from the annotation server.
782593171adb comments for annotations plugin.
robcast
parents: 1084
diff changeset
172 */
1083
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
173 var loadAnnotations = function(data) {
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
174 var settings = data.settings;
1086
782593171adb comments for annotations plugin.
robcast
parents: 1084
diff changeset
175 // we use the search API
1083
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
176 var url = settings.annotationServerUrl + '/search';
1084
1d3e8f853b9c annotations basically working.
robcast
parents: 1083
diff changeset
177 var pageUrl = getAnnotationPageUrl(data);
1083
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
178 // send authentication token in header
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
179 headers = {
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
180 'x-annotator-auth-token' : data.annotationToken
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
181 };
1086
782593171adb comments for annotations plugin.
robcast
parents: 1084
diff changeset
182 // get only 20 annotations with this url
1083
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
183 var query = {
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
184 limit : 20,
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
185 uri : pageUrl
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
186 };
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
187 $.ajax(url, {
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
188 dataType : 'json',
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
189 data : query,
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
190 headers : headers,
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
191 success : function(annotData, annotStatus) {
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
192 console.debug("got annotation data=", annotData);
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
193 data.annotationData = annotData;
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
194 parseAnnotations(data, annotData);
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
195 renderAnnotations(data);
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 };
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
199
1086
782593171adb comments for annotations plugin.
robcast
parents: 1084
diff changeset
200 /**
782593171adb comments for annotations plugin.
robcast
parents: 1084
diff changeset
201 * parse all JSON-annotations in annotationData.rows and put in data.annotations
782593171adb comments for annotations plugin.
robcast
parents: 1084
diff changeset
202 */
1083
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
203 var parseAnnotations = function(data, annotationData) {
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
204 var annotations = [];
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
205 for (var i = 0; i < annotationData.rows.length; ++i) {
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
206 var ann = annotationData.rows[i];
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
207 var annot = parseAnnotation(ann)
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
208 if (annot != null) {
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
209 annotations.push(annot);
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
210 }
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
211 }
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
212 data.annotations = annotations;
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
213 };
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
214
1086
782593171adb comments for annotations plugin.
robcast
parents: 1084
diff changeset
215 /**
782593171adb comments for annotations plugin.
robcast
parents: 1084
diff changeset
216 * Parse a JSON-annotation.
782593171adb comments for annotations plugin.
robcast
parents: 1084
diff changeset
217 *
782593171adb comments for annotations plugin.
robcast
parents: 1084
diff changeset
218 * Returns an annotation object.
782593171adb comments for annotations plugin.
robcast
parents: 1084
diff changeset
219 */
1083
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
220 var parseAnnotation = function(ann) {
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
221 // TODO: check validity of annotation data
1084
1d3e8f853b9c annotations basically working.
robcast
parents: 1083
diff changeset
222 if (ann.areas != null && ann.areas.length > 0) {
1d3e8f853b9c annotations basically working.
robcast
parents: 1083
diff changeset
223 var area = ann.areas[0];
1086
782593171adb comments for annotations plugin.
robcast
parents: 1084
diff changeset
224 // currently only point annotations
1084
1d3e8f853b9c annotations basically working.
robcast
parents: 1083
diff changeset
225 var pos = geom.position(area.x, area.y);
1083
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
226 return newAnnotation(pos, ann.text, ann.id, ann.uri, ann.user);
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
227 }
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
228 return null;
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
229 };
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
230
1086
782593171adb comments for annotations plugin.
robcast
parents: 1084
diff changeset
231 /**
782593171adb comments for annotations plugin.
robcast
parents: 1084
diff changeset
232 * Store an annotation on the annotation server.
782593171adb comments for annotations plugin.
robcast
parents: 1084
diff changeset
233 */
1083
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
234 var storeAnnotation = function(data, annotation) {
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
235 console.debug("storeAnnotation:", annotation);
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
236 var settings = data.settings;
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
237 var url = settings.annotationServerUrl + '/annotations';
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
238 var pageUrl = getAnnotationPageUrl(data);
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
239 // send authentication token in header
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
240 headers = {
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
241 'x-annotator-auth-token' : data.annotationToken
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
242 };
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
243 // create annotation object to send
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
244 var annotData = {
1084
1d3e8f853b9c annotations basically working.
robcast
parents: 1083
diff changeset
245 areas : [{
1d3e8f853b9c annotations basically working.
robcast
parents: 1083
diff changeset
246 x : annotation.pos.x,
1d3e8f853b9c annotations basically working.
robcast
parents: 1083
diff changeset
247 y : annotation.pos.y
1d3e8f853b9c annotations basically working.
robcast
parents: 1083
diff changeset
248 }],
1083
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
249 text : annotation.text,
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
250 uri : pageUrl,
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
251 user : settings.annotationUser
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
252 };
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
253 var dataString = JSON.stringify(annotData);
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
254 $.ajax(url, {
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
255 type : 'POST',
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
256 dataType : 'json',
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
257 contentType : 'application/json',
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
258 data : dataString,
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
259 headers : headers,
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
260 success : function(annotData, annotStatus) {
1084
1d3e8f853b9c annotations basically working.
robcast
parents: 1083
diff changeset
261 console.debug("sent annotation data, got=", annotData, " status=" + annotStatus);
1083
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
262 var annot = parseAnnotation(annotData);
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
263 // TODO: we have to add the returned data to the real annotation!
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
264 //renderAnnotations(data);
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
265 }
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
266 });
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
267
1086
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 /**
782593171adb comments for annotations plugin.
robcast
parents: 1084
diff changeset
271 * install additional buttons
782593171adb comments for annotations plugin.
robcast
parents: 1084
diff changeset
272 */
1083
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
273 var installButtons = function(data) {
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
274 var settings = data.settings;
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
275 var mode = settings.interactionMode;
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
276 var buttonSettings = settings.buttonSettings[mode];
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
277 // configure buttons through digilib "annotationSet" option
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
278 var buttonSet = settings.annotationSet || annotationSet;
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
279 // set annotationSet to [] or '' for no buttons (when showing annotations only)
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
280 if (buttonSet.length && buttonSet.length > 0) {
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
281 buttonSettings.annotationSet = buttonSet;
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
282 buttonSettings.buttonSets.push('annotationSet');
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
283 }
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
284 };
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
285
1086
782593171adb comments for annotations plugin.
robcast
parents: 1084
diff changeset
286 /**
782593171adb comments for annotations plugin.
robcast
parents: 1084
diff changeset
287 * plugin installation. called by digilib on plugin object.
782593171adb comments for annotations plugin.
robcast
parents: 1084
diff changeset
288 */
1083
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
289 var install = function(plugin) {
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
290 digilib = plugin;
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
291 console.debug('installing annotations plugin. digilib:', digilib);
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
292 // import geometry classes
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
293 geom = digilib.fn.geometry;
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
294 FULL_AREA = geom.rectangle(0, 0, 1, 1);
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
295 // add defaults, actins, buttons
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
296 $.extend(digilib.defaults, defaults);
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
297 $.extend(digilib.actions, actions);
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
298 $.extend(digilib.buttons, buttons);
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
299 };
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
300
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
301 /** plugin initialization */
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
302 var init = function(data) {
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
303 console.debug('initialising annotations plugin. data:', data);
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
304 var $data = $(data);
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
305 // set up
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
306 data.annotations = [];
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
307 if (digilib.plugins.buttons != null) {
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
308 installButtons(data);
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
309 }
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
310 // install event handler
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
311 $data.bind('setup', handleSetup);
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
312 $data.bind('update', handleUpdate);
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
313 };
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
314
1086
782593171adb comments for annotations plugin.
robcast
parents: 1084
diff changeset
315 /**
782593171adb comments for annotations plugin.
robcast
parents: 1084
diff changeset
316 * setup loads all annotations.
782593171adb comments for annotations plugin.
robcast
parents: 1084
diff changeset
317 */
1083
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
318 var handleSetup = function(evt) {
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
319 console.debug("annotations: handleSetup");
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
320 var data = this;
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
321 // load annotations from server
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
322 loadAnnotationToken(data);
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
323 };
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
324
1086
782593171adb comments for annotations plugin.
robcast
parents: 1084
diff changeset
325 /**
782593171adb comments for annotations plugin.
robcast
parents: 1084
diff changeset
326 * update renders all annotations.
782593171adb comments for annotations plugin.
robcast
parents: 1084
diff changeset
327 */
1083
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
328 var handleUpdate = function(evt) {
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
329 console.debug("annotations: handleUpdate");
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
330 var data = this;
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
331 if (data.marks != null) {
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
332 renderAnnotations(data);
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
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
336 // plugin object with name and init
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
337 // shared objects filled by digilib on registration
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
338 var plugin = {
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
339 name : 'annotations',
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
340 install : install,
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
341 init : init,
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
342 buttons : {},
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
343 actions : {},
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
344 fn : {},
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
345 plugins : {}
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
346 };
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
347
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
348 if ($.fn.digilib == null) {
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
349 $.error("jquery.digilib.annotations must be loaded after jquery.digilib!");
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
350 } else {
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
351 $.fn.digilib('plugin', plugin);
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
352 }
bab0bdac6d92 first version of annotations plugin.
robcast
parents:
diff changeset
353 })(jQuery);