OpenLayers OpenLayers

Ticket #902: handler.evt.patch

File handler.evt.patch, 8.4 kB (added by tschaub, 1 year ago)

add evt property to handlers

  • lib/OpenLayers/Handler.js

    old new  
    198198    */ 
    199199    register: function (name, method) { 
    200200        // TODO: deal with registerPriority in 3.0 
    201         this.map.events.registerPriority(name, this, method);    
     201        this.map.events.registerPriority(name, this, method); 
     202        this.map.events.registerPriority(name, this, this.setEvent); 
    202203    }, 
    203204 
    204205    /** 
     
    207208    */ 
    208209    unregister: function (name, method) { 
    209210        this.map.events.unregister(name, this, method);    
     211        this.map.events.unregister(name, this, this.setEvent); 
    210212    }, 
     213     
     214    /** 
     215     * Method: setEvent 
     216     * With each registered browser event, the handler sets its own evt 
     217     *     property.  This property can be accessed by controls if needed 
     218     *     to get more information about the event that the handler is 
     219     *     processing. 
     220     * 
     221     * This allows modifier keys on the event to be checked (alt, shift, 
     222     *     and ctrl cannot be checked with the keyboard handler).  For a 
     223     *     control to determine which modifier keys are associated with the 
     224     *     event that a handler is currently processing, it should access 
     225     *     (code)handler.evt.altKey || handler.evt.shiftKey || 
     226     *     handler.evt.ctrlKey(end). 
     227     * 
     228     * Parameters: 
     229     * evt - {Event} The browser event. 
     230     */ 
     231    setEvent: function(evt) { 
     232        this.evt = evt; 
     233        return true; 
     234    }, 
    211235 
    212236    /** 
    213237     * Method: destroy 
  • tests/Handler/test_Drag.html

    old new  
    5757        var events = ["mousedown", "mouseup", "mousemove", "mouseout", "click"]; 
    5858        var nonevents = ["dblclick", "resize", "focus", "blur"]; 
    5959        map.events.registerPriority = function(type, obj, func) { 
    60             t.eq(OpenLayers.Util.indexOf(nonevents, type), -1, 
    61                  "registered method is not one of the events " + 
    62                  "that should not be handled"); 
    63             t.ok(OpenLayers.Util.indexOf(events, type) > -1, 
    64                  "activate calls registerPriority with browser event: " + type); 
    65             t.eq(typeof func, "function", 
    66                  "activate calls registerPriority with a function"); 
    67             t.eq(func(), type, 
    68                  "activate calls registerPriority with the correct method"); 
    69             t.eq(obj["CLASS_NAME"], "OpenLayers.Handler.Drag", 
    70                  "activate calls registerPriority with the handler"); 
     60            var r = func(); 
     61            if(typeof r == "string") { 
     62                // this is one of the mock handler methods 
     63                t.eq(OpenLayers.Util.indexOf(nonevents, type), -1, 
     64                     "registered method is not one of the events " + 
     65                     "that should not be handled"); 
     66                t.ok(OpenLayers.Util.indexOf(events, type) > -1, 
     67                     "activate calls registerPriority with browser event: " + type); 
     68                t.eq(typeof func, "function", 
     69                     "activate calls registerPriority with a function"); 
     70                t.eq(func(), type, 
     71                     "activate calls registerPriority with the correct method"); 
     72                t.eq(obj["CLASS_NAME"], "OpenLayers.Handler.Drag", 
     73                     "activate calls registerPriority with the handler"); 
     74            } 
    7175        } 
    7276         
    7377        // set browser event like properties on the handler 
  • tests/test_Handler.html

    old new  
    2222    } 
    2323     
    2424    function test_Handler_activate(t) { 
    25         t.plan(42); 
     25        t.plan(52); 
    2626        var map = new OpenLayers.Map('map'); 
    2727        var control = new OpenLayers.Control(); 
    2828        map.addControl(control); 
     
    3939 
    4040        handler.active = false; 
    4141        map.events.registerPriority = function(type, obj, func) { 
    42             t.ok(OpenLayers.Util.indexOf(events, type) > -1, 
    43                  "activate calls registerPriority with browser event: " + type); 
    44             t.eq(typeof func, "function", 
    45                  "activate calls registerPriority with a function"); 
    46             t.eq(func(), type, 
    47                  "activate calls registerPriority with the correct method"); 
    48             t.eq(obj["CLASS_NAME"], "OpenLayers.Handler", 
    49                  "activate calls registerPriority with the handler"); 
     42            var r = func(); 
     43            if(typeof r == "string") { 
     44                // this is one of the mock handler methods 
     45                t.ok(OpenLayers.Util.indexOf(events, type) > -1, 
     46                     "activate calls registerPriority with browser event: " + type); 
     47                t.eq(typeof func, "function", 
     48                     "activate calls registerPriority with a function"); 
     49                t.eq(r, type, 
     50                     "activate calls registerPriority with the correct method"); 
     51                t.eq(obj["CLASS_NAME"], "OpenLayers.Handler", 
     52                     "activate calls registerPriority with the handler"); 
     53            } else { 
     54                // this is the call with handler.setEvent as the func 
     55                t.ok(r, "activate calls registerPriority with handler.setEvent"); 
     56            } 
    5057        } 
    5158         
    5259        // set browser event like properties on the handler 
     
    6370    } 
    6471     
    6572    function test_Handler_deactivate(t) { 
    66         t.plan(42); 
     73        t.plan(52); 
    6774        var map = new OpenLayers.Map('map'); 
    6875        var control = new OpenLayers.Control(); 
    6976        map.addControl(control); 
     
    8087 
    8188        handler.activate(); 
    8289        map.events.unregister = function(type, obj, func) { 
    83             t.ok(OpenLayers.Util.indexOf(events, type) > -1, 
    84                  "deactivate calls unregister with browser event: " + type); 
    85             t.eq(typeof func, "function", 
    86                  "activate calls unregister with a function"); 
    87             t.eq(func(), type, 
    88                  "activate calls unregister with the correct method"); 
    89             t.eq(obj["CLASS_NAME"], "OpenLayers.Handler", 
    90                  "activate calls unregister with the handler"); 
     90            var r = func(); 
     91            if(typeof r == "string") { 
     92                // this is one of the mock handler methods 
     93                t.ok(OpenLayers.Util.indexOf(events, type) > -1, 
     94                     "deactivate calls unregister with browser event: " + type); 
     95                t.eq(typeof func, "function", 
     96                     "activate calls unregister with a function"); 
     97                t.eq(func(), type, 
     98                     "activate calls unregister with the correct method"); 
     99                t.eq(obj["CLASS_NAME"], "OpenLayers.Handler", 
     100                     "activate calls unregister with the handler"); 
     101            } else { 
     102                // this is the call with handler.setEvent as the func 
     103                t.ok(r, "activate calls registerPriority with handler.setEvent"); 
     104            } 
    91105        } 
    92106         
    93107        // set browser event like properties on the handler 
     
    103117         
    104118    } 
    105119     
     120    function test_Handler_setEvent(t) { 
     121        t.plan(4); 
     122        var map = new OpenLayers.Map('map'); 
     123        var control = new OpenLayers.Control(); 
     124        map.addControl(control); 
     125        var handler = new OpenLayers.Handler(control); 
     126        handler.click = function(evt) { 
     127        } 
     128        handler.activate(); 
     129        var testEvent = { 
     130            xy: new OpenLayers.Pixel(Math.random(), Math.random()), 
     131            altKey: (Math.random() > 0.5), 
     132            shiftKey: (Math.random() > 0.5), 
     133            ctrlKey: (Math.random() > 0.5) 
     134        } 
     135        map.events.triggerEvent("click", testEvent); 
     136        t.ok(handler.evt.xy.x == testEvent.xy.x && 
     137             handler.evt.xy.y == testEvent.xy.y, 
     138             "handler.evt has proper xy object"); 
     139        t.eq(handler.evt.altKey, testEvent.altKey, 
     140             "handler.evt.altKey correct"); 
     141        t.eq(handler.evt.shiftKey, testEvent.shiftKey, 
     142             "handler.evt.shiftKey correct"); 
     143        t.eq(handler.evt.ctrlKey, testEvent.ctrlKey, 
     144             "handler.evt.ctrlKey correct"); 
     145    } 
     146     
    106147    function test_Handler_destroy(t) { 
    107148        t.plan(4); 
    108149        var map = new OpenLayers.Map('map');