changeset 607:f5d5cae64828 jquery

parameter parsing
author hertzhaft
date Thu, 13 Jan 2011 19:22:17 +0100
parents 8204615dad77
children 774a3f60efd5
files client/digitallibrary/jquery/jquery-test.html client/digitallibrary/jquery/jquery.digilib.js
diffstat 2 files changed, 94 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/client/digitallibrary/jquery/jquery-test.html	Thu Jan 13 19:22:17 2011 +0100
@@ -0,0 +1,54 @@
+<?xml version="1.0" ?>
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml">
+    <head>
+        <title>Digilib jQuery Test HTML</title>
+        
+        <style type="text/css">
+            body {
+                 background: silver;
+            }
+            
+            div.digilib {
+                float: left;
+                padding: 10px;
+            }
+            
+        </style>
+    
+        <script type="text/javascript" src="jquery-1.4.4.js"></script>
+        <script type="text/javascript" src="jquery.digilib.js"></script>
+        
+
+        <script type="text/javascript">
+            $(document).ready(function(){
+                 // $('div.digilib').children('img').attr('src', '');
+                 // $('div.digilib').children('img').remove();
+                 $('div.digilib').digilib({interactionMode : 'embedded'});
+
+                 $('div.digilib').each(function(){
+                         console.log($(this).data('digilib').settings);
+                    });
+              
+            });
+
+        </script>
+    </head>
+
+    <body>
+    
+        <div id="digilib-1" class="digilib single">
+            <img src="http://digilinx:18080/digitallibrary/servlet/Scaler?dw=200&amp;dh=200&amp;fn=/digisprint/jquery/FransHals-WillemVanHeythuysen" />
+        </div>
+        <div id="digilib-2" class="digilib">
+            <img src="http://digilinx:18080/digitallibrary/servlet/Scaler?dw=200&amp;dh=200&amp;fn=/digisprint/jquery/ferrara" />
+        </div>
+        <div id="digilib-3" class="digilib">
+            <img src="http://digilinx:18080/digitallibrary/servlet/Scaler?dw=200&amp;dh=200&amp;fn=/digisprint/jquery/weide" />
+        </div>
+        <div id="digilib-4" class="digilib">
+            <img src="http://digilinx:18080/digitallibrary/servlet/Scaler?dw=200&amp;dh=200&amp;fn=/digisprint/jquery/HansHolbein-NikolausKratzer" />
+        </div>
+    </body>
+</html>
+
--- a/client/digitallibrary/jquery/jquery.digilib.js	Thu Jan 13 14:52:13 2011 +0100
+++ b/client/digitallibrary/jquery/jquery.digilib.js	Thu Jan 13 19:22:17 2011 +0100
@@ -9,7 +9,7 @@
             /* base URL to Scaler servlet */
             'scalerUrl' : 'http://digilib.mpiwg-berlin.mpg.de/digitallibrary/servlet/Scaler',
             /* digilib image path i.e. fn */
-            'imagePath' : '',
+            'fn' : '',
             /* mode of operation. 
              * fullscreen: takes parameters from page URL, keeps state in page URL
              * embedded: takes parameters from Javascript options, keeps state inside object 
@@ -18,23 +18,32 @@
     };
  
     /* parameters from the query string */
-    var params = {};
+    var queryParams = {};
     
     var methods = {
             init : function(options) {
+                var settings = $.extend({}, defaults, options);
+                var isFullscreen = settings.interactionMode === 'fullscreen'; 
+                if (isFullscreen) {
+                    queryParams = parseQueryParams();
+                    };
                 return this.each(function() {
                     var $elem = $(this);
                     var data = $elem.data('digilib');
+                    var elemSettings;
                     // If the plugin hasn't been initialized yet
                     if (!data) {
                         // settings for this digilib instance are merged from defaults and options
-                        var settings = $.extend({}, defaults, options);
                         // merge query parameters
-                        settings = $.extend(settings, parseParams(settings.interactionMode));
+                        if (isFullscreen) {
+                            elemSettings = $.extend({}, settings, queryParams);
+                        } else {
+                            elemSettings = $.extend({}, settings, parseImgParams($elem));
+                        };
                         // store in data element
                         $elem.data('digilib', {
                             target : $elem,
-                            settings : settings
+                            settings : elemSettings
                         });
                     }
                 });
@@ -52,9 +61,32 @@
     };
 
     // returns object with parameters from the query string or an embedded img-tag (depending on interactionMode)
-    var parseParams = function(interactionMode) {
-        alert("parseParams() not implemented");
-    };
+    var parseQueryParams = function() {
+        return parseQueryString(location.search.slice(1));
+        };
+        
+    var parseImgParams = function($elem) {
+        var src = $elem.children('img').attr('src');
+        var pos = src.indexOf('?');
+        var query = (pos < 0) ? '' : src.substring(pos + 1);
+        var scalerUrl = src.substring(0, pos);
+        var hash = parseQueryString(query);
+        hash.scalerUrl = scalerUrl;
+        // console.log(hash);
+        return hash;
+        };
+
+    var parseQueryString = function(query) {
+        var pairs = query.split("&");
+        var hash = {};
+        for (var i = 0; i < pairs.length; i++) {
+            var pair = pairs[i].split("=");
+            if (pair.length === 2) {
+                hash[pair[0]] = pair[1]
+                };
+            };
+        return hash;
+        };
     
     // hook plugin into jquery
     $.fn.digilib = function(method) {