import { StoryFn } from "@storybook/react"; import { LolOpenLayersMap } from "../../map/LolOpenLayersMap"; import { AppComponent } from "../StoryUtil"; import { LolOpenLayersMapContext, LolOpenLayersMapContextProvider, StyleContext, } from "../../context/LolOpenLayersMapContext"; import { ReactElement, useContext, useEffect, useState } from "react"; import { LuiButton, LuiToastMessage, MessageLevel } from "@linzjs/lui"; import { basemap, basinReserveView, IMAGE_LAYER, IMAGE_LAYER_2, oamaruView, testLineVectorQueryLayer, tunnelLineFeature, UNDERLYING_MARKS_LAYER, UNDERLYING_MARKS_SOURCE_URL, UNDERLYING_PARCELS_LAYER, UNDERLYING_PARCELS_SOURCE_URL, vectorFeatureSource, vectorHighlightStyleTestLine, } from "./Common"; import { Meta } from "@storybook/react"; import { LayerType } from "../../layerDefinition/LolOpenLayersLayerDef"; import { Feature } from "ol"; import { Geometry } from "ol/geom"; import { Fill, Style } from "ol/style"; import Stroke from "ol/style/Stroke"; import { SourceType } from "../../sourceDefinition/LolOpenLayersSourceDef"; import { LolOpenLayersFeatureSourceDef } from "../../sourceDefinition/LolOpenLayersFeatureSourceDef"; import { LolOpenLayersVectorLayerDef } from "../../layerDefinition/LolOpenLayersVectorLayerDef"; import { FeatureLike } from "ol/Feature"; import CircleStyle from "ol/style/Circle"; import Text from "ol/style/Text"; import { LolOpenLayersMVTSourceDef } from "../../sourceDefinition/LolOpenLayersMVTSourceDef"; import { LolOpenLayersVectorTileLayerDef } from "../../layerDefinition/LolOpenLayersVectorTileLayerDef"; import { userEvent, within } from "@storybook/testing-library"; import { expect } from "@storybook/jest"; import { QueryClient, QueryClientProvider } from "@tanstack/react-query"; import { range } from "lodash-es"; import { LayerHider } from "../../testSupport/testComponents/TestComponents"; import { LolOpenLayersImageLayerDef } from "../../layerDefinition/LolOpenLayersImageLayerDef"; import { LolOpenLayersGeoImageSourceDef } from "../../sourceDefinition/LolOpenLayersGeoImageSourceDef"; import { mockImageDataUrl, mockImageDataUrl2, } from "../../../test/utils/mockImageData"; // eslint-disable-next-line no-unused-vars export default { title: "Openlayers Map Wrapper/LolOpenLayersMap/Controlled", component: LolOpenLayersMap, docs: { inlineStories: false }, } as Meta; const HideLayerStory: StoryFn = () => (

Map with layer hide

); // eslint-disable-next-line no-unused-vars export const HideLayer = HideLayerStory.bind({}); const ShowMapWithVectorHideStory: StoryFn = () => { return (

