Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

Fix map rendering of layer and zoom-drag speed

Benjamin King4f2a8c294ebb
diff --git a/web/js/combine-ui.js b/web/js/combine-ui.js
index dbc0fe7f3..58b374283 100644
--- a/web/js/combine-ui.js
+++ b/web/js/combine-ui.js
@@ -1,12 +1,6 @@
import util from './util/util';
-// import { dateLabel } from './date/label';
-
-// Timeline
-// import dateWheels from './date/wheels';
-// import { timeline } from './date/timeline';
import { mapui } from './map/ui';
import { mapAnimate } from './map/animate';
-import { debugLayers } from './debug';
import { dataUi } from './map/data/ui';
import naturalEventsUI from './map/natural-events/ui';
@@ -18,7 +12,7 @@ import naturalEventsUI from './map/natural-events/ui';
*/
export function combineUi(models, config, MapMouseEvents, store) {
let ui = {};
- ui.map = mapui(models, config, store);
+ ui.map = mapui(models, config, store, ui);
ui.map.animate = mapAnimate(config, ui, store);
if (config.features.tour) {
// ui.alert = alertUi(ui, config);
@@ -54,7 +48,7 @@ export function combineUi(models, config, MapMouseEvents, store) {
ui.data = dataUi(store, ui, config);
}
if (config.features.naturalEvents) {
- ui.naturalEvents = naturalEventsUI(ui, config, store);
+ ui.naturalEvents = naturalEventsUI(ui, config, store, models);
}
registerMapMouseHandlers(ui.map.proj, MapMouseEvents);
// Sink all focus on inputs if click unhandled
diff --git a/web/js/containers/tour.js b/web/js/containers/tour.js
index 44c1d78a3..c418fa993 100644
--- a/web/js/containers/tour.js
+++ b/web/js/containers/tour.js
@@ -6,7 +6,6 @@ import TourComplete from '../components/tour/modal-tour-complete';
import { connect } from 'react-redux';
import googleTagManager from 'googleTagManager';
import { endTour, selectStory, startTour } from '../modules/tour/actions';
-import { checkTourBuildTimestamp } from '../modules/tour/util';
import { findIndex as lodashFindIndex, get as lodashGet } from 'lodash';
import ErrorBoundary from './error-boundary';
import update from 'immutability-helper';
diff --git a/web/js/main.js b/web/js/main.js
index 178faf39a..54c40a44a 100644
--- a/web/js/main.js
+++ b/web/js/main.js
@@ -99,16 +99,6 @@ const render = (config, parameters, legacyState) => {
responsiveStoreEnhancer
)
);
- lodashEach(models, function(component, key) {
- if (component.load && !component.loaded) {
- component.load(legacyState, errors);
- }
- const dispatchUpdate = lodashDebounce(() => {
- store.dispatch(updateLegacyModule(key, component));
- }, 100);
- // sync old and new state
- component.events.any(dispatchUpdate);
- });
listenForHistoryChange(store, history);
elapsed('Render', startTime, parameters);
diff --git a/web/js/map/model.js b/web/js/map/model.js
index 4bdeb998f..8d2ad1a01 100644
--- a/web/js/map/model.js
+++ b/web/js/map/model.js
@@ -72,7 +72,6 @@ export function mapModel(models, config) {
if (state.v) {
const projId = state.p ? state.p : 'geographic';
var proj = config.projections[projId];
- console.log(proj);
var extent = state.v;
var maxExtent = proj.maxExtent;
diff --git a/web/js/map/natural-events/ui.js b/web/js/map/natural-events/ui.js
index db4792335..b7f8a5bae 100644
--- a/web/js/map/natural-events/ui.js
+++ b/web/js/map/natural-events/ui.js
@@ -1,5 +1,4 @@
import lodashFind from 'lodash/find';
-import lodashEach from 'lodash/each';
import * as olExtent from 'ol/extent';
import * as olProj from 'ol/proj';
@@ -10,11 +9,7 @@ import util from '../../util/util';
import { naturalEventsUtilGetEventById } from './util';
import { CHANGE_TAB as CHANGE_SIDEBAR_TAB } from '../../modules/sidebar/constants';
import * as EVENT_CONSTANTS from '../../modules/natural-events/constants';
-import { exists as layerExists } from '../../modules/layers/util';
-import {
- toggleVisibility as setLayerVisibility,
- addLayer
-} from '../../modules/layers/actions';
+import { activateLayersForEventCategory } from '../../modules/layers/actions';
import { deselectEvent as deselectEventAction } from '../../modules/natural-events/actions';
import { selectDate } from '../../modules/date/actions';
import { CHANGE_PROJECTION } from '../../modules/projection/constants';
@@ -24,7 +19,7 @@ const zoomLevelReference = {
Volcanoes: 6
};
-export default function naturalEventsUI(ui, config, store) {
+export default function naturalEventsUI(ui, config, store, models) {
var self = {};
var eventVisibilityAlert;
var map;
@@ -272,10 +267,7 @@ export default function naturalEventsUI(ui, config, store) {
self.markers = naturalEventMarkers.draw();
zoomPromise.then(function() {
self.selecting = true;
- if (isIdChange && !isSameCategory && !isInitialLoad) {
- activateLayersForCategory(event.categories[0].title);
- }
- if (!isInitialLoad) store.dispatch(selectDate(util.parseDateUTC(date)));
+
/* For Wildfires that didn't happen today, move the timeline forward a day
* to improve the chance that the fire is visible.
* NOTE: If the fire happened yesterday and the imagery isn't yet available
@@ -288,9 +280,14 @@ export default function naturalEventsUI(ui, config, store) {
.toISOString()
.split('T')[0];
if (date !== today || date !== yesterday) {
- store.dispatch(selectDate(util.dateAdd(util.parseDateUTC(date), 'day', 1)));
+ store.dispatch(
+ selectDate(util.dateAdd(util.parseDateUTC(date), 'day', 1))
+ );
}
- }
+ if (isIdChange && !isSameCategory && !isInitialLoad) {
+ activateLayersForCategory(event.categories[0].title);
+ }
+ } else if (!isInitialLoad) store.dispatch(selectDate(util.parseDateUTC(date)));
// Show event visiblity alert
if (!eventVisibilityAlert) {
@@ -317,10 +314,8 @@ export default function naturalEventsUI(ui, config, store) {
}
naturalEventsTrack.update(event, ui.map.selected, date, self.selectEvent);
self.selecting = false;
- self.events.trigger('selection-done', self.selected);
+ models.map.events.trigger('update-layers'); // hack to update layers
});
-
- self.events.trigger('selected-event', self.selected);
};
self.deselectEvent = function() {
@@ -422,28 +417,13 @@ export default function naturalEventsUI(ui, config, store) {
var activateLayersForCategory = function(category) {
const state = store.getState();
- const { layers, compare, proj } = state;
+ const { proj } = state;
category = category || 'Default';
let currentProjection = proj.selected.id;
// Turn on the relevant layers for the event type based on projection and category
var activeLayers = self.layers[currentProjection][category];
if (!activeLayers) activeLayers = self.layers[currentProjection]['Default'];
- // Turn off all layers in list first
- lodashEach(layers[compare.activeString], function(layer) {
- store.dispatch(setLayerVisibility(layer.id, false));
- });
- // Turn on or add new layers
- lodashEach(activeLayers, function(layer) {
- var id = layer[0];
- var visible = layer[1];
- if (layerExists(id, layers[compare.activeString])) {
- store.dispatch(setLayerVisibility(id, visible));
- } else {
- addLayer(id, {
- visible: visible
- });
- }
- });
+ store.dispatch(activateLayersForEventCategory(activeLayers));
};
var zoomToEvent = function(event, date, rotation, isSameEventID) {
diff --git a/web/js/map/rotation.js b/web/js/map/rotation.js
index fa5f7b7f0..77b0044d3 100644
--- a/web/js/map/rotation.js
+++ b/web/js/map/rotation.js
@@ -1,7 +1,7 @@
import 'jquery-ui-bundle/jquery-ui';
import util from '../util/util';
-export function MapRotate(ui, models, map) {
+export function MapRotate(ui, models, store) {
this.evts = util.events();
this.intervalId = null;
var self = this;
@@ -75,6 +75,7 @@ export function MapRotate(ui, models, map) {
* @returns {void}
*/
this.setRotationEvents = function(map, id) {
+ const legacyState = store.getState().legacy;
var dur = 500;
var $leftButton = $('#wv-map-' + id + ' .wv-map-rotate-left');
var $rightButton = $('#wv-map-' + id + ' .wv-map-rotate-right');
@@ -123,7 +124,7 @@ export function MapRotate(ui, models, map) {
$resetButton
.button({
- label: Number(models.map.rotation * (180 / Math.PI)).toFixed()
+ label: Number(legacyState.map.rotation * (180 / Math.PI)).toFixed()
})
.mousedown(function() {
// reset rotation
@@ -174,7 +175,7 @@ export function MapRotate(ui, models, map) {
currentView = ui.selected.getView();
radians = currentView.getRotation();
- models.map.rotation = radians;
+ store.dispatch({ type: 'MAP/UPDATE_ROTATION', rotation: radians });
self.setResetButton(radians);
currentDeg = currentView.getRotation() * (180.0 / Math.PI);
diff --git a/web/js/map/ui.js b/web/js/map/ui.js
index 0819316b6..7257b81f3 100644
--- a/web/js/map/ui.js
+++ b/web/js/map/ui.js
@@ -3,9 +3,7 @@ import lodashFindIndex from 'lodash/findIndex';
import lodashEach from 'lodash/each';
import lodashForOwn from 'lodash/forOwn';
import lodashThrottle from 'lodash/throttle';
-import lodashCloneDeep from 'lodash/cloneDeep';
-import lodashFind from 'lodash/find';
-import { get as lodashGet } from 'lodash';
+import { get as lodashGet, debounce as lodashDebounce, cloneDeep as lodashCloneDeep, find as lodashFind} from 'lodash';
import util from '../util/util';
import OlMap from 'ol/Map';
import OlView from 'ol/View';
@@ -42,15 +40,14 @@ import {
isRenderable as isRenderableLayer
} from '../modules/layers/selectors';
-
-export function mapui(models, config, store) {
+export function mapui(models, config, store, ui) {
var layerBuilder, createLayer;
var id = 'wv-map';
var selector = '#' + id;
var animationDuration = 250;
var self = {};
var cache = models.map.cache;// Save layers from days visited
- var rotation = new MapRotate(self, models);
+ var rotation = new MapRotate(self, models, store);
var dateline = mapDateLineBuilder(models, config, store);
var precache = mapPrecacheTile(models, config, cache, self);
var compareMapUi = mapCompare(config, store);
@@ -105,13 +102,12 @@ export function mapui(models, config, store) {
return updateLayerVisibilities(action);
case layerConstants.UPDATE_OPACITY:
return updateOpacity(action);
- case layerConstants.REORDER_LAYER_GROUP:
- return reloadLayers();
case compareConstants.CHANGE_STATE:
if (action.mode === 'spy') {
return reloadLayers();
}
return;
+ case layerConstants.REORDER_LAYER_GROUP:
case compareConstants.TOGGLE_ON_OFF:
case compareConstants.CHANGE_MODE:
return reloadLayers();
@@ -143,7 +139,7 @@ export function mapui(models, config, store) {
var map = createMap(proj);
self.proj[proj.id] = map;
});
-
+ models.map.events.on('update-layers', reloadLayers);
store.subscribe(subscribeToStore);
updateProjection(true);
};
@@ -178,13 +174,13 @@ export function mapui(models, config, store) {
}
self.selected = self.proj[proj.id];
var map = self.selected;
- models.map.updateMap(map, self);
+ store.dispatch({ type: 'MAP/UPDATE_MAP_UI', ui: self });
reloadLayers();
// Update the rotation buttons if polar projection to display correct value
if (proj.id !== 'geographic' && proj.id !== 'webmerc') {
let currentRotation = map.getView().getRotation();
- models.map.rotation = currentRotation;
+ store.dispatch({ type: 'MAP/UPDATE_ROTATION', rotation: currentRotation });
rotation.setResetButton(currentRotation);
}
@@ -302,7 +298,7 @@ export function mapui(models, config, store) {
const state = store.getState();
const { layers, proj } = state;
const compareState = state.compare;
- var layerGroupStr = compareState.isCompareA ? 'active' : 'activeB';
+ var layerGroupStr = compareState.activeString;
var activeLayers = layers[layerGroupStr];
if (!config.features.compare || !compareState.active) {
@@ -771,7 +767,7 @@ export function mapui(models, config, store) {
const map = self.selected;
const view = map.getView();
const extent = view.calculateExtent(map.getSize());
- models.map.update(extent);
+ store.dispatch({ type: 'MAP/UPDATE_MAP_EXTENT', extent });
triggerExtent();
};
/*
@@ -884,8 +880,8 @@ export function mapui(models, config, store) {
}
// Set event listeners for changes on the map view (when rotated, zoomed, panned)
- map.getView().on('change:center', updateExtent);
- map.getView().on('change:resolution', updateExtent);
+ map.getView().on('change:center', lodashDebounce(updateExtent, 300));
+ map.getView().on('change:resolution', lodashDebounce(updateExtent, 300));
map.getView().on('change:rotation', lodashThrottle(onRotate, 300));
map.on('pointerdrag', function() {
self.mapIsbeingDragged = true;
@@ -906,10 +902,11 @@ export function mapui(models, config, store) {
self.mapIsbeingZoomed = false;
}, 200);
});
- const completeKey = map.on('rendercomplete', () => {
- models.map.updateMap(self.selected, self);
- map.unset(completeKey, true);
- });
+ const onRenderComplete = () => {
+ store.dispatch({ type: 'MAP/UPDATE_MAP_UI', ui: self });
+ map.un('rendercomplete', onRenderComplete);
+ };
+ map.on('rendercomplete', onRenderComplete);
// Clicking on a vector shows it's attributes in console.
map.on('click', function(e) {
map.forEachFeatureAtPixel(e.pixel, function(feature, layer) {
@@ -1064,7 +1061,7 @@ export function mapui(models, config, store) {
typeof models.anim !== 'undefined' && models.anim.rangeState.playing;
if (isEventsTabActive || isDataTabActive || isMapAnimating) return;
- dataRunner.newPoint(pixels, map);
+ if (!self.mapIsbeingDragged && !self.mapIsbeingZoomed) dataRunner.newPoint(pixels, map);
}
$(map.getViewport())
.mouseout(function(e) {
diff --git a/web/js/modules/combine-reducers.js b/web/js/modules/combine-reducers.js
index 42a8258f9..963e10fdd 100644
--- a/web/js/modules/combine-reducers.js
+++ b/web/js/modules/combine-reducers.js
@@ -102,7 +102,7 @@ const reducers = {
modalAboutPage,
shortLink,
notificationsRequest,
- lastAction
+ lastAction: lastAction
};
export default combineReducers(reducers);
diff --git a/web/js/modules/layers/actions.js b/web/js/modules/layers/actions.js
index 2e9ec9225..fbed7becb 100644
--- a/web/js/modules/layers/actions.js
+++ b/web/js/modules/layers/actions.js
@@ -1,8 +1,9 @@
-import { findIndex as lodashFindIndex } from 'lodash';
+import { findIndex as lodashFindIndex, each as lodashEach } from 'lodash';
import {
addLayer as addLayerSelector,
resetLayers as resetLayersSelector,
- getLayers as getLayersSelector
+ getLayers as getLayersSelector,
+ activateLayersForEventCategory as activateLayersForEventCategorySelector
} from './selectors';
import {
@@ -13,7 +14,8 @@ import {
ON_LAYER_HOVER,
TOGGLE_LAYER_VISIBILITY,
REMOVE_LAYER,
- UPDATE_OPACITY
+ UPDATE_OPACITY,
+ ADD_LAYERS_FOR_EVENT
} from './constants';
export function resetLayers(activeString) {
@@ -31,6 +33,21 @@ export function resetLayers(activeString) {
});
};
}
+export function activateLayersForEventCategory(activeLayers) {
+ return (dispatch, getState) => {
+ const state = getState();
+ const newLayers = activateLayersForEventCategorySelector(
+ activeLayers,
+ state
+ );
+
+ dispatch({
+ type: ADD_LAYERS_FOR_EVENT,
+ activeString: state.compare.activeString,
+ layers: newLayers
+ });
+ };
+}
export function addLayer(id, spec) {
spec = spec || {};
return (dispatch, getState) => {
diff --git a/web/js/modules/layers/constants.js b/web/js/modules/layers/constants.js
index 8eba6b4da..9903dec8a 100644
--- a/web/js/modules/layers/constants.js
+++ b/web/js/modules/layers/constants.js
@@ -7,3 +7,4 @@ export const ON_LAYER_HOVER = 'LAYERS/ON_LAYER_HOVER';
export const TOGGLE_LAYER_VISIBILITY = 'LAYERS/TOGGLE_LAYER_VISIBILITY';
export const REMOVE_LAYER = 'LAYERS/REMOVE_LAYER';
export const UPDATE_OPACITY = 'LAYERS/UPDATE_OPACITY';
+export const ADD_LAYERS_FOR_EVENT = 'LAYERS/ADD_LAYERS_FOR_EVENT';
diff --git a/web/js/modules/layers/reducers.js b/web/js/modules/layers/reducers.js
index d7c736537..9df037211 100644
--- a/web/js/modules/layers/reducers.js
+++ b/web/js/modules/layers/reducers.js
@@ -6,7 +6,8 @@ import {
ON_LAYER_HOVER,
TOGGLE_LAYER_VISIBILITY,
REMOVE_LAYER,
- UPDATE_OPACITY
+ UPDATE_OPACITY,
+ ADD_LAYERS_FOR_EVENT
} from './constants';
import {
SET_CUSTOM as SET_CUSTOM_PALETTE,
@@ -40,7 +41,9 @@ export function getInitialState(config) {
export function layerReducer(state = initialState, action) {
const layerGroupStr = action.activeString;
switch (action.type) {
- case RESET_LAYERS || ADD_LAYER:
+ case RESET_LAYERS:
+ case ADD_LAYER:
+ case ADD_LAYERS_FOR_EVENT:
return lodashAssign({}, state, {
[layerGroupStr]: action.layers
});
diff --git a/web/js/modules/layers/selectors.js b/web/js/modules/layers/selectors.js
index 844a09592..b239a057b 100644
--- a/web/js/modules/layers/selectors.js
+++ b/web/js/modules/layers/selectors.js
@@ -10,9 +10,8 @@ import {
indexOf as lodashIndexOf,
findIndex as lodashFindIndex
} from 'lodash';
-
+import update from 'immutability-helper';
import util from '../../util/util';
-import { compareAsc } from 'date-fns';
export function hasMeasurementSource(current, config, projId) {
var hasSource;
lodashValues(current.sources).forEach(function(source) {
@@ -107,7 +106,7 @@ export function addLayer(id, spec, layers, layerConfig, overlayLength) {
if (!def) {
throw new Error('No such layer: ' + id);
}
- def.visible = true;
+ def.visible = spec.visible || true;
def.min = spec.min || undefined;
def.custom = spec.custom || undefined;
def.max = spec.max || undefined;
@@ -200,7 +199,10 @@ function forGroup(group, spec, activeLayers, state) {
) {
return;
}
- if (spec.renderable && !isRenderable(def.id, activeLayers, spec.date, state)) {
+ if (
+ spec.renderable &&
+ !isRenderable(def.id, activeLayers, spec.date, state)
+ ) {
return;
}
if (spec.visible && !def.visible) {
@@ -428,7 +430,36 @@ export function lastDateTime(activeLayers, config) {
}
return endDate;
}
-
+export function activateLayersForEventCategory(activeLayers, state) {
+ const { layers, compare } = state;
+ // Turn off all layers in list first
+ let newLayers = layers[compare.activeString];
+ lodashEach(newLayers, function(layer, index) {
+ newLayers = update(newLayers, {
+ [index]: { visible: { $set: false } }
+ });
+ });
+ // Turn on or add new layers
+ lodashEach(activeLayers, function(layer) {
+ var id = layer[0];
+ var visible = layer[1];
+ let index = lodashFindIndex(newLayers, { id });
+ if (index >= 0) {
+ newLayers = update(newLayers, {
+ [index]: { visible: { $set: visible } }
+ });
+ } else {
+ newLayers = addLayer(
+ id,
+ { visible },
+ newLayers,
+ layers.layerConfig,
+ getLayers(newLayers, { group: 'all' }, state).overlays.length
+ );
+ }
+ });
+ return newLayers;
+}
export function getZotsForActiveLayers(config, models, ui) {
// var zotObj = {};
// var sources = config.sources;
diff --git a/web/js/modules/migration/reducers.js b/web/js/modules/migration/reducers.js
index 728871ac3..77f00ed06 100644
--- a/web/js/modules/migration/reducers.js
+++ b/web/js/modules/migration/reducers.js
@@ -1,41 +1,19 @@
-import {
- CONFIG_HAS_BEEN_LOADED,
- INIT_COMPLETE,
- UPDATE_IN_LEGACY_STATE
-} from './constants';
-import { assign as lodashAssign, cloneDeep as lodashCloneDeep } from 'lodash';
+import update from 'immutability-helper';
const legacyState = {
loaded: false,
- models: {},
- config: {},
- compare: {
- active: false
- },
- date: {
- now: ''
- },
- layers: {
- active: [],
- activeB: []
- },
map: { selectedMap: null, rotation: 0 },
initComplete: false
};
export default function legacyReducer(state = legacyState, action) {
switch (action.type) {
- case CONFIG_HAS_BEEN_LOADED:
- return lodashAssign({}, state, {
- models: action.models,
- config: action.config,
- loaded: true
- });
- case UPDATE_IN_LEGACY_STATE:
- const model = lodashCloneDeep(action.model);
- return lodashAssign({}, state, { [action.key]: model });
- case INIT_COMPLETE:
- return lodashAssign({}, state, { initComplete: true });
+ case 'MAP/UPDATE_MAP_EXTENT':
+ return update(state, { map: { extent: { $set: action.extent } } });
+ case 'MAP/UPDATE_MAP_UI':
+ return update(state, { map: { selectedMap: { $set: action.ui } } });
+ case 'MAP/UPDATE_ROTATION':
+ return update(state, { map: { rotation: { $set: action.rotation } } });
default:
return state;
}