Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

reorganize vector interaction code

Benjamin Kingd8e005dfe6f8
diff --git a/web/js/components/map/ol-vectors.js b/web/js/components/map/ol-vectors.js
new file mode 100644
index 000000000..8f6e0ddf9
--- /dev/null
+++ b/web/js/components/map/ol-vectors.js
@@ -0,0 +1,113 @@
+import {
+ debounce as lodashDebounce,
+ get as lodashGet,
+ includes as lodashIncludes,
+} from 'lodash';
+import React from 'react';
+import PropTypes from 'prop-types';
+import { transform } from 'ol/proj';
+import { isFromActiveCompareRegion } from '../../modules/compare/util';
+import { hasNonClickableVectorLayer } from '../../modules/layers/util';
+
+
+export default class OlVectorInteractions extends React.Component {
+ constructor(props) {
+ super(props);
+ this.mouseMove = lodashDebounce(this.mouseMove.bind(this), 8);
+ this.singleClick = this.singleClick.bind(this);
+ this.registerMouseListeners();
+ }
+
+ registerMouseListeners() {
+ const { mouseEvents } = this.props;
+ mouseEvents.on('mousemove', this.mouseMove);
+ mouseEvents.on('singleclick', this.singleClick);
+ }
+
+ mouseMove(event, map, crs) {
+ const pixels = map.getEventPixel(event);
+ const coord = map.getCoordinateFromPixel(pixels);
+
+ const {
+ isShowingClick, changeCursor, measureIsActive, compareState, swipeOffset, proj,
+ } = this.props;
+ const [lon, lat] = transform(coord, crs, 'EPSG:4326');
+ if (lon < -250 || lon > 250 || lat < -90 || lat > 90) {
+ return;
+ }
+ const hasFeatures = map.hasFeatureAtPixel(pixels);
+ if (hasFeatures && !isShowingClick && !measureIsActive) {
+ let isActiveLayer = false;
+ map.forEachFeatureAtPixel(pixels, (feature, layer) => {
+ const def = lodashGet(layer, 'wv.def');
+ if (!def || lodashIncludes(def.clickDisabledFeatures, feature.getType())) return;
+ const isWrapped = proj.id === 'geographic' && (def.wrapadjacentdays || def.wrapX);
+ const isRenderedFeature = isWrapped ? lon > -250 || lon < 250 || lat > -90 || lat < 90 : true;
+ if (isRenderedFeature && isFromActiveCompareRegion(map, pixels, layer.wv, compareState, swipeOffset)) {
+ isActiveLayer = true;
+ }
+ });
+ if (isActiveLayer) changeCursor(true);
+ } else if (!hasFeatures && isShowingClick) {
+ changeCursor(false);
+ }
+ }
+
+ singleClick(e, map) {
+ const {
+ lastSelected, openVectorDiaglog, onCloseModal, selectVectorFeatures,
+ modalState, getDialogObject, measureIsActive, isMobile, activeLayers,
+ activateVectorAlert, proj,
+ } = this.props;
+
+ if (measureIsActive) return;
+ const isVectorModalOpen = modalState.id.includes('vector_dialog') && modalState.isOpen;
+ const pixels = e.pixel;
+ const clickObj = getDialogObject(pixels, map);
+ const metaArray = clickObj.metaArray || [];
+ const selected = clickObj.selected || {};
+ const offsetLeft = clickObj.offsetLeft || 10;
+ const offsetTop = clickObj.offsetTop || 100;
+ const dialogId = isVectorModalOpen ? modalState.id : `vector_dialog${pixels[0]}${pixels[1]}`;
+
+ if (metaArray.length) {
+ openVectorDiaglog(dialogId, metaArray, offsetLeft, offsetTop, isMobile);
+ } else {
+ const mapRes = map.getView().getResolution();
+ const hasNonClickableVectorLayerType = hasNonClickableVectorLayer(activeLayers, mapRes, proj.id);
+
+ if (hasNonClickableVectorLayerType) {
+ activateVectorAlert();
+ }
+ }
+ if (Object.entries(selected).length || (Object.entries(lastSelected).length && !isVectorModalOpen)) {
+ selectVectorFeatures(selected);
+ } else if (isVectorModalOpen && !Object.entries(selected).length) {
+ onCloseModal();
+ selectVectorFeatures({});
+ }
+ }
+
+ render() {
+ return null;
+ }
+}
+
+OlVectorInteractions.propTypes = {
+ changeCursor: PropTypes.func.isRequired,
+ getDialogObject: PropTypes.func.isRequired,
+ isShowingClick: PropTypes.bool.isRequired,
+ measureIsActive: PropTypes.bool.isRequired,
+ modalState: PropTypes.object.isRequired,
+ mouseEvents: PropTypes.object.isRequired,
+ onCloseModal: PropTypes.func.isRequired,
+ openVectorDiaglog: PropTypes.func.isRequired,
+ selectVectorFeatures: PropTypes.func.isRequired,
+ compareState: PropTypes.object,
+ isMobile: PropTypes.bool,
+ lastSelected: PropTypes.object,
+ proj: PropTypes.object,
+ swipeOffset: PropTypes.number,
+ activeLayers: PropTypes.array,
+ activateVectorAlert: PropTypes.func,
+};
diff --git a/web/js/containers/map-interactions.js b/web/js/containers/map-interactions.js
index 25152cdf0..ed0077e5b 100644
--- a/web/js/containers/map-interactions.js
+++ b/web/js/containers/map-interactions.js
@@ -3,106 +3,36 @@ import { connect } from 'react-redux';
import PropTypes from 'prop-types';
import {
groupBy as lodashGroupBy,
- debounce as lodashDebounce,
- get as lodashGet,
- includes as lodashIncludes,
} from 'lodash';
-import { transform } from 'ol/proj';
import OlCoordinates from '../components/map/ol-coordinates';
import vectorDialog from './vector-dialog';
import { onMapClickGetVectorFeatures } from '../modules/vector-styles/util';
import { openCustomContent, onClose } from '../modules/modal/actions';
import { selectVectorFeatures as selectVectorFeaturesActionCreator } from '../modules/vector-styles/actions';
import { changeCursor as changeCursorActionCreator } from '../modules/map/actions';
-import { isFromActiveCompareRegion } from '../modules/compare/util';
-import { hasNonClickableVectorLayer } from '../modules/layers/util';
import { ACTIVATE_VECTOR_ALERT } from '../modules/alerts/constants';
+import OlVectorInteractions from '../components/map/ol-vectors';
-
-export class MapInteractions extends React.Component {
- constructor(props) {
- super(props);
- this.mouseMove = lodashDebounce(this.mouseMove.bind(this), 8);
- this.singleClick = this.singleClick.bind(this);
- this.registerMouseListeners();
- }
-
- registerMouseListeners() {
- const { mouseEvents } = this.props;
- mouseEvents.on('mousemove', this.mouseMove);
- mouseEvents.on('singleclick', this.singleClick);
- }
-
-
- singleClick(e, map) {
- const {
- lastSelected, openVectorDiaglog, onCloseModal, selectVectorFeatures,
- modalState, getDialogObject, measureIsActive, isMobile, activeLayers,
- activateVectorAlert, proj,
- } = this.props;
-
- if (measureIsActive) return;
- const isVectorModalOpen = modalState.id.includes('vector_dialog') && modalState.isOpen;
- const pixels = e.pixel;
- const clickObj = getDialogObject(pixels, map);
- const metaArray = clickObj.metaArray || [];
- const selected = clickObj.selected || {};
- const offsetLeft = clickObj.offsetLeft || 10;
- const offsetTop = clickObj.offsetTop || 100;
- const dialogId = isVectorModalOpen ? modalState.id : `vector_dialog${pixels[0]}${pixels[1]}`;
-
- if (metaArray.length) {
- openVectorDiaglog(dialogId, metaArray, offsetLeft, offsetTop, isMobile);
- } else {
- const mapRes = map.getView().getResolution();
- const hasNonClickableVectorLayerType = hasNonClickableVectorLayer(activeLayers, mapRes, proj.id);
-
- if (hasNonClickableVectorLayerType) {
- activateVectorAlert();
- }
- }
- if (Object.entries(selected).length || (Object.entries(lastSelected).length && !isVectorModalOpen)) {
- selectVectorFeatures(selected);
- } else if (isVectorModalOpen && !Object.entries(selected).length) {
- onCloseModal();
- selectVectorFeatures({});
- }
- }
-
- mouseMove(event, map, crs) {
- const pixels = map.getEventPixel(event);
- const coord = map.getCoordinateFromPixel(pixels);
-
- const {
- isShowingClick, changeCursor, measureIsActive, compareState, swipeOffset, proj,
- } = this.props;
- const [lon, lat] = transform(coord, crs, 'EPSG:4326');
- if (lon < -250 || lon > 250 || lat < -90 || lat > 90) {
- return;
- }
- const hasFeatures = map.hasFeatureAtPixel(pixels);
- if (hasFeatures && !isShowingClick && !measureIsActive) {
- let isActiveLayer = false;
- map.forEachFeatureAtPixel(pixels, (feature, layer) => {
- const def = lodashGet(layer, 'wv.def');
- if (!def || lodashIncludes(def.clickDisabledFeatures, feature.getType())) return;
- const isWrapped = proj.id === 'geographic' && (def.wrapadjacentdays || def.wrapX);
- const isRenderedFeature = isWrapped ? lon > -250 || lon < 250 || lat > -90 || lat < 90 : true;
- if (isRenderedFeature && isFromActiveCompareRegion(map, pixels, layer.wv, compareState, swipeOffset)) {
- isActiveLayer = true;
- }
- });
- if (isActiveLayer) changeCursor(true);
- } else if (!hasFeatures && isShowingClick) {
- changeCursor(false);
- }
- }
-
+class MapInteractions extends React.PureComponent {
render() {
const {
isDistractionFreeModeActive,
isShowingClick,
mouseEvents,
+ changeCursor,
+ getDialogObject,
+ measureIsActive,
+ modalState,
+ onCloseModal,
+ openVectorDiaglog,
+ selectVectorFeatures,
+ compareState,
+ isMobile,
+ lastSelected,
+ proj,
+ swipeOffset,
+ activeLayers,
+ activateVectorAlert,
} = this.props;
let mapClasses = isShowingClick
? 'wv-map cursor-pointer'
@@ -122,6 +52,24 @@ export class MapInteractions extends React.Component {
</>
)}
+ <OlVectorInteractions
+ changeCursor={changeCursor}
+ getDialogObject={getDialogObject}
+ isShowingClick={isShowingClick}
+ measureIsActive={measureIsActive}
+ modalState={modalState}
+ mouseEvents={mouseEvents}
+ onCloseModal={onCloseModal}
+ openVectorDiaglog={openVectorDiaglog}
+ selectVectorFeatures={selectVectorFeatures}
+ compareState={compareState}
+ isMobile={isMobile}
+ lastSelected={lastSelected}
+ proj={proj}
+ swipeOffset={swipeOffset}
+ activeLayers={activeLayers}
+ activateVectorAlert={activateVectorAlert}
+ />
</>
);
}