OpenLayers OpenLayers

Ticket #827: drag.4.patch

File drag.4.patch, 6.0 kB (added by tschaub, 1 year ago)

submethods

  • tests/Handler/test_Drag.html

    old new  
    248248         
    249249    } 
    250250 
     251    function test_Handler_Drag_submethods(t) { 
     252        t.plan(4); 
     253         
     254        var map = new OpenLayers.Map('map', {controls: []}); 
     255 
     256        var control = new OpenLayers.Control(); 
     257        map.addControl(control); 
     258         
     259 
     260        var handler = new OpenLayers.Handler.Drag(control, {}); 
     261        // set test events 
     262        var events = ["down", "move", "up", "out"]; 
     263        var testEvents = {}; 
     264        var type, px; 
     265        for(var i=0; i<events.length; ++i) { 
     266            type = events[i]; 
     267            px = new OpenLayers.Pixel(Math.random(), Math.random()); 
     268            testEvents[type] = {xy: px}; 
     269            setMethod(type); 
     270        } 
     271        function setMethod(type) { 
     272            handler[type] = function(evt) { 
     273                t.ok(evt.xy.x == testEvents[type].xy.x && 
     274                     evt.xy.y == testEvents[type].xy.y, 
     275                     "handler." + type + " called with the right event"); 
     276            } 
     277        } 
     278        handler.activate(); 
     279         
     280        // test mousedown 
     281        handler.checkModifiers = function(evt) { 
     282            return true; 
     283        } 
     284        var oldIsLeftClick = OpenLayers.Event.isLeftClick; 
     285        OpenLayers.Event.isLeftClick = function(evt) { 
     286            return true; 
     287        } 
     288        map.events.triggerEvent("mousedown", testEvents.down); 
     289        OpenLayers.Event.isLeftClick = oldIsLeftClick; 
     290 
     291        // test mousemove 
     292        map.events.triggerEvent("mousemove", testEvents.move); 
     293         
     294        // test mouseup 
     295        map.events.triggerEvent("mouseup", testEvents.up); 
     296         
     297        // test mouseout 
     298        var oldMouseLeft = OpenLayers.Util.mouseLeft; 
     299        OpenLayers.Util.mouseLeft = function() { 
     300            return true; 
     301        }; 
     302        handler.started = true; 
     303        map.events.triggerEvent("mouseout", testEvents.out); 
     304        OpenLayers.Util.mouseLeft = oldMouseLeft; 
     305         
     306    } 
     307 
    251308    function test_Handler_Drag_deactivate(t) { 
    252309        t.plan(3); 
    253310        var map = new OpenLayers.Map('map'); 
  • lib/OpenLayers/Handler/Drag.js

    old new  
    7777    }, 
    7878     
    7979    /** 
     80     * The four methods below (down, move, up, and out) are used by subclasses 
     81     *     to do their own processing related to these mouse events. 
     82     */ 
     83     
     84    /** 
     85     * Method: down 
     86     * This method is called during the handling of the mouse down event. 
     87     *     Subclasses can do their own processing here. 
     88     * 
     89     * Parameters: 
     90     * evt - {Event} The mouse down event 
     91     */ 
     92    down: function(evt) { 
     93    }, 
     94     
     95    /** 
     96     * Method: move 
     97     * This method is called during the handling of the mouse move event. 
     98     *     Subclasses can do their own processing here. 
     99     * 
     100     * Parameters: 
     101     * evt - {Event} The mouse move event 
     102     * 
     103     */ 
     104    move: function(evt) { 
     105    }, 
     106 
     107    /** 
     108     * Method: up 
     109     * This method is called during the handling of the mouse up event. 
     110     *     Subclasses can do their own processing here. 
     111     * 
     112     * Parameters: 
     113     * evt - {Event} The mouse up event 
     114     */ 
     115    up: function(evt) { 
     116    }, 
     117 
     118    /** 
     119     * Method: out 
     120     * This method is called during the handling of the mouse out event. 
     121     *     Subclasses can do their own processing here. 
     122     * 
     123     * Parameters: 
     124     * evt - {Event} The mouse out event 
     125     */ 
     126    out: function(evt) { 
     127    }, 
     128 
     129    /** 
    80130     * The methods below are part of the magic of event handling.  Because 
    81131     *     they are named like browser events, they are registered as listeners 
    82132     *     for the events they represent. 
     
    100150            this.last = evt.xy; 
    101151            // TBD replace with CSS classes 
    102152            this.map.div.style.cursor = "move"; 
     153            this.down(evt); 
    103154            this.callback("down", [evt.xy]); 
    104155            OpenLayers.Event.stop(evt); 
    105156            return false; 
     
    121172        if (this.started) { 
    122173            if(evt.xy.x != this.last.x || evt.xy.y != this.last.y) { 
    123174                this.dragging = true; 
     175                this.move(evt); 
    124176                this.callback("move", [evt.xy]); 
    125177                if(!this.oldOnselectstart) { 
    126178                    this.oldOnselectstart = document.onselectstart; 
     
    148200            this.dragging = false; 
    149201            // TBD replace with CSS classes 
    150202            this.map.div.style.cursor = ""; 
     203            this.up(evt); 
    151204            this.callback("up", [evt.xy]); 
    152205            this.callback("done", [evt.xy]); 
    153206            document.onselectstart = this.oldOnselectstart; 
     
    171224            this.dragging = false; 
    172225            // TBD replace with CSS classes 
    173226            this.map.div.style.cursor = ""; 
     227            this.out(evt); 
    174228            this.callback("out", []); 
    175229            if(document.onselectstart) { 
    176230                document.onselectstart = this.oldOnselectstart; 
     
    210264     * {Boolean} The handler was successfully activated. 
    211265     */ 
    212266    activate: function() { 
     267        var activated = false; 
    213268        if(OpenLayers.Handler.prototype.activate.apply(this, arguments)) { 
    214269            this.dragging = false; 
    215             return true; 
    216         } else { 
    217             return false; 
     270            activated = true; 
    218271        } 
     272        return activated; 
    219273    }, 
    220274 
    221275    /** 
     
    226280     * {Boolean} The handler was successfully deactivated. 
    227281     */ 
    228282    deactivate: function() { 
     283        var deactivated = false; 
    229284        if(OpenLayers.Handler.prototype.deactivate.apply(this, arguments)) { 
    230285            this.dragging = false; 
    231             return true; 
    232         } else { 
    233             return false; 
     286            deactivated = true; 
    234287        } 
     288        return deactivated; 
    235289    }, 
    236290 
    237291    CLASS_NAME: "OpenLayers.Handler.Drag"