Changeset 1215

Show
Ignore:
Timestamp:
07/03/09 22:54:20 (4 years ago)
Author:
tschaub
Message:

Use radio in layer node ui to control exclusive visibility. Will be simplified significantly when #109 is closed.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • apps/opengeo/geoexplorer/trunk/externals

    • Property svn:externals changed from
      openlayers http://svn.openlayers.org/tags/openlayers/release-2.8/
      geoext http://svn.geoext.org/core/tags/geoext/release-0.5/
      ext http://svn.geoext.org/ext/2.2.1/
      to
      openlayers http://svn.openlayers.org/tags/openlayers/release-2.8/
      geoext http://svn.geoext.org/core/trunk/geoext
      ext http://svn.geoext.org/ext/2.2.1/
  • apps/opengeo/geoexplorer/trunk/src/script/app/GeoExplorer.js

    r1212 r1215  
    319319        // create layer store 
    320320        this.layers = this.mapPanel.layers; 
    321         this.registerBackgroundListeners();                        
    322321 
    323322        var addLayerButton = new Ext.Button({ 
     
    373372            expanded: true, 
    374373            group: "background", 
     374            defaults: {checkedGroup: "background"}, 
    375375            layerStore: this.mapPanel.layers, 
    376376            singleClickExpand: true, 
     
    501501            } 
    502502        });     
    503     }, 
    504  
    505     registerBackgroundListeners: function() { 
    506          
    507         var updateBackground = function(store, candidate) { 
    508  
    509             var vis = [], all = []; 
    510             store.each(function(record) { 
    511                 if(record.get("group") === "background") { 
    512                     all.push(record); 
    513                     if(record.get("layer").getVisibility()) { 
    514                         vis.push(record); 
    515                     } 
    516                 } 
    517             }); 
    518              
    519             if(all.length) { 
    520                 candidate = candidate || all[0]; 
    521                 if(vis.length === 0) { 
    522                     window.setTimeout(function() { 
    523                         candidate.get("layer").setVisibility(true); 
    524                     }, 0); 
    525                 } else if(vis.length > 1) { 
    526                     Ext.each(vis, function(r) { 
    527                         if(r !== candidate) { 
    528                             window.setTimeout(function() { 
    529                                 r.get("layer").setVisibility(false); 
    530                             }, 0); 
    531                             return false; 
    532                         } 
    533                     }); 
    534                 } 
    535             } 
    536         }; 
    537  
    538         // make sure one base layer is always visible after add, remove or update 
    539         this.layers.on({ 
    540             add: function(store, records, index) { 
    541                 var candidate; 
    542                 Ext.each(records, function(r) { 
    543                     if(r.get("group") === "background") { 
    544                         candidate = r; 
    545                         return false; 
    546                     } 
    547                 }); 
    548                 if(candidate) { 
    549                     updateBackground(store, candidate); 
    550                 } 
    551             }, 
    552             remove: function(store, record, index) { 
    553                 if(record.get("group") === "background") { 
    554                     updateBackground(store); 
    555                 } 
    556             }, 
    557             update: function(store, record, op) { 
    558                 if(record.get("group") === "background") { 
    559                     updateBackground(store, record); 
    560                 } else { 
    561                     // the visible background layer might have been moved to a new group 
    562                     var candidate; 
    563                     store.each(function(r) { 
    564                         if(r.get("group") === "background") { 
    565                             candidate = r; 
    566                             return false; 
    567                         } 
    568                     }); 
    569                     if(candidate) { 
    570                         updateBackground(store, candidate); 
    571                     } 
    572                 } 
    573             } 
    574         }); 
    575  
    576503    }, 
    577504     
  • apps/opengeo/geoexplorer/trunk/src/script/app/GeoExplorer/GroupContainer.js

    r1201 r1215  
    2828    }, 
    2929 
    30     /** 
    31      * Method: addLayerNode 
    32      * Adds a child node representing a overlay layer of the map 
     30    /** private: method[render] 
     31     *  :param bulkRender: ``Boolean`` 
     32     */ 
     33    render: function(bulkRender) { 
     34        var first = !this.rendered; 
     35        GeoExplorer.GroupContainer.superclass.render.call(this, bulkRender); 
     36        if (first) { 
     37            this.layerStore.on({ 
     38                update: this.onStoreUpdate, 
     39                scope: this 
     40            }); 
     41        } 
     42    }, 
     43     
     44    /** private: method[onStoreAdd] 
     45     *  :param store: ``Ext.data.Store`` 
     46     *  :param records: ``Array(Ext.data.Record)`` 
     47     *  :param index: ``Number`` 
     48     *   
     49     *  Listener for the store's add event. 
     50     */ 
     51    onStoreAdd: function(store, records, index) { 
     52        /** 
     53         * Containers with checkedGroup should have only one visible layer. 
     54         * This listener can be removed if the LayerNode is made to fire 
     55         * checkchange when the radio button is unchecked. 
     56         * TODO: http://www.geoext.org/trac/geoext/ticket/109 
     57         */ 
     58        GeoExplorer.GroupContainer.superclass.onStoreAdd.apply(this, arguments); 
     59        if (this.defaults && this.defaults.checkedGroup && !this._reordering) { 
     60            Ext.each(records, function(record) { 
     61                this.enforceOneVisible(record); 
     62            }, this); 
     63        } 
     64    }, 
     65 
     66 
     67    /** private: onStoreUpdate 
     68     *  :param store: ``Ext.data.Store`` 
     69     *  :param record: ``Ext.data.Record`` 
     70     *  :param operation: ``String`` 
     71     *   
     72     *  Listener for the store's update event. 
     73     */ 
     74    onStoreUpdate: function(store, record, operation) { 
     75        /** 
     76         * Containers with checkedGroup should have only one visible layer. 
     77         * This listener can be removed if the LayerNode is made to fire 
     78         * checkchange when the radio button is unchecked. 
     79         * TODO: http://www.geoext.org/trac/geoext/ticket/109 
     80         */ 
     81        if (this.defaults && this.defaults.checkedGroup) { 
     82            this.enforceOneVisible(record); 
     83        } 
     84    }, 
     85     
     86    /** private: enforceOneVisible 
     87     *  TODO: remove this when http://www.geoext.org/trac/geoext/ticket/109 is closed 
     88     */ 
     89    enforceOneVisible: function(record) { 
     90        var layer = record.get("layer"); 
     91        var inGroup = false; 
     92        this.eachChild(function(node) { 
     93            if(node.layer === layer) { 
     94                inGroup = true; 
     95            } 
     96            return !inGroup; 
     97        }); 
     98        if(inGroup) { 
     99            var vis = layer.getVisibility(); 
     100            if(vis) { 
     101                // make sure there is not more than one 
     102                this.eachChild(function(node) { 
     103                    if(node.layer !== layer && node.layer.getVisibility()) { 
     104                        window.setTimeout(function() { 
     105                            node.layer.setVisibility(false); 
     106                        }, 0); 
     107                    } 
     108                }); 
     109            } else { 
     110                // make sure there is at least one 
     111                this.eachChild(function(node) { 
     112                    vis = vis || node.layer.getVisibility(); 
     113                }); 
     114                if(!vis) { 
     115                    var child = this.lastChild; 
     116                    window.setTimeout(function() { 
     117                        child.layer.setVisibility(true); 
     118                    }, 0); 
     119                } 
     120            } 
     121        } 
     122    }, 
     123 
     124    /** private: method[addLayerNode] 
     125     *  :param layerRecord: ``Ext.data.Record`` The layer record containing the 
     126     *      layer to be added. 
     127     *  :param index: ``Number`` Optional index for the new layer.  Default is 0. 
     128     *   
     129     *  Adds a child node representing a layer of the map 
     130     */ 
     131    addLayerNode: function(layerRecord, index) { 
     132        if (layerRecord.get("group") == this.group) { 
     133            GeoExplorer.GroupContainer.superclass.addLayerNode.apply( 
     134                this, arguments 
     135            ); 
     136        } 
     137    }, 
     138     
     139    /** private: method[removeLayerNode] 
     140     *  :param layerRecord: ``Ext.data.Record`` The layer record containing the 
     141     *      layer to be removed. 
    33142     *  
    34      * Parameters: 
    35      * layerRecord - {Ext.data.Record} the layer record to add a node for 
    36      */ 
    37     addLayerNode: function(layerRecord) { 
    38         if (layerRecord.get("group") == this.group) { 
    39             GeoExplorer.GroupContainer.superclass.addLayerNode.call(this, 
    40                                                                     layerRecord); 
    41         } 
    42     }, 
    43      
    44     /** 
    45      * Method: removeLayerNode 
    46      * Removes a child node representing an overlay layer of the map 
    47      *  
    48      * Parameters: 
    49      * layerRecord - {Ext.data.Record} the layer record to remove the node for 
     143     *  Removes a child node representing a layer of the map 
    50144     */ 
    51145    removeLayerNode: function(layerRecord) { 
    52146        if (layerRecord.get("group") == this.group) { 
    53             GeoExplorer.GroupContainer.superclass.removeLayerNode.call( 
    54                 this, layerRecord); 
     147            GeoExplorer.GroupContainer.superclass.removeLayerNode.apply( 
     148                this, arguments 
     149            ); 
    55150        } 
    56151    }, 
     
    89184        var count = store.getCount(); 
    90185        var nodeIndex = -1; 
    91         var record; 
     186        var layer = this.item(index).layer; 
     187        var record, l; 
    92188        for(var i=count-1; i>=0; --i) { 
    93189            record = store.getAt(i); 
    94             if(record.get("layer").displayInLayerSwitcher && 
    95                record.get("group") == this.group) { 
     190            l = record.get("layer"); 
     191            if(l.displayInLayerSwitcher && 
     192               (record.get("group") == this.group || l === layer)) { 
    96193                ++nodeIndex; 
    97194                if(index === nodeIndex) { 
     
    121218        ); 
    122219        if(record.get("group") !== this.group) { 
    123             record.set("group", this.group); 
    124         } 
     220            window.setTimeout(function() { 
     221                var store = container.layerStore; 
     222                var index = container.indexOf(node); 
     223                var recIndex = container.nodeIndexToRecordIndex( 
     224                    container.indexOf(node) 
     225                ); 
     226                store.remove(record); 
     227                node.remove(); 
     228 
     229                // TODO: record.clone() 
     230                Ext.data.Record.AUTO_ID++; 
     231                record = record.copy(Ext.data.Record.AUTO_ID);     
     232                var layer = record.get("layer").clone(); 
     233                record.set("layer", null); 
     234                record.set("layer", layer); 
     235 
     236                record.set("group", container.group); 
     237                store.insert(recIndex, [record]); 
     238            }, 0); 
     239             
     240        } 
     241    }, 
     242 
     243    /** private: method[destroy] 
     244     */ 
     245    destroy: function() { 
     246        if(this.layerStore) { 
     247            this.layerStore.un("update", this.onStoreUpdate, this); 
     248        } 
     249        GeoExplorer.GroupContainer.superclass.destroy.apply(this, arguments); 
    125250    } 
    126251