[OpenLayers-Commits] r11661 - in trunk/openlayers: lib/OpenLayers tests

commits-20090109 at openlayers.org commits-20090109 at openlayers.org
Tue Mar 8 01:13:04 EST 2011


Author: ahocevar
Date: 2011-03-07 22:12:59 -0800 (Mon, 07 Mar 2011)
New Revision: 11661

Modified:
   trunk/openlayers/lib/OpenLayers/Map.js
   trunk/openlayers/tests/Map.html
Log:
fixing the movestart-move-moveend sequence. r=bartvde (closes #3139)

Modified: trunk/openlayers/lib/OpenLayers/Map.js
===================================================================
--- trunk/openlayers/lib/OpenLayers/Map.js	2011-03-07 16:52:01 UTC (rev 11660)
+++ trunk/openlayers/lib/OpenLayers/Map.js	2011-03-08 06:12:59 UTC (rev 11661)
@@ -1581,21 +1581,20 @@
                 this.moveByPx(dx, dy);
             }
         } else {
-            // if we don't have a center, we were using moveByPx previously
-            var forceSetCenter = !this.center;
-            
             // getCenter
             var centerPx = this.getViewPortPxFromLonLat(this.getCachedCenter());
 
             // adjust
             var newCenterPx = centerPx.add(dx, dy);
 
-            if (forceSetCenter || !newCenterPx.equals(centerPx)) {
+            if (this.dragging || !newCenterPx.equals(centerPx)) {
                 var newCenterLonLat = this.getLonLatFromViewPortPx(newCenterPx);
                 if (options.animate) {
                     this.panTo(newCenterLonLat);
                 } else {
-                    this.setCenter(newCenterLonLat, null, options.dragging);
+                    this.moveTo(newCenterLonLat);
+                    this.dragging = false;
+                    this.events.triggerEvent("moveend");
                 }    
             }
         }        
@@ -1618,8 +1617,7 @@
             var center = this.getCachedCenter();
 
             // center will not change, don't do nothing
-            if (lonlat.lon == center.lon &&
-                lonlat.lat == center.lat) {
+            if (lonlat.equals(center)) {
                 return;
             }
 
@@ -1630,9 +1628,6 @@
 
             this.panTween.start( { x: 0, y: 0 }, vector, this.panDuration, {
                 callbacks: {
-                    start: OpenLayers.Function.bind(function() {
-                        this.events.triggerEvent("movestart");
-                    }, this),
                     eachStep: OpenLayers.Function.bind(function(px) {
                         var x = px.x - last.x,
                             y = px.y - last.y;
@@ -1641,7 +1636,8 @@
                         last.y = Math.round(px.y);
                     }, this),
                     done: OpenLayers.Function.bind(function(px) {
-                        this.moveTo(lonlat, this.zoom, {noEvent: true});
+                        this.moveTo(lonlat);
+                        this.dragging = false;
                         this.events.triggerEvent("moveend");
                     }, this)
                 }
@@ -1666,10 +1662,10 @@
      * TBD: reconsider forceZoomChange in 3.0
      */
     setCenter: function(lonlat, zoom, dragging, forceZoomChange) {
+        this.panTween && this.panTween.stop();             
         this.moveTo(lonlat, zoom, {
             'dragging': dragging,
-            'forceZoomChange': forceZoomChange,
-            'caller': 'setCenter'
+            'forceZoomChange': forceZoomChange
         });
     },
     
@@ -1705,6 +1701,10 @@
                     x >= this.minPx.x + xRestriction;
         }
         if (valid) {
+            if (!this.dragging) {
+                this.dragging = true;
+                this.events.triggerEvent("movestart");
+            }
             this.center = null;
             if (dx) {
                 this.layerContainerDiv.style.left =
@@ -1757,16 +1757,10 @@
             }
         }
         // dragging is false by default
-        var dragging = options.dragging;
+        var dragging = options.dragging || this.dragging;
         // forceZoomChange is false by default
         var forceZoomChange = options.forceZoomChange;
-        // noEvent is false by default
-        var noEvent = options.noEvent;
 
-        if (this.panTween && options.caller == "setCenter") {
-            this.panTween.stop();
-        }    
-             
         if (!this.getCachedCenter() && !this.isValidLonLat(lonlat)) {
             lonlat = this.maxExtent.getCenterLonLat();
         }
@@ -1812,14 +1806,10 @@
         var centerChanged = (this.isValidLonLat(lonlat)) && 
                             (!lonlat.equals(this.center));
 
-
         // if neither center nor zoom will change, no need to do anything
-        if (zoomChanged || centerChanged || !dragging) {
+        if (zoomChanged || centerChanged || dragging) {
+            dragging || this.events.triggerEvent("movestart");
 
-            if (!this.dragging && !noEvent) {
-                this.events.triggerEvent("movestart");
-            }
-
             if (centerChanged) {
                 if (!zoomChanged && this.center) { 
                     // if zoom hasnt changed, just slide layerContainer
@@ -1860,14 +1850,10 @@
             //send the move call to the baselayer and all the overlays    
 
             if(this.baseLayer.visibility) {
-                this.baseLayer.moveTo(bounds, zoomChanged, dragging);
-                if(dragging) {
-                    this.baseLayer.events.triggerEvent("move");
-                } else {
-                    this.baseLayer.events.triggerEvent("moveend",
-                        {"zoomChanged": zoomChanged}
-                    );
-                }
+                this.baseLayer.moveTo(bounds, zoomChanged, options.dragging);
+                options.dragging || this.baseLayer.events.triggerEvent(
+                    "moveend", {zoomChanged: zoomChanged}
+                );
             }
             
             bounds = this.baseLayer.getExtent();
@@ -1890,38 +1876,25 @@
                         });
                     }
                     if (inRange && layer.visibility) {
-                        layer.moveTo(bounds, zoomChanged, dragging);
-                        if(dragging) {
-                            layer.events.triggerEvent("move");
-                        } else {
-                            layer.events.triggerEvent("moveend",
-                                {"zoomChanged": zoomChanged}
-                            );
-                        }
+                        layer.moveTo(bounds, zoomChanged, options.dragging);
+                        options.dragging || layer.events.triggerEvent(
+                            "moveend", {zoomChanged: zoomChanged}
+                        );
                     }
                 }                
             }
             
+            this.events.triggerEvent("move");
+            dragging || this.events.triggerEvent("moveend");
+
             if (zoomChanged) {
                 //redraw popups
                 for (var i=0, len=this.popups.length; i<len; i++) {
                     this.popups[i].updatePosition();
                 }
-            }    
-            
-            this.events.triggerEvent("move");
-    
-            if (zoomChanged) { this.events.triggerEvent("zoomend"); }
+                this.events.triggerEvent("zoomend");
+            }
         }
