changeset 992:4fcc85d4db8f

pack and unpack parameters now as events. used in marks plugin.
author robcast
date Mon, 06 Feb 2012 23:03:17 +0100
parents 497ab03868ab
children 862d2ae56c20
files webapp/src/main/webapp/jquery/jquery.digilib.js webapp/src/main/webapp/jquery/jquery.digilib.marks.js
diffstat 2 files changed, 119 insertions(+), 99 deletions(-) [+]
line wrap: on
line diff
--- a/webapp/src/main/webapp/jquery/jquery.digilib.js	Mon Feb 06 20:13:11 2012 +0100
+++ b/webapp/src/main/webapp/jquery/jquery.digilib.js	Mon Feb 06 23:03:17 2012 +0100
@@ -38,7 +38,7 @@
 
     var defaults = {
         // version of this script
-        'version' : 'jquery.digilib.js 2.1.3a1',
+        'version' : 'jquery.digilib.js 2.1.3b1',
         // logo url
         'logoUrl' : 'img/digilib-logo-text1.png',
         // homepage url (behind logo)
@@ -229,6 +229,8 @@
                         p.init(data);
                     }
                 }
+                // trigger unpack params handlers
+                $(data).trigger('unpack');
                 // get image info from server if needed
                 if (data.scaleMode === 'pixel' || data.scaleMode === 'size') {
                     loadImageInfo(data); // triggers "imageInfo" on completion
@@ -659,24 +661,6 @@
         // zoom area
         var zoomArea = geom.rectangle(settings.wx, settings.wy, settings.ww, settings.wh);
         data.zoomArea = zoomArea;
-        // marks
-        var marks = [];
-        var pa;
-        if (settings.mk) {
-            var mk = settings.mk;
-            if (mk.indexOf(";") >= 0) {
-                pa = mk.split(";");    // old format with ";"
-            } else {
-                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 = {};
         if (settings.mo) {
@@ -702,22 +686,6 @@
         settings.wh = cropFloat(area.height);
     };
 
-    /** put marks into parameters
-     * 
-     */
-    var packMarks = function (settings, marks) {
-        if (!marks) return;
-        settings.mk = '';
-        for (var i = 0; i < marks.length; i++) {
-            if (i) {
-                settings.mk += ',';
-                }
-            settings.mk +=
-                cropFloatStr(marks[i].x) + '/' + 
-                cropFloatStr(marks[i].y);
-            }
-    };
-
     /** pack scaler flags into parameters
      * 
      */
@@ -739,10 +707,11 @@
     var packParams = function (data) {
         var settings = data.settings;
         packArea(settings, data.zoomArea);
-        packMarks(settings, data.marks);
         packScalerFlags(settings, data.scalerFlags);
         // store user interface options in cookie
         storeOptions(data);
+        // trigger pack handlers
+        $(data).trigger('pack');
     };
 
     /** store digilib options in a cookie   
@@ -1577,7 +1546,6 @@
             unpackParams : unpackParams,
             packParams : packParams,
             packArea : packArea,
-            packMarks : packMarks,
             packScalerFlags : packScalerFlags,
             storeOptions : storeOptions,
             redisplay : redisplay,
--- a/webapp/src/main/webapp/jquery/jquery.digilib.marks.js	Mon Feb 06 20:13:11 2012 +0100
+++ b/webapp/src/main/webapp/jquery/jquery.digilib.marks.js	Mon Feb 06 23:03:17 2012 +0100
@@ -1,5 +1,5 @@
 /**
-digilib plugin stub
+ * digilib plugin stub
  */
 
 (function($) {
@@ -10,46 +10,48 @@
     var digilib = null;
 
     var buttons = {
-            mark : {
-                onclick : "setMark",
-                tooltip : "set a mark",
-                icon : "mark.png"
-                },
-            delmark : {
-                onclick : "removeMark",
-                tooltip : "delete the last mark",
-                icon : "delmark.png"
-                }
+        mark : {
+            onclick : "setMark",
+            tooltip : "set a mark",
+            icon : "mark.png"
+        },
+        delmark : {
+            onclick : "removeMark",
+            tooltip : "delete the last mark",
+            icon : "delmark.png"
+        }
     };
 
     var defaults = {
     };
 
     var actions = {
-            /** set a mark by clicking (or giving a position)
-             * 
-             * @param data
-             * @param mpos
-             */
-            setMark : function (data, mpos) {
-                if (mpos == null) {
-                    // interactive
-                    setMark(data);
-                } else {
-                    // use position
-                    data.marks.push(pos);
-                    digilib.fn.redisplay(data);
-                }
-            },
-
-            /** remove the last mark
-             * 
-             * @param data
-             */
-            removeMark : function (data) {
-                data.marks.pop();
+        /**
+         * set a mark by clicking (or giving a position)
+         * 
+         * @param data
+         * @param mpos
+         */
+        setMark : function(data, mpos) {
+            if (mpos == null) {
+                // interactive
+                setMark(data);
+            } else {
+                // use position
+                data.marks.push(pos);
                 digilib.fn.redisplay(data);
             }
+        },
+
+        /**
+         * remove the last mark
+         * 
+         * @param data
+         */
+        removeMark : function(data) {
+            data.marks.pop();
+            digilib.fn.redisplay(data);
+        }
     };
 
     // plugin installation called by digilib on plugin object.
@@ -62,63 +64,113 @@
         $.extend(true, digilib.defaults, defaults);
         $.extend(digilib.actions, actions);
         $.extend(true, digilib.buttons, buttons);
+        // export packMarks for regions.js
+        digilib.fn.packMarks = packMarks;
     };
 
     // plugin initialization
-    var init = function (data) {
+    var init = function(data) {
         console.debug('initialising marks plugin. data:', data);
         var $data = $(data);
         // install event handler
-        $data.bind('setup', handleSetup);
-        $data.bind('update', handleUpdate);
+        $data.on('update', handleUpdate);
+        $data.on('pack', handlePack);
+        $data.on('unpack', handleUnpack);
     };
 
-
-    var handleSetup = function (evt) {
-        console.debug("stub: handleSetup");
-        var data = this;
-    };
-
-    var handleUpdate = function (evt) {
+    var handleUpdate = function(evt) {
         console.debug("stub: handleUpdate");
         var data = this;
         renderMarks(data);
     };
 
-    /** place marks on the image
+    /**
+     * unpack mk parameter into marks array
+     */
+    var handleUnpack = function(evt) {
+        var data = this;
+        var settings = data.settings;
+        var marks = [];
+        var pa;
+        if (settings.mk) {
+            var mk = settings.mk;
+            if (mk.indexOf(";") >= 0) {
+                pa = mk.split(";"); // old format with ";"
+            } else {
+                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;
+    };
+
+    /**
+     * pack marks into parameters
+     */
+    var handlePack = function(data) {
+        var data = this;
+        var settings = data.settings;
+        var marks = data.marks;
+        packMarks(settings, marks);
+    };
+
+    /**
+     * pack marks into parameter
+     */
+    var packMarks = function(settings, marks) {
+        if (!marks)
+            return;
+        settings.mk = '';
+        for ( var i = 0; i < marks.length; i++) {
+            if (i) {
+                settings.mk += ',';
+            }
+            settings.mk += digilib.fn.cropFloatStr(marks[i].x) + '/' + digilib.fn.cropFloatStr(marks[i].y);
+        }
+    };
+
+    /**
+     * place marks on the image
      * 
      */
-    var renderMarks = function (data) {
-        if (data.$img == null || data.imgTrafo == null) return;
-        console.debug("renderMarks: img=",data.$img," imgtrafo=",data.imgTrafo);
+    var renderMarks = function(data) {
+        if (data.marks == null || data.$img == null || data.imgTrafo == null)
+            return;
+        console.debug("renderMarks: img=", data.$img, " imgtrafo=", data.imgTrafo);
         var $elem = data.$elem;
         var marks = data.marks;
         // clear marks
         $elem.find('div.mark').remove();
-        for (var i = 0; i < marks.length; i++) {
+        for ( var i = 0; i < marks.length; i++) {
             var mark = marks[i];
             if (data.zoomArea.containsPosition(mark)) {
                 var mpos = data.imgTrafo.transform(mark);
-                console.debug("renderMarks: pos=",mpos);
+                console.debug("renderMarks: pos=", mpos);
                 // create mark
-                var html = '<div class="mark overlay">'+(i+1)+'</div>';
+                var html = '<div class="mark overlay">' + (i + 1) + '</div>';
                 var $mark = $(html);
-                $mark.attr("id", "digilib-mark-"+(i+1));
+                $mark.attr("id", "digilib-mark-" + (i + 1));
                 $elem.append($mark);
                 mpos.adjustDiv($mark);
-                }
             }
+        }
     };
 
-    /** add a mark where clicked.
+    /**
+     * add a mark where clicked.
      * 
      */
-    var setMark = function (data) {
+    var setMark = function(data) {
         var $scaler = data.$scaler;
         // unbind other handler TODO: do we need to do this?
         $scaler.off(".dlZoomDrag");
         // start event capturing
-        $scaler.one('mousedown.dlSetMark', function (evt) {
+        $scaler.one('mousedown.dlSetMark', function(evt) {
             // event handler adding a new mark
             console.log("setmark at=", evt);
             var mpos = geom.position(evt);
@@ -132,13 +184,13 @@
     // plugin object with name and init
     // shared objects filled by digilib on registration
     var plugin = {
-            name : 'marks',
-            install : install,
-            init : init,
-            buttons : {},
-            actions : {},
-            fn : {},
-            plugins : {}
+        name : 'marks',
+        install : install,
+        init : init,
+        buttons : {},
+        actions : {},
+        fn : {},
+        plugins : {}
     };
 
     if ($.fn.digilib == null) {