Source: BKGWebMap/Control/LayerSwitcher/Component.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/Control/LayerSwitcher.js
 * @requires BKGWebMap/Util.js
 */

/**
 * @classdesc Basisklasse für zusätzliche Kompnenten eines LayerEntry.
 *
 * @constructor BKGWebMap.Control.LayerSwitcher.Component
 */
BKGWebMap.Control.LayerSwitcher.Component = OpenLayers.Class({

	/**
	 * Der Layer dem die Komponente zugeorndet ist
	 * @memberOf BKGWebMap.Control.LayerSwitcher.LayerEntry
	 * @type OpenLayers.Layer
	 */
	layer: null,

	/**
	 * Der LayerEntry dem die Komponente zugeorndet ist
	 * @memberOf BKGWebMap.Control.LayerSwitcher.LayerEntry
	 * @type BKGWebMap.Control.LayerSwitcher
	 */
	parent: null,

    initialize: function(options) {
        OpenLayers.Util.extend(this, options);
    },

    destroy: function() {
        this.parent = null;
        this.layer = null;
    },

    /**
     * Erzeugt die HTML-Darstellung für einen Layer-Eintrag 
     * @param {BKGWebMap.Control.LayerSwitcher.LayerEntry} parent - Der LayerEntry
     * @memberOf BKGWebMap.Control.LayerSwitcher.Component
     */
    draw: function(parent){
        this.parent = parent;
        this.layer = parent.layer;
    },

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

/**
 * @classdesc Basisklasse für zusätzliche Kompnenten eines LayerEntry.
 *
 * @constructor BKGWebMap.Control.LayerSwitcher.BuzzyIndicator
 */
BKGWebMap.Control.LayerSwitcher.BuzzyIndicator = OpenLayers.Class(BKGWebMap.Control.LayerSwitcher.Component, {

    styleLoading: 'loading',

    initialize: function(options) {
        BKGWebMap.Control.LayerSwitcher.Component.prototype.initialize.apply(this, [options]);
    },

    destroy: function() {
        layer.events.unregister("loadstart", this, this.onLoadStart);
        layer.events.unregister("loadend", this, this.onLoadEnd);
        BKGWebMap.Control.LayerSwitcher.Component.prototype.destroy.apply(this);
    },

    /**
     * Wird verwendet, um die loading-Events des Layers zu registrieren.
     * @param {BKGWebMap.Control.LayerSwitcher.LayerEntry} parent - Der LayerEntry
     * @memberOf BKGWebMap.Control.LayerSwitcher.BuzzyIndicator
     */
    draw: function(parent){
        BKGWebMap.Control.LayerSwitcher.Component.prototype.draw.apply(this, [parent]);
        this.registerEvents(this.layer);
    },

    /**
     * Wird verwendet, um die loading-Events des Layers zu registrieren.
     * @param {OpenLayers.Layer} layer - Der Layer bei dem die Loading Events registriert werden.
     * @memberOf BKGWebMap.Control.LayerSwitcher.BuzzyIndicator
     */
    registerEvents: function(layer) {
        if(layer.visibility && layer.loading)
            this.onLoadStart();

        layer.events.register("loadstart", this, this.onLoadStart);
        layer.events.register("loadend", this, this.onLoadEnd);
        layer.events.register("visibilitychanged", this, this.onVisibilityChanged);
    },

    onLoadStart: function() {
        if(!OpenLayers.Element.hasClass(this.parent.div, this.styleLoading)) {
            OpenLayers.Element.addClass(this.parent.div, this.styleLoading);
        }
    },

    onLoadEnd: function() {
        if(OpenLayers.Element.hasClass(this.parent.div, this.styleLoading)) {
            OpenLayers.Element.removeClass(this.parent.div, this.styleLoading);
        }
    },

    onVisibilityChanged: function() {
        if(!this.layer.visibility) this.onLoadEnd();
    },

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