OpenLayers OpenLayers

Changeset 4062

Show
Ignore:
Timestamp:
08/27/07 15:55:04 (1 year ago)
Author:
tschaub
Message:

Give handlers a non-API evt property - this to be used by other controls in the library only. Eventually, we may decide to restructure this. (Closes #902)

Files:

Legend:

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

    r3984 r4062  
    7373     */ 
    7474    active: false, 
     75     
     76    /** 
     77     * Property: evt 
     78     * {Event} This property references the last event handled by the handler. 
     79     *     Note that this property is not part of the stable API.  Use of the 
     80     *     evt property should be restricted to controls in the library 
     81     *     or other applications that are willing to update with changes to 
     82     *     the OpenLayers code. 
     83     */ 
     84    evt: null, 
    7585 
    7686    /** 
     
    199209    register: function (name, method) { 
    200210        // TODO: deal with registerPriority in 3.0 
    201         this.map.events.registerPriority(name, this, method);    
     211        this.map.events.registerPriority(name, this, method); 
     212        this.map.events.registerPriority(name, this, this.setEvent); 
    202213    }, 
    203214 
     
    208219    unregister: function (name, method) { 
    209220        this.map.events.unregister(name, this, method);    
     221        this.map.events.unregister(name, this, this.setEvent); 
     222    }, 
     223     
     224    /** 
     225     * Method: setEvent 
     226     * With each registered browser event, the handler sets its own evt 
     227     *     property.  This property can be accessed by controls if needed 
     228     *     to get more information about the event that the handler is 
     229     *     processing. 
     230     * 
     231     * This allows modifier keys on the event to be checked (alt, shift, 
     232     *     and ctrl cannot be checked with the keyboard handler).  For a 
     233     *     control to determine which modifier keys are associated with the 
     234     *     event that a handler is currently processing, it should access 
     235     *     (code)handler.evt.altKey || handler.evt.shiftKey || 
     236     *     handler.evt.ctrlKey(end). 
     237     * 
     238     * Parameters: 
     239     * evt - {Event} The browser event. 
     240     */ 
     241    setEvent: function(evt) { 
     242        this.evt = evt; 
     243        return true; 
    210244    }, 
    211245 
  • trunk/openlayers/tests/Handler/test_Drag.html

    r4059 r4062  
    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         
  • trunk/openlayers/tests/Handler/test_MouseWheel.html

    r4059 r4062  
    7373        var nonevents = ["dblclick", "resize", "focus", "blur"]; 
    7474        map.events.registerPriority = function(type, obj, func) { 
    75             t.eq(OpenLayers.Util.indexOf(nonevents, type), -1, 
    76                  "registered method is not one of the events " + 
    77                  "that should not be handled"); 
    78             t.ok(OpenLayers.Util.indexOf(events, type) > -1, 
    79                  "activate calls registerPriority with browser event: " + type); 
    80             t.eq(typeof func, "function", 
    81                  "activate calls registerPriority with a function"); 
    82             t.eq(func(), type, 
    83                  "activate calls registerPriority with the correct method"); 
    84             t.eq(obj["CLASS_NAME"], "OpenLayers.Handler.MouseWheel", 
    85                  "activate calls registerPriority with the handler"); 
     75            var r = func(); 
     76            if(typeof r == "string") { 
     77                t.eq(OpenLayers.Util.indexOf(nonevents, type), -1, 
     78                     "registered method is not one of the events " + 
     79                     "that should not be handled"); 
     80                t.ok(OpenLayers.Util.indexOf(events, type) > -1, 
     81                     "activate calls registerPriority with browser event: " + type); 
     82                t.eq(typeof func, "function", 
     83                     "activate calls registerPriority with a function"); 
     84                t.eq(func(), type, 
     85                     "activate calls registerPriority with the correct method"); 
     86                t.eq(obj["CLASS_NAME"], "OpenLayers.Handler.MouseWheel", 
     87                     "activate calls registerPriority with the handler"); 
     88            } 
    8689        } 
    8790         
  • trunk/openlayers/tests/test_Handler.html

    r4059 r4062  
    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(); 
     
    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         
     
    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(); 
     
    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         
     
    102116             "deactivated returns true if the handler is already active"); 
    103117         
     118    } 
     119     
     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"); 
    104145    } 
    105146