Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

WV-2126 Right Click Menu (#3810)

Jason Hurleyff668838515b
diff --git a/package-lock.json b/package-lock.json
index 638241a12..eb1aa3965 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -20887,6 +20887,15 @@
}
}
},
+ "react-contextmenu": {
+ "version": "2.14.0",
+ "resolved": "https://registry.npmjs.org/react-contextmenu/-/react-contextmenu-2.14.0.tgz",
+ "integrity": "sha512-ktqMOuad6sCFNJs/ltEwppN8F0YeXmqoZfwycgtZR/MxOXMYx1xgYC44SzWH259HdGyshk1/7sXGuIRwj9hzbw==",
+ "requires": {
+ "classnames": "^2.2.5",
+ "object-assign": "^4.1.0"
+ }
+ },
"react-dom": {
"version": "16.13.0",
"resolved": "https://registry.npmjs.org/react-dom/-/react-dom-16.13.0.tgz",
diff --git a/package.json b/package.json
index c7b7a87fc..d63ebda1c 100644
--- a/package.json
+++ b/package.json
@@ -200,6 +200,7 @@
"react": "16.11.x",
"react-autocomplete": "^1.8.1",
"react-beautiful-dnd": "^13.0.0",
+ "react-contextmenu": "^2.14.0",
"react-dom": "^16.12.0",
"react-draggable": "^4.1.0",
"react-image-crop": "^8.5.0",
diff --git a/web/css/context-menu.css b/web/css/context-menu.css
new file mode 100644
index 000000000..1fbab0d04
--- /dev/null
+++ b/web/css/context-menu.css
@@ -0,0 +1,77 @@
+.react-contextmenu {
+ background-color: #333;
+ background-clip: padding-box;
+ border: 1px solid #555;
+ border-radius: 6px;
+ color: #fff;
+ font-size: 16px;
+ margin: 2px 0 0;
+ min-width: 160px;
+ outline: none;
+ box-shadow: 2px 2px 16px rgba(0, 0, 0, 0.75);
+ opacity: 0;
+ padding: 5px 0;
+ pointer-events: none;
+ text-align: left;
+ transition: opacity 250ms ease !important;
+}
+
+.react-contextmenu.react-contextmenu--visible {
+ opacity: 1;
+ pointer-events: auto;
+ z-index: 9999;
+}
+
+.react-contextmenu-item {
+ background: 0 0;
+ border: 0;
+ color: #fff;
+ cursor: pointer;
+ font-weight: 400;
+ line-height: 1.5;
+ padding: 3px 10px;
+ text-align: inherit;
+ white-space: nowrap;
+}
+
+.react-contextmenu-item.react-contextmenu-item--active,
+.react-contextmenu-item.react-contextmenu-item--selected {
+ color: #282828;
+ background-color: #eee;
+ border-color: #eee;
+ text-decoration: none;
+}
+
+.react-contextmenu-item.react-contextmenu-item--disabled,
+.react-contextmenu-item.react-contextmenu-item--disabled:hover {
+ background-color: transparent;
+ border-color: rgba(0, 0, 0, 0.15);
+ color: #878a8c;
+}
+
+.react-contextmenu-item--divider {
+ border-bottom: 1px solid rgba(255, 255, 255, 0.25);
+ cursor: inherit;
+ margin-bottom: 3px;
+ padding: 2px 0;
+}
+.react-contextmenu-item--divider:hover {
+ background-color: transparent;
+ border-color: rgba(0, 0, 0, 0.15);
+}
+
+.react-contextmenu-item.react-contextmenu-submenu {
+ padding: 0;
+}
+
+.react-contextmenu-item.react-contextmenu-submenu > .react-contextmenu-item::after {
+ content: "▶";
+ display: inline-block;
+ position: absolute;
+ right: 7px;
+}
+
+.example-multiple-targets::after {
+ content: attr(data-count);
+ display: block;
+}
diff --git a/web/js/app.js b/web/js/app.js
index a798ba4b7..dbab415d6 100644
--- a/web/js/app.js
+++ b/web/js/app.js
@@ -86,6 +86,7 @@ import '../css/facets.css';
import '../css/recent-layers.css';
import '../css/location-search.css';
import '../css/embed.css';
+import '../css/context-menu.css';
require('@elastic/react-search-ui-views/lib/styles/styles.css');
diff --git a/web/js/components/context-menu/context-menu.js b/web/js/components/context-menu/context-menu.js
new file mode 100644
index 000000000..19e244e0e
--- /dev/null
+++ b/web/js/components/context-menu/context-menu.js
@@ -0,0 +1,160 @@
+import { connect } from 'react-redux';
+import React, { useEffect, useState } from 'react';
+import PropTypes from 'prop-types';
+import { FontAwesomeIcon } from '@fortawesome/react-fontawesome';
+import { faToggleOff, faToggleOn } from '@fortawesome/free-solid-svg-icons';
+import { ContextMenu, MenuItem } from 'react-contextmenu';
+import copy from 'copy-to-clipboard';
+import { transform } from 'ol/proj';
+import util from '../../util/util';
+import CopyClipboardTooltip from '../location-search/copy-tooltip';
+import { changeUnits } from '../../modules/measure/actions';
+import { getCoordinateFixedPrecision, getFormattedCoordinates } from '../location-search/util';
+import { setCoordinates } from '../../modules/location-search/actions';
+
+const { events } = util;
+
+function RightClickMenu(props) {
+ const [show, setShow] = useState(false);
+ const [pixelCoords, setPixelCoords] = useState({ pixel: [0, 0] });
+ const [toolTipToggleTime, setToolTipToggleTime] = useState(0);
+ const [formattedCoordinates, setFormattedCoordinates] = useState();
+ const {
+ map, crs, unitOfMeasure, onToggleUnits, updateStateCoordinates, isCoordinateSearchActive,
+ } = props;
+ const [getMap, setMap] = useState(map);
+
+ const handleClick = () => (show ? setShow(false) : null);
+
+ function handleContextEvent(event, olMap) {
+ event.originalEvent.preventDefault();
+ const coord = olMap.getCoordinateFromPixel(event.pixel);
+ const [lon, lat] = transform(coord, crs, 'EPSG:4326');
+ const latitude = getCoordinateFixedPrecision(lat);
+ const longitude = getCoordinateFixedPrecision(lon);
+ setPixelCoords({ pixel: event.pixel });
+ updateStateCoordinates([longitude, latitude]);
+ setFormattedCoordinates(getFormattedCoordinates(lat, lon).join(','));
+ setMap(olMap);
+ setShow(true);
+ }
+
+ function copyCoordsToClipboard() {
+ const options = {
+ format: 'text/plain',
+ };
+ copy(formattedCoordinates, options);
+ setToolTipToggleTime(Date.now());
+ }
+
+ function handleMeasurementMenu(action) {
+ if (action === 'units') {
+ const oppositeUnit = unitOfMeasure === 'km' ? 'mi' : 'km';
+ return onToggleUnits(oppositeUnit);
+ }
+ setShow(false);
+ events.trigger(`measure:${action}`);
+ }
+
+ function addPlaceMarkerHandler(coords, olMap, crsData) {
+ events.trigger('context-menu:location', coords, olMap, crsData);
+ setShow(false);
+ }
+
+ const oppositeUnitOfMeasure = () => {
+ const faToggle = unitOfMeasure === 'km' ? faToggleOff : faToggleOn;
+ const oppositeUnit = unitOfMeasure === 'km' ? 'mi' : 'km';
+ return {
+ oppositeUnit,
+ fontAwesomeTag: <FontAwesomeIcon icon={faToggle} />,
+ };
+ };
+
+ useEffect(() => {
+ if (isCoordinateSearchActive) return;
+ events.on('map:singleclick', handleClick);
+ events.on('map:contextmenu', handleContextEvent);
+ return () => {
+ events.off('map:singleclick', handleClick);
+ events.off('map:contextmenu', handleContextEvent);
+ };
+ });
+
+ if (show) {
+ return (
+ <div>
+ <CopyClipboardTooltip
+ tooltipToggleTime={toolTipToggleTime}
+ clearCopyToClipboardTooltip={() => {}}
+ placement="top"
+ />
+ <ContextMenu id="context-menu-trigger">
+ <MenuItem onClick={() => copyCoordsToClipboard()}>
+ <div id="copy-coordinates-to-clipboard-button">
+ {formattedCoordinates}
+ </div>
+ </MenuItem>
+ <MenuItem onClick={() => addPlaceMarkerHandler(pixelCoords, getMap, crs)}>
+ Add Place Marker
+ </MenuItem>
+ <MenuItem divider />
+ <MenuItem onClick={() => handleMeasurementMenu('distance')}>
+ Measure Distance
+ </MenuItem>
+ <MenuItem onClick={() => handleMeasurementMenu('area')}>
+ Measure Area
+ </MenuItem>
+ <MenuItem onClick={() => handleMeasurementMenu('clear')}>
+ Remove Measurements
+ </MenuItem>
+ <MenuItem onClick={() => handleMeasurementMenu('units')}>
+ Change Units to
+ {' '}
+ {oppositeUnitOfMeasure().oppositeUnit}
+ </MenuItem>
+ </ContextMenu>
+ </div>
+ );
+ }
+
+ return <></>;
+}
+
+function mapStateToProps(state) {
+ const {
+ map, proj, measure, locationSearch, config,
+ } = state;
+ const { unitOfMeasure } = measure;
+ const { crs } = proj.selected;
+ const { coordinates, isCoordinateSearchActive } = locationSearch;
+ return {
+ map,
+ crs,
+ unitOfMeasure,
+ coordinates,
+ isCoordinateSearchActive,
+ config,
+ };
+}
+
+const mapDispatchToProps = (dispatch) => ({
+ onToggleUnits: (unitOfMeasure) => {
+ dispatch(changeUnits(unitOfMeasure));
+ },
+ updateStateCoordinates: (coordinates) => {
+ dispatch(setCoordinates(coordinates));
+ },
+});
+RightClickMenu.propTypes = {
+ map: PropTypes.object,
+ crs: PropTypes.string,
+ unitOfMeasure: PropTypes.string,
+ onToggleUnits: PropTypes.func,
+ updateStateCoordinates: PropTypes.func,
+ isCoordinateSearchActive: PropTypes.bool,
+};
+
+export default connect(
+ mapStateToProps,
+ mapDispatchToProps,
+)(RightClickMenu);
diff --git a/web/js/components/location-search/coordinates-dialog.js b/web/js/components/location-search/coordinates-dialog.js
index 12ae03206..a70a9d6c3 100644
--- a/web/js/components/location-search/coordinates-dialog.js
+++ b/web/js/components/location-search/coordinates-dialog.js
@@ -32,9 +32,12 @@ class CoordinatesDialog extends Component {
});
// Prevent keyboard overlay in iOS
- setTimeout(() => {
- document.getElementById('location-search-autocomplete').blur();
- }, 50);
+ const searchElement = document.getElementById('location-search-autocomplete');
+ if (searchElement) {
+ setTimeout(() => {
+ searchElement.blur();
+ }, 50);
+ }
};
copy(coords, options);
}
@@ -167,6 +170,7 @@ class CoordinatesDialog extends Component {
<CopyClipboardTooltip
tooltipToggleTime={tooltipToggleTime}
clearCopyToClipboardTooltip={this.clearCopyToClipboardTooltip}
+ placement="bottom"
/>
)}
<div className="tooltip-coordinates-title">{title}</div>
diff --git a/web/js/components/location-search/copy-tooltip.js b/web/js/components/location-search/copy-tooltip.js
index d41bf35d6..74bc1f976 100644
--- a/web/js/components/location-search/copy-tooltip.js
+++ b/web/js/components/location-search/copy-tooltip.js
@@ -41,9 +41,10 @@ class CopyClipboardTooltip extends PureComponent {
render() {
const { showCopiedToolTip } = this.state;
+ const { placement } = this.props;
return (
<Tooltip
- placement="bottom"
+ placement={placement}
isOpen={showCopiedToolTip}
hideArrow
target="copy-coordinates-to-clipboard-button"
@@ -57,6 +58,7 @@ class CopyClipboardTooltip extends PureComponent {
CopyClipboardTooltip.propTypes = {
clearCopyToClipboardTooltip: PropTypes.func,
tooltipToggleTime: PropTypes.number,
+ placement: PropTypes.string,
};
export default CopyClipboardTooltip;
diff --git a/web/js/components/location-search/ol-coordinates-marker.js b/web/js/components/location-search/ol-coordinates-marker.js
index 16d3cbc8e..4e5752d8a 100644
--- a/web/js/components/location-search/ol-coordinates-marker.js
+++ b/web/js/components/location-search/ol-coordinates-marker.js
@@ -27,22 +27,29 @@ export class CoordinatesMarker extends Component {
}
componentDidMount() {
- events.on('map:singleclick', this.singleClick);
- events.on('map:contextmenu', this.rightClick);
+ events.on('context-menu:location', this.singleClick);
+ }
+
+ componentDidUpdate(prevProps) {
+ const { isCoordinateSearchActive } = this.props;
+ if (isCoordinateSearchActive) {
+ events.on('map:singleclick', this.singleClick);
+ events.on('map:contextmenu', this.rightClick);
+ } else if (prevProps.isCoordinateSearchActive && !isCoordinateSearchActive) {
+ events.off('map:singleclick', this.singleClick);
+ events.off('map:contextmenu', this.rightClick);
+ }
}
componentWillUnmount() {
- events.off('map:singleclick', this.singleClick);
- events.off('map:contextmenu', this.rightClick);
+ events.off('context-menu:location', this.singleClick);
}
rightClick(e) {
const {
- isCoordinateSearchActive,
toggleReverseGeocodeActive,
} = this.props;
- if (!isCoordinateSearchActive) return;
e.preventDefault();
toggleReverseGeocodeActive(false);
}
@@ -50,15 +57,11 @@ export class CoordinatesMarker extends Component {
singleClick(e, map, crs) {
const {
config,
- isCoordinateSearchActive,
- measureIsActive,
proj,
setPlaceMarker,
toggleReverseGeocodeActive,
} = this.props;
- if (measureIsActive || !isCoordinateSearchActive) return;
-
// handle reverse geocoding mouse click
const pixels = e.pixel;
const coord = map.getCoordinateFromPixel(pixels);
@@ -137,7 +140,6 @@ const mapDispatchToProps = (dispatch) => ({
CoordinatesMarker.propTypes = {
config: PropTypes.object.isRequired,
isCoordinateSearchActive: PropTypes.bool.isRequired,
- measureIsActive: PropTypes.bool.isRequired,
proj: PropTypes.object,
setPlaceMarker: PropTypes.func.isRequired,
toggleReverseGeocodeActive: PropTypes.func.isRequired,
diff --git a/web/js/containers/map-interactions/map-interactions.js b/web/js/containers/map-interactions/map-interactions.js
index 29b1824ce..a36ddc1a3 100644
--- a/web/js/containers/map-interactions/map-interactions.js
+++ b/web/js/containers/map-interactions/map-interactions.js
@@ -1,12 +1,14 @@
import React, { PureComponent } from 'react';
import { connect } from 'react-redux';
import PropTypes from 'prop-types';
+import { ContextMenuTrigger } from 'react-contextmenu';
import OlCoordinates from '../../components/map/ol-coordinates';
import OlVectorInteractions from './ol-vector-interactions';
import OlMeasureTool from '../../components/map/ol-measure-tool';
import OlCoordinatesMarker from '../../components/location-search/ol-coordinates-marker';
import OlRotationButtons from '../../components/map/rotation';
import OlZoomButtons from '../../components/map/zoom';
+import RightClickMenu from '../../components/context-menu/context-menu';
import NaturalEvents from '../../map/natural-events/natural-events';
class MapInteractions extends PureComponent {
@@ -27,9 +29,12 @@ class MapInteractions extends PureComponent {
const mapClasses = this.getMapClasses();
return (
<>
+ <RightClickMenu />
<OlZoomButtons />
<OlRotationButtons />
- <div id="wv-map" className={mapClasses} />
+ <ContextMenuTrigger id="context-menu-trigger">
+ <div id="wv-map" className={mapClasses} />
+ </ContextMenuTrigger>
{!isDistractionFreeModeActive && (
<OlCoordinates />
)}
diff --git a/web/js/modules/location-search/actions.js b/web/js/modules/location-search/actions.js
index 6595d33f0..d800da7d5 100644
--- a/web/js/modules/location-search/actions.js
+++ b/web/js/modules/location-search/actions.js
@@ -3,6 +3,7 @@ import {
CLEAR_SUGGESTIONS,
SET_MARKER,
SET_SUGGESTION,
+ SET_COORDINATES,
TOGGLE_DIALOG_VISIBLE,
TOGGLE_REVERSE_GEOCODE,
TOGGLE_SHOW_LOCATION_SEARCH,
@@ -141,6 +142,19 @@ export function setSuggestion(suggestion) {
};
}
+/**
+ * Set coordinates
+ * @param {Array} coordinates
+ */
+export function setCoordinates(coordinates) {
+ return (dispatch) => {
+ dispatch({
+ type: SET_COORDINATES,
+ coordinates,
+ });
+ };
+}
+
/**
* Get place suggestions using ArcGIS suggest API
* @param {String} val | input text to suggest places
diff --git a/web/js/modules/location-search/constants.js b/web/js/modules/location-search/constants.js
index 322249869..87d7ca94f 100644
--- a/web/js/modules/location-search/constants.js
+++ b/web/js/modules/location-search/constants.js
@@ -5,6 +5,8 @@ export const REQUEST_SUGGEST_PLACE_SUCCESS = 'LOCATION_SEARCH/REQUEST_SUGGEST_PL
export const SET_REVERSE_GEOCODE_RESULTS = 'LOCATION_SEARCH/SET_REVERSE_GEOCODE_RESULTS';
export const SET_MARKER = 'LOCATION_SEARCH/SET_MARKER';
export const SET_SUGGESTION = 'LOCATION_SEARCH/SET_SUGGESTION';
+export const SET_COORDINATES = 'LOCATION_SEARCH/SET_COORDINATES';
export const TOGGLE_DIALOG_VISIBLE = 'LOCATION_SEARCH/TOGGLE_DIALOG_VISIBLE';
export const TOGGLE_REVERSE_GEOCODE = 'LOCATION_SEARCH/TOGGLE_REVERSE_GEOCODE';
export const TOGGLE_SHOW_LOCATION_SEARCH = 'LOCATION_SEARCH/TOGGLE_SHOW_LOCATION_SEARCH';
+
diff --git a/web/js/modules/location-search/reducers.js b/web/js/modules/location-search/reducers.js
index 79dd2fc93..cdc9ae986 100644
--- a/web/js/modules/location-search/reducers.js
+++ b/web/js/modules/location-search/reducers.js
@@ -6,6 +6,7 @@ import {
SET_REVERSE_GEOCODE_RESULTS,
SET_MARKER,
SET_SUGGESTION,
+ SET_COORDINATES,
TOGGLE_DIALOG_VISIBLE,
TOGGLE_REVERSE_GEOCODE,
TOGGLE_SHOW_LOCATION_SEARCH,
@@ -65,6 +66,11 @@ export function locationSearchReducer(state = locationSearchState, action) {
...state,
suggestedPlace: action.value,
};
+ case SET_COORDINATES:
+ return {
+ ...state,
+ coordinates: action.coordinates,
+ };
case REQUEST_SUGGEST_PLACE_SUCCESS:
return {
...state,