<commit_before>import * as React from "react";
import {
    ReduxDispatch,
    IApplicationState,
    GenericEvent,
    GenericEventHandler,
    WmsCapabilitiesDocument,
    WMSLayerStyle
} from "../../api/common";
import { tr } from "../../api/i18n";
import { Error } from "../error";
import * as Runtime from "../../api/runtime";
import { Client } from "../../api/client";
import { WmsCapabilitiesTree } from "../wms-capabilities-tree";
import olWmsParser from "ol/format/wmscapabilities";
import olTileLayer from "ol/layer/tile";
import olImageLayer from "ol/layer/image";
import olWmsSource from "ol/source/imagewms";
import olTiledWmsSource from "ol/source/tilewms";

export interface IAddWmsLayerProps {
    locale: string | undefined;
}

export class AddWmsLayer extends React.Component<IAddWmsLayerProps, any> {
    private fnWmsUrlChange: GenericEventHandler;
    private fnLoadCaps: GenericEventHandler;
    private fnAddLayer: (name: string, style: WMSLayerStyle | undefined) => void;
    constructor(props: IAddWmsLayerProps) {
        super(props);
        this.fnWmsUrlChange = this.onWmsUrlChange.bind(this);
        this.fnLoadCaps = this.onLoadCaps.bind(this);
        this.fnAddLayer = this.onAddLayer.bind(this);
        this.state = {
            wmsUrl: "",
            loadingCapabilities: false,
            caps: null,
            error: null
        };
    }
    private onAddLayer(name: string, style: WMSLayerStyle | undefined) {
        const bTiled = true;
        const { locale } = this.props;
        const caps: WmsCapabilitiesDocument = this.state.caps;
        const viewer = Runtime.getViewer();
        if (caps && viewer) {
            const params: any = {
                LAYERS: name
            };
            if (style) {
                params.STYLE = style;
            }
            if (bTiled) {
                params.TILED = true;
            }
            let layer;
            if (bTiled) {
                layer = new olTileLayer({
                    source: new olTiledWmsSource({
                        url: caps.Capability.Request.GetMap.DCPType[0].HTTP.Get.OnlineResource,
                        params: params
                    })
                });
            } else {
                layer = new olImageLayer({
                    source: new olWmsSource({
                        url: caps.Capability.Request.GetMap.DCPType[0].HTTP.Get.OnlineResource,
                        params: params
                    })
                });
            }
            layer.set("LAYER_TYPE", "WMS");
            viewer.getLayerManager().addLayer(name, layer);
            viewer.toastSuccess("icon-success", tr("ADDED_LAYER", locale, { name: name }));
        }
    }
    private onLoadCaps(e: GenericEvent) {
        const { wmsUrl } = this.state;
        this.setState({
            caps: null,
            loadingCapabilities: true
        });
        const client = new Client("", "mapagent");
        client.getText(wmsUrl).then(s => {
            const parser = new olWmsParser();
            const caps = parser.read(s);
            this.setState({
                loadingCapabilities: false,
                caps: caps,
                error: null
            });
        }).catch(err => {<commit_msg>#324: The OL WMS capabilities parser only understands 1.3.0, so just error out for unsupported WMS versions<commit_after>import * as React from "react";
import {
    ReduxDispatch,
    IApplicationState,
    GenericEvent,
    GenericEventHandler,
    WmsCapabilitiesDocument,
    WMSLayerStyle
} from "../../api/common";
import { tr } from "../../api/i18n";
import { Error } from "../error";
import * as Runtime from "../../api/runtime";
import { Client } from "../../api/client";
import { WmsCapabilitiesTree } from "../wms-capabilities-tree";
import olWmsParser from "ol/format/wmscapabilities";
import olTileLayer from "ol/layer/tile";
import olImageLayer from "ol/layer/image";
import olWmsSource from "ol/source/imagewms";
import olTiledWmsSource from "ol/source/tilewms";

export interface IAddWmsLayerProps {
    locale: string | undefined;
}

export class AddWmsLayer extends React.Component<IAddWmsLayerProps, any> {
    private fnWmsUrlChange: GenericEventHandler;
    private fnLoadCaps: GenericEventHandler;
    private fnAddLayer: (name: string, style: WMSLayerStyle | undefined) => void;
    constructor(props: IAddWmsLayerProps) {
        super(props);
        this.fnWmsUrlChange = this.onWmsUrlChange.bind(this);
        this.fnLoadCaps = this.onLoadCaps.bind(this);
        this.fnAddLayer = this.onAddLayer.bind(this);
        this.state = {
            wmsUrl: "",
            loadingCapabilities: false,
            caps: null,
            error: null
        };
    }
    private onAddLayer(name: string, style: WMSLayerStyle | undefined) {
        const bTiled = true;
        const { locale } = this.props;
        const caps: WmsCapabilitiesDocument = this.state.caps;
        const viewer = Runtime.getViewer();
        if (caps && viewer) {
            const params: any = {
                LAYERS: name
            };
            if (style) {
                params.STYLE = style;
            }
            if (bTiled) {
                params.TILED = true;
            }
            let layer;
            if (bTiled) {
                layer = new olTileLayer({
                    source: new olTiledWmsSource({
                        url: caps.Capability.Request.GetMap.DCPType[0].HTTP.Get.OnlineResource,
                        params: params
                    })
                });
            } else {
                layer = new olImageLayer({
                    source: new olWmsSource({
                        url: caps.Capability.Request.GetMap.DCPType[0].HTTP.Get.OnlineResource,
                        params: params
                    })
                });
            }
            layer.set("LAYER_TYPE", "WMS");
            viewer.getLayerManager().addLayer(name, layer);
            viewer.toastSuccess("icon-success", tr("ADDED_LAYER", locale, { name: name }));
        }
    }
    private onLoadCaps(e: GenericEvent) {
        const { wmsUrl } = this.state;
        this.setState({
            caps: null,
            loadingCapabilities: true
        });
        const client = new Client("", "mapagent");
        client.getText(wmsUrl).then(s => {
            const parser = new olWmsParser();
            const caps = parser.read(s);
            if (caps.version != "1.3.0") {
                this.setState({
                    loadingCapabilities: false,
                    caps: null,
                    error: `Unsupported WMS version: ${caps.version}`
                });
            } else {
                this.setState({
                    loadingCapabilities: false,
                    caps: caps,
                    error: null
                });
            }
        }).catch(err => {
{'fraction_non_alphanumeric': 0.11165608770917484, 'fraction_numerical': 0.0024523946912867857, 'mean_word_length': 1.7209576138147566, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 11, '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': '5555353', 'n_tokens_mistral': 1923, 'n_tokens_neox': 1885, 'n_words': 428}