Mercurial > hg > digilib-old
annotate client/digitallibrary/jquery/jquery.digilib.js @ 655:72acc47f1831 jquery
first try at birdview indicator
author | hertzhaft |
---|---|
date | Fri, 21 Jan 2011 00:33:33 +0100 |
parents | b21c1a539af3 |
children | 436e10669df8 |
rev | line source |
---|---|
604 | 1 /* |
2 * digilib jQuery plugin | |
3 * | |
4 */ | |
655 | 5 |
636 | 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 }; |
655 | 201 |
608 | 202 // affine geometry classes |
203 var geom = dlGeometry(); | |
655 | 204 |
643 | 205 var MAX_ZOOMAREA = geom.rectangle(0, 0, 1, 1); |
655 | 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 } | |
655 | 286 // TODO: keep bird view visible after reload (parameter, cookie?) |
635 | 287 data.settings.isBirdDivVisible = showDiv(data.settings.isBirdDivVisible, data.$birdDiv, show); |
655 | 288 showBirdIndicator(data); |
623 | 289 }, |
655 | 290 |
623 | 291 // goto given page nr (+/-: relative) |
651
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
292 gotoPage : function (data, pageNr) { |
636 | 293 var settings = data.settings; |
623 | 294 var oldpn = settings.pn; |
624 | 295 var pn = setNumValue(settings, "pn", pageNr); |
296 if (pn == null) return false; // nothing happened | |
623 | 297 if (pn < 1) { |
298 alert("no such page (page number too low)"); | |
299 settings.pn = oldpn; | |
300 return false; | |
634 | 301 } |
623 | 302 if (settings.pt) { |
303 if (pn > settings.pt) { | |
304 alert("no such page (page number too high)"); | |
305 settings.pn = oldpn; | |
306 return false; | |
307 } | |
634 | 308 } |
644
ee8ccd84c5f8
reload doesn't take list of changed parameters any more
robcast
parents:
643
diff
changeset
|
309 // reset mk and others(?) |
643 | 310 data.marks = []; |
311 data.zoomArea = MAX_ZOOMAREA; | |
636 | 312 // then reload |
644
ee8ccd84c5f8
reload doesn't take list of changed parameters any more
robcast
parents:
643
diff
changeset
|
313 redisplay(data); |
643 | 314 }, |
655 | 315 |
643 | 316 // zoom by a given factor |
651
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
317 zoomBy : function (data, factor) { |
643 | 318 zoomBy(data, factor); |
647 | 319 }, |
320 | |
321 // zoom out to full page | |
651
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
322 zoomFull : function (data, mode) { |
647 | 323 data.zoomArea = MAX_ZOOMAREA; |
650 | 324 if (mode === 'width') { |
325 data.dlOpts.fitwidth = 1; | |
326 delete data.dlOpts.fitheight; | |
327 } else if (mode === 'height') { | |
328 data.dlOpts.fitheight = 1; | |
329 delete data.dlOpts.fitwidth; | |
330 } else { | |
331 delete data.dlOpts.fitwidth; | |
332 delete data.dlOpts.fitheight; | |
333 } | |
647 | 334 redisplay(data); |
335 }, | |
336 | |
337 // set a mark by clicking (or giving a position) | |
651
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
338 setMark : function (data, mpos) { |
647 | 339 if (mpos == null) { |
340 // interactive | |
341 setMark(data); | |
342 } else { | |
343 // use position | |
344 data.marks.push(pos); | |
345 redisplay(data); | |
346 } | |
650 | 347 }, |
348 | |
349 // remove the last mark | |
651
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
350 removeMark : function (data) { |
650 | 351 data.marks.pop(); |
352 redisplay(data); | |
604 | 353 } |
650 | 354 |
605 | 355 }; |
604 | 356 |
608 | 357 // returns parameters from page url |
607 | 358 var parseQueryParams = function() { |
636 | 359 return parseQueryString(window.location.search.slice(1)); |
360 }; | |
655 | 361 |
636 | 362 // returns parameters from embedded img-element |
607 | 363 var parseImgParams = function($elem) { |
613
53ee659e2d00
keeping img tag for embedded mode seems to work now
robcast
parents:
612
diff
changeset
|
364 var src = $elem.find('img').first().attr('src'); |
53ee659e2d00
keeping img tag for embedded mode seems to work now
robcast
parents:
612
diff
changeset
|
365 if (!src) { |
53ee659e2d00
keeping img tag for embedded mode seems to work now
robcast
parents:
612
diff
changeset
|
366 return null; |
53ee659e2d00
keeping img tag for embedded mode seems to work now
robcast
parents:
612
diff
changeset
|
367 } |
607 | 368 var pos = src.indexOf('?'); |
369 var query = (pos < 0) ? '' : src.substring(pos + 1); | |
370 var scalerUrl = src.substring(0, pos); | |
627 | 371 var params = parseQueryString(query); |
372 params.scalerBaseUrl = scalerUrl; | |
373 return params; | |
636 | 374 }; |
607 | 375 |
608 | 376 // parses query parameter string into parameter object |
607 | 377 var parseQueryString = function(query) { |
378 var pairs = query.split("&"); | |
627 | 379 var params = {}; |
636 | 380 //var keys = []; |
607 | 381 for (var i = 0; i < pairs.length; i++) { |
382 var pair = pairs[i].split("="); | |
383 if (pair.length === 2) { | |
627 | 384 params[pair[0]] = pair[1]; |
636 | 385 //keys.push(pair[0]); |
634 | 386 } |
636 | 387 } |
627 | 388 return params; |
636 | 389 }; |
604 | 390 |
643 | 391 // returns a query string from key names from a parameter hash (ignoring if the same value is in defaults) |
392 var getParamString = function (settings, keys, defaults) { | |
615 | 393 var paramString = ''; |
650 | 394 var nx = false; |
615 | 395 for (i = 0; i < keys.length; ++i) { |
396 var key = keys[i]; | |
643 | 397 if ((settings[key] != null) && ((defaults == null) || (settings[key] !== defaults[key]))) { |
615 | 398 // first param gets no '&' |
650 | 399 if (nx) { |
400 paramString += '&'; | |
401 } else { | |
402 nx = true; | |
403 } | |
615 | 404 // add parm=val |
405 paramString += key + '=' + settings[key]; | |
643 | 406 } |
615 | 407 } |
408 return paramString; | |
631 | 409 }; |
410 | |
636 | 411 // returns URL and query string for Scaler |
412 var getScalerUrl = function (data) { | |
653 | 413 var settings = data.settings; |
652 | 414 if (settings.scalerBaseUrl == null) { |
415 alert("ERROR: URL of digilib Scaler servlet missing!"); | |
416 } | |
636 | 417 var keys = settings.scalerParamNames; |
643 | 418 var queryString = getParamString(settings, keys, defaults); |
636 | 419 var url = settings.scalerBaseUrl + '?' + queryString; |
420 return url; | |
421 }; | |
422 | |
423 // 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
|
424 var getDigilibUrl = function (data) { |
643 | 425 packParams(data); |
636 | 426 var settings = data.settings; |
644
ee8ccd84c5f8
reload doesn't take list of changed parameters any more
robcast
parents:
643
diff
changeset
|
427 var queryString = getParamString(settings, settings.digilibParamNames, defaults); |
636 | 428 var url = window.location.toString(); |
429 var pos = url.indexOf('?'); | |
430 var baseUrl = url.substring(0, pos); | |
431 var newurl = baseUrl + '?' + queryString; | |
432 return newurl; | |
433 }; | |
434 | |
655 | 435 // processes some parameters into objects and stuff |
631 | 436 var unpackParams = function (data) { |
437 var settings = data.settings; | |
636 | 438 // zoom area |
631 | 439 var zoomArea = geom.rectangle(settings.wx, settings.wy, settings.ww, settings.wh); |
643 | 440 data.zoomArea = zoomArea; |
636 | 441 // marks |
631 | 442 var marks = []; |
650 | 443 if (settings.mk) { |
444 var mk = settings.mk; | |
445 if (mk.indexOf(";") >= 0) { | |
446 var pa = mk.split(";"); // old format with ";" | |
447 } else { | |
448 var pa = mk.split(","); // new format | |
449 } | |
450 for (var i = 0; i < pa.length ; i++) { | |
451 var pos = pa[i].split("/"); | |
452 if (pos.length > 1) { | |
453 marks.push(geom.position(pos[0], pos[1])); | |
454 } | |
633 | 455 } |
631 | 456 } |
643 | 457 data.marks = marks; |
650 | 458 // mo (Scaler flags) |
459 var flags = {}; | |
460 if (settings.mo) { | |
461 var pa = settings.mo.split(","); | |
462 for (var i = 0; i < pa.length ; i++) { | |
463 flags[pa[i]] = pa[i]; | |
464 } | |
465 } | |
466 data.scalerFlags = flags; | |
467 // clop (digilib options) | |
468 var opts = {}; | |
469 if (settings.clop) { | |
470 var pa = settings.clop.split(","); | |
471 for (var i = 0; i < pa.length ; i++) { | |
472 opts[pa[i]] = pa[i]; | |
473 } | |
474 } | |
475 data.dlOpts = opts; | |
655 | 476 }; |
477 | |
631 | 478 // put objects back into parameters |
479 var packParams = function (data) { | |
480 var settings = data.settings; | |
636 | 481 // zoom area |
643 | 482 if (data.zoomArea) { |
647 | 483 settings.wx = cropFloat(data.zoomArea.x); |
484 settings.wy = cropFloat(data.zoomArea.y); | |
485 settings.ww = cropFloat(data.zoomArea.width); | |
486 settings.wh = cropFloat(data.zoomArea.height); | |
631 | 487 } |
636 | 488 // marks |
643 | 489 if (data.marks) { |
650 | 490 settings.mk = ''; |
643 | 491 for (var i = 0; i < data.marks.length; i++) { |
650 | 492 if (i) { |
493 settings.mk += ','; | |
494 } | |
495 settings.mk += cropFloat(data.marks[i].x).toString() + '/' + cropFloat(data.marks[i].y).toString(); | |
631 | 496 } |
650 | 497 } |
498 // Scaler flags | |
499 if (data.scalerFlags) { | |
500 var mo = ''; | |
501 for (var f in data.scalerFlags) { | |
502 if (mo) { | |
503 mo += ','; | |
504 } | |
505 mo += f; | |
506 } | |
507 settings.mo = mo; | |
508 } | |
509 // digilib options | |
510 if (data.dlOpts) { | |
511 var clop = ''; | |
512 for (var o in data.dlOpts) { | |
513 if (clop) { | |
514 clop += ','; | |
515 } | |
516 clop += o; | |
517 } | |
518 settings.clop = clop; | |
631 | 519 } |
520 }; | |
655 | 521 |
609 | 522 // returns maximum size for scaler img in fullscreen mode |
523 var getFullscreenImgSize = function($elem) { | |
524 var winH = $(window).height(); | |
525 var winW = $(window).width(); | |
526 // TODO: account for borders? | |
527 return geom.size(winW, winH); | |
608 | 528 }; |
655 | 529 |
622 | 530 // (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
|
531 var redisplay = function (data) { |
636 | 532 var settings = data.settings; |
533 if (settings.interactionMode === 'fullscreen') { | |
534 // 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
|
535 var url = getDigilibUrl(data); |
636 | 536 var history = window.history; |
537 if (typeof(history.pushState) === 'function') { | |
538 console.debug("we could modify history, but we don't..."); | |
539 } | |
540 window.location = url; | |
541 } else { | |
542 // embedded mode -- just change img src | |
543 var url = getScalerUrl(data); | |
544 data.$img.attr('src', url); | |
545 } | |
622 | 546 }; |
547 | |
608 | 548 // creates HTML structure for digilib in elem |
627 | 549 var setupScalerDiv = function (data) { |
550 var settings = data.settings; | |
647 | 551 var $elem = data.$elem; |
623 | 552 var $img; |
608 | 553 if (settings.interactionMode === 'fullscreen') { |
609 | 554 // fullscreen |
555 var imgSize = getFullscreenImgSize($elem); | |
650 | 556 // fitwidth/height omits destination height/width |
557 if (data.dlOpts['fitheight'] == null) { | |
558 settings.dw = imgSize.width; | |
559 } | |
560 if (data.dlOpts['fitwidth'] == null) { | |
561 settings.dh = imgSize.height; | |
562 } | |
613
53ee659e2d00
keeping img tag for embedded mode seems to work now
robcast
parents:
612
diff
changeset
|
563 $img = $('<img/>'); |
636 | 564 var scalerUrl = getScalerUrl(data); |
565 $img.attr('src', scalerUrl); | |
609 | 566 } else { |
613
53ee659e2d00
keeping img tag for embedded mode seems to work now
robcast
parents:
612
diff
changeset
|
567 // embedded mode -- try to keep img tag |
623 | 568 $img = $elem.find('img'); |
613
53ee659e2d00
keeping img tag for embedded mode seems to work now
robcast
parents:
612
diff
changeset
|
569 if ($img.length > 0) { |
53ee659e2d00
keeping img tag for embedded mode seems to work now
robcast
parents:
612
diff
changeset
|
570 console.debug("img detach:",$img); |
53ee659e2d00
keeping img tag for embedded mode seems to work now
robcast
parents:
612
diff
changeset
|
571 $img.detach(); |
53ee659e2d00
keeping img tag for embedded mode seems to work now
robcast
parents:
612
diff
changeset
|
572 } else { |
53ee659e2d00
keeping img tag for embedded mode seems to work now
robcast
parents:
612
diff
changeset
|
573 $img = $('<img/>'); |
636 | 574 var scalerUrl = getScalerUrl(data); |
575 $img.attr('src', scalerUrl); | |
613
53ee659e2d00
keeping img tag for embedded mode seems to work now
robcast
parents:
612
diff
changeset
|
576 } |
608 | 577 } |
613
53ee659e2d00
keeping img tag for embedded mode seems to work now
robcast
parents:
612
diff
changeset
|
578 // create new html |
53ee659e2d00
keeping img tag for embedded mode seems to work now
robcast
parents:
612
diff
changeset
|
579 $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
|
580 var $scaler = $('<div class="scaler"/>'); |
53ee659e2d00
keeping img tag for embedded mode seems to work now
robcast
parents:
612
diff
changeset
|
581 $elem.append($scaler); |
53ee659e2d00
keeping img tag for embedded mode seems to work now
robcast
parents:
612
diff
changeset
|
582 $scaler.append($img); |
622 | 583 $img.addClass('pic'); |
649 | 584 data.$scaler = $scaler; |
636 | 585 data.$img = $img; |
586 $img.load(scalerImgLoadedHandler(data)); | |
608 | 587 }; |
615 | 588 |
610 | 589 // creates HTML structure for buttons in elem |
628
1f077f8afc5e
changed methods to pass "data" around instead of "settings"
robcast
parents:
627
diff
changeset
|
590 var setupButtons = function (data, actionGroup) { |
647 | 591 var $elem = data.$elem; |
628
1f077f8afc5e
changed methods to pass "data" around instead of "settings"
robcast
parents:
627
diff
changeset
|
592 var settings = data.settings; |
610 | 593 if (settings.interactionMode === 'fullscreen') { |
594 // fullscreen -- create new | |
612 | 595 var $buttonsDiv = $('<div class="buttons"></div>'); |
596 $elem.append($buttonsDiv); | |
618
09167ef1512e
some refactoring, onclick handlers now work with arguments
hertzhaft
parents:
617
diff
changeset
|
597 var actionNames = settings[actionGroup]; |
09167ef1512e
some refactoring, onclick handlers now work with arguments
hertzhaft
parents:
617
diff
changeset
|
598 for (var i = 0; i < actionNames.length; i++) { |
09167ef1512e
some refactoring, onclick handlers now work with arguments
hertzhaft
parents:
617
diff
changeset
|
599 var actionName = actionNames[i]; |
651
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
600 var buttonSettings = settings.buttons[actionName]; |
612 | 601 // construct the button html |
602 var $button = $('<div class="button"></div>'); | |
603 var $a = $('<a/>'); | |
614 | 604 var $img = $('<img class="button"/>'); |
612 | 605 $buttonsDiv.append($button); |
606 $button.append($a); | |
607 $a.append($img); | |
608 // add attributes and bindings | |
651
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
609 $button.attr('title', buttonSettings.tooltip); |
618
09167ef1512e
some refactoring, onclick handlers now work with arguments
hertzhaft
parents:
617
diff
changeset
|
610 $button.addClass('button-' + actionName); |
635 | 611 // create handler for the buttons |
649 | 612 $a.bind('click.digilib', (function () { |
651
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
613 // 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
|
614 var action = buttonSettings.onclick; |
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
615 if ($.isArray(action)) { |
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
616 // the handler function calls digilib with action and parameters |
649 | 617 return function (evt) { |
651
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
618 console.debug('click action=', action, ' evt=', evt); |
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
619 $elem.digilib.apply($elem, action); |
649 | 620 return false; |
619 | 621 }; |
635 | 622 } else { |
651
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
623 // the handler function calls digilib with action |
649 | 624 return function (evt) { |
651
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
625 console.debug('click action=', action, ' evt=', evt); |
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
626 $elem.digilib(action); |
649 | 627 return false; |
635 | 628 }; |
629 } | |
630 })()); | |
651
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
631 $img.attr('src', settings.buttonsImagePath + buttonSettings.img); |
637 | 632 } |
611 | 633 } |
612 | 634 return $buttonsDiv; |
610 | 635 }; |
615 | 636 |
637 // creates HTML structure for the bird's eye view in elem | |
638 | 638 var setupBirdDiv = function (data) { |
647 | 639 var $elem = data.$elem; |
628
1f077f8afc5e
changed methods to pass "data" around instead of "settings"
robcast
parents:
627
diff
changeset
|
640 var settings = data.settings; |
615 | 641 // use only the relevant parameters |
642 var keys = ['fn','pn','dw','dh']; | |
639 | 643 var birdSettings = $.extend({}, settings, settings.birdDivOptions); |
628
1f077f8afc5e
changed methods to pass "data" around instead of "settings"
robcast
parents:
627
diff
changeset
|
644 var birdUrl = settings.scalerBaseUrl + '?' + getParamString(birdSettings, keys); |
615 | 645 // the bird's eye div |
625 | 646 var $birdviewDiv = $('<div class="birdview" style="display:none"/>'); |
615 | 647 // the detail indicator frame |
655 | 648 var $birdzoomDiv = $('<div class="birdzoom" style="position: absolute; background-color: transparent;"/>'); |
615 | 649 // the small image |
650 var $birdImg = $('<img class="birdimg"/>'); | |
651 $elem.append($birdviewDiv); | |
652 $birdviewDiv.append($birdzoomDiv); | |
653 $birdviewDiv.append($birdImg); | |
654 $birdImg.attr('src', birdUrl); | |
640
fcb887110471
enabling bird's eye view in options now even works...
robcast
parents:
639
diff
changeset
|
655 if (data.settings.isBirdDivVisible) { |
fcb887110471
enabling bird's eye view in options now even works...
robcast
parents:
639
diff
changeset
|
656 $birdviewDiv.fadeIn(); |
fcb887110471
enabling bird's eye view in options now even works...
robcast
parents:
639
diff
changeset
|
657 } |
635 | 658 data.$birdDiv = $birdviewDiv; |
640
fcb887110471
enabling bird's eye view in options now even works...
robcast
parents:
639
diff
changeset
|
659 }; |
615 | 660 |
625 | 661 // 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
|
662 var setupAboutDiv = function (data) { |
647 | 663 var $elem = data.$elem; |
628
1f077f8afc5e
changed methods to pass "data" around instead of "settings"
robcast
parents:
627
diff
changeset
|
664 var settings = data.settings; |
625 | 665 var $aboutDiv = $('<div class="about" style="display:none"/>'); |
616 | 666 var $header = $('<p>Digilib Graphic Viewer</p>'); |
667 var $link = $('<a/>'); | |
668 var $logo = $('<img class="logo" title="digilib"/>'); | |
669 var $content = $('<p/>'); | |
670 $elem.append($aboutDiv); | |
671 $aboutDiv.append($header); | |
672 $aboutDiv.append($link); | |
673 $aboutDiv.append($content); | |
674 $link.append($logo); | |
675 $logo.attr('src', settings.logoUrl); | |
635 | 676 $link.attr('href', settings.homeUrl); |
616 | 677 $content.text('Version: ' + settings.version); |
635 | 678 // click hides |
649 | 679 $aboutDiv.bind('click.digilib', function () { |
646 | 680 settings.isAboutDivVisible = showDiv(settings.isAboutDivVisible, $aboutDiv, 0); |
649 | 681 return false; |
682 }); | |
635 | 683 data.$aboutDiv = $aboutDiv; |
634 | 684 }; |
685 | |
635 | 686 // shows some window e.g. 'about' (toggle visibility if show is null) |
687 var showDiv = function (isVisible, $div, show) { | |
645 | 688 if (show == null) { |
635 | 689 // toggle visibility |
690 isVisible = !isVisible; | |
691 } else { | |
692 // set visibility | |
693 isVisible = show; | |
694 } | |
695 if (isVisible) { | |
696 $div.fadeIn(); | |
697 } else { | |
698 $div.fadeOut(); | |
699 } | |
700 return isVisible; | |
634 | 701 }; |
616 | 702 |
655 | 703 // shows bird view indicator |
704 var showBirdIndicator = function (data) { | |
705 if (!data.settings.isBirdDivVisible || isFullArea(data)) return; | |
706 // TODO: more conditions: original size, pixel by pixel? | |
707 var $birdDiv = data.$birdDiv; | |
708 var $birdImg = $birdDiv.find('img.birdimg'); | |
709 var pos = $birdImg.offset(); | |
710 var birdRect = geom.rectangle(pos.left, pos.top, $birdImg.width(), $birdImg.height()); | |
711 var area = data.zoomArea; | |
712 // TODO: couldn't we do a trafo here? :-) | |
713 var indRect = geom.rectangle( | |
714 birdRect.x + birdRect.width * area.x, | |
715 birdRect.y + birdRect.height * area.y, | |
716 birdRect.width * area.width, | |
717 birdRect.height * area.height | |
718 ); | |
719 var $ind = $birdDiv.find('div.birdzoom'); | |
720 // TODO: set the coordinates all in one call? | |
721 $ind.width(indRect.width); | |
722 $ind.height(indRect.height); | |
723 $ind.offset({ left : indRect.x, top : indRect.y }); | |
724 // TODO: how to override this style with a CSS stylesheet? | |
725 if (!$ind.css('border')) $ind.css('border', '2px solid #ff0000'); | |
726 return; | |
727 }; | |
728 | |
613
53ee659e2d00
keeping img tag for embedded mode seems to work now
robcast
parents:
612
diff
changeset
|
729 // returns function for load event of scaler img |
636 | 730 var scalerImgLoadedHandler = function (data) { |
627 | 731 var settings = data.settings; |
647 | 732 var $elem = data.$elem; |
636 | 733 var $img = data.$img; |
655 | 734 |
633 | 735 return function () { |
627 | 736 console.debug("img loaded! this=", this, " data=", data); |
643 | 737 var area = data.zoomArea; |
627 | 738 // create Transform from current area and picsize |
739 var picpos = $img.offset(); | |
740 var picrect = geom.rectangle(picpos.left, picpos.top, $img.width(), $img.height()); | |
741 var trafo = geom.transform(); | |
742 // subtract area offset and size | |
647 | 743 trafo.concat(trafo.getTranslation(geom.position(- area.x, - area.y))); |
627 | 744 trafo.concat(trafo.getScale(geom.size(1/area.width, 1/area.height))); |
745 // scale to screen size | |
746 trafo.concat(trafo.getScale(picrect)); | |
747 trafo.concat(trafo.getTranslation(picrect)); | |
748 data.imgTrafo = trafo; | |
749 // display marks | |
633 | 750 renderMarks(data); |
627 | 751 //digilib.showArrows(); // show arrow overlays for zoom navigation |
641 | 752 // done -- hide about div --- |
753 // --- why? This only leads to suprise effects when displayed programmatically | |
754 // settings.isAboutDivVisible = showDiv(null, data.$aboutDiv, 0); | |
655 | 755 showBirdIndicator(data); |
613
53ee659e2d00
keeping img tag for embedded mode seems to work now
robcast
parents:
612
diff
changeset
|
756 }; |
53ee659e2d00
keeping img tag for embedded mode seems to work now
robcast
parents:
612
diff
changeset
|
757 }; |
633 | 758 |
759 // place marks on the image | |
760 var renderMarks = function (data) { | |
647 | 761 var $elem = data.$elem; |
643 | 762 var marks = data.marks; |
649 | 763 // TODO: clear marks first(?) |
633 | 764 for (var i = 0; i < marks.length; i++) { |
765 var mark = marks[i]; | |
643 | 766 if (data.zoomArea.containsPosition(mark)) { |
633 | 767 var mpos = data.imgTrafo.transform(mark); |
768 // create mark | |
769 var html = '<div class="mark">'+(i+1)+'</div>'; | |
770 var $mark = $(html); | |
771 $elem.append($mark); | |
772 $mark.offset({ left : mpos.x, top : mpos.y}); | |
773 } | |
774 } | |
775 }; | |
655 | 776 |
647 | 777 // zooms by the given factor |
643 | 778 var zoomBy = function(data, factor) { |
779 var area = data.zoomArea; | |
780 var newarea = area.copy(); | |
781 // scale | |
782 newarea.width /= factor; | |
783 newarea.height /= factor; | |
784 // and recenter | |
785 newarea.x -= 0.5 * (newarea.width - area.width); | |
786 newarea.y -= 0.5 * (newarea.height - area.height); | |
787 newarea = MAX_ZOOMAREA.fit(newarea); | |
788 data.zoomArea = newarea; | |
644
ee8ccd84c5f8
reload doesn't take list of changed parameters any more
robcast
parents:
643
diff
changeset
|
789 redisplay(data); |
643 | 790 }; |
791 | |
647 | 792 // add a mark where clicked |
793 var setMark = function (data) { | |
649 | 794 var $scaler = data.$scaler; |
647 | 795 console.debug("setmark"); |
796 // start event capturing | |
649 | 797 $scaler.one('click.digilib', function (evt) { |
647 | 798 // event handler adding a new mark |
799 console.debug("setmark.click evt=",evt); | |
800 var mpos = geom.position(evt.pageX, evt.pageY); | |
801 var pos = data.imgTrafo.invtransform(mpos); | |
802 data.marks.push(pos); | |
803 redisplay(data); | |
649 | 804 return false; // do we even get here? |
647 | 805 }); |
806 }; | |
807 | |
650 | 808 // sets a key to a value (relative values with +/- if relative=true) |
809 var setNumValue = function(settings, key, value) { | |
810 if (isNumber(value)) return settings[key] = value; | |
811 var sign = value.substring(0,1); | |
812 if (sign === '+' || sign === '-') { | |
813 if (settings[key] == null) { | |
651
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
814 // this isn't perfect but still... |
650 | 815 settings[key] = 0; |
816 } | |
817 settings[key] = parseFloat(settings[key]) + parseFloat(value); | |
818 } else { | |
819 settings[key] = value; | |
820 } | |
821 return settings[key]; | |
822 }; | |
655 | 823 |
824 // auxiliary function (from old dllib.js) | |
825 isFullArea = function(data) { | |
826 var area = data.zoomArea; | |
827 return (area.width == 1.0) && (area.height == 1.0); | |
828 }; | |
829 | |
641 | 830 // auxiliary function (from Douglas Crockford, A.10) |
831 var isNumber = function isNumber(value) { | |
832 return typeof value === 'number' && isFinite(value); | |
833 }; | |
655 | 834 |
631 | 835 // auxiliary function to crop senseless precision |
836 var cropFloat = function (x) { | |
637 | 837 return parseInt(10000 * x, 10) / 10000; |
631 | 838 }; |
655 | 839 |
605 | 840 // hook plugin into jquery |
651
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
841 $.fn.digilib = function(action) { |
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
842 if (actions[action]) { |
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
843 // 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
|
844 var $elem = $(this); |
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
845 var data = $elem.data('digilib'); |
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
846 var args = Array.prototype.slice.call(arguments, 1); |
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
847 args.unshift(data); |
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
848 return actions[action].apply(this, args); |
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
849 } else if (typeof(action) === 'object' || !action) { |
634 | 850 // call init on this |
651
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
851 return actions.init.apply(this, arguments); |
605 | 852 } else { |
651
694bdb86cbad
renamed 'actions' to 'buttons' and 'methods' to 'actions'
robcast
parents:
650
diff
changeset
|
853 $.error( 'action ' + action + ' does not exist on jQuery.digilib' ); |
605 | 854 } |
855 }; | |
604 | 856 |
857 })(jQuery); |