source: main/trunk/model-cols-dev/pei-jones/collect/photos/script/image-annotator/js/jquery.annotate2.js@ 28055

Last change on this file since 28055 was 28055, checked in by sjm84, 11 years ago

Some fixes for the photo annotations

File size: 23.1 KB
Line 
1/// <reference path="jquery-1.2.6-vsdoc.js" />
2(function($) {
3
4 var _posCount = 1;
5 var _page;
6 /**
7 * Creates annotations on the given image.
8 * Images are loaded from the "getUrl" propety
9 * passed into the options.
10 **/
11 $.fn.annotateImage = function(options) {
12
13 var opts = $.extend({}, $.fn.annotateImage.defaults, options);
14 var image = this;
15
16 this.image = this;
17 this.mode = 'view';
18
19 // Assign defaults
20 this.getUrl = opts.getUrl;
21 this.saveUrl = opts.saveUrl;
22 this.deleteUrl = opts.deleteUrl;
23 this.editable = opts.editable;
24 this.useAjax = opts.useAjax;
25 this.notes = opts.notes;
26 _page = opts.page;
27
28 //With paged document images, the width and height are not being read, hence the addition of these if statements.
29 if(this.width() == 0 && opts.width !== undefined)
30 this.width(opts.width);
31 if(this.height() == 0 && opts.width !== undefined)
32 this.height(opts.height);
33
34 //get the last ID number and set _posCount to be the next number.
35 if(this.notes !== null && this.notes !== undefined){
36
37 if(this.notes.length > 0){
38
39 var lastId = this.notes[this.notes.length-1].id;
40
41 if(lastId != null && lastId !== undefined)
42 _posCount = lastId + 1;
43 }
44
45 }
46
47 // Add the canvas
48 this.canvas = $('<div class="image-annotate-canvas"><div class="image-annotate-view"></div><div class="image-annotate-edit"><div class="image-annotate-edit-area"></div></div></div>');
49 this.canvas.children('.image-annotate-edit').hide();
50 this.canvas.children('.image-annotate-view').hide();
51 this.image.after(this.canvas);
52
53 // Give the canvas and the container their size and background
54 this.canvas.height(this.height());
55 this.canvas.width(this.width());
56
57 this.canvas.css('background-image', 'url("' + this.attr('src') + '")');
58 this.canvas.children('.image-annotate-view, .image-annotate-edit').height(this.height());
59 this.canvas.children('.image-annotate-view, .image-annotate-edit').width(this.width());
60
61 // Add the behavior: hide/show the notes when hovering the picture
62 this.canvas.hover(function() {
63 if ($(this).children('.image-annotate-edit').css('display') == 'none') {
64 $(this).children('.image-annotate-view').show();
65 }
66 }, function() {
67 $(this).children('.image-annotate-view').hide();
68 });
69
70 this.canvas.children('.image-annotate-view').hover(function() {
71 $(this).show();
72 }, function() {
73 $(this).hide();
74 });
75
76 // load the notes
77 if (this.useAjax) {
78 $.fn.annotateImage.ajaxLoad(this);
79 } else {
80 $.fn.annotateImage.load(this);
81 }
82
83 // Add the "Add a note" button
84 if (this.editable) {
85
86 this.button = $('<button type="button" id="image-annotate-add">Add Note</button>');
87
88 this.button.click(function() {
89 $.fn.annotateImage.add(image);
90 });
91
92 //this.canvas.after(this.button);
93
94 this.br = $('<br/>');
95
96 this.canvas.before(this.button);
97 this.canvas.before(this.br);
98 }
99
100 // Hide the original image
101 this.hide();
102
103 return this;
104 };
105
106 /**
107 * Plugin Defaults
108 **/
109 $.fn.annotateImage.defaults = {
110 getUrl: 'cgi-bin/metadata-server.pl?a=get-metadata&site=' + gs.xsltParams.site_name + '&c=' + gs.cgiParams.c + '&d=' + gs.cgiParams.d + '&metaname=gsimg.Annotation',
111 saveUrl: 'cgi-bin/metadata-server.pl?a=insert-metadata&site=' + gs.xsltParams.site_name + '&c=' + gs.cgiParams.c + '&d=' + gs.cgiParams.d + '&metaname=gsimg.Annotation&metamode=accumulate&metavalue=',
112 deleteUrl: 'cgi-bin/metadata-server.pl?a=remove-metadata&site=' + gs.xsltParams.site_name + '&c=' + gs.cgiParams.c + '&d=' + gs.cgiParams.d + '&metaname=gsimg.Annotation&metamode=override&metavalue=' + 'testing',
113 editable: true,
114 useAjax: true,
115 notes: new Array(),
116 page: 0 //TODO: use this to annotate correct image when dealing with GSDL paged documents//
117 };
118
119 /**
120 * Clears all existing annotations from the image.
121 **/
122 $.fn.annotateImage.clear = function(image) {
123
124 for (var i = 0; i < image.notes.length; i++) {
125 image.notes[image.notes[i]].destroy();
126 }
127 image.notes = new Array();
128 };
129
130 /**
131 * Loads the annotations from the "getUrl" property passed in on the options object.
132 **/
133 $.fn.annotateImage.ajaxLoad = function(image) {
134
135 $.fn.annotateImage.load(image);
136
137 };
138
139 /**
140 * Loads the annotations from the notes property
141 * passed in on the options object (this method is
142 * only used if "useAjax" is set to false.
143 **/
144 $.fn.annotateImage.load = function(image) {
145
146 if(image.notes === undefined || image.notes === null){
147 console.log("No annotations to load in.");
148 image.notes = new Array();
149 }else{
150
151 if(image.notes.length > 0){
152 for (var i = 0; i < image.notes.length; i++) {
153 image.notes[image.notes[i]] = new $.fn.annotateView(image, image.notes[i]);
154 }
155 }else{
156 console.log("No annotations to load in.");
157 }
158 }
159
160 };
161
162 /**
163 * Gets a count of the ticks for the current date.
164 * This is used to ensure that URLs are always
165 * unique and not cached by the browser.
166 **/
167 $.fn.annotateImage.getTicks = function() {
168
169 var now = new Date();
170 return now.getTime();
171 };
172
173 /**
174 * Adds a note to the image.
175 **/
176 $.fn.annotateImage.add = function(image) {
177
178 console.log("image mode = " + image.mode);
179 if (image.mode == 'view') {
180 image.mode = 'edit';
181
182 // Create/prepare the editable note elements
183 var editable = new $.fn.annotateEdit(image);
184
185 $.fn.annotateImage.createSaveButton(editable, image);
186 $.fn.annotateImage.createCancelButton(editable, image);
187 }
188 };
189
190 /**
191 *Creates an OK button on the editable note.
192 **/
193 $.fn.annotateImage.createSaveButton = function(editable, image, note, prev) {
194
195 var ok = $('<a class="image-annotate-edit-ok">OK</a>');
196
197 ok.click(function() {
198
199 var form = $('#image-annotate-edit-form form');
200 var text = $('#image-annotate-text').val();
201 $.fn.annotateImage.appendPosition(form, editable)
202 image.mode = 'view';
203
204 // Save via AJAX
205 if (image.useAjax) {
206
207 var left_pos = editable.area.position().left;
208 var top_pos = editable.area.position().top;
209 var width = editable.area.width();
210 var height = editable.area.height();
211 var id = editable.note.id;
212 //var page = editable.note.page;
213 console.log(editable.note);
214
215 //var json = '{ "top": ' + top_pos + ', "left": ' + left_pos + ', "width": ' + width + ', "height": ' + height + ', "text": "' + text + '", "id": ' + id + ', "editable": true }';
216
217 var json = '{ "top": ' + top_pos + ', "left": ' + left_pos + ', "width": ' + width + ', "height": ' + height + ', "text": "' + text + '", "id": ' + id + ', "editable": true, "page": ' + _page + ' }';
218 var metaname = "gsimg.Annotation";
219 var metadata_server_url = "cgi-bin/metadata-server.pl?";
220
221 var saveIndexUrl = metadata_server_url + "a=set-metadata&site=" + gs.xsltParams.site_name + "&c=" + gs.cgiParams.c + "&d=" + gs.cgiParams.d + "&metaname=" + metaname + "&metavalue=" + json;
222 var saveArchivesUrl = metadata_server_url + "a=set-archives-metadata&site=" + gs.xsltParams.site_name + "&c=" + gs.cgiParams.c + "&d=" + gs.cgiParams.d + "&metaname=" + metaname + "&metavalue=" + json;
223 var saveImportUrl = metadata_server_url + "a=set-import-metadata&site=" + gs.xsltParams.site_name + "&c=" + gs.cgiParams.c + "&d=" + gs.cgiParams.d + "&metaname=" + metaname + "&metavalue=" + json;
224
225 if(prev !== undefined && prev !== null)
226 {
227 var prevJson = '{ "top": ' + prev.top + ', "left": ' + prev.left + ', "width": ' + prev.width + ', "height": ' + height + ', "text": "' + prev.text + '", "id": ' + prev.id + ', "editable": true, "page": ' + _page + ' }';
228 saveIndexUrl += "&prevmetavalue=" + prevJson + "&metamode=override";
229 saveArchivesUrl += "&prevmetavalue=" + prevJson + "&metamode=override";
230 saveImportUrl += "&prevmetavalue=" + prevJson + "&metamode=override";
231 }
232
233 var saveIndexCallBack = {
234 success: function(response)
235 {
236 YAHOO.util.Connect.asyncRequest("GET", gs.xsltParams.library_name + "?a=s&sa=c&c=" + gs.cgiParams.c, null);
237 console.log("Save index metata successful! Note id: " + id);
238 },
239 failed: function(response){ console.log("Failed to save index metadata of note with id: " + id);}
240 };
241
242 var saveImportCallBack =
243 {
244 sucess: function(response){ console.log("Save import metdata successful! Note id: " + id);},
245 failed: function(response){console.log("Failed in save import metadata of note with id: " + id);}
246 };
247
248 var saveArchivesCallBack = {
249 success: function(response){
250 //apparently should rebuild collection here.
251 console.log("Save archives metadata successful! Note id: " + id);
252 },
253 failed: function(response){
254 console.log("Failed to save archives metadata of note with id: " + id);
255 }
256 };
257
258 YAHOO.util.Connect.asyncRequest("GET",saveIndexUrl,saveIndexCallBack);
259 YAHOO.util.Connect.asyncRequest("GET",saveArchivesUrl,saveArchivesUrl);
260 YAHOO.util.Connect.asyncRequest("GET",saveImportUrl,saveImportCallBack);
261 }
262
263 // Add to canvas
264 if (note) {
265 note.resetPosition(editable, text);
266 } else {
267 editable.note.editable = true;
268 note = new $.fn.annotateView(image, editable.note);
269 note.resetPosition(editable, text);
270 image.notes.push(editable.note);
271 }
272
273 editable.destroy();
274 });
275 editable.form.append(ok);
276 };
277
278 /**
279 * Creates a cancel button on the editable note.
280 **/
281 $.fn.annotateImage.createCancelButton = function(editable, image) {
282
283 var cancel = $('<a class="image-annotate-edit-close">Cancel</a>');
284 cancel.click(function() {
285 editable.destroy();
286 image.mode = 'view';
287 });
288 editable.form.append(cancel);
289 };
290
291 $.fn.annotateImage.saveAsHtml = function(image, target) {
292 var element = $(target);
293 var html = "";
294 for (var i = 0; i < image.notes.length; i++) {
295 html += $.fn.annotateImage.createHiddenField("text_" + i, image.notes[i].text);
296 html += $.fn.annotateImage.createHiddenField("top_" + i, image.notes[i].top);
297 html += $.fn.annotateImage.createHiddenField("left_" + i, image.notes[i].left);
298 html += $.fn.annotateImage.createHiddenField("height_" + i, image.notes[i].height);
299 html += $.fn.annotateImage.createHiddenField("width_" + i, image.notes[i].width);
300 }
301 element.html(html);
302 };
303
304 $.fn.annotateImage.createHiddenField = function(name, value) {
305 return '&lt;input type="hidden" name="' + name + '" value="' + value + '" /&gt;<br />';
306 };
307
308 /**
309 * Defines an editable annotation area.
310 **/
311 $.fn.annotateEdit = function(image, note) {
312
313 this.image = image;
314
315 if (note) {
316 this.note = note;
317 } else {
318 var newNote = new Object();
319
320 newNote.id = _posCount;
321 _posCount++;
322 newNote.top = 30;
323 newNote.left = 30;
324 newNote.width = 30;
325 newNote.height = 30;
326 newNote.text = "";
327 newNote.page = _page;
328 this.note = newNote;
329 }
330
331 // Set area
332 var area = image.canvas.children('.image-annotate-edit').children('.image-annotate-edit-area');
333 this.area = area;
334 this.area.css('height', this.note.height + 'px');
335 this.area.css('width', this.note.width + 'px');
336 this.area.css('left', this.note.left + 'px');
337 this.area.css('top', this.note.top + 'px');
338
339 // Show the edition canvas and hide the view canvas
340 image.canvas.children('.image-annotate-view').hide();
341 image.canvas.children('.image-annotate-edit').show();
342
343 // Add the note (which we'll load with the form afterwards)
344 var form = $('<div id="image-annotate-edit-form"><form><textarea id="image-annotate-text" name="text" rows="3" cols="30">' + this.note.text + '</textarea></form></div>');
345 this.form = form;
346
347 $('body').append(this.form);
348 this.form.css('left', this.area.offset().left + 'px');
349 this.form.css('top', (parseInt(this.area.offset().top) + parseInt(this.area.height()) + 7) + 'px');
350
351 // Set the area as a draggable/resizable element contained in the image canvas.
352 // Would be better to use the containment option for resizable but buggy
353 area.resizable({
354 handles: 'all',
355
356 resize: function(e, ui) {
357 form.css('left', area.offset().left + 'px');
358 form.css('top', (parseInt(area.offset().top) + parseInt(area.height()) + 2) + 'px');
359 },
360 stop: function(e, ui) {
361 form.css('left', area.offset().left + 'px');
362 form.css('top', (parseInt(area.offset().top) + parseInt(area.height()) + 2) + 'px');
363 }
364 })
365 .draggable({
366 containment: image.canvas,
367 drag: function(e, ui) {
368 form.css('left', area.offset().left + 'px');
369 form.css('top', (parseInt(area.offset().top) + parseInt(area.height()) + 2) + 'px');
370 },
371 stop: function(e, ui) {
372 form.css('left', area.offset().left + 'px');
373 form.css('top', (parseInt(area.offset().top) + parseInt(area.height()) + 2) + 'px');
374 }
375 });
376 return this;
377 };
378
379 /**
380 * Destroys an editable annotation area.
381 **/
382 $.fn.annotateEdit.prototype.destroy = function() {
383
384 this.image.canvas.children('.image-annotate-edit').hide();
385 this.area.resizable('destroy');
386 this.area.draggable('destroy');
387 this.area.css('height', '');
388 this.area.css('width', '');
389 this.area.css('left', '');
390 this.area.css('top', '');
391 this.form.remove();
392 }
393
394 /**
395 * Defines an annotation area.
396 **/
397 $.fn.annotateView = function(image, note) {
398
399 this.image = image;
400
401 this.note = note;
402
403 this.editable = (note.editable && image.editable);
404
405 // Add the area
406 this.area = $('<div class="image-annotate-area' + (this.editable ? ' image-annotate-area-editable' : '') + '"><div></div></div>');
407 image.canvas.children('.image-annotate-view').prepend(this.area);
408
409 // Add the note
410 this.form = $('<div class="image-annotate-note">' + note.text + '</div>');
411 this.form.hide();
412 image.canvas.children('.image-annotate-view').append(this.form);
413 this.form.children('span.actions').hide();
414
415 // Set the position and size of the note
416 this.setPosition();
417
418 // Add the behavior: hide/display the note when hovering the area
419 var annotation = this;
420 this.area.hover(function() {
421 annotation.show();
422 }, function() {
423 annotation.hide();
424 });
425
426 // Edit a note feature
427 if (this.editable) {
428 var form = this;
429 this.area.click(function() {
430 form.edit();
431 });
432 }
433 };
434
435 $.fn.annotateView.prototype.setPosition = function() {
436 /// <summary>
437 /// Sets the position of an annotation.
438 /// </summary>
439 this.area.children('div').height((parseInt(this.note.height) - 2) + 'px');
440 this.area.children('div').width((parseInt(this.note.width) - 2) + 'px');
441 this.area.css('left', (this.note.left) + 'px');
442 this.area.css('top', (this.note.top) + 'px');
443 this.form.css('left', (this.note.left) + 'px');
444 this.form.css('top', (parseInt(this.note.top) + parseInt(this.note.height) + 7) + 'px');
445 };
446
447 $.fn.annotateView.prototype.show = function() {
448 /// <summary>
449 /// Highlights the annotation
450 /// </summary>
451 this.form.fadeIn(250);
452 if (!this.editable) {
453 this.area.addClass('image-annotate-area-hover');
454 } else {
455 this.area.addClass('image-annotate-area-editable-hover');
456 }
457 };
458
459 /**
460 * Removes the highlight from the annotation.
461 **/
462 $.fn.annotateView.prototype.hide = function() {
463
464 this.form.fadeOut(250);
465 this.area.removeClass('image-annotate-area-hover');
466 this.area.removeClass('image-annotate-area-editable-hover');
467 };
468
469 /**
470 * Destroys the annotation.
471 **/
472 $.fn.annotateView.prototype.destroy = function() {
473
474 this.area.remove();
475 this.form.remove();
476 }
477
478 /**
479 * Edits the annotation.
480 **/
481 $.fn.annotateView.prototype.edit = function() {
482
483 if (this.image.mode == 'view') {
484 this.image.mode = 'edit';
485 var annotation = this;
486
487 //Save the previous note (before the edit was made).
488 var storePrevNotes = this.image.notes;
489
490 console.log("this note's id: " + this.note.id);
491
492 var prev = null;
493 for(var i = 0; i < storePrevNotes.length; i++){
494
495 var curr = storePrevNotes[i];
496
497 if(curr.id == this.note.id){
498 prev = curr;
499 }
500 }
501
502 // Create/prepare the editable note elements
503 var editable = new $.fn.annotateEdit(this.image, this.note);
504
505 $.fn.annotateImage.createSaveButton(editable, this.image, annotation,prev);
506
507 // Add the delete button
508 var del = $('<a class="image-annotate-edit-delete">Delete</a>');
509 del.click(function() {
510
511 var form = $('#image-annotate-edit-form form');
512
513 $.fn.annotateImage.appendPosition(form, editable)
514
515 if (annotation.image.useAjax) {
516
517 var left_pos = annotation.note.left;
518 var top_pos = annotation.note.top;
519 var width = annotation.note.width;
520 var height = annotation.note.height;
521 var id = annotation.note.id;
522 var text = annotation.note.text;
523
524 //var page = annotation.note.page;
525
526 var json = '{ "top": ' + top_pos + ', "left": ' + left_pos + ', "width": ' + width + ', "height": ' + height + ', "text": "' + text + '", "id": ' + id + ', "editable": true ' + ', "page": ' + _page + ' }';
527
528 var metaposVal = id-1;
529
530 //make below variables global...
531 var metaname = "gsimg.Annotation";
532
533 var metadata_server_url = "cgi-bin/metadata-server.pl?";
534
535 var removeFromIndexUrl = metadata_server_url + "a=remove-metadata&site=" + gs.xsltParams.site_name + "&c=" + gs.cgiParams.c + "&d=" + gs.cgiParams.d + "&metaname=" + metaname + "&metavalue=" + json + "&metapos=" + metaposVal;
536
537 var removeFromArchivesUrl = metadata_server_url + "a=remove-archives-metadata&site=" + gs.xsltParams.site_name + "&c=" + gs.cgiParams.c + "&d=" + gs.cgiParams.d + "&metaname=" + metaname + "&metavalue=" + json + "&metapos=" + metaposVal;
538
539 var removeFromImportUrl = metadata_server_url + "a=remove-import-metadata&site=" + gs.xsltParams.site_name + "&c=" + gs.cgiParams.c + "&d=" + gs.cgiParams.d + "&metaname=" + metaname + "&metavalue=" + json;
540
541 var indexCallBack = {
542 success: function(response){
543
544 //need to renumber ids so that the correct note is deleted next time
545 for(var i = 0; i < annotation.image.notes.length; i++)
546 {
547 var currNote = annotation.image.notes[i];
548
549 if(currNote.id > id)
550 currNote.id--;
551 }
552 //_posCount--;
553
554 console.log("Index metadata successfully removed. Note id: " + id);
555 },
556 failed: function(){
557 console.log("Failed to delete index metadata of note with id: " + id);
558 }
559 };
560
561 var archiveCallBack = {
562 success: function(response){
563 //need to rebuild collection here.
564 console.log("Metadata successfully removed from archives. Note id: " + id);
565 },
566 failed: function(){
567 console.log("Failed to remove metadata from archives. Note id: " + id);
568 }
569 };
570
571 var importCallBack = {
572 success: function(){},
573 failed: function(){ alert("Failed to delete import metadata of note with id: " + id);}
574 };
575
576 YAHOO.util.Connect.asyncRequest("GET",removeFromIndexUrl,indexCallBack);
577 YAHOO.util.Connect.asyncRequest("GET",removeFromArchivesUrl,archiveCallBack);
578 YAHOO.util.Connect.asyncRequest("GET",removeFromImportUrl,importCallBack);
579
580 }
581
582 annotation.image.mode = 'view';
583 editable.destroy();
584 annotation.destroy();
585
586 });
587 editable.form.append(del);
588
589 $.fn.annotateImage.createCancelButton(editable, this.image);
590 }
591 };
592
593 /**
594 * Appends the annotations coordinates to the given form that is posted to the server.
595 **/
596 $.fn.annotateImage.appendPosition = function(form, editable) {
597
598 var areaFields = $('<input type="hidden" value="' + editable.area.height() + '" name="height"/>' +
599 '<input type="hidden" value="' + editable.area.width() + '" name="width"/>' +
600 '<input type="hidden" value="' + editable.area.position().top + '" name="top"/>' +
601 '<input type="hidden" value="' + editable.area.position().left + '" name="left"/>' +
602 '<input type="hidden" value="' + editable.note.id + '" name="id"/>');
603 form.append(areaFields);
604 }
605
606 /**
607 * Sets the position of an annotation.
608 **/
609 $.fn.annotateView.prototype.resetPosition = function(editable, text) {
610
611 this.form.html(text);
612 this.form.hide();
613
614 // Resize
615 this.area.children('div').height(editable.area.height() + 'px');
616 this.area.children('div').width((editable.area.width() - 2) + 'px');
617 this.area.css('left', (editable.area.position().left) + 'px');
618 this.area.css('top', (editable.area.position().top) + 'px');
619 this.form.css('left', (editable.area.position().left) + 'px');
620 this.form.css('top', (parseInt(editable.area.position().top) + parseInt(editable.area.height()) + 7) + 'px');
621
622 // Save new position to note
623 this.note.top = editable.area.position().top;
624 this.note.left = editable.area.position().left;
625 this.note.height = editable.area.height();
626 this.note.width = editable.area.width();
627 this.note.text = text;
628 this.note.id = editable.note.id;
629 this.editable = true;
630 };
631
632})(jQuery);
Note: See TracBrowser for help on using the repository browser.