changeset 19:9da037cd96fe

unbind document event listeners on drag end.
author Atul Varma <avarma@mozilla.com>
date Tue, 04 May 2010 23:56:46 -0700
parents eb6a0fe69683
children 9734ab260438
files index.html
diffstat 1 files changed, 29 insertions(+), 20 deletions(-) [+]
line wrap: on
line diff
--- a/index.html	Tue May 04 23:52:45 2010 -0700
+++ b/index.html	Tue May 04 23:56:46 2010 -0700
@@ -99,7 +99,12 @@
   },
   bind: function(name, handler) {
     this.elems.forEach(function(elem) {
-     elem.addEventListener(name, handler, false);
+      elem.addEventListener(name, handler, false);
+    });
+  },
+  unbind: function(name, handler) {
+    this.elems.forEach(function(elem) {
+      elem.removeEventListener(name, handler, false);
     });
   },
   css: function(options) {
@@ -189,12 +194,35 @@
       $(node).find(".info").text(msg);
     }
 
+    function onMouseMove(event) {
+      if (isMouseDown) {
+        currX = event.clientX;
+        currY = event.clientY;
+        move(this);
+        startX = currX;
+        startY = currY;
+        updateInfo();
+      }
+    }
+
+    function onMouseUp(event) {
+      if (isMouseDown) {
+        isMouseDown = false;
+        onDragEnd(event);
+        $(node.ownerDocument).unbind("mousemove", onMouseMove);
+        $(node.ownerDocument).unbind("mouseup", onMouseUp);
+        updateInfo();
+      }
+    }
+
     function onMouseDown(event) {
       event.preventDefault();
       startX = event.clientX;
       startY = event.clientY;
       isMouseDown = true;
       $(node).addClass("active");
+      $(node.ownerDocument).bind("mousemove", onMouseMove);
+      $(node.ownerDocument).bind("mouseup", onMouseUp);
       updateInfo();
     }
 
@@ -202,25 +230,6 @@
       onMouseDown(event);
 
     $(node).bind("mousedown", onMouseDown);
-
-    $(node.ownerDocument).bind("mousemove", function(event) {
-     if (isMouseDown) {
-        currX = event.clientX;
-        currY = event.clientY;
-        move(this);
-        startX = currX;
-        startY = currY;
-        updateInfo();
-      }
-    });
-
-    $(node.ownerDocument).bind("mouseup", function(event) {
-      if (isMouseDown) {
-        isMouseDown = false;
-        onDragEnd(event);
-        updateInfo();
-      }
-    });
   } else {
     $(node).bind("touchstart", function(event) {
       event.preventDefault();