<commit_before>import {Component, SimpleChanges} from '@angular/core';
import {MapComponent} from 'idai-components-2/idai-field-map';
import {Messages} from 'idai-components-2/messages';
import {ConfigLoader} from 'idai-components-2/configuration';
import {ImageContainer} from '../../../../core/imagestore/image-container';
import {LayerManager, ListDiffResult} from './layer-manager';
import {IdaiFieldImageDocument} from '../../../../core/model/idai-field-image-document';
import {LayerImageProvider} from './layer-image-provider';


@Component({
    moduleId: module.id,
    selector: 'layer-map',
    templateUrl: './layer-map.html'
})
/**
 * @author <NAME>
 * @author <NAME>
 */
export class LayerMapComponent extends MapComponent {

    public layers: Array<IdaiFieldImageDocument> = [];

    private panes: { [resourceId: string]: any } = {};
    private imageOverlays: { [resourceId: string]: L.ImageOverlay } = {};
    private layersUpdate: boolean = false;


    constructor(private layerManager: LayerManager,
                private layerImageProvider: LayerImageProvider,
                protected messages: Messages,
                configLoader: ConfigLoader) {

        super(configLoader);

        this.layerManager.reset();
    }


    public toggleLayer(layer: IdaiFieldImageDocument) {

        if (this.layerManager.toggleLayer(layer.resource.id, this.mainTypeDocument)) {
            this.addLayerToMap(layer.resource.id);
        } else {
            this.removeLayerFromMap(layer.resource.id);
        }
    }


    /**
     * Called by MapComponent.ngOnChange
     */
    protected async updateMap(changes: SimpleChanges): Promise<any> {

        if (changes['documents'] && changes['documents'].currentValue) this.layersUpdate = true;

        if (!this.update) return Promise.resolve();

        await super.updateMap(changes);

        if (this.layersUpdate) {
            this.layersUpdate = false;
            return this.updateLayers();
        }
    }


    private async updateLayers(): Promise<any> {

        this.layerImageProvider.reset();

        const { layers, activeLayersChange } =
            await this.layerManager.initializeLayers(this.mainTypeDocument);

        this.layers = layers;
        this.initializePanes();
        this.handleActiveLayersChange(activeLayersChange);
    }


    private handleActiveLayersChange(change: ListDiffResult) {

        change.removed.forEach(layerId => this.removeLayerFromMap(layerId));
        change.added.forEach(layerId => this.addLayerToMap(layerId));
    }


    private initializePanes() {

        let zIndex = 0;
        this.layers
            .filter(layer => !this.panes[layer.resource.id])
            .forEach(layer => {
                const pane = this.map.createPane(layer.resource.id);
                pane.style.zIndex = String(zIndex++);
                this.panes[layer.resource.id] = pane;
            });
    }


    private async addLayerToMap(resourceId: string) {

        const layerDocument: IdaiFieldImageDocument
            = this.layers.find(layer => layer.resource.id == resourceId);
        if (!layerDocument) return;

        const imageContainer: ImageContainer = await this.layerImageProvider.getImageContainer(resourceId);

        const georeference = layerDocument.resource.georeference;
        this.imageOverlays[resourceId] = L.imageOverlay(imageContainer.imgSrc,
            [georeference.topLeftCoordinates,
             georeference.topRightCoordinates,
             georeference.bottomLeftCoordinates],
            { pane: layerDocument.resource.id }).addTo(this.map);
    }


    private async removeLayerFromMap(resourceId: string) {

        const imageOverlay = this.imageOverlays[resourceId];
        if (!imageOverlay) {
            console.warn('Failed to remove image ' + resourceId + ' from map. Image overlay not found.');
            return;
        }

        this.map.removeLayer(imageOverlay);
    }


    public focusLayer(layer: IdaiFieldImageDocument) {

        let georeference = layer.resource.georeference;
        let bounds = [];

        bounds.push(L.latLng(georeference.topLeftCoordinates));
        bounds.push(L.latLng(georeference.topRightCoordinates));
        bounds.push(L.latLng(georeference.bottomLeftCoordinates));

        this.map.fitBounds(bounds);
    }
}<commit_msg>get rid of promise warning<commit_after>import {Component, SimpleChanges} from '@angular/core';
import {MapComponent} from 'idai-components-2/idai-field-map';
import {Messages} from 'idai-components-2/messages';
import {ConfigLoader} from 'idai-components-2/configuration';
import {ImageContainer} from '../../../../core/imagestore/image-container';
import {LayerManager, ListDiffResult} from './layer-manager';
import {IdaiFieldImageDocument} from '../../../../core/model/idai-field-image-document';
import {LayerImageProvider} from './layer-image-provider';


