Source: BKGWebMap/Control/LayerSwitcher/LayerEntry.js

/*
 * Copyright (c) 2013 Bundesamt für Kartographie und Geodäsie.
 * See license.txt in the BKG WebMap distribution or repository for the
 * full text of the license.
 *
 * Author: Dirk Thalheim
 */

/**
 * @requires OpenLayers/BaseTypes/Class.js
 * @requires OpenLayers/BaseTypes/Element.js
 * @requires OpenLayers/Util.js
 * @requires BKGWebMap/Util.js
 * @requires BKGWebMap/Control/LayerSwitcher.js
 * @requires BKGWebMap/Control/LayerSwitcher/Component.js
 */

/**
 * @classdesc Repräsemtationsklasse für einen simplen Layereintrag im Layerswitcher.
 *
 * @constructor BKGWebMap.Control.LayerSwitcher.LayerEntry
 */
BKGWebMap.Control.LayerSwitcher.LayerEntry = OpenLayers.Class({

	/**
	 * Der Layer des LayerEntries
	 * @memberOf BKGWebMap.Control.LayerSwitcher.LayerEntry
	 * @type OpenLayers.Layer
	 */
	layer: null,
	
	/**
	 * Das HTML-Element für die Repräsentation
	 * @memberOf BKGWebMap.Control.LayerSwitcher.LayerEntry
	 * @type HTMLElement
	 */
	div: null,

	/**
	 * Control-Element, indem der LayerEntry eingebunden ist.
	 * @memberOf BKGWebMap.Control.LayerSwitcher.LayerEntry
	 * @type OpenLayers.Control
	 */
	parent: null,
	
	/**
	 * CSS-Klasse für das HTML-Element der Repräsentation
	 * @memberOf BKGWebMap.Control.LayerSwitcher.LayerEntry
	 * @type string
	 */
	style: 'layerEntry',
	
	/**
	 * CSS-Klasse für selektierten Layereintrag
	 * @memberOf BKGWebMap.Control.LayerSwitcher.LayerEntry
	 * @type string
	 */
	styleSelected: 'selected',

	/**
	 * CSS-Klasse für inaktiven Layereintrag
	 * @memberOf BKGWebMap.Control.LayerSwitcher.LayerEntry
	 * @type string
	 */
	styleInactive: 'inactive',

    /**
     * Zusätzliche Komponenten, die für den Layer gerendert werden sollen.
     * @memberOf BKGWebMap.Control.LayerSwitcher.LayerEntry
     * @type Array<BKGWebMap.Control.LayerSwitcher.Component>
     */
    components: null,

    initialize: function(layer, parent, options) {
        OpenLayers.Util.extend(this, options);
        if(this.components==null)
            this.components = [new BKGWebMap.Control.LayerSwitcher.BuzzyIndicator];
        this.layer = layer;
        this.parent = parent;
    },

    destroy: function() {
        // TODO: ...
    },
    
    /** 
     * Erzeugt die HTML-Darstellung für einen Layer-Eintrag 
     * @param {OpenLayers.Layer} layer - Der aktuelle Layer
     * @memberOf BKGWebMap.Control.LayerSwitcher
     */
    draw: function(){
        if (!this.layer.displayInLayerSwitcher) return null;

        var baseLayer = this.layer.isBaseLayer;
        var checked = (baseLayer) ? (this.layer == this.parent.map.baseLayer) : this.layer.getVisibility();
        
        // create input element
        this.div = document.createElement("div");
    	OpenLayers.Element.addClass(this.div, this.style);        	
        this.div.innerHTML = this.layer.name;

        if (checked) {
        	OpenLayers.Element.addClass(this.div, this.styleSelected);
            // TODO: status an Layertree weiterreichen, damit Gruppentitel aktiviert wird
        }
        if (!baseLayer && !this.layer.inRange) {
        	OpenLayers.Element.addClass(this.div, this.styleInactive);
        }

        // register events
        OpenLayers.Event.observe(this.div, "click", OpenLayers.Function.bindAsEventListener(this.toggleLayer, this));

        // render additional components
        for(var i=0;i<this.components.length;i++) {
            this.components[i].draw(this);
        }

        return this.div;
    },
    
    /**
     * Wechselt den Layerstatus
     * @param {Event} event
     * @memberOf BKGWebMap.Control.LayerSwitcher.LayerEntry
     */
    toggleLayer: function(event) {
        var disabled = OpenLayers.Element.hasClass(this.div, this.styleInactive);
    	if(disabled) return;

        if(this.layer.isBaseLayer) {
            this.parent.map.setBaseLayer(this.layer);
        } else {
            this.layer.setVisibility(!this.layer.visibility);
        }
    },    
    

    CLASS_NAME: "BKGWebMap.Control.LayerSwitcher.LayerEntry"
});