OpenLayers OpenLayers

root/sandbox/jvanulden/openlayers/lib/OpenLayers/Control/LayerSwitcher.js

Revision 9340, 33.6 kB (checked in by jvanulden, 11 months ago)

Includes checks to make sure that an error isn't thrown if layer.PARAMS is null.

  • Property svn:eol-style set to native
Line 
1 /* Copyright (c) 2006-2008 MetaCarta, Inc., published under the Clear BSD
2  * license.  See http://svn.openlayers.org/trunk/openlayers/license.txt for the
3  * full text of the license. */
4
5 /**
6  * @requires OpenLayers/Control.js
7  */
8
9 /**
10  * Class: OpenLayers.Control.LayerSwitcher
11  *
12  * Inherits from:
13  *  - <OpenLayers.Control>
14  */
15 OpenLayers.Control.LayerSwitcher =
16   OpenLayers.Class(OpenLayers.Control, {
17    
18     /** 
19      * Property: activeColor
20      * {String}
21      */
22     activeColor: "white",
23    
24     /** 
25      * Property: layerStates
26      * {Array(Object)} Basically a copy of the "state" of the map's layers
27      *     the last time the control was drawn. We have this in order to avoid
28      *     unnecessarily redrawing the control.
29      */
30     layerStates: null,   
31        
32     /**
33      * Property: useLegendGraphics
34      */
35     useLegendGraphics: false,   
36
37   // DOM Elements
38  
39     /**
40      * Property: layersDiv
41      * {DOMElement}
42      */
43     layersDiv: null,
44    
45     /**
46      * Property: baseLayersDiv
47      * {DOMElement}
48      */
49     baseLayersDiv: null,
50
51     /**
52      * Property: baseLayers
53      * {Array(<OpenLayers.Layer>)}
54      */
55     baseLayers: null,   
56    
57     /**
58      * Property: dataLayersDiv
59      * {DOMElement}
60      */
61     dataLayersDiv: null,
62
63     /**
64      * Property: dataLayers
65      * {Array(<OpenLayers.Layer>)}
66      */
67     dataLayers: null,
68
69     /**
70      * Property: activeLayer
71      */
72     activeLayer: null,
73    
74     /**
75      * Property: minimizeDiv
76      * {DOMElement}
77      */
78     minimizeDiv: null,
79
80     /**
81      * Property: maximizeDiv
82      * {DOMElement}
83      */
84     maximizeDiv: null,
85    
86     /**
87      * APIProperty: ascending
88      * {Boolean}
89      */
90     ascending: true,
91  
92     /**
93      * Constructor: OpenLayers.Control.LayerSwitcher
94      *
95      * Parameters:
96      * options - {Object}
97      */
98     initialize: function(options) {
99         OpenLayers.Control.prototype.initialize.apply(this, arguments);
100         this.layerStates = [];     
101     },
102
103     /**
104      * APIMethod: destroy
105      */   
106     destroy: function() {
107        
108         OpenLayers.Event.stopObservingElement(this.div);
109
110         OpenLayers.Event.stopObservingElement(this.minimizeDiv);
111         OpenLayers.Event.stopObservingElement(this.maximizeDiv);
112
113         //clear out layers info and unregister their events
114         this.clearLayersArray("base");
115         this.clearLayersArray("data");
116        
117         this.map.events.un({
118             "addlayer": this.redraw,
119             "changelayer": this.redraw,
120             "removelayer": this.redraw,
121             "changebaselayer": this.redraw,
122             scope: this
123         });
124
125         OpenLayers.Control.prototype.destroy.apply(this, arguments);
126     },
127
128     /**
129      * Method: setMap
130      *
131      * Properties:
132      * map - {<OpenLayers.Map>}
133      */
134     setMap: function(map) {
135         OpenLayers.Control.prototype.setMap.apply(this, arguments);
136        
137         this.map.events.on({
138             "addlayer": this.redraw,
139             "changelayer": this.redraw,
140             "removelayer": this.redraw,
141             "changebaselayer": this.redraw,
142             scope: this
143         });
144     },
145
146     /**
147      * Method: draw
148      *
149      * Returns:
150      * {DOMElement} A reference to the DIV DOMElement containing the
151      *     switcher tabs.
152      */ 
153     draw: function() {
154         OpenLayers.Control.prototype.draw.apply(this);
155
156         // create layout divs
157         this.loadContents();
158
159         // set mode to minimize
160         if(!this.outsideViewport) {
161             this.minimizeControl();
162         }
163
164         // populate div with current info
165         this.redraw();
166
167         return this.div;       
168     },
169
170     /**
171      * Method: clearLayersArray
172      * User specifies either "base" or "data". we then clear all the
173      *     corresponding listeners, the div, and reinitialize a new array.
174      *
175      * Parameters:
176      * layersType - {String} 
177      */
178     clearLayersArray: function(layersType) {
179         var layers = this[layersType + "Layers"];
180         if (layers) {
181             for(var i=0, len=layers.length; i<len ; i++) {
182                 var layer = layers[i];
183                 OpenLayers.Event.stopObservingElement(layer.inputElem);
184                 OpenLayers.Event.stopObservingElement(layer.labelSpan);
185             }
186         }
187         this[layersType + "LayersDiv"].innerHTML = "";
188         this[layersType + "Layers"] = [];
189     },
190
191
192     /**
193      * Method: checkRedraw
194      * Checks if the layer state has changed since the last redraw() call.
195      *
196      * Returns:
197      * {Boolean} The layer state changed since the last redraw() call.
198      */
199     checkRedraw: function() {
200         var redraw = false;
201         if ( !this.layerStates.length ||
202              (this.map.layers.length != this.layerStates.length) ) {
203             redraw = true;
204         } else {
205             for (var i=0, len=this.layerStates.length; i<len; i++) {
206                 var layerState = this.layerStates[i];
207                 var layer = this.map.layers[i];
208                 if ( (layerState.name != layer.name) ||
209                      (layerState.inRange != layer.inRange) ||
210                      (layerState.id != layer.id) ||
211                      (layerState.visibility != layer.visibility) ) {
212                     redraw = true;
213                     break;
214                 }   
215             }
216         }   
217         return redraw;
218     },
219    
220     /**
221      * Method: redraw
222      * Goes through and takes the current state of the Map and rebuilds the
223      *     control to display that state. Groups base layers into a
224      *     radio-button group and lists each data layer with a checkbox.
225      *
226      * Returns:
227      * {DOMElement} A reference to the DIV DOMElement containing the control
228      */ 
229     redraw: function() {
230         // if the state hasn't changed since last redraw, no need
231         // to do anything. Just return the existing div.
232         if (!this.checkRedraw()) {       
233             return this.div;
234         }
235
236         //clear out previous layers
237         this.clearLayersArray("base");
238         this.clearLayersArray("data");
239        
240         var containsOverlays = false;
241         var containsBaseLayers = false;
242        
243         // Save state -- for checking layer if the map state changed.
244         // We save this before redrawing, because in the process of redrawing
245         // we will trigger more visibility changes, and we want to not redraw
246         // and enter an infinite loop.
247         var len = this.map.layers.length;
248         this.layerStates = new Array(len);
249         for (var i=0; i <len; i++) {
250             var layer = this.map.layers[i];           
251             this.layerStates[i] = {
252                 'name': layer.name,
253                 'visibility': layer.visibility,
254                 'inRange': layer.inRange,
255                 'id': layer.id               
256             };
257         }   
258
259         var layers = this.map.layers.slice();
260         if (!this.ascending) { layers.reverse(); }
261         for(var i=0, len=layers.length; i<len; i++) {
262             var layer = layers[i];
263             var baseLayer = layer.isBaseLayer;
264            
265             if (layer.displayInLayerSwitcher) {
266
267                 if (baseLayer) {
268                     containsBaseLayers = true;
269                 } else {
270                     containsOverlays = true;
271                 }               
272
273                 // only check a baselayer if it is *the* baselayer, check data
274                 // layers if they are visible
275                 var checked = (baseLayer) ? (layer == this.map.baseLayer)
276                                           : layer.getVisibility();             
277                
278                 var layerWrapper = document.createElement("div");
279                 layerWrapper.style.margin = "8px 0px 8px 4px";
280                 layerWrapper.id = "layer_" + layer.id;             
281            
282                 // create input element
283                 var inputElem = document.createElement("input");
284                 inputElem.id = this.id + "_input_" + layer.name;
285                 inputElem.name = (baseLayer) ? "baseLayers" : layer.name;
286                 inputElem.type = (baseLayer) ? "radio" : "checkbox";
287                 inputElem.value = layer.name;
288                 inputElem.checked = checked;
289                 inputElem.defaultChecked = checked;
290
291                 if (!baseLayer && !layer.inRange) {
292                     inputElem.disabled = true;
293                 }
294
295                 // create the label span
296                 var labelSpan = document.createElement("span");
297                 if (!baseLayer && !layer.inRange) {
298                     labelSpan.style.color = "gray";
299                 }
300                
301                 if(layer.queryable) {
302                     labelSpan.style.cursor = "pointer";
303                 }
304                 labelSpan.innerHTML = layer.name;
305                 labelSpan.style.display = "block";
306                 labelSpan.style.width = "210px";
307                 labelSpan.style.padding = "0px 6px 2px 4px";
308                 labelSpan.style.verticalAlign = (baseLayer) ? "bottom" : "baseline";     
309                
310                 var abstractSpan = document.createElement("span");
311                 abstractSpan.id = "abstract_" + layer.id;
312                 abstractSpan.innerHTML = layer.description;
313                 abstractSpan.style.display = "none";
314                 abstractSpan.style.fontWeight = "normal";
315                 abstractSpan.style.padding = "0px 6px 2px 5px";
316                 abstractSpan.style.fontSize = "11px";
317                
318                 var abstractToolbarSpan = document.createElement("span");
319                 abstractToolbarSpan.style.display = "block";
320                    
321                 abstractSpan.appendChild(abstractToolbarSpan);
322                
323                 if(layer.dataURL) {
324                     var dataUrlLink = document.createElement("a");
325                     dataUrlLink.style.cursor = "pointer";
326                     dataUrlLink.style.display = "inline-block";
327                     dataUrlLink.alt = "download";
328                     dataUrlLink.title = "download";
329                     dataUrlLink.innerHTML = '<img src="' + OpenLayers.Util.getImagesLocation() + 'download.png" style="border: none;" />';
330                    
331                     var dataUrlLinkContext = {
332                         'url': layer.dataURL
333                     };
334                    
335                     OpenLayers.Event.observe(dataUrlLink, "click",
336                         OpenLayers.Function.bindAsEventListener(this.onDataUrlClick, dataUrlLinkContext)
337                     );
338                 }
339                
340                 if(layer.metadataURL) {
341                     var metadataUrlLink = document.createElement("a");
342                     metadataUrlLink.style.cursor = "pointer";
343                     metadataUrlLink.style.display = "inline-block";
344                     metadataUrlLink.style.margin = "5px 0px 0px 0px";
345                     metadataUrlLink.alt = "metadata";
346                     metadataUrlLink.title = "metadata";
347                     metadataUrlLink.innerHTML = '<img src="' + OpenLayers.Util.getImagesLocation() + 'metadata.png" style="border: none;" />';
348                    
349                     var metadataUrlLinkContext = {
350                         'url': layer.metadataURL
351                     };
352                    
353                     OpenLayers.Event.observe(metadataUrlLink, "click",
354                         OpenLayers.Function.bindAsEventListener(this.onMetadataUrlClick, metadataUrlLinkContext)
355                     );
356                
357                     abstractToolbarSpan.appendChild(metadataUrlLink);
358                 }
359                
360                 // create the title div
361                 var titleDiv = document.createElement("div");
362                 titleDiv.id = "title_" + layer.id;
363                                
364                 if(this.activeLayer == layer.id)
365                 {
366                     titleDiv.style.backgroundColor = "#999";
367                     titleDiv.style.border = "solid 1px #999";
368                 }
369                 else
370                 {
371                     titleDiv.style.backgroundColor = "#e1e1e1";
372                     titleDiv.style.border = "solid 1px #e1e1e1";
373                 }
374                
375                 titleDiv.style.width = "220px";
376                 titleDiv.style.padding = "2px";                     
377                 titleDiv.style.position = "relative";
378                
379                 // create the layer operation panel
380                 var buttonSpan = document.createElement("span");
381                 buttonSpan.style.padding = "3px 3px 3px 0";
382                
383                 // remove control
384                 var removeButton = document.createElement("img");
385                 removeButton.src = OpenLayers.Util.getImagesLocation() + "del.png";
386                 removeButton.style.cursor = "pointer";
387                 removeButton.alt = "remove layer";
388                 removeButton.title = "remove layer";     
389                                
390                 // layer order controls
391                 var upButton = document.createElement("img");
392                 upButton.src = OpenLayers.Util.getImagesLocation() + "up.png";
393                 upButton.style.cursor = "pointer"; 
394                 upButton.alt = "move layer up";
395                 upButton.title = "move layer up";
396                
397                 var downButton = document.createElement("img");
398                 downButton.src = OpenLayers.Util.getImagesLocation() + "down.png";
399                 downButton.style.cursor = "pointer";
400                 downButton.alt = "move layer down";
401                 downButton.title = "move layer down";
402                
403                 // opacity controls
404                 var opacityMinusButton = document.createElement("img");
405                 opacityMinusButton.src = OpenLayers.Util.getImagesLocation() + "minus.png";
406                 opacityMinusButton.style.cursor = "pointer";
407                 opacityMinusButton.alt = "decrease opacity";
408                 opacityMinusButton.title = "decrease opacity";
409                                
410                 // set the default opacity
411                 layer.setOpacity(layer.opacity);
412                
413                 var opacitySpan = document.createElement("span");               
414                 opacitySpan.setAttribute("id", "opacityValue_" + layer.id);               
415                 opacitySpan.style.display = "inline-block";
416                 opacitySpan.style.width = "23px";       
417                
418                 var opacityImg = document.createElement("img");
419                 opacityImg.setAttribute("id", "opacityImg_" + layer.id);
420                 opacityImg.src = OpenLayers.Util.getImagesLocation() + "opacity.png";
421                 opacityImg.width = (layer.opacity != null) ? (layer.opacity * 23).toFixed(0) : "23";
422                 opacityImg.height = "12";
423                 opacityImg.alt = "opacity";
424                 opacityImg.title = "opacity";
425                
426                 var opacityTextInput = document.createElement("input");               
427                 opacityTextInput.setAttribute("id", "opacity_" + layer.id);
428                 opacityTextInput.setAttribute("type", "hidden");
429                 opacityTextInput.setAttribute("value", "1.0");
430                
431                 var opacityPlusButton = document.createElement("img");
432                 opacityPlusButton.src = OpenLayers.Util.getImagesLocation() + "plus.png";
433                 opacityPlusButton.style.cursor = "pointer";
434                 opacityPlusButton.alt = "increase opacity";
435                 opacityPlusButton.title = "increase opacity";
436                
437                 var abstractButton = document.createElement("img");
438                 abstractButton.setAttribute("id", "abstractButton_" + layer.id);
439                 abstractButton.src = OpenLayers.Util.getImagesLocation() + "expand.png";
440                 abstractButton.style.cursor = "pointer";
441                 abstractButton.style.position = "absolute";
442                 abstractButton.style.top = "0";
443                 abstractButton.style.right = "0";
444                 abstractButton.style.padding = "5px";                   
445                
446                 var context = {
447                     'layer': layer,
448                     'inputElem': inputElem,
449                     'titleDiv': titleDiv,
450                     'layerSwitcher': this
451                 };                   
452                                              
453                 OpenLayers.Event.observe(inputElem, "mouseup",
454                     OpenLayers.Function.bindAsEventListener(this.onInputClick, context)
455                 );
456                
457                 if(layer.queryable) {
458                
459                     var queryableButton = document.createElement("img");
460                     queryableButton.src = OpenLayers.Util.getImagesLocation() + "queryable.png";
461                     queryableButton.style.cursor = "pointer";
462                     queryableButton.alt = "select for query";
463                     queryableButton.title = "select for query";
464                    
465                     OpenLayers.Event.observe(labelSpan, "click",
466                         OpenLayers.Function.bindAsEventListener(this.onTitleClick, context)
467                     );
468                    
469                     OpenLayers.Event.observe(queryableButton, "click",
470                         OpenLayers.Function.bindAsEventListener(this.onTitleClick, context)
471                     );
472                 }
473                
474                 OpenLayers.Event.observe(upButton, "click",
475                     OpenLayers.Function.bindAsEventListener(this.onUpClick, context)
476                 );
477                
478                 OpenLayers.Event.observe(downButton, "click",
479                     OpenLayers.Function.bindAsEventListener(this.onDownClick, context)
480                 );
481                
482                 OpenLayers.Event.observe(removeButton, "click",
483                     OpenLayers.Function.bindAsEventListener(this.onRemoveClick, context)
484                 );
485                
486                 var opacityMinusContext = {
487                     'layer': layer,
488                     'byOpacity': '-0.1',
489                     'layerSwitcher': this
490                 };
491                 OpenLayers.Event.observe(opacityMinusButton, "click",
492                     OpenLayers.Function.bindAsEventListener(this.changeLayerOpacity, opacityMinusContext)
493                 );
494                
495                 var opacityPlusContext = {
496                     'layer': layer,
497                     'byOpacity': '0.1',
498                     'layerSwitcher': this
499                 };
500                 OpenLayers.Event.observe(opacityPlusButton, "click",
501                     OpenLayers.Function.bindAsEventListener(this.changeLayerOpacity, opacityPlusContext)
502                 );
503                
504                 var abstractContext = {
505                     'layer': layer,
506                     'button' : abstractButton
507                 };
508                 OpenLayers.Event.observe(abstractButton, "mouseup",
509                     OpenLayers.Function.bindAsEventListener(this.toggleAbstract, abstractContext)
510                 );               
511                
512                 // create line break
513                 var br = document.createElement("br");
514                
515                 var groupArray = (baseLayer) ? this.baseLayers
516                                              : this.dataLayers;
517                 groupArray.push({
518                     'layer': layer,
519                     'inputElem': inputElem,
520                     'titleDiv': titleDiv,
521                     'labelSpan': labelSpan
522                 });
523                                                      
524                 var groupDiv = (baseLayer) ? this.baseLayersDiv
525                                            : this.dataLayersDiv;
526                                            
527                 groupDiv.appendChild(layerWrapper);
528                 layerWrapper.appendChild(titleDiv); 
529                 titleDiv.appendChild(inputElem);
530                 titleDiv.appendChild(buttonSpan);               
531                 buttonSpan.appendChild(upButton);
532                 buttonSpan.appendChild(downButton);
533                 buttonSpan.appendChild(removeButton);
534                 buttonSpan.appendChild(opacityMinusButton);
535                 opacitySpan.appendChild(opacityImg);
536                 buttonSpan.appendChild(opacitySpan);
537                 buttonSpan.appendChild(opacityTextInput);
538                 buttonSpan.appendChild(opacityPlusButton);
539                
540                 if(layer.description) {
541                     titleDiv.appendChild(abstractButton);
542                 }
543                 if(layer.queryable) {
544                     buttonSpan.appendChild(queryableButton);
545                 }
546                 if(layer.dataURL) {
547                     buttonSpan.appendChild(dataUrlLink);
548                 }
549                
550                 titleDiv.appendChild(labelSpan);
551                 titleDiv.appendChild(abstractSpan);
552                
553                 if(this.useLegendGraphics && layer.params) {
554                     var legendGraphicURL = layer.getFullRequestString({
555                     REQUEST: "GetLegendGraphic",
556                     LAYER: layer.params.LAYERS,
557                     FORMAT: "image/png",
558                     WIDTH: "150"});
559
560                     var imgSpan = document.createElement('span');
561                     imgSpan.innerHTML = "<img style=\"display:none\" src=\"" + legendGraphicURL + "\" onload=\"this.style.display = 'inline'\" alt=\"\" onerror=\"this.src='" + OpenLayers.Util.getImagesLocation() + "blank.gif" + "'\" />";
562                     layerWrapper.appendChild(imgSpan);                         
563                 }                         
564             }   
565         }
566        
567         return this.div;       
568            
569     },   
570    
571     /**
572      * Method:
573      * A label has been clicked, check or uncheck its corresponding input
574      *
575      * Parameters:
576      * e - {Event}
577      *
578      * Context: 
579      *  - {DOMElement} inputElem
580      *  - {<OpenLayers.Control.LayerSwitcher>} layerSwitcher
581      *  - {<OpenLayers.Layer>} layer
582      */
583
584     onInputClick: function(e) {
585         if (!this.inputElem.disabled) {
586             if (this.inputElem.type == "radio") {
587                 this.inputElem.checked = true;
588                 this.layer.map.setBaseLayer(this.layer);
589             } else {
590                 this.inputElem.checked = !this.inputElem.checked;
591                 this.layerSwitcher.updateMap();
592             }
593         }
594        
595         if (e != null) {
596             OpenLayers.Event.stop(e);                                           
597         }
598     },   
599
600     /**
601      * Method: onRemoveClick
602      * Remove the layer from the map
603      *
604      * Parameters:
605      * e - {Event}
606      */
607     onRemoveClick: function(e)
608     {
609         map.removeLayer(this.layer);
610        
611         if (e != null) {
612             OpenLayers.Event.stop(e);                                           
613         }
614     },
615    
616     /**
617      * Method: onDownClick
618      * Set the layer position down one level
619      *
620      * Parameters:
621      * e - {Event}
622      */
623     onDownClick: function(e)
624     {
625         map.raiseLayer(this.layer, -1); 
626        
627         if (e != null) {
628             OpenLayers.Event.stop(e);                                           
629         }
630     },
631    
632     /**
633      * Method: onUpClick
634      * Set the layer position up one level
635      *
636      * Parameters:
637      * e - {Event}
638      */
639     onUpClick: function(e)
640     {
641         map.raiseLayer(this.layer, 1); 
642        
643         if (e != null) {
644             OpenLayers.Event.stop(e);                                           
645         } 
646     },
647    
648      /**
649      * Method: onTitleClick
650      * Set the active layer
651      *
652      * Parameters:
653      * e - {Event}
654      */
655     onTitleClick: function(e)
656     {
657         var id = this.layer.id;
658        
659         layerSwitcher.activeLayer = id;
660        
661         for (var i=0; i<map.layers.length;i++) {
662           var layer = map.layers[i];
663          
664           if(id == layer.id) {
665             this.titleDiv.style.backgroundColor = "#999";
666             this.titleDiv.style.border = "solid 1px #999";
667           }
668           else {           
669             var div = OpenLayers.Util.getElement("title_" + layer.id);
670            
671             if(div) {
672                 div.style.backgroundColor = "#e1e1e1";
673                 div.style.border = "solid 1px #e1e1e1";
674             }
675           }           
676         }       
677        
678         if (e != null) {
679             OpenLayers.Event.stop(e);                                           
680         }
681     },
682    
683     toggleAbstract: function(e)
684     {       
685         var span = OpenLayers.Util.getElement("abstract_" + this.layer.id);
686         var button = this.button;
687            
688         if(span && button) {
689             var display = span.style.display;
690            
691             if(display == "block") {
692                 span.style.display = "none";
693                 button.src = OpenLayers.Util.getImagesLocation() + "expand.png";
694             }
695             else {
696                 span.style.display = "block";
697                 button.src = OpenLayers.Util.getImagesLocation() + "collapse.png";
698             }
699         }
700        
701     },
702    
703     /**
704      * Method: onDataUrlClick
705      * Open new window and redirect to URL.
706      *
707      * Parameters:
708      * e - {Event}
709      *
710      * Context: 
711      *  - {string} url to redirect to
712      */
713     onDataUrlClick: function(e)
714     {   
715         window.open(this.url , "data", "width=550,height=350,status=yes,scrollbars=yes,resizable=yes");
716     },
717    
718     /**
719      * Method: onMetadataUrlClick
720      * Open new window and redirect to URL.
721      *
722      * Parameters:
723      * e - {Event}
724      *
725      * Context: 
726      *  - {string} url to redirect to
727      */
728     onMetadataUrlClick: function(e)
729     {   
730         window.open(this.url , "metadata", "width=550,height=350,status=yes,scrollbars=yes,resizable=yes");
731     },
732    
733     /**
734      * Method: onLayerClick
735      * Need to update the map accordingly whenever user clicks in either of
736      *     the layers.
737      *
738      * Parameters:
739      * e - {Event}
740      */
741     onLayerClick: function(e) {
742         this.updateMap();
743     },
744
745     /**
746      * Method: changeLayerOpacity
747      * Changes opacity of a given layer for a given delta
748      *
749      * Parameters:
750      * e - {Event}
751      *
752      * Context: 
753      *  - {string} amount to increase or decrease opacity value
754      *  - {<OpenLayers.Layer>} layer
755      *  - {<OpenLayers.Control.LayerSwitcher>} layerSwitcher
756      */
757     changeLayerOpacity: function(e)
758     {   
759         var maxOpacity = 1.0;
760         var minOpacity = 0.1;
761         var opacity = (this.layer.opacity != null) ? this.layer.opacity : 1.0;
762         var i = parseFloat(this.byOpacity);
763         var opacityElement = "opacity_" + this.layer.id; 
764         var opacityImg = "opacityImg_" + this.layer.id;
765         var newOpacity = (parseFloat(opacity + i)).toFixed(1);
766        
767         newOpacity = Math.min(maxOpacity, Math.max(minOpacity, newOpacity));
768        
769         OpenLayers.Util.getElement(opacityElement).value = newOpacity;       
770         OpenLayers.Util.getElement(opacityImg).width = (newOpacity * 23).toFixed(0);
771        
772         this.layer.setOpacity(newOpacity);
773     },
774
775     /**
776      * Method: updateMap
777      * Cycles through the loaded data and base layer input arrays and makes
778      *     the necessary calls to the Map object such that that the map's
779      *     visual state corresponds to what the user has selected in
780      *     the control.
781      */
782     updateMap: function()
783     {
784         // set the newly selected base layer       
785         for(var i=0, len=this.baseLayers.length; i<len; i++) {
786             var layerEntry = this.baseLayers[i];
787             if (layerEntry.inputElem.checked) {
788                 this.map.setBaseLayer(layerEntry.layer, false);
789             }
790         }
791
792         // set the correct visibilities for the overlays
793         for(var i=0, len=this.dataLayers.length; i<len; i++) {
794             var layerEntry = this.dataLayers[i];   
795             layerEntry.layer.setVisibility(layerEntry.inputElem.checked);
796         }
797     },
798
799     /**
800      * Method: maximizeControl
801      * Set up the labels and divs for the control
802      *
803      * Parameters:
804      * e - {Event}
805      */
806     maximizeControl: function(e)
807     {
808         this.div.style.width = "20em";
809         this.div.style.height = "100%";
810         this.div.style.borderLeft = "solid 1px #999";
811
812         this.showControls(false);
813
814         if (e != null) {
815             OpenLayers.Event.stop(e);                                           
816         }
817     },
818    
819     /**
820      * Method: minimizeControl
821      * Hide all the contents of the control, shrink the size,
822      *     add the maximize icon
823      *
824      * Parameters:
825      * e - {Event}
826      */
827     minimizeControl: function(e)
828     {
829         this.div.style.width = "0px";
830         this.div.style.height = "100%";
831         this.div.style.borderLeft = "none";
832
833         this.showControls(true);
834
835         if (e != null) {
836             OpenLayers.Event.stop(e);                                           
837         }
838     },
839
840     /**
841      * Method: showControls
842      * Hide/Show all LayerSwitcher controls depending on whether we are
843      *     minimized or not
844      *
845      * Parameters:
846      * minimize - {Boolean}
847      */
848     showControls: function(minimize) {
849
850         this.maximizeDiv.style.display = minimize ? "" : "none";
851         this.minimizeDiv.style.display = minimize ? "none" : "";
852
853         this.layersDiv.style.display = minimize ? "none" : "";
854     },
855    
856     /**
857      * Method: loadContents
858      * Set up the labels and divs for the control
859      */
860     loadContents: function() {
861
862         //configure main div
863         this.div.style.position = "absolute";
864         this.div.style.top = "0px";
865         this.div.style.right = "0px";
866         this.div.style.left = "";
867         this.div.style.fontFamily = "sans-serif";
868         this.div.style.fontWeight = "bold";
869         this.div.style.fontSize = "13px";   
870         this.div.style.color = "#333";   
871         this.div.style.backgroundColor = this.activeColor;
872         this.div.style.height = "100%";
873    
874         OpenLayers.Event.observe(this.div, "mouseup",
875             OpenLayers.Function.bindAsEventListener(this.mouseUp, this));
876         OpenLayers.Event.observe(this.div, "click",
877                       this.ignoreEvent);
878         OpenLayers.Event.observe(this.div, "mousedown",
879             OpenLayers.Function.bindAsEventListener(this.mouseDown, this));
880         OpenLayers.Event.observe(this.div, "dblclick", this.ignoreEvent);
881
882         // layers list div       
883         this.layersDiv = document.createElement("div");
884         this.layersDiv.setAttribute("className", "olLayerSwitcherLayerContainer");
885         this.layersDiv.id = this.id + "_layersDiv";       
886         this.layersDiv.style.overflowX = "hidden";
887         this.layersDiv.style.overflowY = "scroll";
888         this.layersDiv.style.position = "relative";
889         this.layersDiv.style.height = "100%";       
890        
891         // ignore any mousewheel events
892         OpenLayers.Event.observe(this.layersDiv, "mousewheel", this.ignoreEvent);
893
894         // had to set width/height to get transparency in IE to work.
895         // thanks -- http://jszen.blogspot.com/2005/04/ie6-opacity-filter-caveat.html
896        
897         this.baseLayersDiv = document.createElement("div");
898         this.baseLayersDiv.style.display = "none";
899         this.dataLayersDiv = document.createElement("div");
900         this.dataLayersDiv.style.paddingLeft = "5px";
901        
902         if (this.ascending) {
903             this.layersDiv.appendChild(this.baseLayersDiv);
904             this.layersDiv.appendChild(this.dataLayersDiv);
905         } else {
906             this.layersDiv.appendChild(this.dataLayersDiv);
907             this.layersDiv.appendChild(this.baseLayersDiv);
908         }   
909        
910         this.div.appendChild(this.layersDiv);
911         //OpenLayers.Rico.Corner.changeOpacity(this.layersDiv, 0.95);       
912        
913         var imgLocation = OpenLayers.Util.getImagesLocation();
914         var sz = new OpenLayers.Size(20,60);       
915
916         // maximize button div
917         var img = imgLocation + 'layer-switcher-maximize.png';
918         this.maximizeDiv = OpenLayers.Util.createAlphaImageDiv(
919                                     "OpenLayers_Control_MaximizeDiv",
920                                     null,
921                                     sz,
922                                     img,
923                                     "absolute");
924         this.maximizeDiv.style.top = "50%";
925         this.maximizeDiv.style.marginTop = "-30px";
926         this.maximizeDiv.style.right = "0px";
927         this.maximizeDiv.style.left = "";
928         this.maximizeDiv.style.display = "none";
929         OpenLayers.Event.observe(this.maximizeDiv, "click",
930             OpenLayers.Function.bindAsEventListener(this.maximizeControl, this)
931         );
932        
933         this.div.appendChild(this.maximizeDiv);
934         //OpenLayers.Rico.Corner.changeOpacity(this.maximizeDiv, 0.95);
935
936         // minimize button div
937         var img = imgLocation + 'layer-switcher-minimize.png';
938         var sz = new OpenLayers.Size(20,60);       
939         this.minimizeDiv = OpenLayers.Util.createAlphaImageDiv(
940                                     "OpenLayers_Control_MinimizeDiv",
941                                     null,
942                                     sz,
943                                     img,
944                                     "absolute");
945         this.minimizeDiv.style.top = "50%";
946         this.minimizeDiv.style.marginTop = "-30px";
947         this.minimizeDiv.style.right = "258px";
948         this.minimizeDiv.style.left = "";
949         OpenLayers.Event.observe(this.minimizeDiv, "click",
950             OpenLayers.Function.bindAsEventListener(this.minimizeControl, this)
951         );
952
953         this.div.appendChild(this.minimizeDiv);
954         //OpenLayers.Rico.Corner.changeOpacity(this.minimizeDiv, 0.95);
955        
956        
957     },
958    
959     /**
960      * Method: ignoreEvent
961      *
962      * Parameters:
963      * evt - {Event}
964      */
965     ignoreEvent: function(evt) {
966         OpenLayers.Event.stop(evt);
967     },
968
969     /**
970      * Method: mouseDown
971      * Register a local 'mouseDown' flag so that we'll know whether or not
972      *     to ignore a mouseUp event
973      *
974      * Parameters:
975      * evt - {Event}
976      */
977     mouseDown: function(evt) {
978         this.isMouseDown = true;
979         this.ignoreEvent(evt);
980     },
981
982     /**
983      * Method: mouseUp
984      * If the 'isMouseDown' flag has been set, that means that the drag was
985      *     started from within the LayerSwitcher control, and thus we can
986      *     ignore the mouseup. Otherwise, let the Event continue.
987      * 
988      * Parameters:
989      * evt - {Event}
990      */
991     mouseUp: function(evt) {
992         if (this.isMouseDown) {
993             this.isMouseDown = false;
994             this.ignoreEvent(evt);
995         }
996     },
997    
998     CLASS_NAME: "OpenLayers.Control.LayerSwitcher"
999 });
Note: See TracBrowser for help on using the browser.