-
-        // even if nothing was done, we want to notify of this
-        if (!dragging && !noEvent) {
-            this.events.triggerEvent("moveend");
-        }
-        
-        // Store the map dragging state for later use
-        this.dragging = !!dragging; 
-
     },
 
     /** 

Modified: trunk/openlayers/tests/Map.html
===================================================================
--- trunk/openlayers/tests/Map.html	2011-03-07 16:52:01 UTC (rev 11660)
+++ trunk/openlayers/tests/Map.html	2011-03-08 06:12:59 UTC (rev 11661)
@@ -163,8 +163,15 @@
     }
 
     function test_Map_center(t) {
-        t.plan(8);
-        map = new OpenLayers.Map('map');
+        t.plan(11);
+        var log = [];
+        map = new OpenLayers.Map('map', {
+            eventListeners: {
+                "movestart": function() {log.push("movestart");},
+                "move": function() {log.push("move");},
+                "moveend": function() {log.push("moveend");}
+            }            
+        });
         var baseLayer = new OpenLayers.Layer.WMS("Test Layer", 
             "http://octo.metacarta.com/cgi-bin/mapserv?",
             {map: "/mapdata/vmap_wms.map", layers: "basic"} );
@@ -180,7 +187,11 @@
         map.zoomOut();
         t.eq( map.getZoom(), 0, "map.zoom is correct after calling setCenter,zoom in, zoom out");
 
+        log = [];
         map.zoomTo(5);
+        t.eq(log[0], "movestart", "zoomTo fires movestart event");
+        t.eq(log[1], "move", "zoomTo fires move event");
+        t.eq(log[2], "moveend", "zoomTo fires moveend event");
         t.eq( map.getZoom(), 5, "map.zoom is correct after calling zoomTo" );
 
     /**
@@ -1527,20 +1538,61 @@
     
     function test_panTo(t) {
         
-        t.plan(2);
+        t.plan(6);
         
-        var map = new OpenLayers.Map("map");
+        var log = [];
+        var map = new OpenLayers.Map("map", {
+            eventListeners: {
+                "movestart": function() {log.push("movestart");},
+                "move": function() {log.push("move");},
+                "moveend": function() {log.push("moveend");}
+            }            
+        });
         map.addLayer(
             new OpenLayers.Layer(null, {isBaseLayer: true})
         );
         map.setCenter(new OpenLayers.LonLat(0, 0), 0);
+        t.eq(log[log.length-1], "moveend", "moveend fired when map center is set");
+        log = [];
         
         map.panTo(new OpenLayers.LonLat(1, 0));
         t.eq(map.panTween.playing, true, "the map pan tween is playing before destroy");
         
-        map.destroy();
-        t.ok(!map.panTween || !map.panTween.playing, "the map pan tween is not playing after destroy");
+        t.delay_call(2, function() {
+            t.eq(log[0], "movestart", "panTo starts with movestart event");
+            t.eq(log[1], "move", "move events fired while panning");
+            t.eq(log[log.length-1], "moveend", "panTo finishes with moveend event");
+            map.destroy();
+            t.ok(!map.panTween || !map.panTween.playing, "the map pan tween is not playing after destroy");
+        });
     }
+    
+    function test_pan(t) {
+        t.plan(4);
+        
+        var log = [];
+        var map = new OpenLayers.Map("map");
+        map.addLayer(
+            new OpenLayers.Layer(null, {isBaseLayer: true})
+        );
+        map.setCenter(new OpenLayers.LonLat(0, 0), 5);
+        var log = [];
+        map.events.on({
+            "movestart": function() {log.push("movestart");},
+            "move": function() {log.push("move");},
+            "moveend": function() {log.push("moveend");}
+        });
+        
+        // simulate the drag sequence of the DragPan control;
+        map.pan(5,5, {animate: false, dragging: true});
+        map.pan(1,1, {animate: false, dragging: false});
+        
+        t.eq(log[0], "movestart", "pan sequence starts with movestart");
+        t.eq(log[1], "move", "followed by move,");
+        t.eq(log[log.length-2], "move", "move again before we stop panning,");
+        t.eq(log[log.length-1], "moveend", "and moveend when we're done.");
+        
+    }
 
     function test_updateSize(t) {
         t.plan(2);



More information about the Commits mailing list