/* * Copyright OpenSearch Contributors * SPDX-License-Identifier: Apache-2.0 */ import React, { useEffect, useState } from 'react'; import { EuiSpacer, EuiPanel, EuiForm, EuiFieldText, EuiSelect, EuiFormRow } from '@elastic/eui'; import { CustomLayerSpecification } from '../../../model/mapLayerType'; interface Props { selectedLayerConfig: CustomLayerSpecification; setSelectedLayerConfig: Function; setIsUpdateDisabled: Function; } export const CustomMapSource = ({ selectedLayerConfig, setSelectedLayerConfig, setIsUpdateDisabled, }: Props) => { const customMapTypeOptions = [ { value: 'tms', text: 'Tile Map Service (TMS)' }, { value: 'wms', text: 'Web Map Service (WMS)' }, ]; const [customMapURL, setCustomMapURL] = useState(''); const [customMapAttribution, setCustomMapAttribution] = useState(''); const [customType, setCustomType] = useState(customMapTypeOptions[1].value); const [WMSLayers, setWMSLayers] = useState(''); const [WMSVersion, setWMSVersion] = useState(''); const [WMSFormat, setWMSFormat] = useState(''); const [WMSStyles, setWMSStyles] = useState(''); // CRS: Coordinate reference systems in WMS const [WMSCoordinateSystem, setWMSCoordinateSystem] = useState(''); const [WMSBbox, setWMSBbox] = useState(''); const onChangeCustomMapURL = (e: any) => { setCustomMapURL(e.target.value); setSelectedLayerConfig({ ...selectedLayerConfig, source: { ...selectedLayerConfig?.source, url: e.target.value, }, }); }; const onChangeCustomMapAttribution = (e: any) => { setCustomMapAttribution(e.target.value); setSelectedLayerConfig({ ...selectedLayerConfig, source: { ...selectedLayerConfig?.source, attribution: e.target.value, }, }); }; const onChangeCustomType = (e: any) => { setCustomType(e.target.value); setSelectedLayerConfig({ ...selectedLayerConfig, source: { ...selectedLayerConfig?.source, customType: e.target.value, }, }); }; const onChangeWMSLayers = (e: any) => { setWMSLayers(e.target.value); setSelectedLayerConfig({ ...selectedLayerConfig, source: { ...selectedLayerConfig?.source, layers: e.target.value, }, }); }; const onChangeWMSVersion = (e: any) => { setWMSVersion(e.target.value); setSelectedLayerConfig({ ...selectedLayerConfig, source: { ...selectedLayerConfig?.source, version: e.target.value, }, }); }; const onChangeWMSFormat = (e: any) => { setWMSFormat(e.target.value); setSelectedLayerConfig({ ...selectedLayerConfig, source: { ...selectedLayerConfig?.source, format: e.target.value, }, }); }; const onChangeWMSStyles = (e: any) => { setWMSStyles(e.target.value); setSelectedLayerConfig({ ...selectedLayerConfig, source: { ...selectedLayerConfig?.source, styles: e.target.value, }, }); }; const onChangeWMSCoordinateSystem = (e: any) => { setWMSCoordinateSystem(e.target.value); setSelectedLayerConfig({ ...selectedLayerConfig, source: { ...selectedLayerConfig?.source, crs: e.target.value, }, }); }; const onChangeWMSBbox = (e: any) => { setWMSBbox(e.target.value); setSelectedLayerConfig({ ...selectedLayerConfig, source: { ...selectedLayerConfig?.source, bbox: e.target.value, }, }); }; const isInvalidURL = (url: string): boolean => { if (url === '') return false; try { new URL(url); return false; } catch (e) { return true; } }; useEffect(() => { setCustomMapURL(selectedLayerConfig.source.url); setCustomType(selectedLayerConfig.source.customType); setCustomMapAttribution(selectedLayerConfig.source.attribution); if (selectedLayerConfig.source.customType === 'wms') { setWMSLayers(selectedLayerConfig.source.layers); setWMSVersion(selectedLayerConfig.source.version); setWMSFormat(selectedLayerConfig.source.format); setWMSStyles(selectedLayerConfig.source.styles); setWMSCoordinateSystem(selectedLayerConfig.source.crs); setWMSBbox(selectedLayerConfig.source.bbox); } }, [selectedLayerConfig]); useEffect(() => { setCustomMapAttribution(selectedLayerConfig.source.attribution); }, [selectedLayerConfig.source.attribution]); useEffect(() => { if (customType === 'wms') { setIsUpdateDisabled( customMapURL === '' || WMSLayers === '' || WMSVersion === '' || WMSFormat === '' || isInvalidURL(customMapURL) ); } else { setIsUpdateDisabled(customMapURL === '' || isInvalidURL(customMapURL)); } }, [WMSFormat, WMSLayers, WMSVersion, customMapURL, customType, setIsUpdateDisabled]); return (
{selectedLayerConfig.source.customType === 'tms' && ( )} {selectedLayerConfig.source.customType === 'wms' && ( )}
); };