Mercurial > hg > digilib-old
annotate client/digitallibrary/jquery/jquery.digilib.js @ 653:b21c1a539af3 jquery
fixed dumb bug
author | robcast |
---|---|
date | Thu, 20 Jan 2011 20:21:11 +0100 |
parents | 01557c5e0682 |
children | b3c05e1568cf 72acc47f1831 |
rev | line source |
---|---|
604 | 1 /* |
2 * digilib jQuery plugin | |
3 * | |
4 */ | |
636 | 5 |
6 // fallback for console.log calls | |
7 if (typeof(console) === 'undefined') { | |
8 var console = { | |
9 log : function(){}, | |
10 debug : function(){}, | |
11 error : function(){} | |
12 }; | |
13 } | |
604 | 14 |
15 (function($) { | |
651
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
16 var buttons = { |
610 | 17 reference : { |
18 onclick : "javascript:getRefWin()", | |
19 tooltip : "get a reference URL", | |
20 img : "reference.png" | |
21 }, | |
22 zoomin : { | |
643 | 23 onclick : ["zoomBy", 1.4], |
610 | 24 tooltip : "zoom in", |
25 img : "zoom-in.png" | |
26 }, | |
27 zoomout : { | |
643 | 28 onclick : ["zoomBy", 0.7], |
610 | 29 tooltip : "zoom out", |
30 img : "zoom-out.png" | |
31 }, | |
32 zoomarea : { | |
33 onclick : "javascript:zoomArea()", | |
34 tooltip : "zoom area", | |
35 img : "zoom-area.png" | |
36 }, | |
37 zoomfull : { | |
650 | 38 onclick : "zoomFull", |
610 | 39 tooltip : "view the whole image", |
40 img : "zoom-full.png" | |
41 }, | |
42 pagewidth : { | |
650 | 43 onclick : ["zoomFull", "width"], |
610 | 44 tooltip : "page width", |
45 img : "pagewidth.png" | |
46 }, | |
47 back : { | |
623 | 48 onclick : ["gotoPage", "-1"], |
610 | 49 tooltip : "goto previous image", |
50 img : "back.png" | |
51 }, | |
52 fwd : { | |
623 | 53 onclick : ["gotoPage", "+1"], |
610 | 54 tooltip : "goto next image", |
55 img : "fwd.png" | |
56 }, | |
57 page : { | |
58 onclick : "javascript:gotoPageWin()", | |
59 tooltip : "specify image", | |
60 img : "page.png" | |
61 }, | |
62 bird : { | |
635 | 63 onclick : "showBirdDiv", |
610 | 64 tooltip : "show bird's eye view", |
65 img : "birds-eye.png" | |
66 }, | |
67 help : { | |
635 | 68 onclick : "showAboutDiv", |
610 | 69 tooltip : "about Digilib", |
70 img : "help.png" | |
71 }, | |
72 reset : { | |
73 onclick : "javascript:resetImage()", | |
74 tooltip : "reset image", | |
75 img : "reset.png" | |
76 }, | |
77 mark : { | |
647 | 78 onclick : "setMark", |
610 | 79 tooltip : "set a mark", |
80 img : "mark.png" | |
81 }, | |
82 delmark : { | |
650 | 83 onclick : "removeMark", |
610 | 84 tooltip : "delete the last mark", |
85 img : "delmark.png" | |
86 }, | |
87 hmir : { | |
88 onclick : "javascript:mirror('h')", | |
89 tooltip : "mirror horizontally", | |
90 img : "mirror-horizontal.png" | |
91 }, | |
92 vmir : { | |
93 onclick : "javascript:mirror('v')", | |
94 tooltip : "mirror vertically", | |
95 img : "mirror-vertical.png" | |
96 }, | |
97 rot : { | |
98 onclick : "javascript:setParamWin('rot', 'Rotate (0..360) clockwise')", | |
99 tooltip : "rotate image", | |
100 img : "rotate.png" | |
101 }, | |
102 brgt : { | |
103 onclick : "javascript:setParamWin('brgt', 'Brightness (-255..255)')", | |
104 tooltip : "set brightness", | |
105 img : "brightness.png" | |
106 }, | |
107 cont : { | |
108 onclick : "javascript:setParamWin('cont', 'Contrast (0..8)')", | |
109 tooltip : "set contrast", | |
110 img : "contrast.png" | |
111 }, | |
112 rgb : { | |
113 onclick : "javascript:setParamWin('rgb', '...')", | |
114 tooltip : "set rgb values", | |
115 img : "rgb.png" | |
116 }, | |
117 quality : { | |
118 onclick : "javascript:setQualityWin('Quality (0..2)')", | |
119 tooltip : "set image quality", | |
120 img : "quality.png" | |
121 }, | |
122 size : { | |
123 onclick : "javascript:toggleSizeMenu()", | |
124 tooltip : "set page size", | |
125 img : "size.png" | |
126 }, | |
127 calibrationx : { | |
128 onclick : "javascript:calibrate('x')", | |
129 tooltip : "calibrate screen x-ratio", | |
130 img : "calibration-x.png" | |
131 }, | |
132 scale : { | |
133 onclick : "javascript:toggleScaleMenu()", | |
134 tooltip : "change image scale", | |
135 img : "original-size.png" | |
136 }, | |
137 options : { | |
138 onclick : "javascript:toggleOptionDiv()", | |
139 tooltip : "hide options", | |
140 img : "options.png" | |
141 }, | |
142 SEP : { | |
143 img : "sep.png" | |
144 } | |
145 }; | |
617
cd846b5c8be8
toggle view event handlers for bird's eye and 'about windows
hertzhaft
parents:
616
diff
changeset
|
146 |
605 | 147 var defaults = { |
616 | 148 // version of this script |
635 | 149 'version' : 'jquery.digilib.js 0.9', |
616 | 150 // logo url |
151 'logoUrl' : '../img/digilib-logo-text1.png', | |
635 | 152 // homepage url (behind logo) |
153 'homeUrl' : 'http://digilib.berlios.de', | |
610 | 154 // base URL to Scaler servlet |
652 | 155 'scalerBaseUrl' : null, |
610 | 156 // list of Scaler parameters |
627 | 157 'scalerParamNames' : ['fn','pn','dw','dh','ww','wh','wx','wy','ws','mo', |
158 'rot','cont','brgt','rgbm','rgba','ddpi','ddpix','ddpiy'], | |
159 // Scaler parameter defaults | |
643 | 160 'pn' : 1, |
627 | 161 'ww' : 1.0, |
162 'wh' : 1.0, | |
163 'wx' : 0.0, | |
164 'wy' : 0.0, | |
165 'ws' : 1.0, | |
643 | 166 'mo' : '', |
167 'rot' : 0, | |
168 'cont' : 0, | |
169 'brgt' : 0, | |
170 'rgbm' : '0/0/0', | |
171 'rgba' : '0/0/0', | |
172 'ddpi' : null, | |
173 'ddpix' : null, | |
174 'ddpiy' : null, | |
644
ee8ccd84c5f8
reload doesn't take list of changed parameters any more
robcast
parents:
643
diff
changeset
|
175 // list of digilib parameters |
650 | 176 'digilibParamNames' : ['fn','pn','ww','wh','wx','wy','ws','mo','rot','cont','brgt','rgbm','rgba','mk','clop'], |
644
ee8ccd84c5f8
reload doesn't take list of changed parameters any more
robcast
parents:
643
diff
changeset
|
177 // digilib parameter defaults |
643 | 178 'mk' : '', |
650 | 179 'clop' : '', |
643 | 180 // mode of operation: |
181 // fullscreen = take parameters from page URL, keep state in page URL | |
182 // embedded = take parameters from Javascript options, keep state inside object | |
610 | 183 'interactionMode' : 'fullscreen', |
651
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
184 // buttons |
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
185 'buttons' : buttons, |
612 | 186 // path to button images (must end with a slash) |
187 'buttonsImagePath' : '../greyskin/', | |
652 | 188 // button groups |
651
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
189 //'buttonsStandard' : ["reference","zoomin","zoomout","zoomarea","zoomfull","pagewidth","back","fwd","page","bird","SEP","help","reset","options"], |
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
190 'buttonsStandard' : ["reference","zoomin","zoomout","zoomarea","zoomfull","pagewidth","mark","delmark","back","fwd","page","bird","SEP","help","reset","options"], |
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
191 'buttonsSpecial' : ["mark","delmark","hmir","vmir","rot","brgt","cont","rgb","quality","size","calibrationx","scale","SEP","options"], |
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
192 'buttonsCustom' : [], |
616 | 193 // is birdView shown? |
194 'isBirdDivVisible' : false, | |
615 | 195 // dimensions of bird's eye window |
639 | 196 'birdDivOptions' : {'dw' : 200, 'dh' : 200}, |
616 | 197 // is the "about" window shown? |
198 'isAboutDivVisible' : false | |
199 | |
610 | 200 }; |
605 | 201 |
608 | 202 // affine geometry classes |
203 var geom = dlGeometry(); | |
604 | 204 |
643 | 205 var MAX_ZOOMAREA = geom.rectangle(0, 0, 1, 1); |
206 | |
651
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
207 var actions = { |
608 | 208 // digilib initialization |
604 | 209 init : function(options) { |
608 | 210 // settings for this digilib instance are merged from defaults and options |
607 | 211 var settings = $.extend({}, defaults, options); |
636 | 212 var isFullscreen = settings.interactionMode === 'fullscreen'; |
213 var queryParams = {}; | |
607 | 214 if (isFullscreen) { |
215 queryParams = parseQueryParams(); | |
652 | 216 // check scalerBaseUrl |
217 if (settings.scalerBaseUrl == null) { | |
218 // try the host this came from | |
219 var h = window.location.host; | |
220 if (window.location.host) { | |
221 var url = window.location.href; | |
222 // assume the page lives in [webapp]/jquery/ | |
223 var pos = url.indexOf('jquery/'); | |
224 if (pos > 0) { | |
225 settings.scalerBaseUrl = url.substring(0, pos) + 'servlet/Scaler'; | |
226 } | |
227 } | |
228 } | |
636 | 229 } |
604 | 230 return this.each(function() { |
605 | 231 var $elem = $(this); |
232 var data = $elem.data('digilib'); | |
607 | 233 var elemSettings; |
608 | 234 // if the plugin hasn't been initialized yet |
604 | 235 if (!data) { |
605 | 236 // merge query parameters |
607 | 237 if (isFullscreen) { |
238 elemSettings = $.extend({}, settings, queryParams); | |
239 } else { | |
240 elemSettings = $.extend({}, settings, parseImgParams($elem)); | |
634 | 241 } |
617
cd846b5c8be8
toggle view event handlers for bird's eye and 'about windows
hertzhaft
parents:
616
diff
changeset
|
242 // store $(this) element in the settings |
627 | 243 data = { |
647 | 244 $elem : $elem, |
636 | 245 settings : elemSettings, |
246 queryParams : queryParams | |
627 | 247 }; |
605 | 248 // store in data element |
627 | 249 $elem.data('digilib', data); |
604 | 250 } |
633 | 251 unpackParams(data); |
608 | 252 // create HTML structure |
627 | 253 setupScalerDiv(data); |
651
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
254 setupButtons(data, 'buttonsStandard'); |
639 | 255 // bird's eye view creation |
256 if (settings.isBirdDivVisible) { | |
641 | 257 setupBirdDiv(data); |
639 | 258 } |
627 | 259 // about window creation - TODO: could be deferred? restrict to only one item? |
628
1f077f8afc5e
changed methods to pass "data" around instead of "settings"
robcast
parents:
627
diff
changeset
|
260 setupAboutDiv(data); |
604 | 261 }); |
262 }, | |
608 | 263 |
264 // clean up digilib | |
651
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
265 destroy : function(data) { |
604 | 266 return this.each(function(){ |
636 | 267 var $elem = $(this); |
604 | 268 // Namespacing FTW |
605 | 269 $(window).unbind('.digilib'); // unbinds all digilibs(?) |
604 | 270 data.digilib.remove(); |
636 | 271 $elem.removeData('digilib'); |
604 | 272 }); |
617
cd846b5c8be8
toggle view event handlers for bird's eye and 'about windows
hertzhaft
parents:
616
diff
changeset
|
273 }, |
cd846b5c8be8
toggle view event handlers for bird's eye and 'about windows
hertzhaft
parents:
616
diff
changeset
|
274 |
635 | 275 // show or hide the 'about' window |
651
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
276 showAboutDiv : function(data, show) { |
635 | 277 data.settings.isAboutDivVisible = showDiv(data.settings.isAboutDivVisible, data.$aboutDiv, show); |
617
cd846b5c8be8
toggle view event handlers for bird's eye and 'about windows
hertzhaft
parents:
616
diff
changeset
|
278 }, |
cd846b5c8be8
toggle view event handlers for bird's eye and 'about windows
hertzhaft
parents:
616
diff
changeset
|
279 |
cd846b5c8be8
toggle view event handlers for bird's eye and 'about windows
hertzhaft
parents:
616
diff
changeset
|
280 // event handler: toggles the visibility of the bird's eye window |
651
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
281 showBirdDiv : function (data, show) { |
638 | 282 if (data.$birdDiv == null) { |
283 // no bird div -> create | |
284 setupBirdDiv(data); | |
285 } | |
635 | 286 data.settings.isBirdDivVisible = showDiv(data.settings.isBirdDivVisible, data.$birdDiv, show); |
623 | 287 }, |
288 | |
289 // goto given page nr (+/-: relative) | |
651
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
290 gotoPage : function (data, pageNr) { |
636 | 291 var settings = data.settings; |
623 | 292 var oldpn = settings.pn; |
624 | 293 var pn = setNumValue(settings, "pn", pageNr); |
294 if (pn == null) return false; // nothing happened | |
623 | 295 if (pn < 1) { |
296 alert("no such page (page number too low)"); | |
297 settings.pn = oldpn; | |
298 return false; | |
634 | 299 } |
623 | 300 if (settings.pt) { |
301 if (pn > settings.pt) { | |
302 alert("no such page (page number too high)"); | |
303 settings.pn = oldpn; | |
304 return false; | |
305 } | |
634 | 306 } |
644
ee8ccd84c5f8
reload doesn't take list of changed parameters any more
robcast
parents:
643
diff
changeset
|
307 // reset mk and others(?) |
643 | 308 data.marks = []; |
309 data.zoomArea = MAX_ZOOMAREA; | |
636 | 310 // then reload |
644
ee8ccd84c5f8
reload doesn't take list of changed parameters any more
robcast
parents:
643
diff
changeset
|
311 redisplay(data); |
643 | 312 }, |
313 | |
314 // zoom by a given factor | |
651
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
315 zoomBy : function (data, factor) { |
643 | 316 zoomBy(data, factor); |
647 | 317 }, |
318 | |
319 // zoom out to full page | |
651
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
320 zoomFull : function (data, mode) { |
647 | 321 data.zoomArea = MAX_ZOOMAREA; |
650 | 322 if (mode === 'width') { |
323 data.dlOpts.fitwidth = 1; | |
324 delete data.dlOpts.fitheight; | |
325 } else if (mode === 'height') { | |
326 data.dlOpts.fitheight = 1; | |
327 delete data.dlOpts.fitwidth; | |
328 } else { | |
329 delete data.dlOpts.fitwidth; | |
330 delete data.dlOpts.fitheight; | |
331 } | |
647 | 332 redisplay(data); |
333 }, | |
334 | |
335 // set a mark by clicking (or giving a position) | |
651
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
336 setMark : function (data, mpos) { |
647 | 337 if (mpos == null) { |
338 // interactive | |
339 setMark(data); | |
340 } else { | |
341 // use position | |
342 data.marks.push(pos); | |
343 redisplay(data); | |
344 } | |
650 | 345 }, |
346 | |
347 // remove the last mark | |
651
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
348 removeMark : function (data) { |
650 | 349 data.marks.pop(); |
350 redisplay(data); | |
604 | 351 } |
650 | 352 |
605 | 353 }; |
604 | 354 |
608 | 355 // returns parameters from page url |
607 | 356 var parseQueryParams = function() { |
636 | 357 return parseQueryString(window.location.search.slice(1)); |
358 }; | |
607 | 359 |
636 | 360 // returns parameters from embedded img-element |
607 | 361 var parseImgParams = function($elem) { |
613
53ee659e2d00
keeping img tag for embedded mode seems to work now
robcast
parents:
612
diff
changeset
|
362 var src = $elem.find('img').first().attr('src'); |
53ee659e2d00
keeping img tag for embedded mode seems to work now
robcast
parents:
612
diff
changeset
|
363 if (!src) { |
53ee659e2d00
keeping img tag for embedded mode seems to work now
robcast
parents:
612
diff
changeset
|
364 return null; |
53ee659e2d00
keeping img tag for embedded mode seems to work now
robcast
parents:
612
diff
changeset
|
365 } |
607 | 366 var pos = src.indexOf('?'); |
367 var query = (pos < 0) ? '' : src.substring(pos + 1); | |
368 var scalerUrl = src.substring(0, pos); | |
627 | 369 var params = parseQueryString(query); |
370 params.scalerBaseUrl = scalerUrl; | |
371 return params; | |
636 | 372 }; |
607 | 373 |
608 | 374 // parses query parameter string into parameter object |
607 | 375 var parseQueryString = function(query) { |
376 var pairs = query.split("&"); | |
627 | 377 var params = {}; |
636 | 378 //var keys = []; |
607 | 379 for (var i = 0; i < pairs.length; i++) { |
380 var pair = pairs[i].split("="); | |
381 if (pair.length === 2) { | |
627 | 382 params[pair[0]] = pair[1]; |
636 | 383 //keys.push(pair[0]); |
634 | 384 } |
636 | 385 } |
627 | 386 return params; |
636 | 387 }; |
604 | 388 |
643 | 389 // returns a query string from key names from a parameter hash (ignoring if the same value is in defaults) |
390 var getParamString = function (settings, keys, defaults) { | |
615 | 391 var paramString = ''; |
650 | 392 var nx = false; |
615 | 393 for (i = 0; i < keys.length; ++i) { |
394 var key = keys[i]; | |
643 | 395 if ((settings[key] != null) && ((defaults == null) || (settings[key] !== defaults[key]))) { |
615 | 396 // first param gets no '&' |
650 | 397 if (nx) { |
398 paramString += '&'; | |
399 } else { | |
400 nx = true; | |
401 } | |
615 | 402 // add parm=val |
403 paramString += key + '=' + settings[key]; | |
643 | 404 } |
615 | 405 } |
406 return paramString; | |
631 | 407 }; |
408 | |
636 | 409 // returns URL and query string for Scaler |
410 var getScalerUrl = function (data) { | |
653 | 411 var settings = data.settings; |
652 | 412 if (settings.scalerBaseUrl == null) { |
413 alert("ERROR: URL of digilib Scaler servlet missing!"); | |
414 } | |
636 | 415 var keys = settings.scalerParamNames; |
643 | 416 var queryString = getParamString(settings, keys, defaults); |
636 | 417 var url = settings.scalerBaseUrl + '?' + queryString; |
418 return url; | |
419 }; | |
420 | |
421 // returns URL and query string for current digilib | |
644
ee8ccd84c5f8
reload doesn't take list of changed parameters any more
robcast
parents:
643
diff
changeset
|
422 var getDigilibUrl = function (data) { |
643 | 423 packParams(data); |
636 | 424 var settings = data.settings; |
644
ee8ccd84c5f8
reload doesn't take list of changed parameters any more
robcast
parents:
643
diff
changeset
|
425 var queryString = getParamString(settings, settings.digilibParamNames, defaults); |
636 | 426 var url = window.location.toString(); |
427 var pos = url.indexOf('?'); | |
428 var baseUrl = url.substring(0, pos); | |
429 var newurl = baseUrl + '?' + queryString; | |
430 return newurl; | |
431 }; | |
432 | |
631 | 433 // processes some parameters into objects and stuff |
434 var unpackParams = function (data) { | |
435 var settings = data.settings; | |
636 | 436 // zoom area |
631 | 437 var zoomArea = geom.rectangle(settings.wx, settings.wy, settings.ww, settings.wh); |
643 | 438 data.zoomArea = zoomArea; |
636 | 439 // marks |
631 | 440 var marks = []; |
650 | 441 if (settings.mk) { |
442 var mk = settings.mk; | |
443 if (mk.indexOf(";") >= 0) { | |
444 var pa = mk.split(";"); // old format with ";" | |
445 } else { | |
446 var pa = mk.split(","); // new format | |
447 } | |
448 for (var i = 0; i < pa.length ; i++) { | |
449 var pos = pa[i].split("/"); | |
450 if (pos.length > 1) { | |
451 marks.push(geom.position(pos[0], pos[1])); | |
452 } | |
633 | 453 } |
631 | 454 } |
643 | 455 data.marks = marks; |
650 | 456 // mo (Scaler flags) |
457 var flags = {}; | |
458 if (settings.mo) { | |
459 var pa = settings.mo.split(","); | |
460 for (var i = 0; i < pa.length ; i++) { | |
461 flags[pa[i]] = pa[i]; | |
462 } | |
463 } | |
464 data.scalerFlags = flags; | |
465 // clop (digilib options) | |
466 var opts = {}; | |
467 if (settings.clop) { | |
468 var pa = settings.clop.split(","); | |
469 for (var i = 0; i < pa.length ; i++) { | |
470 opts[pa[i]] = pa[i]; | |
471 } | |
472 } | |
473 data.dlOpts = opts; | |
631 | 474 }; |
475 | |
476 // put objects back into parameters | |
477 var packParams = function (data) { | |
478 var settings = data.settings; | |
636 | 479 // zoom area |
643 | 480 if (data.zoomArea) { |
647 | 481 settings.wx = cropFloat(data.zoomArea.x); |
482 settings.wy = cropFloat(data.zoomArea.y); | |
483 settings.ww = cropFloat(data.zoomArea.width); | |
484 settings.wh = cropFloat(data.zoomArea.height); | |
631 | 485 } |
636 | 486 // marks |
643 | 487 if (data.marks) { |
650 | 488 settings.mk = ''; |
643 | 489 for (var i = 0; i < data.marks.length; i++) { |
650 | 490 if (i) { |
491 settings.mk += ','; | |
492 } | |
493 settings.mk += cropFloat(data.marks[i].x).toString() + '/' + cropFloat(data.marks[i].y).toString(); | |
631 | 494 } |
650 | 495 } |
496 // Scaler flags | |
497 if (data.scalerFlags) { | |
498 var mo = ''; | |
499 for (var f in data.scalerFlags) { | |
500 if (mo) { | |
501 mo += ','; | |
502 } | |
503 mo += f; | |
504 } | |
505 settings.mo = mo; | |
506 } | |
507 // digilib options | |
508 if (data.dlOpts) { | |
509 var clop = ''; | |
510 for (var o in data.dlOpts) { | |
511 if (clop) { | |
512 clop += ','; | |
513 } | |
514 clop += o; | |
515 } | |
516 settings.clop = clop; | |
631 | 517 } |
518 }; | |
519 | |
609 | 520 // returns maximum size for scaler img in fullscreen mode |
521 var getFullscreenImgSize = function($elem) { | |
522 var winH = $(window).height(); | |
523 var winW = $(window).width(); | |
524 // TODO: account for borders? | |
525 return geom.size(winW, winH); | |
608 | 526 }; |
527 | |
622 | 528 // (re)load the img from a new scaler URL |
644
ee8ccd84c5f8
reload doesn't take list of changed parameters any more
robcast
parents:
643
diff
changeset
|
529 var redisplay = function (data) { |
636 | 530 var settings = data.settings; |
531 if (settings.interactionMode === 'fullscreen') { | |
532 // update location.href (browser URL) in fullscreen mode | |
644
ee8ccd84c5f8
reload doesn't take list of changed parameters any more
robcast
parents:
643
diff
changeset
|
533 var url = getDigilibUrl(data); |
636 | 534 var history = window.history; |
535 if (typeof(history.pushState) === 'function') { | |
536 console.debug("we could modify history, but we don't..."); | |
537 } | |
538 window.location = url; | |
539 } else { | |
540 // embedded mode -- just change img src | |
541 var url = getScalerUrl(data); | |
542 data.$img.attr('src', url); | |
543 } | |
622 | 544 }; |
545 | |
608 | 546 // creates HTML structure for digilib in elem |
627 | 547 var setupScalerDiv = function (data) { |
548 var settings = data.settings; | |
647 | 549 var $elem = data.$elem; |
623 | 550 var $img; |
608 | 551 if (settings.interactionMode === 'fullscreen') { |
609 | 552 // fullscreen |
553 var imgSize = getFullscreenImgSize($elem); | |
650 | 554 // fitwidth/height omits destination height/width |
555 if (data.dlOpts['fitheight'] == null) { | |
556 settings.dw = imgSize.width; | |
557 } | |
558 if (data.dlOpts['fitwidth'] == null) { | |
559 settings.dh = imgSize.height; | |
560 } | |
613
53ee659e2d00
keeping img tag for embedded mode seems to work now
robcast
parents:
612
diff
changeset
|
561 $img = $('<img/>'); |
636 | 562 var scalerUrl = getScalerUrl(data); |
563 $img.attr('src', scalerUrl); | |
609 | 564 } else { |
613
53ee659e2d00
keeping img tag for embedded mode seems to work now
robcast
parents:
612
diff
changeset
|
565 // embedded mode -- try to keep img tag |
623 | 566 $img = $elem.find('img'); |
613
53ee659e2d00
keeping img tag for embedded mode seems to work now
robcast
parents:
612
diff
changeset
|
567 if ($img.length > 0) { |
53ee659e2d00
keeping img tag for embedded mode seems to work now
robcast
parents:
612
diff
changeset
|
568 console.debug("img detach:",$img); |
53ee659e2d00
keeping img tag for embedded mode seems to work now
robcast
parents:
612
diff
changeset
|
569 $img.detach(); |
53ee659e2d00
keeping img tag for embedded mode seems to work now
robcast
parents:
612
diff
changeset
|
570 } else { |
53ee659e2d00
keeping img tag for embedded mode seems to work now
robcast
parents:
612
diff
changeset
|
571 $img = $('<img/>'); |
636 | 572 var scalerUrl = getScalerUrl(data); |
573 $img.attr('src', scalerUrl); | |
613
53ee659e2d00
keeping img tag for embedded mode seems to work now
robcast
parents:
612
diff
changeset
|
574 } |
608 | 575 } |
613
53ee659e2d00
keeping img tag for embedded mode seems to work now
robcast
parents:
612
diff
changeset
|
576 // create new html |
53ee659e2d00
keeping img tag for embedded mode seems to work now
robcast
parents:
612
diff
changeset
|
577 $elem.empty(); // TODO: should we keep stuff for customization? |
53ee659e2d00
keeping img tag for embedded mode seems to work now
robcast
parents:
612
diff
changeset
|
578 var $scaler = $('<div class="scaler"/>'); |
53ee659e2d00
keeping img tag for embedded mode seems to work now
robcast
parents:
612
diff
changeset
|
579 $elem.append($scaler); |
53ee659e2d00
keeping img tag for embedded mode seems to work now
robcast
parents:
612
diff
changeset
|
580 $scaler.append($img); |
622 | 581 $img.addClass('pic'); |
649 | 582 data.$scaler = $scaler; |
636 | 583 data.$img = $img; |
584 $img.load(scalerImgLoadedHandler(data)); | |
608 | 585 }; |
615 | 586 |
610 | 587 // creates HTML structure for buttons in elem |
628
1f077f8afc5e
changed methods to pass "data" around instead of "settings"
robcast
parents:
627
diff
changeset
|
588 var setupButtons = function (data, actionGroup) { |
647 | 589 var $elem = data.$elem; |
628
1f077f8afc5e
changed methods to pass "data" around instead of "settings"
robcast
parents:
627
diff
changeset
|
590 var settings = data.settings; |
610 | 591 if (settings.interactionMode === 'fullscreen') { |
592 // fullscreen -- create new | |
612 | 593 var $buttonsDiv = $('<div class="buttons"></div>'); |
594 $elem.append($buttonsDiv); | |
618
09167ef1512e
some refactoring, onclick handlers now work with arguments
hertzhaft
parents:
617
diff
changeset
|
595 var actionNames = settings[actionGroup]; |
09167ef1512e
some refactoring, onclick handlers now work with arguments
hertzhaft
parents:
617
diff
changeset
|
596 for (var i = 0; i < actionNames.length; i++) { |
09167ef1512e
some refactoring, onclick handlers now work with arguments
hertzhaft
parents:
617
diff
changeset
|
597 var actionName = actionNames[i]; |
651
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
598 var buttonSettings = settings.buttons[actionName]; |
612 | 599 // construct the button html |
600 var $button = $('<div class="button"></div>'); | |
601 var $a = $('<a/>'); | |
614 | 602 var $img = $('<img class="button"/>'); |
612 | 603 $buttonsDiv.append($button); |
604 $button.append($a); | |
605 $a.append($img); | |
606 // add attributes and bindings | |
651
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
607 $button.attr('title', buttonSettings.tooltip); |
618
09167ef1512e
some refactoring, onclick handlers now work with arguments
hertzhaft
parents:
617
diff
changeset
|
608 $button.addClass('button-' + actionName); |
635 | 609 // create handler for the buttons |
649 | 610 $a.bind('click.digilib', (function () { |
651
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
611 // we create a new closure to capture the value of action |
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
612 var action = buttonSettings.onclick; |
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
613 if ($.isArray(action)) { |
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
614 // the handler function calls digilib with action and parameters |
649 | 615 return function (evt) { |
651
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
616 console.debug('click action=', action, ' evt=', evt); |
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
617 $elem.digilib.apply($elem, action); |
649 | 618 return false; |
619 | 619 }; |
635 | 620 } else { |
651
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
621 // the handler function calls digilib with action |
649 | 622 return function (evt) { |
651
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
623 console.debug('click action=', action, ' evt=', evt); |
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
624 $elem.digilib(action); |
649 | 625 return false; |
635 | 626 }; |
627 } | |
628 })()); | |
651
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
629 $img.attr('src', settings.buttonsImagePath + buttonSettings.img); |
637 | 630 } |
611 | 631 } |
612 | 632 return $buttonsDiv; |
610 | 633 }; |
615 | 634 |
635 // creates HTML structure for the bird's eye view in elem | |
638 | 636 var setupBirdDiv = function (data) { |
647 | 637 var $elem = data.$elem; |
628
1f077f8afc5e
changed methods to pass "data" around instead of "settings"
robcast
parents:
627
diff
changeset
|
638 var settings = data.settings; |
615 | 639 // use only the relevant parameters |
640 var keys = ['fn','pn','dw','dh']; | |
639 | 641 var birdSettings = $.extend({}, settings, settings.birdDivOptions); |
628
1f077f8afc5e
changed methods to pass "data" around instead of "settings"
robcast
parents:
627
diff
changeset
|
642 var birdUrl = settings.scalerBaseUrl + '?' + getParamString(birdSettings, keys); |
615 | 643 // the bird's eye div |
625 | 644 var $birdviewDiv = $('<div class="birdview" style="display:none"/>'); |
615 | 645 // the detail indicator frame |
646 var $birdzoomDiv = $('<div class="birdzoom"/>'); | |
647 // the small image | |
648 var $birdImg = $('<img class="birdimg"/>'); | |
649 $elem.append($birdviewDiv); | |
650 $birdviewDiv.append($birdzoomDiv); | |
651 $birdviewDiv.append($birdImg); | |
652 $birdImg.attr('src', birdUrl); | |
640
fcb887110471
enabling bird's eye view in options now even works...
robcast
parents:
639
diff
changeset
|
653 if (data.settings.isBirdDivVisible) { |
fcb887110471
enabling bird's eye view in options now even works...
robcast
parents:
639
diff
changeset
|
654 $birdviewDiv.fadeIn(); |
fcb887110471
enabling bird's eye view in options now even works...
robcast
parents:
639
diff
changeset
|
655 } |
635 | 656 data.$birdDiv = $birdviewDiv; |
640
fcb887110471
enabling bird's eye view in options now even works...
robcast
parents:
639
diff
changeset
|
657 }; |
615 | 658 |
625 | 659 // creates HTML structure for the about view in elem |
628
1f077f8afc5e
changed methods to pass "data" around instead of "settings"
robcast
parents:
627
diff
changeset
|
660 var setupAboutDiv = function (data) { |
647 | 661 var $elem = data.$elem; |
628
1f077f8afc5e
changed methods to pass "data" around instead of "settings"
robcast
parents:
627
diff
changeset
|
662 var settings = data.settings; |
625 | 663 var $aboutDiv = $('<div class="about" style="display:none"/>'); |
616 | 664 var $header = $('<p>Digilib Graphic Viewer</p>'); |
665 var $link = $('<a/>'); | |
666 var $logo = $('<img class="logo" title="digilib"/>'); | |
667 var $content = $('<p/>'); | |
668 $elem.append($aboutDiv); | |
669 $aboutDiv.append($header); | |
670 $aboutDiv.append($link); | |
671 $aboutDiv.append($content); | |
672 $link.append($logo); | |
673 $logo.attr('src', settings.logoUrl); | |
635 | 674 $link.attr('href', settings.homeUrl); |
616 | 675 $content.text('Version: ' + settings.version); |
635 | 676 // click hides |
649 | 677 $aboutDiv.bind('click.digilib', function () { |
646 | 678 settings.isAboutDivVisible = showDiv(settings.isAboutDivVisible, $aboutDiv, 0); |
649 | 679 return false; |
680 }); | |
635 | 681 data.$aboutDiv = $aboutDiv; |
634 | 682 }; |
683 | |
635 | 684 // shows some window e.g. 'about' (toggle visibility if show is null) |
685 var showDiv = function (isVisible, $div, show) { | |
645 | 686 if (show == null) { |
635 | 687 // toggle visibility |
688 isVisible = !isVisible; | |
689 } else { | |
690 // set visibility | |
691 isVisible = show; | |
692 } | |
693 if (isVisible) { | |
694 $div.fadeIn(); | |
695 } else { | |
696 $div.fadeOut(); | |
697 } | |
698 return isVisible; | |
634 | 699 }; |
616 | 700 |
613
53ee659e2d00
keeping img tag for embedded mode seems to work now
robcast
parents:
612
diff
changeset
|
701 // returns function for load event of scaler img |
636 | 702 var scalerImgLoadedHandler = function (data) { |
627 | 703 var settings = data.settings; |
647 | 704 var $elem = data.$elem; |
636 | 705 var $img = data.$img; |
627 | 706 |
633 | 707 return function () { |
627 | 708 console.debug("img loaded! this=", this, " data=", data); |
643 | 709 var area = data.zoomArea; |
627 | 710 // create Transform from current area and picsize |
711 var picpos = $img.offset(); | |
712 var picrect = geom.rectangle(picpos.left, picpos.top, $img.width(), $img.height()); | |
713 var trafo = geom.transform(); | |
714 // subtract area offset and size | |
647 | 715 trafo.concat(trafo.getTranslation(geom.position(- area.x, - area.y))); |
627 | 716 trafo.concat(trafo.getScale(geom.size(1/area.width, 1/area.height))); |
717 // scale to screen size | |
718 trafo.concat(trafo.getScale(picrect)); | |
719 trafo.concat(trafo.getTranslation(picrect)); | |
720 data.imgTrafo = trafo; | |
721 // display marks | |
633 | 722 renderMarks(data); |
627 | 723 //digilib.showArrows(); // show arrow overlays for zoom navigation |
641 | 724 // done -- hide about div --- |
725 // --- why? This only leads to suprise effects when displayed programmatically | |
726 // settings.isAboutDivVisible = showDiv(null, data.$aboutDiv, 0); | |
613
53ee659e2d00
keeping img tag for embedded mode seems to work now
robcast
parents:
612
diff
changeset
|
727 }; |
53ee659e2d00
keeping img tag for embedded mode seems to work now
robcast
parents:
612
diff
changeset
|
728 }; |
633 | 729 |
730 // place marks on the image | |
731 var renderMarks = function (data) { | |
647 | 732 var $elem = data.$elem; |
643 | 733 var marks = data.marks; |
649 | 734 // TODO: clear marks first(?) |
633 | 735 for (var i = 0; i < marks.length; i++) { |
736 var mark = marks[i]; | |
643 | 737 if (data.zoomArea.containsPosition(mark)) { |
633 | 738 var mpos = data.imgTrafo.transform(mark); |
739 // create mark | |
740 var html = '<div class="mark">'+(i+1)+'</div>'; | |
741 var $mark = $(html); | |
742 $elem.append($mark); | |
743 $mark.offset({ left : mpos.x, top : mpos.y}); | |
744 } | |
745 } | |
746 }; | |
631 | 747 |
647 | 748 // zooms by the given factor |
643 | 749 var zoomBy = function(data, factor) { |
750 var area = data.zoomArea; | |
751 var newarea = area.copy(); | |
752 // scale | |
753 newarea.width /= factor; | |
754 newarea.height /= factor; | |
755 // and recenter | |
756 newarea.x -= 0.5 * (newarea.width - area.width); | |
757 newarea.y -= 0.5 * (newarea.height - area.height); | |
758 newarea = MAX_ZOOMAREA.fit(newarea); | |
759 data.zoomArea = newarea; | |
644
ee8ccd84c5f8
reload doesn't take list of changed parameters any more
robcast
parents:
643
diff
changeset
|
760 redisplay(data); |
643 | 761 }; |
762 | |
647 | 763 // add a mark where clicked |
764 var setMark = function (data) { | |
649 | 765 var $scaler = data.$scaler; |
647 | 766 console.debug("setmark"); |
767 // start event capturing | |
649 | 768 $scaler.one('click.digilib', function (evt) { |
647 | 769 // event handler adding a new mark |
770 console.debug("setmark.click evt=",evt); | |
771 var mpos = geom.position(evt.pageX, evt.pageY); | |
772 var pos = data.imgTrafo.invtransform(mpos); | |
773 data.marks.push(pos); | |
774 redisplay(data); | |
649 | 775 return false; // do we even get here? |
647 | 776 }); |
777 }; | |
778 | |
650 | 779 // sets a key to a value (relative values with +/- if relative=true) |
780 var setNumValue = function(settings, key, value) { | |
781 if (isNumber(value)) return settings[key] = value; | |
782 var sign = value.substring(0,1); | |
783 if (sign === '+' || sign === '-') { | |
784 if (settings[key] == null) { | |
651
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
785 // this isn't perfect but still... |
650 | 786 settings[key] = 0; |
787 } | |
788 settings[key] = parseFloat(settings[key]) + parseFloat(value); | |
789 } else { | |
790 settings[key] = value; | |
791 } | |
792 return settings[key]; | |
793 }; | |
794 | |
641 | 795 // auxiliary function (from Douglas Crockford, A.10) |
796 var isNumber = function isNumber(value) { | |
797 return typeof value === 'number' && isFinite(value); | |
798 }; | |
799 | |
631 | 800 // auxiliary function to crop senseless precision |
801 var cropFloat = function (x) { | |
637 | 802 return parseInt(10000 * x, 10) / 10000; |
631 | 803 }; |
636 | 804 |
605 | 805 // hook plugin into jquery |
651
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
806 $.fn.digilib = function(action) { |
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
807 if (actions[action]) { |
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
808 // call action on this with the remaining arguments (inserting data as first argument) |
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
809 var $elem = $(this); |
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
810 var data = $elem.data('digilib'); |
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
811 var args = Array.prototype.slice.call(arguments, 1); |
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
812 args.unshift(data); |
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
813 return actions[action].apply(this, args); |
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
814 } else if (typeof(action) === 'object' || !action) { |
634 | 815 // call init on this |
651
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
816 return actions.init.apply(this, arguments); |
605 | 817 } else { |
651
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
818 $.error( 'action ' + action + ' does not exist on jQuery.digilib' ); |
605 | 819 } |
820 }; | |
604 | 821 |
822 })(jQuery); |