OpenLayers OpenLayers

Changeset 6167

Show
Ignore:
Timestamp:
02/09/08 11:46:02 (7 months ago)
Author:
tschaub
Message:

Giving all controls an events instance. You can now listen for activate and deactivate on any control. Panel controls do this to know when they should redraw. Navigation history control demonstrates the effect of this change. r=elemoine (closes #1346)

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/openlayers/examples/navigation-history.html

    r6157 r6167  
    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 
  • trunk/openlayers/lib/OpenLayers/Control.js

    r6106 r6167  
    105105    handler: null, 
    106106 
     107    /**  
     108     * Property: events 
     109     * {<OpenLayers.Events>} Events instance for triggering control specific 
     110     *     events. 
     111     */ 
     112    events: null, 
     113 
     114    /** 
     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 
    107137    /** 
    108138     * Constructor: OpenLayers.Control 
     
    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    }, 
     
    135166     */ 
    136167    destroy: function () { 
     168        this.events.destroy(); 
    137169        // eliminate circular references 
    138170        if (this.handler) { 
     
    233265        } 
    234266        this.active = true; 
     267        this.events.triggerEvent("activate"); 
    235268        return true; 
    236269    }, 
     
    251284            } 
    252285            this.active = false; 
     286            this.events.triggerEvent("deactivate"); 
    253287            return true; 
    254288        } 
  • trunk/openlayers/lib/OpenLayers/Control/NavigationHistory.js

    r6157 r6167  
    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 
     
    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) { 
     
    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) { 
     
    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    }, 
     
    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    }, 
     
    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; 
     
    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                    ); 
  • trunk/openlayers/lib/OpenLayers/Control/Panel.js

    r5910 r6167  
    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; 
     
    7681                this.controls[i].deactivate(); 
    7782            }     
    78             this.redraw(); 
    7983            return true; 
    8084        } else { 
     
    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(); 
     
    146155            } 
    147156        } 
    148         this.redraw(); 
    149157    }, 
    150158 
     
    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(); 
  • trunk/openlayers/tests/Control/test_Navigation.html

    r6131 r6167  
    1818 
    1919    function test_Control_Navigation_destroy (t) { 
    20         t.plan(9); 
     20        t.plan(10); 
    2121         
    2222        var temp = OpenLayers.Control.prototype.destroy; 
     
    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");