Toggle vector layer

) => { return new Style({ stroke: new Stroke({ color: "#1122AA", width: 2, }), }); }, source: { type: SourceType.FEATURES, data: [tunnelLineFeature], maxZoom: 28, } as LolOpenLayersFeatureSourceDef, visible: false, inInitialZoom: true, } as LolOpenLayersVectorLayerDef, ]} useLoadingIndicator={true} />
); }; export const ShowMapWithVectorHideDefaultAndShow = ShowMapWithVectorHideStory.bind({}); ShowMapWithVectorHideDefaultAndShow.play = async ({ canvasElement, step }) => { const canvas = within(canvasElement); await step("click the show button", async () => { const showButton = await canvas.findByRole("button", { name: /Show/i }); expect(showButton).toBeInTheDocument(); // verify default state await userEvent.click(showButton); }); await step("find the hide button", async () => { const hideButton = await canvas.findByRole( "button", { name: /Hide/i }, { timeout: 2000 } ); expect(hideButton).toBeInTheDocument(); }); }; export const ShowMapWithVectorHideShowAndHide = ShowMapWithVectorHideStory.bind( {} ); ShowMapWithVectorHideShowAndHide.play = async ({ canvasElement, step }) => { const canvas = within(canvasElement); await step("find and click the show button", async () => { const showButton = await canvas.findByRole("button", { name: /Show/i }); await userEvent.click(showButton); }); await step("find and click the hide button", async () => { const hideButton = await canvas.findByRole( "button", { name: /Hide/i }, { timeout: 2000 } ); await userEvent.click(hideButton); }); await step("find and check the show button", async () => { const showButton = await canvas.findByRole("button", { name: /Show/i }); expect(showButton).toBeInTheDocument(); }); }; const LoadLayer = (): ReactElement => { const mapContext = useContext(LolOpenLayersMapContext); useEffect(() => { mapContext.setFeatureData("test_line", [tunnelLineFeature]); }, []); return <>; }; const ZoomToExtentButton = (): ReactElement => { const mapContext = useContext(LolOpenLayersMapContext); return ( { mapContext.zoomToFit(); }} > Zoom to Extent ); }; const ZoomToExtentStory: StoryFn = () => (

Map with zooming, zoom in or out and zoom to extent

) => { return new Style({ stroke: new Stroke({ color: "#1122AA", width: 2, }), }); }, source: { type: SourceType.FEATURES, data: [tunnelLineFeature], maxZoom: 28, } as LolOpenLayersFeatureSourceDef, visible: true, inInitialZoom: true, } as LolOpenLayersVectorLayerDef, ]} useLoadingIndicator={true} disableDoubleClickZoom={true} />
); export const ZoomToExtent = ZoomToExtentStory.bind({}); const ShowMapWithVectorsInStateStory: StoryFn = () => { useEffect(() => {}); return (

Map with vector layer loaded from state

) => { return new Style({ stroke: new Stroke({ color: "#11AA44", width: 2, }), }); }, source: { type: SourceType.FEATURES, maxZoom: 28, } as LolOpenLayersFeatureSourceDef, visible: true, } as LolOpenLayersVectorLayerDef, ]} useLoadingIndicator={true} />
); }; export const ShowMapWithVectorsInState = ShowMapWithVectorsInStateStory.bind( {} ); const showFeatures = [ [0, 0], [0, 1], [1, 2], [2, 3], [0, 1], [0, 2], [0, 3], [0, 0], ]; const SequenceVectorInLayer = (): ReactElement => { const mapContext = useContext(LolOpenLayersMapContext); const [times, setTimes] = useState(0); useEffect(() => { const featuresToRender = vectorFeatureSource.slice( showFeatures[times][0], showFeatures[times][1] ); mapContext.setFeatureData("test_line", featuresToRender); }, [times]); return ( setTimes((t) => (t + 1 >= showFeatures.length ? 0 : t + 1)) } > Next ); }; const ShowMapWithVectorsDynamicStory: StoryFn = () => { useEffect(() => {}); return (

Map with changing vector layer loaded from state

) => { return new Style({ stroke: new Stroke({ color: "#11AA44", width: 2, }), }); }, source: { type: SourceType.FEATURES, maxZoom: 28, } as LolOpenLayersFeatureSourceDef, visible: true, } as LolOpenLayersVectorLayerDef, ]} useLoadingIndicator={true} />
); }; export const ShowMapWithVectorsDynamic = ShowMapWithVectorsDynamicStory.bind( {} ); const SequenceSelect = (): ReactElement => { const mapContext = useContext(LolOpenLayersMapContext); const [times, setTimes] = useState(0); useEffect(() => { mapContext.setFeatureSelect("test_line", [ new Feature(vectorFeatureSource[times]), ]); }, [times]); return ( setTimes((t) => (t >= vectorFeatureSource.length - 1 ? 0 : t + 1)) } > Next ); }; const ShowMapWithFeatureSelectionHighlightStory: StoryFn< typeof LolOpenLayersMap > = () => { return (

Map with feature selection by highlight style

); }; export const ShowMapWithFeatureSelectionHighlight = ShowMapWithFeatureSelectionHighlightStory.bind({}); const markIds = [58602, 58600, 58608, 58614, 58595, 58594]; const MarkSequenceSelect = (): ReactElement => { const mapContext = useContext(LolOpenLayersMapContext); const [times, setTimes] = useState(1); useEffect(() => { mapContext.setFeatureSelect("underlying_marks", markIds.slice(0, times)); }, [times]); return ( setTimes((t) => (t + 1 >= markIds.length ? 0 : t + 1))} > Next ); }; const ShowMapWithMarkSelectionStyleStory: StoryFn< typeof LolOpenLayersMap > = () => { return (

Map with mark selection by highlight style

{ return new Style({ image: new CircleStyle({ radius: 2, fill: new Fill({ color: "#ff0000" }), stroke: new Stroke({ color: "#ff0000", width: 2, }), }), text: new Text({ text: `${feature.getId()}`, }), }); }, highlightStyle: new Style({ image: new CircleStyle({ radius: 4, fill: new Fill({ color: "#00FF00" }), stroke: new Stroke({ color: "#ff0000", width: 4, }), }), }), source: { type: SourceType.MVT, url: UNDERLYING_MARKS_SOURCE_URL, idProperty: "mark_id", } as LolOpenLayersMVTSourceDef, visible: true, } as LolOpenLayersVectorTileLayerDef, ]} useLoadingIndicator={true} />
); }; export const ShowMapWithMarkSelectionStyle = ShowMapWithMarkSelectionStyleStory.bind({}); const ShowMapWithFeatureSelectionLayerStory: StoryFn< typeof LolOpenLayersMap > = () => { return (

Map with vector selection by highlight layer

) => { return new Style({ stroke: new Stroke({ color: "#11CC11", width: 2, }), }); }, highlightInLayer: true, source: { type: SourceType.FEATURES, data: vectorFeatureSource, maxZoom: 28, } as LolOpenLayersFeatureSourceDef, visible: true, } as LolOpenLayersVectorLayerDef, ]} useLoadingIndicator={true} />
); }; export const ShowMapWithFeatureSelectionLayer = ShowMapWithFeatureSelectionLayerStory.bind({}); const UrlSwitcher = (): ReactElement => { const mapContext = useContext(LolOpenLayersMapContext); const [urlIndex, setUrlIndex] = useState(0); const urls = [ "https://services.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer/tile/{z}/{y}/{x}", "https://services.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}", ]; useEffect(() => { mapContext.setFeatureData("basemap", urls[urlIndex]); }, [urlIndex]); const changeToType = urlIndex === 0 ? "Satellite" : "Map"; return ( setUrlIndex((t) => (t + 1) % 2)}> Show {changeToType} ); }; const ShowMapWithChangableUrlStory: StoryFn = () => { return (

Map with changeable tile url

) => { return new Style({ stroke: new Stroke({ color: "#11AA44", width: 2, }), }); }, source: { type: SourceType.FEATURES, maxZoom: 28, } as LolOpenLayersFeatureSourceDef, visible: true, } as LolOpenLayersVectorLayerDef, ]} useLoadingIndicator={true} />
); }; export const ShowMapWithChangableUrl = ShowMapWithChangableUrlStory.bind({}); const StyleContextSwitcher = (): ReactElement => { const mapContext = useContext(LolOpenLayersMapContext); const [filled, setFilled] = useState(false); useEffect(() => { mapContext.changeStyleContext( [UNDERLYING_PARCELS_LAYER], () => ({ filled, }) as ParcelStyleContext ); }, [filled]); const changeToType = filled ? "Outlined" : "Filled"; return ( setFilled((f) => !f)}> Show {changeToType} ); }; const ShowMapWithContextStyleStory: StoryFn = () => { const outlineStyle = new Style({ fill: new Fill({ color: "rgba(0,0,0,0)", }), stroke: new Stroke({ color: "#AA5555", width: 2, }), }); const filledStyle = new Style({ fill: new Fill({ color: "rgba(0,100,0,0.2)", }), stroke: new Stroke({ color: "#55AA55", width: 1, }), }); return (

Map with parcel style from context

{ return (context as ParcelStyleContext)?.filled ? filledStyle : outlineStyle; }, labelStyle: (feature, _resolution, context) => { return new Style({ text: new Text({ text: feature.get("appellation"), stroke: new Stroke({ color: (context as ParcelStyleContext)?.filled ? "#CCCC00" : "#00CC00", }), }), }); }, source: { type: SourceType.MVT, url: UNDERLYING_PARCELS_SOURCE_URL, maxZoom: 28, } as LolOpenLayersMVTSourceDef, visible: true, } as LolOpenLayersVectorTileLayerDef, ]} useLoadingIndicator={true} />
); }; export const ShowMapWithContextStyle = ShowMapWithContextStyleStory.bind({}); const FilterContextSwitcher = (): ReactElement => { const mapContext = useContext(LolOpenLayersMapContext); const styleContext = mapContext.getStyleContext( UNDERLYING_PARCELS_LAYER ) as ParcelStyleContext; const filters = styleContext?.layerFilters as string[]; const toggleLayerFilter = (topo: string) => { mapContext.changeStyleContext(UNDERLYING_PARCELS_LAYER, (styleContext) => { const filters = (styleContext as ParcelStyleContext) ?.layerFilters as string[]; return { layerFilters: filters?.includes(topo) ? filters?.filter((f) => f !== topo) : [...filters, topo], } as ParcelStyleContext; }); }; return (
); }; interface ParcelStyleContext extends StyleContext { filled: boolean; layerFilters: string[]; showLabelsOnMap?: boolean; } const InnerLayerFilterMap = () => { const outlineStyle = new Style({ fill: new Fill({ color: "rgba(0,0,0,0)", }), stroke: new Stroke({ color: "#AA5555", width: 2, }), }); const mapContext = useContext(LolOpenLayersMapContext); useEffect(() => { mapContext.changeStyleContext( UNDERLYING_PARCELS_LAYER, (_) => ({ layerFilters: ["SECO"], }) as StyleContext ); }, []); const featureInFilter = ( context: StyleContext | undefined, feature: FeatureLike ) => { const filters = (context as ParcelStyleContext)?.layerFilters as string[]; const featureParcelTopologyClass = feature.get("toc_code"); return filters?.includes(featureParcelTopologyClass); }; return ( <>

Map with parcel filtering style from context

Note: we have no secondary or tertiary topo in the tiles

{ return featureInFilter(context, feature) ? outlineStyle : new Style(); }, labelStyle: (feature, _resolution, context) => { if (featureInFilter(context, feature)) { return new Style({ text: new Text({ text: feature.get("appellation"), stroke: new Stroke({ color: (context as ParcelStyleContext)?.filled ? "#CCCC00" : "#00CC00", }), }), }); } else return new Style(); }, source: { type: SourceType.MVT, url: UNDERLYING_PARCELS_SOURCE_URL, maxZoom: 28, } as LolOpenLayersMVTSourceDef, visible: true, } as LolOpenLayersVectorTileLayerDef, ]} useLoadingIndicator={true} /> ); }; const ShowMapWithFilterStyleStory: StoryFn = () => { return (
); }; export const ShowMapWithFilterStyle = ShowMapWithFilterStyleStory.bind({}); const MessageDisplayer = (): ReactElement => { const [showMessage, setShowMessage] = useState(false); const [level, setLevel] = useState("success"); const [message, setMessage] = useState("Hello world"); return (
{ setShowMessage(true); setLevel("info"); setMessage("Hello info message"); }} > Show Info { setShowMessage(true); setLevel("success"); setMessage("Hello success message"); }} > Show success { setShowMessage(true); setLevel("warning"); setMessage("Hello warning message"); }} > Show Warning { setShowMessage(true); setLevel("error"); setMessage("Hello error message"); }} > Show Error {showMessage && ( {message} )}
); }; const DisplayMessageStory: StoryFn = () => (

Map with toast messages displayed overtop

); // eslint-disable-next-line no-unused-vars export const DisplayMessage = DisplayMessageStory.bind({}); const Zoomer = () => { const mapContext = useContext(LolOpenLayersMapContext); return (
mapContext.zoomByDelta(5)} > Zoom in +5 mapContext.zoomByDelta(1)} > Zoom in +1 mapContext.zoomByDelta(-1)} > Zoom out -1 mapContext.zoomByDelta(-5)} > Zoom out -5
); }; const ZoomByDeltaStory: StoryFn = () => (

Map with zoom by delta amounts

); // eslint-disable-next-line no-unused-vars export const ZoomByDelta = ZoomByDeltaStory.bind({}); const LabelMarkToggle = (): ReactElement => { const mapContext = useContext(LolOpenLayersMapContext); const [showLabelsOnMap, setShowLabelsOnMap] = useState(false); useEffect(() => { // tell the style to show/hide labels mapContext.changeStyleContext( [UNDERLYING_MARKS_LAYER], () => ({ showLabelsOnMap, }) as ParcelStyleContext ); }, [showLabelsOnMap]); const currentLayerStatus = mapContext.layerStatus(UNDERLYING_MARKS_LAYER); const currentVisibility = currentLayerStatus?.layerVisibility; return ( <> {currentVisibility && ( { setShowLabelsOnMap((f) => !f); // tell the map to show/hide labels mapContext.setLayerStatus(UNDERLYING_MARKS_LAYER, { ...currentLayerStatus, layerLabelsChecked: !showLabelsOnMap, layerBadgesChecked: !showLabelsOnMap, showLabelsOnMap: !showLabelsOnMap, showBadgesOnMap: !showLabelsOnMap, }); }} > {showLabelsOnMap ? "Hide" : "Show "} labels )} { if (currentVisibility) setShowLabelsOnMap(false); mapContext.setLayerStatus(UNDERLYING_MARKS_LAYER, { ...currentLayerStatus, layerVisibility: !currentVisibility, layerLabelsChecked: currentVisibility, layerBadgesChecked: currentVisibility, showLabelsOnMap: currentVisibility, showBadgesOnMap: currentVisibility, }); }} > {currentVisibility ? "Hide" : "Show"} marks ); }; const ToggleLabeledMarkStyleZoomedStory: StoryFn< typeof LolOpenLayersMap > = () => { return (

