annotate webapp/src/main/webapp/jquery/jquery.digilib.annotations.js @ 1083:bab0bdac6d92

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