annotate webapp/src/main/webapp/jquery/jquery.digilib.annotations.js @ 1101:34f893492adb

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