Changeset 430

Show
Ignore:
Timestamp:
04/23/08 14:05:46 (5 years ago)
Author:
fvanderbiest
Message:

fixed #94 : toolbar buttons now reflect underlying OL control state

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/MapFish/client/mfbase/mapfish/widgets/toolbar/Toolbar.js

    r415 r430  
    6666     */ 
    6767    configurable: false, 
     68     
     69    /**  
     70     * Property: buttons  
     71     * Array({<Ext.Toolbar.Button>})  
     72     */  
     73    buttons: [], 
    6874 
    6975    /** 
     
    8995     * Parameters: 
    9096     * control - {<OpenLayers.Control>} 
    91      * options - the config object 
     97     * options - the config object for the newly created Ext.Toolbar.Button 
    9298     * 
    9399     * Returns: 
    94      * An instance of Ext.Toolbar.Button 
     100     * {<Ext.Toolbar.Button>} The added button instance 
    95101     */ 
    96102    addControl: function (control, options) { 
     
    100106        var mb = new Ext.Toolbar.Button(options); 
    101107        mb.tooltip = control.title; 
    102         mb.enableToggle = true; 
     108        mb.enableToggle = (control.type == OpenLayers.Control.TYPE_TOGGLE); 
    103109        if (control.isDefault) { 
    104110            mb.pressed = true; 
     
    106112        } 
    107113        mb.scope = this; 
    108         mb.handler = function() { this.activateControl(control); } 
     114        mb.handler = function() {  
     115            this.activateControl(control);  
     116        }; 
    109117        this.add(mb); 
     118        this.buttons.push(mb); 
    110119        return mb; 
    111120    }, 
     
    115124     * Pass in the CLASS_NAME of a control as a string and return the control itself 
    116125     * 
    117      * Paramaters: 
     126     * Parameters:  
    118127     * className - string 
     128     * 
     129     * Returns: 
     130     * {<OpenLayers.Control>} The requested control. 
    119131     */ 
    120132    getControlByClassName: function(className) { 
     
    126138            } 
    127139        } 
     140        return null; 
     141    }, 
     142 
     143    /** 
     144     * Method: getButtonForControl 
     145     * Pass in a control and return the button attached to this control 
     146     * 
     147     * Parameters: 
     148     * control - {<OpenLayers.Control>} A control which was previously added to the toolbar 
     149     * 
     150     * Returns: 
     151     * {<Ext.Toolbar.Button>} The requested button. 
     152     */ 
     153    getButtonForControl: function(control) {  
     154        if (this.controls) {  
     155            for (var i = 0;  i < this.controls.length; i++) {  
     156                if (this.controls[i] == control) {  
     157                    return this.buttons[i]; 
     158                }  
     159            }  
     160        }  
     161        return null; 
    128162    }, 
    129163 
    130164    /** 
    131165     * Method: activate 
     166     * Activates the toolbar, either by restoring a given state (if configurable) or the default one. 
    132167     */ 
    133168    activate: function() { 
     
    138173            mb.menu = new Ext.menu.Menu(); 
    139174            for(var i = 0; i < this.controls.length; i++) { 
    140                 mb.menu.add({'style': 'height:25px', 
    141                             'text': '<div style="position: relative; left: 25px; top: -15px;" class="' + this.items.items[i].iconCls + '"/>', 
    142                             checked: this.controls[i].visible, 
    143                             scope: {toolbar: this, button: this.items.items[i], control: this.controls[i]}, 
    144                             checkHandler: function(item, checked) { 
    145                                 if (checked) { 
    146                                     this.control.visible = true; 
    147                                     if (this.control == this.toolbar.defaultControl) { 
    148                                         this.toolbar.activateControl(this.control); 
    149                                     } this.button.show(); 
    150                                 } else { 
    151                                     this.control.visible = false; 
    152                                     this.control.deactivate(); 
    153                                     this.button.hide(); 
    154                                 } 
    155                                 this.toolbar.saveState(); 
    156                         }}); 
     175                mb.menu.add({ 
     176                    'style': 'height:25px', 
     177                    'text': '<div style="position: relative; left: 25px; top: -15px;" class="' + this.buttons[i].iconCls + '"/>', 
     178                    checked: this.controls[i].visible, 
     179                    scope: { 
     180                        toolbar: this,  
     181                        button: this.buttons[i],  
     182                        control: this.controls[i] 
     183                    }, 
     184                    checkHandler: function(item, checked) { 
     185                        if (checked) { 
     186                            this.control.visible = true; 
     187                            if (this.control == this.toolbar.defaultControl) { 
     188                                this.toolbar.activateControl(this.control); 
     189                            }  
     190                            this.button.show(); 
     191                        } else { 
     192                            this.control.visible = false; 
     193                            this.control.deactivate(); 
     194                            this.button.hide(); 
     195                        } 
     196                        this.toolbar.saveState(); 
     197                    } 
     198                }); 
    157199            } 
    158200            this.add(mb); 
    159         } else { 
     201        } else if (this.defaultControl) { 
    160202            this.activateControl(this.defaultControl); 
    161203        } 
     
    164206    /** 
    165207     * Method: deactivate 
     208     * Deactivates all controls in this toolbar. 
    166209     */ 
    167210    deactivate: function() { 
    168211        for(var i = 0; i < this.controls.length; i++) { 
    169212            this.controls[i].deactivate(); 
     213            if (this.controls[i].type != OpenLayers.Control.TYPE_BUTTON) {  
     214                this.buttons[i].toggle(false);  
     215            } 
    170216        } 
    171217    }, 
     
    191237                    c.visible = s.visible; 
    192238                    if (!c.visible) { 
    193                         this.items.items[i].hide(); 
     239                        this.buttons[i].hide(); 
    194240                    } 
    195241                } 
     
    216262    /** 
    217263     * Method: activateControl 
    218      * Activate a control on the map 
     264     * Activates a control on the map 
    219265     * (Taken from OpenLayers.Panel) 
    220266     * 
     
    223269     */ 
    224270    activateControl: function (control) { 
     271        var button = this.getButtonForControl(control); 
     272        if (!button) { 
     273            OpenLayers.Console.warn("Toolbar.activateControl : button was not found"); 
     274            return; 
     275        } 
    225276        if (control.type == OpenLayers.Control.TYPE_BUTTON) { 
    226277            control.trigger(); 
     
    230281            if (control.active) { 
    231282                control.deactivate(); 
     283                button.toggle(false);  
    232284            } else { 
    233285                control.activate(); 
     286                button.toggle(true);  
    234287            } 
    235288            return; 
     
    238291            if (this.controls[i] == control && control.visible) { 
    239292                control.activate(); 
     293                button.toggle(true);  
    240294            } else { 
    241295                if (this.controls[i].type != OpenLayers.Control.TYPE_TOGGLE) { 
    242296                    this.controls[i].deactivate(); 
     297                    this.buttons[i].toggle(false);  
    243298                } 
    244299            }