Skip to content

Commit

Permalink
Atlas #4 - Adding PlaceLayersSelector component; Updating OverlayLaye…
Browse files Browse the repository at this point in the history
…rs to allow GeoJSON data or URL
  • Loading branch information
dleadbetter committed Apr 2, 2024
1 parent e355286 commit 3fd0ccc
Show file tree
Hide file tree
Showing 7 changed files with 2,376 additions and 7 deletions.
3 changes: 2 additions & 1 deletion packages/core-data/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
},
"dependencies": {
"@radix-ui/react-accordion": "^1.1.2",
"@radix-ui/react-checkbox": "^1.0.4",
"@radix-ui/react-dialog": "^1.0.5",
"@radix-ui/react-dropdown-menu": "^2.0.6",
"@samvera/clover-iiif": "^2.3.2",
Expand Down Expand Up @@ -56,4 +57,4 @@
"vite": "^5.1.4",
"vite-plugin-copy": "^0.1.6"
}
}
}
23 changes: 17 additions & 6 deletions packages/core-data/src/components/OverlayLayers.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,17 @@ import { MapStyles } from '@performant-software/geospatial';
import { GeoJSONLayer, RasterLayer } from '@peripleo/maplibre';
import React from 'react';
import _ from 'underscore';
import type { Layer as LayerType } from '../types/RuntimeConfig';

interface OverlayLayerProps {
overlay: Layer;
}
type Layer = {
layer_type: string,
data?: { [key: string]: any },
name: string,
url?: string
};

type OverlayLayerProps = {
overlay: Layer
};

const OverlayLayer = (props: OverlayLayerProps) => {
const { overlay } = props;
Expand All @@ -17,7 +23,7 @@ const OverlayLayer = (props: OverlayLayerProps) => {
return (
<GeoJSONLayer
id={overlay.name}
data={overlay.url}
data={overlay.data || overlay.url}
fillStyle={MapStyles.fill}
pointStyle={MapStyles.point}
strokeStyle={MapStyles.stroke}
Expand All @@ -42,7 +48,12 @@ type Props = {
};

const OverlayLayers = (props: Props) => (
_.map(props.overlays, (overlay: any) => <OverlayLayer overlay={overlay} />)
_.map(props.overlays, (overlay: LayerType, index: number) => (
<OverlayLayer
key={index}
overlay={overlay}
/>
))
);

export default OverlayLayers;
96 changes: 96 additions & 0 deletions packages/core-data/src/components/PlaceLayersSelector.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
// @flow

import * as Checkbox from '@radix-ui/react-checkbox';
import { CheckSquare2, Square } from 'lucide-react';
import React, { useCallback, useState } from 'react';
import _ from 'underscore';
import OverlayLayers from './OverlayLayers';
import type { Layer as LayerType } from '../types/RuntimeConfig';

type Props = {
label: string,
layers: Array<LayerType>
};

const PlaceLayersSelector = (props: Props) => {
const [selectedLayers, setSelectedLayers] = useState([]);

/**
* Returns true if the passed ID is in the collection of selected IDs.
*
* @type {function(*): boolean}
*/
const isSelected = useCallback(({ name }) => _.findWhere(selectedLayers, { name }), [selectedLayers]);

/**
* Toggles selection for the passed ID.
*
* @type {(function(*): void)|*}
*/
const onChange = useCallback((layer) => {
if (isSelected(layer)) {
setSelectedLayers((prevSelected) => _.filter(prevSelected, (l) => l.url !== layer.url));
} else {
setSelectedLayers((prevSelected) => [...prevSelected, layer]);
}
}, [isSelected]);

if (_.isEmpty(props.layers)) {
return null;
}

return (
<div
className='px-3 pb-2 pt-4 text-sm border-t'
>
<OverlayLayers
overlays={selectedLayers}
/>
<h2
className='font-medium'
>
{ props.label }
</h2>
<ul
className='py-3 leading-relaxed'
>
{ _.map(props.layers, (layer, index) => (
<li
className='py-1'
key={index}
>
<div
className='flex gap-1.5'
>
<Checkbox.Root
className='hover:bg-transparent mb-0.5'
id={layer.name}
checked={isSelected(layer)}
onCheckedChange={() => onChange(layer)}
>
{ isSelected(layer) && (
<CheckSquare2
className='w-5 h-5'
/>
)}
{ !isSelected(layer) && (
<Square
className='w-5 h-5'
/>
)}
</Checkbox.Root>
<label
className='cursor-pointer grow'
htmlFor={layer.url}
>
{ layer.name }
</label>
</div>
</li>
))}
</ul>
</div>
);
};

export default PlaceLayersSelector;
1 change: 1 addition & 0 deletions packages/core-data/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export { default as MediaGallery } from './components/MediaGallery';
export { default as OverlayLayers } from './components/OverlayLayers';
export { default as PersistentSearchStateContextProvider } from './components/PersistentSearchStateContextProvider';
export { default as PlaceDetails } from './components/PlaceDetails';
export { default as PlaceLayersSelector } from './components/PlaceLayersSelector';
export { default as PlaceMarkers } from './components/PlaceMarkers';
export { default as PlaceResultsList } from './components/PlaceResultsList';
export { default as RefinementListProxy } from './components/RefinementListProxy';
Expand Down
61 changes: 61 additions & 0 deletions packages/storybook/src/core-data/PlaceLayersSelector.stories.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// @flow

import { Map, Zoom } from '@peripleo/maplibre';
import { Controls, Peripleo } from '@peripleo/peripleo';
import React from 'react';
import LocationMarkers from '../../../geospatial/src/components/LocationMarkers';
import mapStyle from '../data/MapStyles.json';
import PlaceLayersSelector from '../../../core-data/src/components/PlaceLayersSelector';
import data from '../data/GeorgiaBorder.json';

export default {
title: 'Components/Core Data/PlaceLayersSelector',
component: PlaceLayersSelector
};

export const Default = () => (
<Peripleo>
<Map
style={mapStyle}
>
<Controls
position='topright'
>
<Zoom />
</Controls>
<div
style={{
width: '100%',
height: '300px'
}}
>
<LocationMarkers
buffer={100}
data={{
type: 'Point',
coordinates: [
-81.2653727,
31.4252249
]
}}
/>
</div>
</Map>
<PlaceLayersSelector
label='Layers'
layers={[{
layer_type: 'raster',
name: 'Georgia Area',
url: 'https://mapwarper.net/maps/tile/51704/{z}/{x}/{y}.png'
}, {
layer_type: 'geojson',
name: 'Georgia Border',
data
}, {
layer_type: 'geojson',
name: 'Chatham County',
url: 'https://raw.githubusercontent.com/johan/world.geo.json/master/countries/USA/GA/Chatham.geo.json'
}]}
/>
</Peripleo>
);
Loading

0 comments on commit 3fd0ccc

Please sign in to comment.