@Component({
    moduleId: module.id,
    selector: 'layer-map',
    templateUrl: './layer-map.html'
})
/**
 * @author <NAME>
 * @author <NAME>
 */
export class LayerMapComponent extends MapComponent {

    public layers: Array<IdaiFieldImageDocument> = [];

    private panes: { [resourceId: string]: any } = {};
    private imageOverlays: { [resourceId: string]: L.ImageOverlay } = {};
    private layersUpdate: boolean = false;


    constructor(private layerManager: LayerManager,
                private layerImageProvider: LayerImageProvider,
                protected messages: Messages,
                configLoader: ConfigLoader) {

        super(configLoader);

        this.layerManager.reset();
    }


    public async toggleLayer(layer: IdaiFieldImageDocument) {

        if (this.layerManager.toggleLayer(layer.resource.id, this.mainTypeDocument)) {
            await this.addLayerToMap(layer.resource.id);
        } else {
            this.removeLayerFromMap(layer.resource.id);
        }
    }


    /**
     * Called by MapComponent.ngOnChange
     */
    protected async updateMap(changes: SimpleChanges): Promise<any> {

        if (changes['documents'] && changes['documents'].currentValue) this.layersUpdate = true;

        if (!this.update) return Promise.resolve();

        await super.updateMap(changes);

        if (this.layersUpdate) {
            this.layersUpdate = false;
            return this.updateLayers();
        }
    }


    private async updateLayers(): Promise<any> {

        this.layerImageProvider.reset();

        const { layers, activeLayersChange } =
            await this.layerManager.initializeLayers(this.mainTypeDocument);

        this.layers = layers;
        this.initializePanes();
        this.handleActiveLayersChange(activeLayersChange);
    }


    private handleActiveLayersChange(change: ListDiffResult) {

        change.removed.forEach(layerId => this.removeLayerFromMap(layerId));
        change.added.forEach(layerId => this.addLayerToMap(layerId));
    }


    private initializePanes() {

        let zIndex = 0;
        this.layers
            .filter(layer => !this.panes[layer.resource.id])
            .forEach(layer => {
                const pane = this.map.createPane(layer.resource.id);
                pane.style.zIndex = String(zIndex++);
                this.panes[layer.resource.id] = pane;
            });
    }


    private async addLayerToMap(resourceId: string) {

        const layerDocument: IdaiFieldImageDocument
            = this.layers.find(layer => layer.resource.id == resourceId);
        if (!layerDocument) return;

        const imageContainer: ImageContainer = await this.layerImageProvider.getImageContainer(resourceId);

        const georeference = layerDocument.resource.georeference;
        this.imageOverlays[resourceId] = L.imageOverlay(imageContainer.imgSrc,
            [georeference.topLeftCoordinates,
             georeference.topRightCoordinates,
             georeference.bottomLeftCoordinates],
            { pane: layerDocument.resource.id }).addTo(this.map);
    }


    private removeLayerFromMap(resourceId: string) {

        const imageOverlay = this.imageOverlays[resourceId];
        if (!imageOverlay) {
            console.warn('Failed to remove image ' + resourceId + ' from map. Image overlay not found.');
            return;
        }

        this.map.removeLayer(imageOverlay);
    }


    public focusLayer(layer: IdaiFieldImageDocument) {

        let georeference = layer.resource.georeference;
        let bounds = [];

        bounds.push(L.latLng(georeference.topLeftCoordinates));
        bounds.push(L.latLng(georeference.topRightCoordinates));
        bounds.push(L.latLng(georeference.bottomLeftCoordinates));

        this.map.fitBounds(bounds);
    }
}
{'fraction_non_alphanumeric': 0.11766057681259336, 'fraction_numerical': 0.0009192232563483856, 'mean_word_length': 2.7712305025996535, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2764526', 'n_tokens_mistral': 2454, 'n_tokens_neox': 2355, 'n_words': 433}