OpenLayers OpenLayers

Ticket #1346: control_events.patch

File control_events.patch, 9.4 kB (added by tschaub, 1 year ago)

give controls an events instance

  • tests/Control/test_Navigation.html

    old new  
    1717    } 
    1818 
    1919    function test_Control_Navigation_destroy (t) { 
    20         t.plan(9); 
     20        t.plan(10); 
    2121         
    2222        var temp = OpenLayers.Control.prototype.destroy; 
    2323        OpenLayers.Control.prototype.destroy = function() { 
     
    2626        }; 
    2727 
    2828        var control = { 
     29            events: { 
     30                destroy: function() { 
     31                    t.ok(true, "events destroyed"); 
     32                } 
     33            }, 
    2934            'deactivate': function() { 
    3035                t.ok(true, "navigation control deactivated before being destroyed"); 
    3136            }, 
  • lib/OpenLayers/Control.js

    old new  
    104104     */ 
    105105    handler: null, 
    106106 
     107    /**  
     108     * Property: events 
     109     * {<OpenLayers.Events>} Events instance for triggering control specific 
     110     *     events. 
     111     */ 
     112    events: null, 
     113 
    107114    /** 
     115     * Constant: EVENT_TYPES 
     116     * {Array(String)} Supported application event types.  Register a listener 
     117     *     for a particular event with the following syntax: 
     118     * (code) 
     119     * control.events.register(type, obj, listener); 
     120     * (end) 
     121     * 
     122     * Listeners will be called with a reference to an event object.  The 
     123     *     properties of this event depends on exactly what happened. 
     124     * 
     125     * All event objects have at least the following properties: 
     126     *  - *object* {Object} A reference to control.events.object (a reference 
     127     *      to the control). 
     128     *  - *element* {DOMElement} A reference to control.events.element (which 
     129     *      will be null unless documented otherwise). 
     130     * 
     131     * Supported map event types: 
     132     *  - *activate* Triggered when activated. 
     133     *  - *deactivate* Triggered when deactivated. 
     134     */ 
     135    EVENT_TYPES: ["activate", "deactivate"], 
     136 
     137    /** 
    108138     * Constructor: OpenLayers.Control 
    109139     * Create an OpenLayers Control.  The options passed as a parameter 
    110140     * directly extend the control.  For example passing the following: 
     
    124154         
    125155        OpenLayers.Util.extend(this, options); 
    126156         
     157        this.events = new OpenLayers.Events(this, null, this.EVENT_TYPES); 
    127158        this.id = OpenLayers.Util.createUniqueID(this.CLASS_NAME + "_"); 
    128159    }, 
    129160 
     
    134165     * to prevent memory leaks. 
    135166     */ 
    136167    destroy: function () { 
     168        this.events.destroy(); 
    137169        // eliminate circular references 
    138170        if (this.handler) { 
    139171            this.handler.destroy(); 
     
    232264            this.handler.activate(); 
    233265        } 
    234266        this.active = true; 
     267        this.events.triggerEvent("activate"); 
    235268        return true; 
    236269    }, 
    237270     
     
    250283                this.handler.deactivate(); 
    251284            } 
    252285            this.active = false; 
     286            this.events.triggerEvent("deactivate"); 
    253287            return true; 
    254288        } 
    255289        return false; 
  • lib/OpenLayers/Control/NavigationHistory.js

    old new  
    8080    clearOnDeactivate: false, 
    8181 
    8282    /** 
    83      * Property: events 
    84      * {<OpenLayers.Events>} An events object that will be used for registering 
    85      *     listeners.  Defaults to the map events for this control. 
    86      */ 
    87     events: null, 
    88  
    89     /** 
    9083     * Property: registry 
    9184     * {Object} An object with keys corresponding to event types.  Values 
    9285     *     are functions that return an object representing the current state. 
     
    144137 
    145138        var previousOptions = { 
    146139            trigger: OpenLayers.Function.bind(this.previousTrigger, this), 
    147             displayClass: this.displayClass + "Previous", 
    148             onActivate: function() {}, 
    149             onDeactivate: function() {} 
     140            displayClass: this.displayClass + "Previous" 
    150141        }; 
    151142        if(options) { 
    152143            OpenLayers.Util.extend(previousOptions, options.previousOptions); 
     
    155146         
    156147        var nextOptions = { 
    157148            trigger: OpenLayers.Function.bind(this.nextTrigger, this), 
    158             displayClass: this.displayClass + "Next", 
    159             onActivate: function() {}, 
    160             onDeactivate: function() {} 
     149            displayClass: this.displayClass + "Next" 
    161150        }; 
    162151        if(options) { 
    163152            OpenLayers.Util.extend(nextOptions, options.nextOptions); 
     
    179168    onPreviousChange: function(state, length) { 
    180169        if(state && !this.previous.active) { 
    181170            this.previous.activate(); 
    182             this.previous.onActivate(); 
    183171        } else if(!state && this.previous.active) { 
    184172            this.previous.deactivate(); 
    185             this.previous.onDeactivate(); 
    186173        } 
    187174    }, 
    188175     
     
    199186    onNextChange: function(state, length) { 
    200187        if(state && !this.next.active) { 
    201188            this.next.activate(); 
    202             this.next.onActivate(); 
    203189        } else if(!state && this.next.active) { 
    204190            this.next.deactivate(); 
    205             this.next.onDeactivate(); 
    206191        } 
    207192    }, 
    208193     
     
    364349                if(this.listeners == null) { 
    365350                    this.setListeners(); 
    366351                } 
    367                 if(!this.events) { 
    368                     this.events = this.map.events; 
    369                 } 
    370352                for(var type in this.listeners) { 
    371                     this.events.register(type, this, this.listeners[type]); 
     353                    this.map.events.register(type, this, this.listeners[type]); 
    372354                } 
    373355                activated = true; 
    374356                if(this.previousStack.length == 0) { 
     
    402384        if(this.map) { 
    403385            if(OpenLayers.Control.prototype.deactivate.apply(this)) { 
    404386                for(var type in this.listeners) { 
    405                     this.events.unregister( 
     387                    this.map.events.unregister( 
    406388                        type, this, this.listeners[type] 
    407389                    ); 
    408390                } 
  • lib/OpenLayers/Control/Panel.js

    old new  
    4545    destroy: function() { 
    4646        OpenLayers.Control.prototype.destroy.apply(this, arguments); 
    4747        for(var i = this.controls.length - 1 ; i >= 0; i--) { 
     48            this.controls[i].events.un({ 
     49                "activate": this.redraw, 
     50                "deactivate": this.redraw, 
     51                scope: this 
     52            }); 
    4853            OpenLayers.Event.stopObservingElement(this.controls[i].panel_div); 
    4954            this.controls[i].panel_div = null; 
    5055        }     
     
    7580            for(var i = 0; i < this.controls.length; i++) { 
    7681                this.controls[i].deactivate(); 
    7782            }     
    78             this.redraw(); 
    7983            return true; 
    8084        } else { 
    8185            return false; 
     
    9397        for (var i = 0; i < this.controls.length; i++) { 
    9498            this.map.addControl(this.controls[i]); 
    9599            this.controls[i].deactivate(); 
     100            this.controls[i].events.on({ 
     101                "activate": this.redraw, 
     102                "deactivate": this.redraw, 
     103                scope: this 
     104            }); 
    96105        } 
    97106        this.activate(); 
    98107        return this.div; 
     
    145154                } 
    146155            } 
    147156        } 
    148         this.redraw(); 
    149157    }, 
    150158 
    151159    /** 
     
    185193            for (var i = 0; i < controls.length; i++) { 
    186194                this.map.addControl(controls[i]); 
    187195                controls[i].deactivate(); 
     196                controls[i].events.on({ 
     197                    "activate": this.redraw, 
     198                    "deactivate": this.redraw, 
     199                    scope: this 
     200                }); 
    188201            } 
    189202            this.redraw(); 
    190203        } 
  • examples/navigation-history.html

    old new  
    3636            function init(){ 
    3737                map = new OpenLayers.Map('map'); 
    3838                 
    39                 // set any application specific behavior here 
    40                 // this will become unnecessary when controls have better event handling 
    41                 var previousOptions = { 
    42                     onActivate: function() {panel.redraw();}, 
    43                     onDeactivate: function() {panel.redraw();} 
    44                 }; 
    45                 var nextOptions = { 
    46                     onActivate: function() {panel.redraw();}, 
    47                     onDeactivate: function() {panel.redraw();} 
    48                 }; 
    49                 var options = { 
    50                     previousOptions: previousOptions, 
    51                     nextOptions: nextOptions 
    52                 }; 
    53                 nav = new OpenLayers.Control.NavigationHistory(options); 
     39                nav = new OpenLayers.Control.NavigationHistory(); 
     40                // parent control must be added to the map 
    5441                map.addControl(nav); 
    5542 
    5643                panel = new OpenLayers.Control.Panel(