diff client/digitallibrary/jquery/jquery.digilib.js @ 713:c1fa722c570e jquery

more cosmetics: added options to validate with http://www.jslint.com still doesn't fully validate because of "== null" and if without block. removed unnecessary semicolons after if and for.
author robcast
date Sun, 30 Jan 2011 17:58:56 +0100
parents eab17859be79
children 667a316edb89
line wrap: on
line diff
--- a/client/digitallibrary/jquery/jquery.digilib.js	Sun Jan 30 16:37:05 2011 +0100
+++ b/client/digitallibrary/jquery/jquery.digilib.js	Sun Jan 30 17:58:56 2011 +0100
@@ -1,8 +1,29 @@
-/*
+/* Copyright (c) 2011 Martin Raspe, Robert Casties
+ 
+This program is free software: you can redistribute it and/or modify
+it under the terms of the GNU Lesser General Public License as published by
+the Free Software Foundation, either version 2 of the License, or
+(at your option) any later version.
+
+This program is distributed in the hope that it will be useful,
+but WITHOUT ANY WARRANTY; without even the implied warranty of
+MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+GNU Lesser General Public License for more details.
+
+You should have received a copy of the GNU Lesser General Public License
+along with this program.  If not, see <http://www.gnu.org/licenses/>.
+ 
+Authors:
+  Martin Raspe, Robert Casties, 11.1.2011
+*/
+
+/**
  * digilib jQuery plugin
- *
+ * 
  */
 
+/*jslint browser: true, debug: true, forin: true */
+
 // fallback for console.log calls
 if (typeof(console) === 'undefined') {
     var console = {
@@ -11,7 +32,7 @@
         error : function(){}
         };
     var customConsole = true;
-};
+}
 
 (function($) {
     var buttons = {
@@ -265,7 +286,7 @@
                         params = queryParams;
                     } else {
                         params = parseImgParams($elem);
-                    };
+                    }
                     // store $(this) element in the settings
                     elemSettings = $.extend({}, settings, params);
                     data = {
@@ -282,11 +303,11 @@
                 // add buttons
                 for (var i = 0; i < elemSettings.visibleButtonSets; ++i) {
                     showButtons(data, true, i);
-                    };
+                    }
                 // bird's eye view creation
                 if (elemSettings.isBirdDivVisible) {
                     setupBirdDiv(data);
-                    };
+                    }
                 // about window creation - TODO: could be deferred? restrict to only one item?
                 setupAboutDiv(data);
                 // TODO: the actual moving code
@@ -475,15 +496,15 @@
             for (var i = 0; i < paramNames.length; i++) {
                 var paramName = paramNames[i];
                 delete settings[paramName];
-                };
+                }
             // fullscreen: restore only fn/pn parameters 
             if (settings.interactionMode === 'fullscreen') {
-                settings['fn'] = params.fn || ''; // no default defined
-                settings['pn'] = params.pn || defaults.pn;
+                settings.fn = params.fn || ''; // no default defined
+                settings.pn = params.pn || defaults.pn;
             // embedded: restore original parameters 
             } else {
                 $.extend(settings, params);
-                };
+                }
             // TODO: should we really reset all user preferences here?
             settings.isBirdDivVisible = false;
             settings.visibleButtonSets = 1;
@@ -498,7 +519,7 @@
                 url = getDigilibUrl(data);
             } else {
                 url = getScalerUrl(data);
-                };
+                }
             if (noprompt == null) {
                 window.prompt("URL reference to the current view", url);
             }
@@ -577,7 +598,7 @@
         var settings = data.settings;
         if (settings.scalerBaseUrl == null) {
             alert("ERROR: URL of digilib Scaler servlet missing!");
-            };
+            }
         packParams(data);
         var keys = settings.scalerParamNames;
         var queryString = getParamString(settings, keys, defaults);
@@ -611,14 +632,14 @@
                 var pa = mk.split(";");    // old format with ";"
             } else {
                 var pa = mk.split(",");    // new format
-            };
+            }
             for (var i = 0; i < pa.length ; i++) {
                 var pos = pa[i].split("/");
                 if (pos.length > 1) {
                     marks.push(geom.position(pos[0], pos[1]));
-                    };
-                };
-            };
+                    }
+                }
+            }
         data.marks = marks;
         // mo (Scaler flags)
         var flags = {};
@@ -626,8 +647,8 @@
             var pa = settings.mo.split(",");
             for (var i = 0; i < pa.length ; i++) {
                 flags[pa[i]] = pa[i];
-                };
-            };
+                }
+            }
         data.scalerFlags = flags;
         retrieveOptionsCookie(data);
     };
@@ -641,19 +662,18 @@
             settings.wy = cropFloat(data.zoomArea.y);
             settings.ww = cropFloat(data.zoomArea.width);
             settings.wh = cropFloat(data.zoomArea.height);
