Changeset 1226

Show
Ignore:
Timestamp:
10/16/08 17:29:47 (3 months ago)
Author:
ojohner
Message:

Add radio button configuration for all layers

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • sandbox/camptocamp/MapFishAeai/client/mfbase/mapfish/widgets/tree/LayerTree.js

    r1045 r1226  
    4040 
    4141        var cb = typeof a.checked == 'boolean'; 
     42        var radioGrp = n.attributes.radioGrp || "radioGrp"; 
    4243 
    4344        var href = a.href ? a.href : Ext.isGecko ? "" : "#"; 
     
    4647            '<img src="', this.emptyIcon, '" class="x-tree-ec-icon x-tree-elbow" />', 
    4748            '<img src="', a.icon || this.emptyIcon, '" class="x-tree-node-icon',(a.icon ? " x-tree-node-inline-icon" : ""),(a.iconCls ? " "+a.iconCls : ""),'" unselectable="on" />', 
    48             cb ? ('<input class="x-tree-node-cb" type="radio" name="radio_' + n.id + '" ' + (a.checked ? 'checked="checked" />' : '/>')) : '', 
     49            cb ? ('<input class="x-tree-node-cb" type="radio" id="'+ n.id + '" name="' + radioGrp + '" ' + (a.checked ? 'checked="checked" />' : '/>')) : '', 
    4950            '<a hidefocus="on" class="x-tree-node-anchor" href="',href,'" tabIndex="1" ', 
    5051             a.hrefTarget ? ' target="'+a.hrefTarget+'"' : "", '><span unselectable="on">',n.text,"</span></a></div>", 
     
    7980                                                  .apply(this, arguments); 
    8081        var cbNode = Ext.DomQuery.selectNode(".x-tree-node-cb", this.elNode); 
     82        var radioGrp =  n.attributes.radioGrp || "radioGrp"; 
    8183        cbNode.setAttribute("type", "radio"); 
    82         cbNode.setAttribute("name", "radio_" + n.id); 
     84        cbNode.setAttribute("id",  n.id); 
     85        cbNode.setAttribute("name", radioGrp); 
    8386    }, 
    8487 
     
    602605                } 
    603606            }, this); 
    604  
    605607            return layerVisibility; 
    606608        } 
     
    624626            this.getRootNode().cascade(function(node) { 
    625627                var checked = node.attributes.checked; 
    626  
    627628                var layerNames = node.attributes.layerNames; 
     629                var radioGrp = null; 
     630 
    628631                if (!layerNames) 
    629632                    return; 
     
    636639                    if (layerVisibility[layerName] == undefined) 
    637640                        OpenLayers.Console.error("Invalid layer: ", layerName); 
     641 
     642                    if (node.attributes.radio) { 
     643                        radioGrp = node.attributes.radioGrp || "radioGrp"; 
     644 
     645                        if (!radioButton[radioGrp]) 
     646                            radioButton[radioGrp] = {}; 
     647 
     648                        radioButton[radioGrp][layerName] = checked; 
     649                    } 
    638650 
    639651                    if (forcedVisibility[layerName]) 
     
    642654                        if (this.baseLayerNames.indexOf(layerName) != -1) { 
    643655                            clickedBaseLayer = layerName; 
     656                        } 
     657                        if (radioGrp) { 
     658                            clickedRadioButton[0]  = radioGrp; 
     659                            clickedRadioButton[1]  = layerName; 
    644660                        } 
    645661                        forcedVisibility[layerName] = true; 
     
    703719 
    704720        /** 
     721         * Ensure only one radio button is checked by group. 
     722         * 
     723         * Parameters: 
     724         * layerVisibility - {Object} Map of layer name to {Boolean} 
     725         * clickedRadiButton - {Array} group name & layer name of the radio button that was clicked. 
     726         * radioButton - {Object} name of all radio button with checked state. 
     727         *               Contain the group name and the layer name. 
     728         * Returns: 
     729         * {Object} updated layerVisibility map 
     730         */ 
     731        function applyRadioButtonRestriction(layerVisibility, clickedRadioButton, 
     732                                             radioButton) { 
     733            for (var radioGrp in radioButton) { 
     734                for (var layerName in radioButton[radioGrp]) { 
     735                    if (clickedRadioButton[0] == radioGrp) { 
     736                        layerVisibility[layerName] = layerName == clickedRadioButton[1]; 
     737                    } else { 
     738                        layerVisibility[layerName] = radioButton[radioGrp][layerName]; 
     739                    } 
     740                } 
     741            } 
     742            return layerVisibility; 
     743        } 
     744 
     745        /** 
    705746         * Updates the tree from the given layerVisibility object. 
    706747         * 
     
    754795 
    755796            for (var layerName in layerVisibility) { 
    756  
    757797                var visible = layerVisibility[layerName]; 
    758798 
     
    842882 
    843883        var currentBaseLayerName; 
     884 
    844885        if (this.map.baseLayer) 
    845886            currentBaseLayerName = this.map.baseLayer.name; 
    846887        var clickedBaseLayer; 
     888        var radioButton = {}; 
     889        var clickedRadioButton = []; 
    847890 
    848891        // Definition: 
     
    855898        applyBaseLayerRestriction.call(this, layerVisibility, clickedBaseLayer, 
    856899                                       currentBaseLayerName); 
     900 
     901        applyRadioButtonRestriction.call(this, layerVisibility, clickedRadioButton, 
     902                                         radioButton); 
    857903 
    858904        updateTreeFromVisibility.call(this, layerVisibility); 
     
    10341080        this.getRootNode().cascade(function(node) { 
    10351081            var layers; 
    1036             if (!this.map || !(layers = this.nodeIdToLayers[node.id])) 
     1082            if (!node.attributes.radio && (!this.map || !(layers = this.nodeIdToLayers[node.id]))) 
    10371083                return; 
    10381084 
    1039             var isBaseLayer = true; 
    1040             Ext.each(layers, function(layer) { 
    1041                 if (!layer.isBaseLayer) { 
    1042                     isBaseLayer = false; 
    1043                     return false; 
    1044                 } 
    1045             }, this); 
    1046  
    1047             if (isBaseLayer) { 
     1085 
     1086            var isBaseLayer = false; 
     1087            if (layers) { 
     1088                isBaseLayer = true; 
     1089                Ext.each(layers, function(layer) { 
     1090                    if (!layer.isBaseLayer) { 
     1091                        isBaseLayer = false; 
     1092                        return false; 
     1093                    } 
     1094                }, this); 
     1095            } 
     1096 
     1097            if (isBaseLayer || node.attributes.radio) { 
    10481098                node.attributes.uiProvider = mapfish.widgets.RadioTreeNodeUI; 
    10491099                // The ui may already habe been instanciated here, so we