[OpenLayers-Commits] r10954 - trunk/openlayers/lib/OpenLayers/Handler

commits-20090109 at openlayers.org commits-20090109 at openlayers.org
Tue Dec 7 18:48:20 EST 2010


Author: ahocevar
Date: 2010-12-07 15:48:20 -0800 (Tue, 07 Dec 2010)
New Revision: 10954

Modified:
   trunk/openlayers/lib/OpenLayers/Handler/Drag.js
Log:
fix document dragging which was broken with r10871. Note that this could also have been fixed by creating the Events instance on the element returned by OpenLayers.Util.getViewportElement() instead of document. r=erilem (closes #2941)


Modified: trunk/openlayers/lib/OpenLayers/Handler/Drag.js
===================================================================
--- trunk/openlayers/lib/OpenLayers/Handler/Drag.js	2010-12-07 23:37:56 UTC (rev 10953)
+++ trunk/openlayers/lib/OpenLayers/Handler/Drag.js	2010-12-07 23:48:20 UTC (rev 10954)
@@ -94,8 +94,7 @@
     
     /**
      * Property: documentEvents
-     * {<OpenLayers.Events>} Event instance for observing document events. Will
-     *     be set on mouseout if documentDrag is set to true.
+     * {Boolean} Are we currently observing document events?
      */
     documentEvents: null,
 
@@ -117,6 +116,19 @@
      */
     initialize: function(control, callbacks, options) {
         OpenLayers.Handler.prototype.initialize.apply(this, arguments);
+        
+        if (this.documentDrag === true) {
+            var me = this;
+            this._docMove = function(evt) {
+                me.mousemove({
+                    xy: {x: evt.clientX, y: evt.clientY},
+                    element: document
+                });
+            };
+            this._docUp = function(evt) {
+                me.mouseup({xy: {x: evt.clientX, y: evt.clientY}});
+            };
+        }
     },
     
     /**
@@ -232,7 +244,7 @@
                     // registered with the map
                     this.setEvent(evt);
                 } else {
-                    this.destroyDocumentEvents();
+                    this.removeDocumentEvents();
                 }
             }
             if (this.interval > 0) {
@@ -272,7 +284,7 @@
         if (this.started) {
             if(this.documentDrag === true && this.documentEvents) {
                 this.adjustXY(evt);
-                this.destroyDocumentEvents();
+                this.removeDocumentEvents();
             }
             var dragged = (this.start != this.last);
             this.started = false;
@@ -303,15 +315,7 @@
     mouseout: function (evt) {
         if (this.started && OpenLayers.Util.mouseLeft(evt, this.map.div)) {
             if(this.documentDrag === true) {
-                this.documentEvents = new OpenLayers.Events(this, document,
-                                            null, null, {includeXY: true});
-                this.documentEvents.on({
-                    mousemove: this.mousemove,
-                    mouseup: this.mouseup
-                });
-                OpenLayers.Element.addClass(
-                    document.body, "olDragDown"
-                );
+                this.addDocumentEvents();
             } else {
                 var dragged = (this.start != this.last);
                 this.started = false; 
@@ -403,18 +407,28 @@
     },
     
     /**
-     * Method: destroyDocumentEvents
-     * Destroys the events instance that gets added to the document body when
-     * documentDrag is true and the mouse cursor leaves the map viewport while
-     * dragging.
+     * Method: addDocumentEvents
+     * Start observing document events when documentDrag is true and the mouse
+     * cursor leaves the map viewport while dragging.
      */
-    destroyDocumentEvents: function() {
-        OpenLayers.Element.removeClass(
-            document.body, "olDragDown"
-        );
-        this.documentEvents.destroy();
-        this.documentEvents = null;
+    addDocumentEvents: function() {
+        OpenLayers.Element.addClass(document.body, "olDragDown");
+        this.documentEvents = true;
+        OpenLayers.Event.observe(document, "mousemove", this._docMove);
+        OpenLayers.Event.observe(document, "mouseup", this._docUp);
     },
+    
+    /**
+     * Method: removeDocumentEvents
+     * Stops observing document events when documentDrag is true and the mouse
+     * cursor re-enters the map viewport while dragging.
+     */
+    removeDocumentEvents: function() {
+        OpenLayers.Element.removeClass(document.body, "olDragDown");
+        this.documentEvents = false;
+        OpenLayers.Event.stopObserving(document, "mousemove", this._docMove);
+        OpenLayers.Event.stopObserving(document, "mouseup", this._docUp);
+    },
 
     CLASS_NAME: "OpenLayers.Handler.Drag"
 });



More information about the Commits mailing list