-            };
+            }
         // marks
         if (data.marks) {
             settings.mk = '';
             for (var i = 0; i < data.marks.length; i++) {
                 if (i) {
                     settings.mk += ',';
-                    };
-                settings.mk
-                    += cropFloat(data.marks[i].x).toString() + '/' 
-                    + cropFloat(data.marks[i].y).toString();
-                };
-            };
+                    }
+                settings.mk += cropFloat(data.marks[i].x).toString() +
+                    '/' + cropFloat(data.marks[i].y).toString();
+                }
+            }
         // Scaler flags
         if (data.scalerFlags) {
             var mo = '';
@@ -692,15 +712,15 @@
             for (var o in data.dlOpts) {
                 if (clop) {
                     clop += '&';
-                    };
+                    }
                 clop += o + '=' + data.dlOpts[o];
-                };
+                }
             if (jQuery.cookie) {
                 var ck = "digilib:fn:" + escape(settings.fn) + ":pn:" + settings.pn;
                 console.debug("set cookie=", ck, " value=", clop);
                 jQuery.cookie(ck, clop);
-                };
-        };
+                }
+        }
     };
 
     var retrieveOptionsCookie = function (data) {
@@ -721,20 +741,21 @@
             console.debug("get cookie=", ck, " value=", cp);
             // in query string format
             opts = parseQueryString(cp);
-            };
+            }
         data.dlOpts = opts;
         // birdview option
         if (opts.birdview != null) {
             settings.isBirdDivVisible = opts.birdview === '1';
-            };
+            }
         // visible button sets
         if (opts.buttons != null) {
             settings.visibleButtonSets = opts.buttons;
-            };
+            }
     };
 
     // clear digilib data for reset
     var resetData = function (data) {
+        // TODO: we should reset instead of delete
         if (data.zoomArea) delete data.zoomArea;
         if (data.marks) delete data.marks;
         if (data.scalerFlags) delete data.scalerFlags;
@@ -749,13 +770,13 @@
             var history = window.history;
             if (typeof(history.pushState) === 'function') {
                 console.debug("we could modify history, but we don't...");
-                };
+                }
             window.location = url;
         } else {
             // embedded mode -- just change img src
             var url = getScalerUrl(data);
             data.$img.attr('src', url);
-            };
+            }
     };
 
     // returns maximum size for scaler img in fullscreen mode
@@ -776,12 +797,12 @@
         if (settings.interactionMode === 'fullscreen') {
             var imgSize = getFullscreenImgSize($elem);
             // fitwidth/height omits destination height/width
-            if (data.dlOpts['fitheight'] == null) {
+            if (data.dlOpts.fitheight == null) {
                 settings.dw = imgSize.width;
-            };
-            if (data.dlOpts['fitwidth'] == null) {
+            }
+            if (data.dlOpts.fitwidth == null) {
                 settings.dh = imgSize.height;
-            };
+            }
             $img = $('<img/>');
             scalerUrl = getScalerUrl(data);
         // embedded mode -- try to keep img tag
@@ -794,7 +815,7 @@
             } else {
                 $img = $('<img/>');
                 scalerUrl = getScalerUrl(data);
-            };
+            }
         }
         // create new html
         $elem.empty(); // TODO: should we keep stuff for customization?
@@ -903,7 +924,7 @@
         $birdImg.attr('src', birdUrl);
         if (data.settings.isBirdDivVisible) {
             $birdDiv.show();
-            };
+            }
         birdZoom(data);
     };
 
@@ -940,12 +961,12 @@
         } else {
             // set visibility
             isVisible = show;
-            };
+            }
         if (isVisible) {
             $div.fadeIn();
         } else {
             $div.fadeOut();
-            };
+            }
         return isVisible;
     };
 
@@ -961,7 +982,7 @@
                 $set = data.$buttonSets[setIdx];
             } else {
                 $set = createButtons(data, setIdx);
-                };
+                }
             if ($set == null) return false;
             var btnWidth = $set.width();
             // move remaining sets left and show new set
@@ -1000,18 +1021,18 @@
             if (hmir) {
                 // mirror about center
                 trafo.concat(trafo.getMirror('y'));
-                };
+                }
             if (vmir) {
                 // mirror about center
                 trafo.concat(trafo.getMirror('x'));
-                };
+                }
             if (rot) {
                 // rotate around center
                 trafo.concat(trafo.getRotation(parseFloat(rot)));
-                };
+                }
             // move back
             trafo.concat(trafo.getTranslation(geom.position(0.5, 0.5)));
-            };
+            }
         // scale to screen position and size
         trafo.concat(trafo.getScale(picrect));
         trafo.concat(trafo.getTranslation(picrect));
