diff client/digitallibrary/jquery/dlGeometry.js @ 654:b3c05e1568cf jquery

zoomarea works now added rectangle and position constructors that take jQuery and event objects fixed bug in rectangle.contains
author robcast
date Fri, 21 Jan 2011 00:00:08 +0100
parents c2566e470569
children 436e10669df8
line wrap: on
line diff
--- a/client/digitallibrary/jquery/dlGeometry.js	Thu Jan 20 20:21:11 2011 +0100
+++ b/client/digitallibrary/jquery/dlGeometry.js	Fri Jan 21 00:00:08 2011 +0100
@@ -24,10 +24,27 @@
  * Position class
  */
         var position = function (x, y) {
-            var that = {
-                    x : parseFloat(x),
-                    y : parseFloat(y)
-            };
+            if (typeof x === "object") {
+                if (x instanceof jQuery) {
+                    // jQuery object
+                    var pos = x.offset();
+                    var that = {
+                            x : pos.left,
+                            y : pos.top
+                    };
+                } else {
+                    // event object(?)
+                    var that = {
+                            x : x.pageX,
+                            y : x.pageY
+                    };
+                }
+            } else {
+                var that = {
+                        x : parseFloat(x),
+                        y : parseFloat(y)
+                };
+            }
             that.equals = function(other) {
                 return (this.x === other.x  &&  this.y === other.y);
             };
@@ -42,13 +59,24 @@
         var rectangle = function (x, y, w, h) {
             var that = {}; 
             if (typeof x === "object") {
-                // assume x and y are Position
-                that = {
-                        x : x.x,
-                        y : x.y,
-                        width : y.x - x.x,
-                        height : y.y - x.y
-                };
+                if (x instanceof jQuery) {
+                    // jQuery object
+                    var pos = x.offset();
+                    that = {
+                            x : pos.left,
+                            y : pos.top,
+                            width : x.width(),
+                            height : x.height()
+                    };
+                } else {
+                    // assume x and y are Position
+                    that = {
+                            x : x.x,
+                            y : x.y,
+                            width : y.x - x.x,
+                            height : y.y - x.y
+                    };
+                }
             } else {
                 that = {
                         x : parseFloat(x),
@@ -119,7 +147,7 @@
             that.containsPosition = function(pos) {
                 // returns if Position "pos" lies inside of this rectangle
                 var ct = ((pos.x >= this.x) && (pos.y >= this.y) && 
-                        (pos.x <= this.x + this.width) && (pos.y <= this.y + this.width));
+                        (pos.x <= this.x + this.width) && (pos.y <= this.y + this.height));
                 return ct;
             };
             that.containsRect = function(rect) {