OpenLayers OpenLayers

Ticket #774: dragPan.patch

File dragPan.patch, 3.9 kB (added by tschaub, 1 year ago)

don't call setCenter on click

  • lib/OpenLayers/Control/DragPan.js

    old new  
    2121    type: OpenLayers.Control.TYPE_TOOL, 
    2222     
    2323    /** 
     24     * Property: panned 
     25     * {Boolean} The map moved. 
     26     */ 
     27    panned: false, 
     28     
     29    /** 
    2430     * Method: draw 
    2531     * Creates a Drag handler, using <OpenLayers.Control.PanMap.panMap> and 
    2632     * <OpenLayers.Control.PanMap.panMapDone> as callbacks. 
    2733     */     
    2834    draw: function() { 
    29         this.handler = new OpenLayers.Handler.Drag( this, 
    30                             {"move": this.panMap, "up": this.panMap} ); 
     35        this.handler = new OpenLayers.Handler.Drag(this, 
     36                            {"move": this.panMap, "done": this.panMapDone}); 
    3137    }, 
    3238 
    3339    /** 
    3440    * Method: panMap 
    3541    * 
    3642    * Parameters: 
    37     * xy - {<OpenLayers.Pixel>} Pixel of the up position 
     43    * xy - {<OpenLayers.Pixel>} Pixel of the mouse position 
    3844    */ 
    39     panMap: function (xy) { 
     45    panMap: function(xy) { 
     46        this.panned = true; 
    4047        var deltaX = this.handler.last.x - xy.x; 
    4148        var deltaY = this.handler.last.y - xy.y; 
    4249        var size = this.map.getSize(); 
    4350        var newXY = new OpenLayers.Pixel(size.w / 2 + deltaX, 
    4451                                         size.h / 2 + deltaY); 
    45         var newCenter = this.map.getLonLatFromViewPortPx( newXY );  
     52        var newCenter = this.map.getLonLatFromViewPortPx( newXY ); 
    4653        this.map.setCenter(newCenter, null, this.handler.dragging); 
    4754    }, 
     55     
     56    /** 
     57     * Method: panMapDone 
     58     * Finish the panning operation.  Only call setCenter (through <panMap>) 
     59     *     if the map has actually been moved. 
     60     * 
     61     * Parameters: 
     62     * xy - {<OpenLayers.Pixel>} Pixel of the mouse position 
     63     */ 
     64    panMapDone: function(xy) { 
     65        if(this.panned) { 
     66            this.panMap(xy); 
     67            this.panned = false; 
     68        } 
     69    }, 
    4870 
    4971    CLASS_NAME: "OpenLayers.Control.DragPan" 
    5072}); 
  • tests/Control/test_DragPan.html

    old new  
    22<head> 
    33  <script src="../../lib/OpenLayers.js"></script> 
    44  <script type="text/javascript"><!-- 
    5     var map;  
     5    var map, control, layer;  
    66 
    77    function init_map() { 
    88        control = new OpenLayers.Control.DragPan(); 
     
    3636        t.eq(map.getCenter().lat, res * 5, "Lat is " + (res * 5) + " after drag"); 
    3737        t.eq(map.getCenter().lon, res * -5, "Lon is " + (res * -5) + " after drag"); 
    3838    } 
     39    function test_Control_DragPan_click(t) { 
     40        t.plan(1); 
     41        var control = new OpenLayers.Control.DragPan(); 
     42        var map = new OpenLayers.Map("map", {controls:[control]}); 
     43        var layer = new OpenLayers.Layer.WMS("OpenLayers WMS",  
     44                                        "http://labs.metacarta.com/wms/vmap0", 
     45                                        {layers: 'basic'}); 
     46        map.addLayer(layer);  
     47        map.zoomToMaxExtent(); 
     48        map.zoomIn(); 
     49        control.activate(); 
     50        map.setCenter = function() { 
     51            t.ok(false, "map.setCenter should not be called here"); 
     52        }; 
     53        var xy = new OpenLayers.Pixel(0, 0); 
     54        var down = { 
     55            'type': 'mousedown', 
     56            'xy': xy, 
     57            'which': 1 
     58        }; 
     59        var move = { 
     60            'type': 'mousemove', 
     61            'xy': xy, 
     62            'which': 1 
     63        }; 
     64        var up = { 
     65            'type': 'mouseup', 
     66            'xy': xy, 
     67            'which': 1 
     68        }; 
     69        map.events.triggerEvent('mousedown', down); 
     70        map.events.triggerEvent('mousemove', move); 
     71        map.events.triggerEvent('mouseup', up); 
     72        t.ok(true, "clicking without moving the mouse does not call setCenter"); 
     73    } 
    3974     
    4075  // --> 
    4176  </script>