Mercurial > hg > digilib
comparison 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 |
comparison
equal
deleted
inserted
replaced
643:abf4c8db18c8 | 644:bffc7d8c3641 |
---|---|
11 error : function(){} | 11 error : function(){} |
12 }; | 12 }; |
13 } | 13 } |
14 | 14 |
15 (function($) { | 15 (function($) { |
16 var actions = { | 16 var buttons = { |
17 reference : { | 17 reference : { |
18 onclick : "javascript:getRefWin()", | 18 onclick : "javascript:getRefWin()", |
19 tooltip : "get a reference URL", | 19 tooltip : "get a reference URL", |
20 img : "reference.png" | 20 img : "reference.png" |
21 }, | 21 }, |
181 'clop' : '', | 181 'clop' : '', |
182 // mode of operation: | 182 // mode of operation: |
183 // fullscreen = take parameters from page URL, keep state in page URL | 183 // fullscreen = take parameters from page URL, keep state in page URL |
184 // embedded = take parameters from Javascript options, keep state inside object | 184 // embedded = take parameters from Javascript options, keep state inside object |
185 'interactionMode' : 'fullscreen', | 185 'interactionMode' : 'fullscreen', |
186 // actions | 186 // buttons |
187 'actions' : actions, | 187 'buttons' : buttons, |
188 // path to button images (must end with a slash) | 188 // path to button images (must end with a slash) |
189 'buttonsImagePath' : '../greyskin/', | 189 'buttonsImagePath' : '../greyskin/', |
190 // actions groups | 190 // buttons groups |
191 //'actionsStandard' : ["reference","zoomin","zoomout","zoomarea","zoomfull","pagewidth","back","fwd","page","bird","SEP","help","reset","options"], | 191 //'buttonsStandard' : ["reference","zoomin","zoomout","zoomarea","zoomfull","pagewidth","back","fwd","page","bird","SEP","help","reset","options"], |
192 'actionsStandard' : ["reference","zoomin","zoomout","zoomarea","zoomfull","pagewidth","mark","delmark","back","fwd","page","bird","SEP","help","reset","options"], | 192 'buttonsStandard' : ["reference","zoomin","zoomout","zoomarea","zoomfull","pagewidth","mark","delmark","back","fwd","page","bird","SEP","help","reset","options"], |
193 'actionsSpecial' : ["mark","delmark","hmir","vmir","rot","brgt","cont","rgb","quality","size","calibrationx","scale","SEP","options"], | 193 'buttonsSpecial' : ["mark","delmark","hmir","vmir","rot","brgt","cont","rgb","quality","size","calibrationx","scale","SEP","options"], |
194 'actionsCustom' : [], | 194 'buttonsCustom' : [], |
195 // is birdView shown? | 195 // is birdView shown? |
196 'isBirdDivVisible' : false, | 196 'isBirdDivVisible' : false, |
197 // dimensions of bird's eye window | 197 // dimensions of bird's eye window |
198 'birdDivOptions' : {'dw' : 200, 'dh' : 200}, | 198 'birdDivOptions' : {'dw' : 200, 'dh' : 200}, |
199 // is the "about" window shown? | 199 // is the "about" window shown? |
204 // affine geometry classes | 204 // affine geometry classes |
205 var geom = dlGeometry(); | 205 var geom = dlGeometry(); |
206 | 206 |
207 var MAX_ZOOMAREA = geom.rectangle(0, 0, 1, 1); | 207 var MAX_ZOOMAREA = geom.rectangle(0, 0, 1, 1); |
208 | 208 |
209 var methods = { | 209 var actions = { |
210 // digilib initialization | 210 // digilib initialization |
211 init : function(options) { | 211 init : function(options) { |
212 // settings for this digilib instance are merged from defaults and options | 212 // settings for this digilib instance are merged from defaults and options |
213 var settings = $.extend({}, defaults, options); | 213 var settings = $.extend({}, defaults, options); |
214 var isFullscreen = settings.interactionMode === 'fullscreen'; | 214 var isFullscreen = settings.interactionMode === 'fullscreen'; |
238 $elem.data('digilib', data); | 238 $elem.data('digilib', data); |
239 } | 239 } |
240 unpackParams(data); | 240 unpackParams(data); |
241 // create HTML structure | 241 // create HTML structure |
242 setupScalerDiv(data); | 242 setupScalerDiv(data); |
243 setupButtons(data, 'actionsStandard'); | 243 setupButtons(data, 'buttonsStandard'); |
244 // bird's eye view creation | 244 // bird's eye view creation |
245 if (settings.isBirdDivVisible) { | 245 if (settings.isBirdDivVisible) { |
246 setupBirdDiv(data); | 246 setupBirdDiv(data); |
247 } | 247 } |
248 // about window creation - TODO: could be deferred? restrict to only one item? | 248 // about window creation - TODO: could be deferred? restrict to only one item? |
249 setupAboutDiv(data); | 249 setupAboutDiv(data); |
250 }); | 250 }); |
251 }, | 251 }, |
252 | 252 |
253 // clean up digilib | 253 // clean up digilib |
254 destroy : function() { | 254 destroy : function(data) { |
255 return this.each(function(){ | 255 return this.each(function(){ |
256 var $elem = $(this); | 256 var $elem = $(this); |
257 var data = $elem.data('digilib'); | |
258 // Namespacing FTW | 257 // Namespacing FTW |
259 $(window).unbind('.digilib'); // unbinds all digilibs(?) | 258 $(window).unbind('.digilib'); // unbinds all digilibs(?) |
260 data.digilib.remove(); | 259 data.digilib.remove(); |
261 $elem.removeData('digilib'); | 260 $elem.removeData('digilib'); |
262 }); | 261 }); |
263 }, | 262 }, |
264 | 263 |
265 // show or hide the 'about' window | 264 // show or hide the 'about' window |
266 showAboutDiv : function(show) { | 265 showAboutDiv : function(data, show) { |
267 var $elem = $(this); | |
268 var data = $elem.data('digilib'); | |
269 data.settings.isAboutDivVisible = showDiv(data.settings.isAboutDivVisible, data.$aboutDiv, show); | 266 data.settings.isAboutDivVisible = showDiv(data.settings.isAboutDivVisible, data.$aboutDiv, show); |
270 }, | 267 }, |
271 | 268 |
272 // event handler: toggles the visibility of the bird's eye window | 269 // event handler: toggles the visibility of the bird's eye window |
273 showBirdDiv : function (show) { | 270 showBirdDiv : function (data, show) { |
274 var $elem = $(this); | |
275 var data = $elem.data('digilib'); | |
276 if (data.$birdDiv == null) { | 271 if (data.$birdDiv == null) { |
277 // no bird div -> create | 272 // no bird div -> create |
278 setupBirdDiv(data); | 273 setupBirdDiv(data); |
279 } | 274 } |
280 data.settings.isBirdDivVisible = showDiv(data.settings.isBirdDivVisible, data.$birdDiv, show); | 275 data.settings.isBirdDivVisible = showDiv(data.settings.isBirdDivVisible, data.$birdDiv, show); |
281 }, | 276 }, |
282 | 277 |
283 // goto given page nr (+/-: relative) | 278 // goto given page nr (+/-: relative) |
284 gotoPage : function (pageNr) { | 279 gotoPage : function (data, pageNr) { |
285 var $elem = $(this); | |
286 var data = $elem.data('digilib'); | |
287 var settings = data.settings; | 280 var settings = data.settings; |
288 var oldpn = settings.pn; | 281 var oldpn = settings.pn; |
289 var pn = setNumValue(settings, "pn", pageNr); | 282 var pn = setNumValue(settings, "pn", pageNr); |
290 if (pn == null) return false; // nothing happened | 283 if (pn == null) return false; // nothing happened |
291 if (pn < 1) { | 284 if (pn < 1) { |
306 // then reload | 299 // then reload |
307 redisplay(data); | 300 redisplay(data); |
308 }, | 301 }, |
309 | 302 |
310 // zoom by a given factor | 303 // zoom by a given factor |
311 zoomBy : function (factor) { | 304 zoomBy : function (data, factor) { |
312 var $elem = $(this); | |
313 var data = $elem.data('digilib'); | |
314 zoomBy(data, factor); | 305 zoomBy(data, factor); |
315 }, | 306 }, |
316 | 307 |
317 // zoom out to full page | 308 // zoom out to full page |
318 zoomFull : function (mode) { | 309 zoomFull : function (data, mode) { |
319 var $elem = $(this); | |
320 var data = $elem.data('digilib'); | |
321 data.zoomArea = MAX_ZOOMAREA; | 310 data.zoomArea = MAX_ZOOMAREA; |
322 if (mode === 'width') { | 311 if (mode === 'width') { |
323 data.dlOpts.fitwidth = 1; | 312 data.dlOpts.fitwidth = 1; |
324 delete data.dlOpts.fitheight; | 313 delete data.dlOpts.fitheight; |
325 } else if (mode === 'height') { | 314 } else if (mode === 'height') { |
331 } | 320 } |
332 redisplay(data); | 321 redisplay(data); |
333 }, | 322 }, |
334 | 323 |
335 // set a mark by clicking (or giving a position) | 324 // set a mark by clicking (or giving a position) |
336 setMark : function (mpos) { | 325 setMark : function (data, mpos) { |
337 var $elem = $(this); | |
338 var data = $elem.data('digilib'); | |
339 if (mpos == null) { | 326 if (mpos == null) { |
340 // interactive | 327 // interactive |
341 setMark(data); | 328 setMark(data); |
342 } else { | 329 } else { |
343 // use position | 330 // use position |
345 redisplay(data); | 332 redisplay(data); |
346 } | 333 } |
347 }, | 334 }, |
348 | 335 |
349 // remove the last mark | 336 // remove the last mark |
350 removeMark : function () { | 337 removeMark : function (data) { |
351 var $elem = $(this); | |
352 var data = $elem.data('digilib'); | |
353 data.marks.pop(); | 338 data.marks.pop(); |
354 redisplay(data); | 339 redisplay(data); |
355 } | 340 } |
356 | 341 |
357 }; | 342 }; |
594 var $buttonsDiv = $('<div class="buttons"></div>'); | 579 var $buttonsDiv = $('<div class="buttons"></div>'); |
595 $elem.append($buttonsDiv); | 580 $elem.append($buttonsDiv); |
596 var actionNames = settings[actionGroup]; | 581 var actionNames = settings[actionGroup]; |
597 for (var i = 0; i < actionNames.length; i++) { | 582 for (var i = 0; i < actionNames.length; i++) { |
598 var actionName = actionNames[i]; | 583 var actionName = actionNames[i]; |
599 var actionSettings = settings.actions[actionName]; | 584 var buttonSettings = settings.buttons[actionName]; |
600 // construct the button html | 585 // construct the button html |
601 var $button = $('<div class="button"></div>'); | 586 var $button = $('<div class="button"></div>'); |
602 var $a = $('<a/>'); | 587 var $a = $('<a/>'); |
603 var $img = $('<img class="button"/>'); | 588 var $img = $('<img class="button"/>'); |
604 $buttonsDiv.append($button); | 589 $buttonsDiv.append($button); |
605 $button.append($a); | 590 $button.append($a); |
606 $a.append($img); | 591 $a.append($img); |
607 // add attributes and bindings | 592 // add attributes and bindings |
608 $button.attr('title', actionSettings.tooltip); | 593 $button.attr('title', buttonSettings.tooltip); |
609 $button.addClass('button-' + actionName); | 594 $button.addClass('button-' + actionName); |
610 // create handler for the buttons | 595 // create handler for the buttons |
611 $a.bind('click.digilib', (function () { | 596 $a.bind('click.digilib', (function () { |
612 // we create a new closure to capture the value of method | 597 // we create a new closure to capture the value of action |
613 var method = actionSettings.onclick; | 598 var action = buttonSettings.onclick; |
614 if ($.isArray(method)) { | 599 if ($.isArray(action)) { |
615 // the handler function calls digilib with method and parameters | 600 // the handler function calls digilib with action and parameters |
616 return function (evt) { | 601 return function (evt) { |
617 console.debug('click method=', method, ' evt=', evt); | 602 console.debug('click action=', action, ' evt=', evt); |
618 $elem.digilib.apply($elem, method); | 603 $elem.digilib.apply($elem, action); |
619 return false; | 604 return false; |
620 }; | 605 }; |
621 } else { | 606 } else { |
622 // the handler function calls digilib with method | 607 // the handler function calls digilib with action |
623 return function (evt) { | 608 return function (evt) { |
624 console.debug('click method=', method, ' evt=', evt); | 609 console.debug('click action=', action, ' evt=', evt); |
625 $elem.digilib(method); | 610 $elem.digilib(action); |
626 return false; | 611 return false; |
627 }; | 612 }; |
628 } | 613 } |
629 })()); | 614 })()); |
630 $img.attr('src', settings.buttonsImagePath + actionSettings.img); | 615 $img.attr('src', settings.buttonsImagePath + buttonSettings.img); |
631 } | 616 } |
632 } | 617 } |
633 return $buttonsDiv; | 618 return $buttonsDiv; |
634 }; | 619 }; |
635 | 620 |
781 var setNumValue = function(settings, key, value) { | 766 var setNumValue = function(settings, key, value) { |
782 if (isNumber(value)) return settings[key] = value; | 767 if (isNumber(value)) return settings[key] = value; |
783 var sign = value.substring(0,1); | 768 var sign = value.substring(0,1); |
784 if (sign === '+' || sign === '-') { | 769 if (sign === '+' || sign === '-') { |
785 if (settings[key] == null) { | 770 if (settings[key] == null) { |
786 // this doesn't make much sense but still... | 771 // this isn't perfect but still... |
787 settings[key] = 0; | 772 settings[key] = 0; |
788 } | 773 } |
789 settings[key] = parseFloat(settings[key]) + parseFloat(value); | 774 settings[key] = parseFloat(settings[key]) + parseFloat(value); |
790 } else { | 775 } else { |
791 settings[key] = value; | 776 settings[key] = value; |
802 var cropFloat = function (x) { | 787 var cropFloat = function (x) { |
803 return parseInt(10000 * x, 10) / 10000; | 788 return parseInt(10000 * x, 10) / 10000; |
804 }; | 789 }; |
805 | 790 |
806 // hook plugin into jquery | 791 // hook plugin into jquery |
807 $.fn.digilib = function(method) { | 792 $.fn.digilib = function(action) { |
808 if (methods[method]) { | 793 if (actions[action]) { |
809 // call method on this with the remaining arguments | 794 // call action on this with the remaining arguments (inserting data as first argument) |
810 return methods[method].apply(this, Array.prototype.slice.call(arguments, 1)); | 795 var $elem = $(this); |
811 } else if (typeof(method) === 'object' || !method) { | 796 var data = $elem.data('digilib'); |
797 var args = Array.prototype.slice.call(arguments, 1); | |
798 args.unshift(data); | |
799 return actions[action].apply(this, args); | |
800 } else if (typeof(action) === 'object' || !action) { | |
812 // call init on this | 801 // call init on this |
813 return methods.init.apply(this, arguments); | 802 return actions.init.apply(this, arguments); |
814 } else { | 803 } else { |
815 $.error( 'Method ' + method + ' does not exist on jQuery.digilib' ); | 804 $.error( 'action ' + action + ' does not exist on jQuery.digilib' ); |
816 } | 805 } |
817 }; | 806 }; |
818 | 807 |
819 })(jQuery); | 808 })(jQuery); |