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