annotate webapp/src/main/webapp/jquery/jquery.digilib.annotations.js @ 1105:ccdac39eb3da

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