OpenLayers OpenLayers

Changeset 7922

Show
Ignore:
Timestamp:
09/02/08 10:53:32 (3 months ago)
Author:
jvanulden
Message:

--

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • sandbox/jvanulden/openlayers/lib/OpenLayers/Control/LayerSwitcher.js

    r7906 r7922  
    7373     * Property: activeLayer 
    7474     */ 
    75     activeLayer: null, 
     75    activeLayer: null,     
     76     
     77    /**  
     78     * Property: useLegendGraphics 
     79     */ 
     80    useLegendGraphics: false, 
    7681     
    7782    /**  
     
    166171 
    167172        // populate div with current info 
    168         this.redraw();       
    169                
     173        this.redraw(); 
     174 
    170175        return this.div; 
    171176    }, 
     
    276281                // layers if they are visible 
    277282                var checked = (baseLayer) ? (layer == this.map.baseLayer) 
    278                                           : layer.getVisibility(); 
    279      
     283                                          : layer.getVisibility();               
     284                 
     285                var layerWrapper = document.createElement("div"); 
     286                layerWrapper.id = "layer_" + layer.id; 
     287                 
    280288                // create input element 
    281289                var inputElem = document.createElement("input"); 
     
    309317                titleDiv.id = "title_" + layer.id;                 
    310318                titleDiv.style.backgroundColor = "#e1e1e1"; 
    311                 titleDiv.style.width = "215px"; 
     319                titleDiv.style.width = "220px"; 
    312320                titleDiv.style.padding = "2px"; 
    313321                titleDiv.style.border = "solid 1px #e1e1e1"; 
     
    364372                 
    365373                // set the default opacity 
    366                 layer.setOpacity("1.0"); 
    367                            
    368                 // create legend div 
    369                 var legendDiv = document.createElement("div"); 
    370                 legendDiv.style.position = "relative"; 
    371                  
    372                 // create legend img element 
    373                 var legendImg = document.createElement("img"); 
    374                 legendImg.id = "leg_" + layer.id; 
    375                 legendImg.style.display = "none"; 
    376  
     374                layer.setOpacity("1.0");     
     375                 
    377376                var context = { 
     377                    'layer': layer, 
    378378                    'inputElem': inputElem, 
    379                     'layer': layer, 
    380379                    'titleDiv': titleDiv, 
    381                     'legendImg': legendImg, 
    382380                    'layerSwitcher': this 
    383                 }; 
    384                  
    385                 // if the service doesn't return an image, insert a transparent gif 
    386                 OpenLayers.Event.observe(legendImg, "error", 
    387                     OpenLayers.Function.bind(this.onLegendImgError, context) 
    388                 );                 
     381                };                    
    389382                                               
    390383                // bind events to the layer operation buttons 
     
    435428                    'layer': layer, 
    436429                    'inputElem': inputElem, 
    437                     'legendDiv': legendDiv, 
    438430                    'titleDiv': titleDiv, 
    439431                    'labelSpan': labelSpan 
     
    442434                var groupDiv = (baseLayer) ? this.baseLayersDiv 
    443435                                           : this.dataLayersDiv; 
    444                  
    445                 groupDiv.appendChild(titleDiv);                            
     436                                            
     437                groupDiv.appendChild(layerWrapper); 
     438                layerWrapper.appendChild(titleDiv);                            
    446439                titleDiv.appendChild(inputElem); 
    447440                titleDiv.appendChild(buttonSpan);             
     
    455448                buttonSpan.appendChild(opacityPlusButton); 
    456449                titleDiv.appendChild(labelSpan);  
    457                 legendDiv.appendChild(legendImg); 
    458                 groupDiv.appendChild(legendDiv); 
    459                 groupDiv.appendChild(br);   
    460                  
     450                groupDiv.appendChild(br);             
     451               
    461452            }   
    462453 
     
    469460        this.baseLbl.style.display = (containsBaseLayers) ? "" : "none";   
    470461         
    471         // load the legend images 
    472         this.loadLegendImages();   
     462        if(this.useLegendGraphics){ this.getLegendGraphics(this.useLegendGraphics) }; 
    473463         
    474464        return this.div; 
     
    477467    /**  
    478468     * Method: 
    479      * Load legend images 
     469     * Load legend images using OGC GetLegendGraphic request 
     470     * 
     471     * NOTE: this will only work for layers that are hosted 
     472     * on an OGC WMS service. 
    480473     *  
    481474     * Parameters: 
    482      * e - {Event}  
    483      */ 
    484     loadLegendImages: function(e) 
     475     * enableRedraw - If true legend graphics are redrawn with 
     476     *                layerSwitcher, if false legend graphics 
     477     *                are only drawn on request. 
     478     */ 
     479    getLegendGraphics: function(enableRedraw) 
    485480    { 
     481        this.useLegendGraphics = (enableRedraw) ? true : false; 
     482         
    486483        var len = this.map.layers.length; 
    487484        for (var i=0; i <len; i++)  
    488485        { 
    489486            var layer = this.map.layers[i]; 
    490             var img = OpenLayers.Util.getElement("leg_" + layer.id); 
     487            var layerWrapper = OpenLayers.Util.getElement("layer_" + layer.id); 
     488            var legendImg = document.createElement("img");     
    491489             
    492             if(img && layer.isBaseLayer == false) 
     490            var context = { 
     491                    'legendImg': legendImg, 
     492                    'layerSwitcher': this 
     493                }; 
     494                 
     495            // if the service doesn't return an image, insert a transparent gif 
     496            OpenLayers.Event.observe(legendImg, "error", 
     497                OpenLayers.Function.bind(this.onLegendImgError, context) 
     498            );         
     499             
     500            if(layer.isBaseLayer == false) 
    493501            { 
    494502                // build getLegendGraphic request 
    495                 var url = layer.getFullRequestString({ 
     503                var legendGraphicURL = layer.getFullRequestString({ 
    496504                          REQUEST: "GetLegendGraphic", 
    497505                          LAYER: layer.params.LAYERS, 
     
    499507                          WIDTH: "150"}); 
    500508                           
    501                 img.src = url; 
    502                 img.style.display = "inline"
     509                legendImg.src = legendGraphicURL;                 
     510                layerWrapper.appendChild(legendImg)
    503511            } 
    504512        }     
     513    }, 
     514     
     515    /** 
     516     * Method: onLegendImgError 
     517     * Load blank image if getLegendGraphic request fails 
     518     *  
     519     * Parameters:  
     520     * e - {Event}  
     521     */ 
     522    onLegendImgError: function(e) 
     523    { 
     524        this.legendImg.src = "../img/blank.gif"; 
     525         
     526        OpenLayers.Event.stop(e);  
    505527    }, 
    506528     
     
    528550            } 
    529551        } 
     552         
    530553        OpenLayers.Event.stop(e); 
    531554    },     
    532  
    533     /** 
    534      * Method: onLegendImgError 
    535      * Load blank image if none found 
    536      *  
    537      * Parameters:  
    538      * e - {Event}  
    539      */ 
    540     onLegendImgError: function(e) 
    541     { 
    542         this.legendImg.src = "../img/blank.gif"; 
    543          
    544         OpenLayers.Event.stop(e);  
    545     }, 
    546555 
    547556    /** 
     
    763772        this.div.style.color = "#333";    
    764773        this.div.style.backgroundColor = "transparent"; 
    765  
    766774     
    767775        OpenLayers.Event.observe(this.div, "mouseup",  
     
    776784        this.layersDiv = document.createElement("div"); 
    777785        this.layersDiv.id = this.id + "_layersDiv"; 
    778         this.layersDiv.style.paddingTop = "10px"; 
    779         this.layersDiv.style.paddingLeft = "10px"; 
    780         this.layersDiv.style.paddingBottom = "5px"; 
     786        this.layersDiv.style.paddingTop = "5px"; 
     787        this.layersDiv.style.paddingLeft = "5px"; 
     788        this.layersDiv.style.paddingBottom = "10px"; 
    781789        this.layersDiv.style.paddingRight = "75px"; 
    782790        this.layersDiv.style.backgroundColor = this.activeColor;  
     
    798806        this.dataLayersDiv = document.createElement("div"); 
    799807        this.dataLayersDiv.style.paddingLeft = "5px"; 
    800  
     808        
    801809        if (this.ascending) { 
    802810            this.layersDiv.appendChild(this.baseLbl); 
     
    810818            this.layersDiv.appendChild(this.baseLayersDiv); 
    811819        }     
    812   
     820 
    813821        this.div.appendChild(this.layersDiv); 
    814822 
     
    818826                                        blend: false}); 
    819827 
    820         OpenLayers.Rico.Corner.changeOpacity(this.layersDiv, 0.90); 
     828        OpenLayers.Rico.Corner.changeOpacity(this.layersDiv, 0.95); 
    821829 
    822830        var imgLocation = OpenLayers.Util.getImagesLocation();