OpenLayers OpenLayers

Changeset 3891

Show
Ignore:
Timestamp:
08/10/07 23:32:41 (1 year ago)
Author:
tschaub
Message:

#774 - drag control only calls move callback with a change in pixel position - DragPan control simplified

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/openlayers/lib/OpenLayers/Control/DragPan.js

    r3787 r3891  
    2828    draw: function() { 
    2929        this.handler = new OpenLayers.Handler.Drag( this, 
    30                             {"move": this.panMap, "up": this.panMapDone } ); 
     30                            {"move": this.panMap, "up": this.panMap} ); 
    3131    }, 
    3232 
     
    3838    */ 
    3939    panMap: function (xy) { 
    40         var deltaX = this.handler.start.x - xy.x; 
    41         var deltaY = this.handler.start.y - xy.y; 
     40        var deltaX = this.handler.last.x - xy.x; 
     41        var deltaY = this.handler.last.y - xy.y; 
    4242        var size = this.map.getSize(); 
    4343        var newXY = new OpenLayers.Pixel(size.w / 2 + deltaX, 
    4444                                         size.h / 2 + deltaY); 
    4545        var newCenter = this.map.getLonLatFromViewPortPx( newXY );  
    46         this.map.setCenter(newCenter, null, true); 
    47         // this assumes xy won't be changed inside Handler.Drag 
    48         // a safe bet for now, and saves us the extra call to clone(). 
    49         this.handler.start = xy; 
    50     }, 
    51      
    52     /** 
    53     * Method: panMapDone 
    54     *  
    55     * Parameters: 
    56     * xy - {<OpenLayers.Pixel>} Pixel of the up position 
    57     */ 
    58     panMapDone: function (xy) { 
    59         var deltaX = this.handler.start.x - xy.x; 
    60         var deltaY = this.handler.start.y - xy.y; 
    61         var size = this.map.getSize(); 
    62         var newXY = new OpenLayers.Pixel(size.w / 2 + deltaX, 
    63                                          size.h / 2 + deltaY); 
    64         var newCenter = this.map.getLonLatFromViewPortPx( newXY );  
    65         this.map.setCenter(newCenter, null, false); 
    66         // this assumes xy won't be changed inside Handler.Drag 
    67         // a safe bet for now, and saves us the extra call to clone(). 
    68         this.handler.start = xy; 
     46        this.map.setCenter(newCenter, null, this.handler.dragging); 
    6947    }, 
    7048 
  • trunk/openlayers/lib/OpenLayers/Handler/Drag.js

    r3787 r3891  
    4040 
    4141    /**  
    42      * Property: start  
     42     * Property: last 
    4343     * {<OpenLayers.Pixel>}  
    4444     */ 
    45     start: null, 
     45    last: null, 
    4646 
    4747    /** 
     
    9191            this.started = true; 
    9292            this.dragging = false; 
    93             this.start = evt.xy.clone()
     93            this.last = evt.xy
    9494            // TBD replace with CSS classes 
    9595            this.map.div.style.cursor = "move"; 
     
    113113    mousemove: function (evt) { 
    114114        if (this.started) { 
    115             this.dragging = true; 
    116             this.callback("move", [evt.xy]); 
    117             if(!this.oldOnselectstart) { 
    118                 this.oldOnselectstart = document.onselectstart; 
    119                 document.onselectstart = function() {return false;} 
     115            if(evt.xy.x != this.last.x || evt.xy.y != this.last.y) { 
     116                this.dragging = true; 
     117                this.callback("move", [evt.xy]); 
     118                if(!this.oldOnselectstart) { 
     119                    this.oldOnselectstart = document.onselectstart; 
     120                    document.onselectstart = function() {return false;} 
     121                } 
     122                this.last = evt.xy; 
    120123            } 
    121124        } 
     
    136139        if (this.started) { 
    137140            this.started = false; 
     141            this.dragging = false; 
    138142            // TBD replace with CSS classes 
    139143            this.map.div.style.cursor = ""; 
  • trunk/openlayers/tests/Handler/test_Drag.html

    r3870 r3891  
    8484 
    8585    function test_Handler_Drag_callbacks(t) { 
    86         t.plan(25); 
     86        t.plan(28); 
    8787         
    8888        var map = new OpenLayers.Map('map', {controls: []}); 
     
    142142        t.ok(handler.started, "mousedown sets the started flag to true"); 
    143143        t.ok(!handler.dragging, "mouse down sets the dragging flag to false"); 
     144        t.ok(handler.last.x == testEvents.down.xy.x && 
     145             handler.last.y == testEvents.down.xy.y, 
     146             "mouse down sets handler.last correctly"); 
    144147        OpenLayers.Event.isLeftClick = oldIsLeftClick; 
    145148        OpenLayers.Event.stop = oldStop; 
     
    157160        map.events.triggerEvent("mousemove", testEvents.move); 
    158161        t.ok(handler.dragging, "mousemove sets the dragging flag to true"); 
     162        t.ok(handler.last.x == testEvents.move.xy.x && 
     163             handler.last.y == testEvents.move.xy.y, 
     164             "mouse move sets handler.last correctly"); 
     165         
     166        // a second move with the same evt.xy should not trigger move callback 
     167        // if it does, the test page will complain about a bad plan number 
     168        var oldMove = handler.callbacks.move; 
     169        handler.callbacks.move = function() { 
     170            t.ok(false, 
     171                 "a second move with the same evt.xy should not trigger a move callback"); 
     172        } 
     173        map.events.triggerEvent("mousemove", testEvents.move); 
     174        handler.callbacks.move = oldMove; 
    159175         
    160176        // test mouseup 
     
    171187        map.events.triggerEvent("mouseup", testEvents.up); 
    172188        t.ok(!this.started, "mouseup sets the started flag to false"); 
     189        t.ok(!this.dragging, "mouseup sets the dragging flag to false"); 
    173190         
    174191        // test mouseout