OpenLayers OpenLayers

Ticket #902: evt.patch

File evt.patch, 3.4 kB (added by euzuro, 1 year ago)

Possible reworking of the functionality to give the handler an 'evt' property on events. This is not a complete patch, only a suggested attack plan. Tests do not pass.

  • lib/OpenLayers/Handler.js

    old new  
    8484    evt: null, 
    8585 
    8686    /** 
     87     * Property: registeredMethods 
     88     * {Object} Hash of registered events. Keyed by the name of the event, the  
     89     *     value is an Array of Objects. Each object has two functions:  
     90     *     'method', the function which the user wishes to register, and 
     91     *     'registeredFunc', which is a specially created wrapper function  
     92     *     which first sets the 'evt' property on the Handler, and then calls 
     93     *     the previously mentioned 'method'.  
     94     */ 
     95    registeredMethods: null, 
     96     
     97    /** 
    8798     * Constructor: OpenLayers.Handler 
    8899     * Construct a handler. 
    89100     * 
     
    109120        OpenLayers.Util.extend(this, options); 
    110121         
    111122        this.id = OpenLayers.Util.createUniqueID(this.CLASS_NAME + "_"); 
     123 
     124        this.registeredMethods = {}; 
    112125    }, 
    113126     
    114127    /** 
     
    208221    */ 
    209222    register: function (name, method) { 
    210223        // TODO: deal with registerPriority in 3.0 
    211         this.map.events.registerPriority(name, this, method); 
    212         this.map.events.registerPriority(name, this, this.setEvent); 
     224         
     225        var wrapperFunction = function(evt) { 
     226            this.evt = evt;  
     227            return method.apply(this, [evt]); 
     228        }; 
     229         
     230        var eventArray = this.registeredMethods.name; 
     231        if (!eventArray) { 
     232            this.registeredMethods.name = []; 
     233            eventArray = this.registeredMethods.name; 
     234        } 
     235        var registryEntry = { 
     236            'method': method, 
     237            'registeredFunction': wrapperFunction 
     238        }; 
     239        eventArray.push(registryEntry); 
     240         
     241        this.map.events.registerPriority(name, this, wrapperFunction); 
    213242    }, 
    214243 
    215244    /** 
     
    217246    * unregister an event from the map 
    218247    */ 
    219248    unregister: function (name, method) { 
    220         this.map.events.unregister(name, this, method);    
    221         this.map.events.unregister(name, this, this.setEvent); 
     249        var eventArray = this.registeredMethods.name; 
     250        if (eventArray) { 
     251            for(var i=0; i < eventArray.length; i++) { 
     252                var registryEntry = eventArray[i]; 
     253                if (registryEntry.method == method) { 
     254                    var registeredFunction = registryEntry.registeredFunction; 
     255                    this.map.events.unregister(name, this, registeredFunction); 
     256                } 
     257            } 
     258        } 
    222259    }, 
    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; 
    244     }, 
    245260 
    246261    /** 
    247262     * Method: destroy