@@ -1034,7 +1055,7 @@
             // should the birdview adapt to mirror or rotation? decision: No. :-) 
             if ($birdImg) {
                 $birdImg.triggerHandler('load');
-                };
+                }
         };
     };
 
@@ -1065,8 +1086,8 @@
                 var $mark = $(html);
                 $elem.append($mark);
                 $mark.offset({left : mpos.x, top : mpos.y});
-                };
-            };
+                }
+            }
     };
 
     // show zoom area indicator on bird's eye view
@@ -1089,7 +1110,7 @@
             $birdZoom.offset(coords);
             $birdZoom.show();
             return;
-            };
+            }
         // nice animation for embedded mode :-)
         var makeCompleteFunction = function($birdZoom, normalSize) {
             return function() { 
@@ -1207,8 +1228,7 @@
                 birdZoomRect.x + dx,
                 birdZoomRect.y + dy,
                 birdZoomRect.width,
-                birdZoomRect.height
-                );
+                birdZoomRect.height);
             // stay within birdimage
             newRect.stayInside(birdImgRect);
             $birdZoom.offset({left : newRect.x, top : newRect.y});
@@ -1226,14 +1246,14 @@
             if (newRect == null) { // no movement happened
                 startPos = birdZoomRect.getCenter();
                 birdZoomMove(evt); // set center to click position
-                };
+                }
             if (data.zoomArea) {
                 // should always be true
                 var x = cropFloat((newRect.x - birdImgRect.x + 2) / birdImgRect.width);
                 var y = cropFloat((newRect.y - birdImgRect.y + 2) / birdImgRect.height);
                 data.zoomArea.x = x;
                 data.zoomArea.y = y;
-                };
+                }
             settings.ws = 1; // zoomed is always fit
             redisplay(data);
             return false;
@@ -1267,13 +1287,12 @@
         if ($bg.length === 0) {
             $bg = $('<div class="bgDrag" style="display:none; position:absolute"/>');
             $scaler.before($bg); // set as background
-            };
+            }
 
         var dragStart = function (evt) {
         // drag the image and load a new detail on mouse up
             // useless if not zoomed
             if (isFullArea(data.zoomArea)) return false;
-            if(evt.preventDefault) evt.preventDefault(); // no Firefox drag and drop (NEEDED?)
             pt1 = geom.position(evt);
             $imgRect = geom.rectangle($img);
             $imgRect.adjustDiv($bg); // set background size
@@ -1288,13 +1307,12 @@
             $bg.show();
             $(document).bind("mousemove.digilib", dragMove);
             $(document).bind("mouseup.digilib", dragEnd);
-            window.focus();
+            return false;
             };
 
         var dragMove = function (evt) {
         // mousemove handler: drag
             var pos = geom.position(evt);
-            if(evt.preventDefault) evt.preventDefault(); // no Firefox drag and drop (NEEDED?)
             dx = pos.x - pt1.x;
             dy = pos.y - pt1.y;
             // move the background image to the new position
@@ -1312,7 +1330,7 @@
             $(document).unbind("mousemove.digilib", dragMove);
             $(document).unbind("mouseup.digilib", dragEnd);
             // calculate relative offset
-            if (dx == 0 && dy == 0) return false; // no movement
+            if (dx === 0 && dy === 0) return false; // no movement
             // reload with scaler image showing the new ausschnitt
             // digilib.moveBy(x, y);
             var pos = geom.position(-dx, -dy);
@@ -1340,23 +1358,26 @@
         // clear flags
         for (var i = 0; i < 3; ++i) {
             delete flags['q'+i];
-            };
+            }
         flags['q'+qual] = 'q'+qual;
     };
 
     // sets a key to a value (relative values with +/- if relative=true)
     var setNumValue = function(settings, key, value) {
-        if (isNumber(value)) return settings[key] = value; 
-        var sign = value.substring(0,1);
+        if (isNumber(value)) {
+            settings[key] = value;
+            return value;
+        }
+        var sign = value[0];
         if (sign === '+' || sign === '-') {
             if (settings[key] == null) {
                 // this isn't perfect but still...
                 settings[key] = 0;
-                };
+                }
             settings[key] = parseFloat(settings[key]) + parseFloat(value);
         } else {
             settings[key] = value;
-            };
+            }
         return settings[key];
     };
 
@@ -1392,7 +1413,7 @@
         console.log = logFunction('_log'); 
         console.debug = logFunction('_debug'); 
         console.error = logFunction('_error');
-        };
+        }
 
     // hook plugin into jquery
     $.fn.digilib = function(action) {
@@ -1407,8 +1428,8 @@
             // call init on this
             return actions.init.apply(this, arguments);
         } else {
-            $.error( 'action ' + action + ' does not exist on jQuery.digilib' );
-            };
+            $.error('action ' + action + ' does not exist on jQuery.digilib');
+        }
     };
 
 })(jQuery);
\ No newline at end of file