-
Notifications
You must be signed in to change notification settings - Fork 32
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
implement popup support for @wq/map-gl
- Loading branch information
Showing
19 changed files
with
280 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import { useEffect } from 'react'; | ||
import { usePluginReducer } from '@wq/react'; | ||
|
||
export default function MapIdentify() { | ||
const [{ instance: map, overlays }, { setHighlight }] = usePluginReducer( | ||
'map' | ||
); | ||
|
||
useEffect(() => { | ||
if (!map || map._alreadyConfiguredHandlers) { | ||
return; | ||
} | ||
map._alreadyConfiguredHandlers = true; | ||
|
||
overlays.forEach(overlay => { | ||
getIdentifyLayers(overlay).forEach(layer => { | ||
map.on('mouseenter', layer, onMouseEnter); | ||
map.on('mouseleave', layer, onMouseLeave); | ||
map.on('click', layer, evt => updateHighlight(evt, overlay)); | ||
}); | ||
}); | ||
|
||
function onMouseEnter() { | ||
map.getCanvas().style.cursor = 'pointer'; | ||
} | ||
function onMouseLeave() { | ||
map.getCanvas().style.cursor = ''; | ||
} | ||
|
||
function updateHighlight(evt, overlay) { | ||
const features = evt.features.map(feature => { | ||
feature.popup = overlay.popup; | ||
return feature; | ||
}); | ||
setHighlight({ type: 'FeatureCollection', features }); | ||
} | ||
}, [map, setHighlight]); | ||
|
||
return null; | ||
} | ||
|
||
function getIdentifyLayers(overlay) { | ||
if (overlay.identifyLayers) { | ||
return overlay.identifyLayers; | ||
} | ||
if (!overlay.popup) { | ||
return []; | ||
} | ||
if (overlay.type === 'geojson') { | ||
return ['symbol', 'line', 'fill', 'fill-extrusion', 'circle'].map( | ||
type => `${overlay.name}-${type}` | ||
); | ||
} else if (overlay.type === 'vector-tile') { | ||
return ((overlay.style || {}).layers || []).map(layer => layer.id); | ||
} else { | ||
return []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function FIXME() { | ||
return null; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import React from 'react'; | ||
import { useComponents, useViewComponents, usePluginReducer } from '@wq/react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
export default function HighlightPopup() { | ||
const { Popup, View, ScrollView, IconButton } = useComponents(), | ||
[{ highlight }, { clearHighlight }] = usePluginReducer('map'), | ||
features = (highlight && highlight.features) || []; | ||
return ( | ||
<View style={{ position: 'absolute', bottom: 0 }}> | ||
<Popup | ||
open={features.length > 0} | ||
onClose={clearHighlight} | ||
variant="persistent" | ||
> | ||
<IconButton | ||
icon="close" | ||
onClick={clearHighlight} | ||
style={{ position: 'absolute', right: 0, top: 0 }} | ||
/> | ||
<ScrollView style={{ maxHeight: '33vh' }}> | ||
{features.map(feature => ( | ||
<PopupContent key={feature.id} feature={feature} /> | ||
))} | ||
</ScrollView> | ||
</Popup> | ||
</View> | ||
); | ||
} | ||
|
||
function PopupContent({ feature }) { | ||
const popupName = feature.popup | ||
? `${feature.popup}-popup` | ||
: 'default-popup', | ||
views = useViewComponents(); | ||
|
||
let View = views[popupName]; | ||
if (!View) { | ||
console.warn(`No popup view named ${popupName}, using default.`); | ||
View = views['default-popup']; | ||
if (!View) { | ||
throw new Error('No popup view named default-popup!'); | ||
} | ||
} | ||
|
||
return <View feature={feature} />; | ||
} | ||
|
||
PopupContent.propTypes = { | ||
feature: PropTypes.object | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import React from 'react'; | ||
import { PropertyTable } from '@wq/react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
export default function PropertyTableWithoutGeometry({ form, values }) { | ||
return <PropertyTable form={withoutGeometry(form)} values={values} />; | ||
} | ||
|
||
function withoutGeometry(form) { | ||
const nform = []; | ||
form.forEach(field => { | ||
if (field.type && field.type.startsWith('geo')) { | ||
return; | ||
} | ||
if (field.type === 'repeat' || field.type === 'group') { | ||
field = { ...field, children: withoutGeometry(field.children) }; | ||
} | ||
nform.push(field); | ||
}); | ||
return nform; | ||
} | ||
|
||
PropertyTableWithoutGeometry.propTypes = { | ||
form: PropTypes.arrayOf(PropTypes.object), | ||
values: PropTypes.object | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
import React from 'react'; | ||
import { | ||
useComponents, | ||
useReverse, | ||
useConfig, | ||
usePluginState | ||
} from '@wq/react'; | ||
import PropTypes from 'prop-types'; | ||
|
||
export default function DefaultPopup({ feature }) { | ||
const reverse = useReverse(), | ||
{ PropertyTable, View, Fab } = useComponents(), | ||
config = useConfig(), | ||
authState = usePluginState('auth'), | ||
page_config = feature.popup ? config.pages[feature.popup] : null, | ||
perms = | ||
page_config && | ||
authState && | ||
authState.config && | ||
authState.config.pages && | ||
authState.config.pages[feature.popup]; | ||
|
||
let form, editUrl; | ||
if (page_config) { | ||
form = page_config.form || [{ name: 'label' }]; | ||
if (perms && perms.can_change) { | ||
editUrl = reverse(`${feature.popup}_edit`, feature.id); | ||
} | ||
} else { | ||
form = Object.keys(feature.properties).map(name => ({ | ||
name | ||
})); | ||
} | ||
return ( | ||
<View | ||
style={{ | ||
maxWidth: '70em', | ||
position: 'relative', | ||
marginLeft: 'auto', | ||
marginRight: 'auto' | ||
}} | ||
> | ||
<PropertyTable form={form} values={feature.properties} /> | ||
{editUrl && <Fab icon="edit" to={editUrl} />} | ||
</View> | ||
); | ||
} | ||
|
||
DefaultPopup.propTypes = { | ||
feature: PropTypes.object | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,5 @@ | ||
import DefaultList from './DefaultList'; | ||
import DefaultDetail from './DefaultDetail'; | ||
import DefaultPopup from './DefaultPopup'; | ||
|
||
export { DefaultList, DefaultDetail }; | ||
export { DefaultList, DefaultDetail, DefaultPopup }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import React from 'react'; | ||
import Drawer from '@material-ui/core/Drawer'; | ||
import PropTypes from 'prop-types'; | ||
|
||
export default function Popup({ | ||
anchor = 'bottom', | ||
children, | ||
open, | ||
onClose, | ||
...rest | ||
}) { | ||
return ( | ||
<Drawer anchor={anchor} open={open} onClose={onClose} {...rest}> | ||
{children} | ||
</Drawer> | ||
); | ||
} | ||
|
||
Popup.propTypes = { | ||
anchor: PropTypes.string, | ||
children: PropTypes.node, | ||
open: PropTypes.bool, | ||
onClose: PropTypes.func | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export default function NotImplemented() { | ||
return null; // FIXME | ||
} |
Oops, something went wrong.