<commit_before>/* 
 * Copyright (C) 2012 Expression organization is undefined on line 4, column 61 in Templates/Licenses/license-gpl30.txt.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


/**
 * Legend
 * Creates the legend of the current switched on layers.
 * This legend uses a queue to load the images.
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */
Ext.define ("viewer.components.Legend",{
    extend: "viewer.components.Component",
    queue : null,
    legends : null,    
    initLegends: null,
    config:{
        // name: "Legend",
        title: "Legend",
        titlebarIcon : "",
        tooltip : "",
        showBackground: false
    },
    constructor: function (conf){
        viewer.components.Legend.superclass.constructor.call(this, conf);
        this.initConfig(conf);
        this.legends={};
        var title = "";
        if(this.config.title && !this.viewerController.layoutManager.isTabComponent(this.name)) title = this.config.title;
        this.panel = Ext.create('Ext.panel.Panel', {
            renderTo: this.getContentDiv(),
            title: title,
            height: "100%",
            html: '<div id="' + this.name + 'legendContainer" style="width: 100%; height: 100%; padding: 10px; overflow: auto;"></div>'
        });
        
        this.legendContainer = document.getElementById(this.name + 'legendContainer');
        this.legendContainer.style.overflow = "auto";
        
        this.viewerController.mapComponent.getMap().registerEvent(viewer.viewercontroller.controller.Event.ON_LAYER_VISIBILITY_CHANGED,this.layerVisibilityChanged,this);
        
        this.viewerController.mapComponent.getMap().registerEvent(viewer.viewercontroller.controller.Event.ON_LAYER_REMOVED,this.layerRemoved,this);
        this.start();
        return this;
    },
    // Construct the list of images to be retrieved by the queue
    makeLegendList : function (){
        this.initLegends = new Array();        
    },
    // Handler for changes to the visibility of layers
    layerVisibilityChanged : function (map,object ){
        var layer = object.layer;
        var vis = object.visible;
        if(vis){
            this.addLayer(layer);
        }else{
           this.removeLayer(layer.id);
        }
    },
    /**
     * Called when a layer is added
     * @param layer a viewer.viewercontroller.controller.Layer
     */
    addLayer : function (layer){
        var serviceId = layer.serviceId;
        var layerName = layer.getAppLayerName();// TODO: not yet correct  
        var appLayer=this.viewerController.getAppLayerById(layer.appLayerId);
        
        /*if already added return; Still use serviceId_layerName because only one of the layer
        graphics must be added*/
        if(this.legends[serviceId  + "_"+ layerName]){
            return;
        }        
        //show backgrounds == false then don't show backgrounds.
        if (!this.getShowBackground()){
            //var appLayer=this.viewerController.getAppLayer(serviceId,layerName);
                                    
            if (appLayer.background){
                return;
            }
        }
        var url = this.viewerController.getLayerLegendImage(appLayer);
        if (url!=null){
            var legend = {
                src: url,
                title: layerName,
                id: serviceId  + "_"+ layerName
            };
            this.legends[serviceId  + "_"+ layerName]=true;
            this.queue.addItem(legend);
        }
    },
    layerRemoved : function(map, object){
        var layer = object.layer;
        if(layer != null){
            this.removeLayer(layer.getId());
        }
    },
    removeLayer: function (layerName){
        var id = layerName+"-div";
        var node =document.getElementById(id);
        if (node!=null){
            this.legendContainer.removeChild(node);
        }
        if (this.legends[layerName]){
            delete this.legends[layerName];
        }
    },
    // Start the legend: make a list of images to be retrieved, make a queue and start it
    start : function (){
        this.makeLegendList();
        var config ={
            legends: this.initLegends, 
            queueSize: 2,
            div: this.legendContainer
        };<commit_msg>Fix for legend component. Legends are removed when layer is not visible.<commit_after>/* 
 * Copyright (C) 2012 Expression organization is undefined on line 4, column 61 in Templates/Licenses/license-gpl30.txt.
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */


/**
 * Legend
 * Creates the legend of the current switched on layers.
 * This legend uses a queue to load the images.
 * @author <a href="mailto:<EMAIL>"><NAME></a>
 */
Ext.define ("viewer.components.Legend",{
    extend: "viewer.components.Component",
    queue : null,
    legends : null,    
    initLegends: null,
    config:{
        // name: "Legend",
        title: "Legend",
        titlebarIcon : "",
        tooltip : "",
        showBackground: false
    },
    constructor: function (conf){
        viewer.components.Legend.superclass.constructor.call(this, conf);
        this.initConfig(conf);
        this.legends={};
        var title = "";
        if(this.config.title && !this.viewerController.layoutManager.isTabComponent(this.name)) title = this.config.title;
        this.panel = Ext.create('Ext.panel.Panel', {
            renderTo: this.getContentDiv(),
            title: title,
            height: "100%",
            html: '<div id="' + this.name + 'legendContainer" style="width: 100%; height: 100%; padding: 10px; overflow: auto;"></div>'
        });
        
        this.legendContainer = document.getElementById(this.name + 'legendContainer');
        this.legendContainer.style.overflow = "auto";
        
        this.viewerController.mapComponent.getMap().registerEvent(viewer.viewercontroller.controller.Event.ON_LAYER_VISIBILITY_CHANGED,this.layerVisibilityChanged,this);
        
        this.viewerController.mapComponent.getMap().registerEvent(viewer.viewercontroller.controller.Event.ON_LAYER_REMOVED,this.layerRemoved,this);
        this.start();
        return this;
    },
    // Construct the list of images to be retrieved by the queue
    makeLegendList : function (){
        this.initLegends = new Array();        
    },
    // Handler for changes to the visibility of layers
    layerVisibilityChanged : function (map,object ){
        var layer = object.layer;
        var vis = object.visible;
        if(vis){
            this.addLayer(layer);
        }else{
           this.removeLayer(layer);
        }
    },
    /**
     * Called when a layer is added
     * @param layer a viewer.viewercontroller.controller.Layer
     */
    addLayer : function (layer){
        var appLayer=this.viewerController.getAppLayerById(layer.appLayerId);
        var layerName = layer.getAppLayerName();
        /*if already added return; Still use serviceId_layerName because only one of the layer
        graphics must be added*/
        if(this.legends[layer.appLayerId]){
            return;
        }        
        //show backgrounds == false then don't show backgrounds.
        if (!this.getShowBackground()){
            //var appLayer=this.viewerController.getAppLayer(serviceId,layerName);
                                    
            if (appLayer.background){
                return;
            }
        }
        var url = this.viewerController.getLayerLegendImage(appLayer);
        if (url!=null){
            var legend = {
                src: url,
                title: layerName,
                id: layer.appLayerId
            };
            this.legends[layer.appLayerId]=true;
            this.queue.addItem(legend);
        }
    },
    layerRemoved : function(map, object){
        var layer = object.layer;
        if(layer != null){
            this.removeLayer(layer.getId());
        }
    },
    removeLayer: function (layer){
        var appLayerId = layer.appLayerId;
        var id = appLayerId+"-div";
        var node =document.getElementById(id);
        if (node!=null){
            this.legendContainer.removeChild(node);
        }
        if (this.legends[appLayerId]){
            delete this.legends[appLayerId];
        }
    },
    // Start the legend: make a list of images to be retrieved, make a queue and start it
    start : function (){
        this.makeLegendList();
        var config ={
            legends: this.initLegends, 
            queueSize: 2,
            div: this.legendContainer
        };
{'fraction_non_alphanumeric': 0.10586302495122703, 'fraction_numerical': 0.004517917650682822, 'mean_word_length': 2.234805712387911, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 17, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1029171', 'n_tokens_mistral': 2678, 'n_tokens_neox': 2583, 'n_words': 734}