'use client'; import { useRef, useState, useEffect } from 'react'; import { Root, createRoot } from 'react-dom/client'; import dynamic from 'next/dynamic'; import { useTranslation } from 'react-i18next'; import MapView from '@arcgis/core/views/MapView'; import WebMap from '@arcgis/core/WebMap'; import esriConfig from '@arcgis/core/config'; import ScaleBar from '@arcgis/core/widgets/ScaleBar'; import Legend from '@arcgis/core/widgets/Legend'; import * as intl from '@arcgis/core/intl'; import TextContent from '@arcgis/core/popup/content/TextContent'; import ExpressionContent from '@arcgis/core/popup/content/ExpressionContent'; import ElementExpressionInfo from '@arcgis/core/popup/ElementExpressionInfo'; import { watch } from '@arcgis/core/core/reactiveUtils'; // @ts-ignore import { getColorsForRendererValues } from '@arcgis/core/renderers/support/utils'; // set asset path for ArcGIS Maps SDK widgets esriConfig.assetsPath = '/assets'; // ids of web map items in portal const webMapDEID = '7d0768f73d3e4be2b32c22274c600cb3'; const webMapENID = 'dbf5532d06954c6a989d4f022de83f70'; // lazy load components const Layers = dynamic(() => import('./layer-list')); const Print = dynamic(() => import('./print')); const Basemaps = dynamic(() => import('./basemap-list')); const Search = dynamic(() => import('./search')); // import Calcite components import '@esri/calcite-components/dist/calcite/calcite.css'; import { setAssetPath } from '@esri/calcite-components/dist/components'; setAssetPath(window.location.href); import '@esri/calcite-components/dist/components/calcite-shell'; import '@esri/calcite-components/dist/components/calcite-shell-panel'; import '@esri/calcite-components/dist/components/calcite-shell-center-row'; import '@esri/calcite-components/dist/components/calcite-action-bar'; import '@esri/calcite-components/dist/components/calcite-action'; import '@esri/calcite-components/dist/components/calcite-panel'; import { CalciteShell, CalciteShellPanel, CalciteActionBar, CalciteAction, CalcitePanel, } from '@esri/calcite-components-react'; import MapImageLayer from '@arcgis/core/layers/MapImageLayer'; export default function MapComponent({ locale }: { locale: string }) { const maskRef = useRef(null); const tableRef = useRef(null); const mapView = useRef(null); const previousId = useRef(null); const mapRef = useRef(null); const layersRef = useRef(null); const layersRoot = useRef(null); const legendRef = useRef(null); const basemapsRef = useRef(null); const basemapsRoot = useRef(null); const printRef = useRef(null); const printRoot = useRef(null); const searchRef = useRef(document.createElement('div')); const searchRoot = useRef(null); const [actionBarExpanded, setActionBarExpanded] = useState(false); const { t } = useTranslation(); useEffect(() => { if (mapRef.current) { // set locale for ArcGIS Maps SDK widgets intl.setLocale(locale); const webMap = new WebMap({ portalItem: { id: locale === 'de' ? webMapDEID : webMapENID, portal: { url: 'https://gis.geosphere.at/portal', }, }, }); const view = new MapView({ container: mapRef.current, map: webMap, padding: { left: 49, }, popup: { dockOptions: { position: 'top-right', breakpoint: false, }, dockEnabled: true, }, extent: { ymax: 6424330, xmin: 923200, xmax: 2017806, ymin: 5616270, spatialReference: { wkid: 3857, }, }, ui: { components: ['attribution'], }, popupEnabled: true, }); mapView.current = view; // add ScaleBar const scaleBar = new ScaleBar({ view: view, unit: 'metric', }); view.ui.add([scaleBar], 'bottom-left'); // render Legend component if (legendRef.current) { new Legend({ view: view, container: legendRef.current, }); } // render Search component if (searchRef.current) { if (!searchRoot.current) { searchRoot.current = createRoot(searchRef.current); } searchRoot.current.render(); } // render Layers component if (layersRef.current) { if (!layersRoot.current) { layersRoot.current = createRoot(layersRef.current); } if (tableRef.current) { layersRoot.current.render(); } } // render Basemaps component if (basemapsRef.current) { if (!basemapsRoot.current) { basemapsRoot.current = createRoot(basemapsRef.current); } basemapsRoot.current.render(); } // // render Print component if (printRef.current) { if (!printRoot.current) { printRoot.current = createRoot(printRef.current); } if (maskRef.current) { printRoot.current.render(); } } watch( () => view.popup?.viewModel?.active, () => console.log(view.popup?.selectedFeature) ); // view.on('immediate-click', (event) => { // const mapImageLayer = view.map.layers.find((layer) => layer.title === 'Profilschnitte') as MapImageLayer; // if (mapImageLayer) { // mapImageLayer.allSublayers.forEach((sublayer) => { // if (sublayer.title === 'Bohrprofile') { // } // }); // } // }); // This function fires each time a LayerView is created // view.on('layerview-create', function (event) { // // The LayerView for the desired layer // if (event.layer.type === 'map-image') { // (event.layer as MapImageLayer).allSublayers.forEach((sublayer) => { // if (sublayer.title === 'Bohrprofile') { // let textElement = new TextContent(); // textElement.text = 'Das ist nur ein Test.'; // if (Array.isArray(sublayer.popupTemplate.content)) sublayer.popupTemplate.content.push(textElement); // sublayer.load().then(async () => { // const renderer = sublayer.renderer; // const fieldToValueColorMap = await getColorsForRendererValues(renderer); // let classBreaks: number[] = []; // let colors: object[] = []; // for (const [key, classBreaksToColorMap] of fieldToValueColorMap) { // for (const [classBreack, color] of classBreaksToColorMap) { // colors.push(color); // classBreaks.push(classBreack); // } // // while (!stopIteration) { // // if ($feature['${key}'] > ${classBreaks}[index]) { // // stopIteration = true; // // color = ${colors}[index] // // } // // } // const expressionContent = new ExpressionContent({ // expressionInfo: { // title: 'Legende', // expression: ` // var stopIteration = true; // var index = 0; // return { // type: "text", // text: "First class break for key ${key}: ${classBreaks[0]} and value: " + $feature['${key}'] // } // `, // }, // }); // if (Array.isArray(sublayer.popupTemplate.content)) { // sublayer.popupTemplate.content.push(expressionContent); // } // } // }); // } // }); // } // }); } }, [locale, t]); const handleCalciteActionBarToggle = () => { setActionBarExpanded(!actionBarExpanded); if (mapView.current) { mapView.current.padding = !actionBarExpanded ? { left: 150 } : { left: 49 }; } if (tableRef.current) { if (!actionBarExpanded) { tableRef.current.classList.add('left-40'); tableRef.current.classList.remove('left-14'); } else { tableRef.current.classList.add('left-14'); tableRef.current.classList.remove('left-40'); } } }; const handleClick = (event: any) => { if (event.target.tagName !== 'CALCITE-ACTION') { return; } const nextId = event.target.dataset.actionId; if (previousId.current) { const previousPanel = document.querySelector(`[data-panel-id=${previousId.current}]`) as HTMLCalcitePanelElement; if (previousPanel) { previousPanel.hidden = true; } if (previousId.current === 'print') { maskRef.current?.classList.add('hidden'); } } const nextPanel = document.querySelector(`[data-panel-id=${nextId}]`) as HTMLCalcitePanelElement; if (nextPanel && nextId !== previousId.current) { nextPanel.hidden = false; previousId.current = nextId; if (nextId === 'print') { maskRef.current?.classList.remove('hidden'); } } else { previousId.current = null; } }; return (
); }