Map with toggle of labelled marks (using labelStyle)

new Style({ image: new CircleStyle({ radius: 2, fill: new Fill({ color: "#ff0000" }), stroke: new Stroke({ color: "#ff0000", width: 2, }), }), }), labelStyle: (feature: FeatureLike, _res, styleContext) => (styleContext as ParcelStyleContext).showLabelsOnMap ? new Style({ text: new Text({ text: feature.get("name"), }), }) : new Style(), declutterLabels: true, source: { type: SourceType.MVT, url: UNDERLYING_MARKS_SOURCE_URL, } as LolOpenLayersMVTSourceDef, visible: false, } as LolOpenLayersVectorTileLayerDef, ]} useLoadingIndicator={true} />
); }; export const ToggleLabeledMarkStyleZoomed = ToggleLabeledMarkStyleZoomedStory.bind({}); const clusteredPointsStyleFunction = (f: FeatureLike): Style | undefined => { //filter out features that shouldn't be shown const features = f.get("features") as FeatureLike[]; //if no features are left, show nothing const size = features.length; if (size === 0) { return new Style(); } //if there is only one display it as a normal label if (size === 1) { return new Style({ text: new Text({ text: "Point", font: 'bold 12px "Open Sans"', stroke: new Stroke({ color: "#1122AA" }), backgroundFill: new Fill({ color: "#CCCCCC" }), backgroundStroke: new Stroke({ color: "#1122AA" }), }), }); } //else display it as a cluster return new Style({ image: new CircleStyle({ radius: 10, stroke: new Stroke({ color: "#fff", }), fill: new Fill({ color: "#00C0C0", }), }), text: new Text({ text: size.toString(), fill: new Fill({ color: "#fff", }), }), }); }; const ShowMapWithClusteredPointsStory: StoryFn< typeof LolOpenLayersMap > = () => { const queryClient = new QueryClient(); const clusterData = range(0, 100).map((n) => ({ id: n, shape: { geometry: { type: "Point", coordinates: [14.755 + (n % 10) / 10000.0, -41.30248 + n / 100000.0], }, }, })); return (

Map with clustered points (delayed)

); }; export const ShowMapWithClusteredPoints = ShowMapWithClusteredPointsStory.bind( {} ); /** Show image layer and layer group in the map */ const ShowMapWithImageLayerAndLayerGroupStory: StoryFn< typeof LolOpenLayersMap > = () => { return (

Map with image layer and layer group, the default is the image layer in layer group.

); }; export const ShowMapWithImageLayer = ShowMapWithImageLayerAndLayerGroupStory.bind({});