annotate webapp/src/main/webapp/jquery/jquery.digilib.annotations.js @ 1100:c5ed20cd24ae

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