1 /* Copyright (c) 2006 MetaCarta, Inc., published under the BSD license.
 
   2  * See http://svn.openlayers.org/trunk/openlayers/license.txt for the full
 
   3  * text of the license. */
 
   4 // @require: OpenLayers/Control.js
 
   8 OpenLayers.Control.LayerSwitcher = Class.create();
 
  10 /** color used in the UI to show a layer is active/displayed
 
  15 OpenLayers.Control.LayerSwitcher.ACTIVE_COLOR = "darkblue";
 
  17 /** color used in the UI to show a layer is deactivated/hidden
 
  22 OpenLayers.Control.LayerSwitcher.NONACTIVE_COLOR = "lightblue";
 
  25 OpenLayers.Control.LayerSwitcher.prototype = 
 
  26   Object.extend( new OpenLayers.Control(), {
 
  40     initialize: function(options) {
 
  41         this.activeColor = OpenLayers.Control.LayerSwitcher.ACTIVE_COLOR;
 
  42         this.nonActiveColor = OpenLayers.Control.LayerSwitcher.NONACTIVE_COLOR;
 
  44         OpenLayers.Control.prototype.initialize.apply(this, arguments);
 
  48     * @returns A reference to the DIV DOMElement containing the switcher tabs
 
  52         // initialize our internal div
 
  53         OpenLayers.Control.prototype.draw.apply(this);
 
  55         this.div.style.position = "absolute";
 
  56         this.div.style.top = "10px";
 
  57         this.div.style.right = "0px";
 
  58         this.div.style.left = "";
 
  59         this.div.style.fontFamily = "sans-serif";
 
  60         this.div.style.color = "white";
 
  61         this.div.style.fontWeight = "bold";
 
  62         this.div.style.marginTop = "3px";
 
  63         this.div.style.marginLeft = "3px";
 
  64         this.div.style.marginBottom = "3px";
 
  65         this.div.style.fontSize="smaller";   
 
  66         this.div.style.width = "10em";
 
  68         this.map.events.register("addlayer", this, this.redraw);
 
  69         this.map.events.register("removelayer", this, this.redraw);
 
  74     * @returns A reference to the DIV DOMElement containing the switcher tabs
 
  79         //clear out previous incarnation of LayerSwitcher tabs
 
  80         this.div.innerHTML = "";
 
  82         for( var i = 0; i < this.map.layers.length; i++) {
 
  83             if (visible && this.mode == "radio") {
 
  84                 this.map.layers[i].setVisibility(false);
 
  86                 visible = this.map.layers[i].getVisibility();
 
  88             this.addTab(this.map.layers[i]);
 
  97     singleClick: function(evt) {
 
  98         var div = Event.element(evt);
 
 100         // See comment about OL #57 fix below.
 
 101         // If the click occurred on the corner spans we need
 
 102         // to make sure we act on the actual label tab instead.
 
 103         div = div.labelElement || div;
 
 105         var layer = div.layer;
 
 106         if (this.mode == "radio") {
 
 107             for(var i=0; i < this.backdrops.length; i++) {
 
 108                 this.setTabActivation(this.backdrops[i], false);
 
 109                 this.backdrops[i].layer.setVisibility(false);
 
 111             this.setTabActivation(div, true);
 
 112             layer.setVisibility(true);
 
 114             var visible = layer.getVisibility();
 
 116             this.setTabActivation(div, !visible);
 
 117             layer.setVisibility(!visible);
 
 127     ignoreEvent: function(evt) {
 
 135     * @param {OpenLayers.Layer} layer
 
 137     addTab: function(layer) {
 
 139         // Outer DIV - for Rico Corners
 
 141         var backdropLabelOuter = document.createElement('div');
 
 142         backdropLabelOuter.id = "LayerSwitcher_" + layer.name + "_Tab";
 
 143         backdropLabelOuter.style.marginTop = "4px";
 
 144         backdropLabelOuter.style.marginBottom = "4px";
 
 146         this._setEventHandlers(backdropLabelOuter);
 
 148         // Inner Label - for Rico Corners
 
 150         var backdropLabel = document.createElement('p');
 
 151         backdropLabel.innerHTML = layer.name;
 
 152         backdropLabel.style.marginTop = "0px";
 
 153         backdropLabel.style.marginBottom = "0px";
 
 154         backdropLabel.style.paddingLeft = "10px";
 
 155         backdropLabel.style.paddingRight = "10px";
 
 157         // add reference to layer onto the div for use in event handlers
 
 158         backdropLabel.layer = layer;
 
 160         // set event handlers
 
 161         this._setEventHandlers(backdropLabel);
 
 164         backdropLabelOuter.appendChild(backdropLabel);
 
 166         this.backdrops.append(backdropLabel); 
 
 168         // add div to main LayerSwitcher Div
 
 169         this.div.appendChild(backdropLabelOuter);
 
 171         Rico.Corner.round(backdropLabelOuter, {corners: "tl bl",
 
 172                                       bgColor: "transparent",
 
 176         // extend the event handlers to operate on the
 
 177         // rounded corners as well. (Fixes OL #57.)
 
 178         var spanElements=backdropLabel.parentNode.getElementsByTagName("span");
 
 180         for (var currIdx = 0; currIdx < spanElements.length; currIdx++) {
 
 181             this._setEventHandlers(spanElements[currIdx], backdropLabel);
 
 184         this.setTabActivation(backdropLabel, layer.getVisibility());
 
 190       @param {DOMElement} div
 
 191       @param {Boolean} activate
 
 193     _setEventHandlers : function(element, labelDiv) {
 
 195         // We only want to respond to a mousedown event.
 
 196         element.onclick = this.singleClick.bindAsEventListener(this);
 
 197         element.ondblclick = this.singleClick.bindAsEventListener(this);
 
 198         element.onmouseup = this.ignoreEvent.bindAsEventListener(this);
 
 199         element.onmousedown = this.ignoreEvent.bindAsEventListener(this);
 
 201         // If we are operating on a corner span we need to store a
 
 202         // reference to the actual tab. (See comment about OL #57 fix above.)
 
 204             element.labelElement = labelDiv;
 
 211     * @param {DOMElement} div
 
 212     * @param {Boolean} activate
 
 214     setTabActivation:function(div, activate) {
 
 215         var color = (activate) ? this.activeColor : this.nonActiveColor;
 
 216         Rico.Corner.changeColor(div, color);
 
 221     /** @final @type String */
 
 222     CLASS_NAME: "OpenLayers.Control.LayerSwitcher"