[OpenLayers-Commits] r11479 - in sandbox/ahocevar/layercontainer: lib/OpenLayers tests

commits-20090109 at openlayers.org commits-20090109 at openlayers.org
Fri Feb 25 06:01:38 EST 2011


Author: erilem
Date: 2011-02-25 03:01:37 -0800 (Fri, 25 Feb 2011)
New Revision: 11479

Modified:
   sandbox/ahocevar/layercontainer/lib/OpenLayers/Map.js
   sandbox/ahocevar/layercontainer/tests/Map.html
Log:
moveByPx now verifies that the new extent is within the restricted extent

Modified: sandbox/ahocevar/layercontainer/lib/OpenLayers/Map.js
===================================================================
--- sandbox/ahocevar/layercontainer/lib/OpenLayers/Map.js	2011-02-25 10:28:52 UTC (rev 11478)
+++ sandbox/ahocevar/layercontainer/lib/OpenLayers/Map.js	2011-02-25 11:01:37 UTC (rev 11479)
@@ -1516,7 +1516,7 @@
     getCachedCenter: function() {
         if (!this.center) {
             this.center = this.getLonLatFromViewPortPx(
-                new OpenLayers.Pixel(map.size.w / 2, map.size.h / 2)
+                new OpenLayers.Pixel(this.size.w / 2, this.size.h / 2)
             );
         }
         return this.center;
@@ -1663,13 +1663,21 @@
     moveByPx: function(dx, dy) {
         dx = Math.round(dx);
         dy = Math.round(dy);
-        var x = this.size.w / 2 + dx;
-        var y = this.size.h / 2 + dy;
+        var hw = this.size.w / 2;
+        var hh = this.size.h / 2;
+        var x = hw + dx;
+        var y = hh + dy;
         var valid = x <= this.maxPx.x &&
                     x >= this.minPx.x &&
                     y <= this.maxPx.y &&
                     y >= this.minPx.y;
-        if (valid === true) {
+        if (this.restrictedExtent && valid) {
+            valid = !(this.maxPx.x - x < hw ||
+                      x - this.minPx.x < hw ||
+                      this.maxPx.y - y < hh ||
+                      y - this.minPx.y < hh);
+        }
+        if (valid) {
             this.center = null;
             if (dx) {
                 this.layerContainerDiv.style.left =
@@ -1792,7 +1800,7 @@
                 this.layerContainerOrigin = this.getCachedCenter();
                 this.layerContainerDiv.style.left = "0px";
                 this.layerContainerDiv.style.top  = "0px";
-                var maxExtent = this.getMaxExtent();
+                var maxExtent = this.getMaxExtent({restricted: true});
                 var maxExtentCenter = maxExtent.getCenterLonLat();
                 var lonDelta = this.center.lon - maxExtentCenter.lon;
                 var latDelta = maxExtentCenter.lat - this.center.lat;

Modified: sandbox/ahocevar/layercontainer/tests/Map.html
===================================================================
--- sandbox/ahocevar/layercontainer/tests/Map.html	2011-02-25 10:28:52 UTC (rev 11478)
+++ sandbox/ahocevar/layercontainer/tests/Map.html	2011-02-25 11:01:37 UTC (rev 11479)
@@ -1319,7 +1319,7 @@
         var m = {
             'baseLayer': { 'units': {} },
             'getSize': function() { return {'w': 10, 'h': 15}; },
-            'getCenter': function() { return {'lon': -5, 'lat': -25}; },
+            'getCachedCenter': function() { return {'lon': -5, 'lat': -25}; },
             'zoomToExtent': function(extent, closest) {
                 t.ok(extent.equals(g_ExpectedExtent), "extent correctly calculated for zoomToExtent()");
                 t.ok(closest == g_Closest, "closest correctly passed on to zoomToExtent()");
@@ -1687,6 +1687,41 @@
         map.destroy();
     }    
 
+    function test_moveByPx(t) {
+        t.plan(8);
+
+        var map = new OpenLayers.Map({
+            div: 'map',
+            maxExtent: new OpenLayers.Bounds(-50, -50, 50, 50),
+            restrictedExtent: new OpenLayers.Bounds(-10, -10, 10, 10),
+            layers: [
+                new OpenLayers.Layer('name', {isBaseLayer: true})
+            ]
+        });
+        map.zoomToExtent(new OpenLayers.Bounds(-1, -1, 1, 1));
+
+        // check initial state
+        t.eq(map.layerContainerDiv.style.left, '0px', 'layer container left correct');
+        t.eq(map.layerContainerDiv.style.top, '0px', 'layer container top correct');
+
+        // move to a valid position
+        map.moveByPx(-455, 455);
+        t.eq(map.layerContainerDiv.style.left, '455px', 'layer container left correct');
+        t.eq(map.layerContainerDiv.style.top, '-455px', 'layer container top correct');
+
+        // move outside the max extent
+        map.moveByPx(-4500, 4500);
+        t.eq(map.layerContainerDiv.style.left, '455px', 'layer container left correct');
+        t.eq(map.layerContainerDiv.style.top, '-455px', 'layer container top correct');
+
+        // move outside the restricted extent
+        map.moveByPx(-500, 500);
+        t.eq(map.layerContainerDiv.style.left, '455px', 'layer container left correct');
+        t.eq(map.layerContainerDiv.style.top, '-455px', 'layer container top correct');
+
+        map.destroy();
+    }
+
   </script>
 </head>
 <body>



More information about the Commits mailing list