Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

handle more cases - handle out-of-step changes - handle direction changes - handle active compare mode switch

Jason Kentd5915c0e0703
diff --git a/web/js/map/ui.js b/web/js/map/ui.js
index a0566b10b..064f4920e 100644
--- a/web/js/map/ui.js
+++ b/web/js/map/ui.js
@@ -861,19 +861,20 @@ export default function mapui(models, config, store, ui) {
*/
async function preloadNextTiles(date, compareString) {
const state = store.getState();
- const { lastPreloadDate, preloaded, arrowDown } = state.date;
+ const {
+ lastPreloadDate, preloaded, lastArrowDirection, arrowDown,
+ } = state.date;
const { activeString } = state.compare;
const useActiveString = compareString || activeString;
- const useDate = date || lastPreloadDate || getSelectedDate(state);
+ const useDate = date || (preloaded ? lastPreloadDate : getSelectedDate(state));
const nextDate = getNextDateTime(state, 1, useDate);
const prevDate = getNextDateTime(state, -1, useDate);
+ const subsequentDate = lastArrowDirection === 'right' ? nextDate : prevDate;
// If we've preloaded N dates out, we need to use the latest
// preloaded date the next time we call this function or the buffer
// won't stay ahead of the 'animation' when holding down timetep arrows
- if (preloaded && arrowDown) {
- const subsequentDate = arrowDown === 'right' ? nextDate : prevDate;
- console.log('preload step:', subsequentDate.toISOString());
+ if (preloaded && lastArrowDirection) {
store.dispatch({
type: dateConstants.SET_PRELOAD,
preloaded: true,
@@ -885,7 +886,7 @@ export default function mapui(models, config, store, ui) {
await promiseImageryForTime(state, nextDate, useActiveString);
await promiseImageryForTime(state, prevDate, useActiveString);
- if (!date) {
+ if (!date && !arrowDown) {
preloadNextTiles(nextDate, useActiveString);
preloadNextTiles(prevDate, useActiveString);
}
@@ -902,15 +903,15 @@ export default function mapui(models, config, store, ui) {
async function bufferQuickAnimate(arrowDown) {
const state = store.getState();
+ const { preloaded, lastPreloadDate } = state.date;
const BUFFER_SIZE = 8;
const preloadPromises = [];
- const currentDate = getSelectedDate(state);
+ const currentDate = preloaded ? lastPreloadDate : getSelectedDate(state);
const direction = arrowDown === 'right' ? 1 : -1;
let nextDate = getNextDateTime(state, direction, currentDate);
for (let step = 1; step <= BUFFER_SIZE; step += 1) {
preloadPromises.push(promiseImageryForTime(state, nextDate));
- console.log(nextDate.toISOString());
if (step !== BUFFER_SIZE) {
nextDate = getNextDateTime(state, direction, nextDate);
}
diff --git a/web/js/modules/compare/actions.js b/web/js/modules/compare/actions.js
index 6acac9c67..0154d630c 100644
--- a/web/js/modules/compare/actions.js
+++ b/web/js/modules/compare/actions.js
@@ -1,5 +1,5 @@
import { initSecondLayerGroup } from '../layers/actions';
-import { initSecondDate } from '../date/actions';
+import { initSecondDate, clearPreload } from '../date/actions';
import {
CHANGE_STATE,
TOGGLE_ON_OFF,
@@ -8,8 +8,9 @@ import {
} from './constants';
export function toggleActiveCompareState() {
- return {
- type: CHANGE_STATE,
+ return (dispatch, getState) => {
+ dispatch(clearPreload());
+ dispatch({ type: CHANGE_STATE });
};
}
export function toggleCompareOnOff() {
diff --git a/web/js/modules/date/actions.js b/web/js/modules/date/actions.js
index b6e3d471c..4de741ff1 100644
--- a/web/js/modules/date/actions.js
+++ b/web/js/modules/date/actions.js
@@ -9,9 +9,10 @@ import {
ARROW_DOWN,
ARROW_UP,
SET_PRELOAD,
+ CLEAR_PRELOAD,
} from './constants';
import { getSelectedDate } from './selectors';
-import { getMaxActiveLayersDate } from './util';
+import { getMaxActiveLayersDate, outOfStepChange } from './util';
export function triggerTodayButton() {
return (dispatch, getState) => {
@@ -53,38 +54,47 @@ export function initSecondDate() {
type: INIT_SECOND_DATE,
};
}
-export function selectDate(value) {
+export function selectDate(date) {
return (dispatch, getState) => {
const state = getState();
- const {
- compare,
- } = state;
- const activeString = compare.isCompareA ? 'selected' : 'selectedB';
+ const { lastArrowDirection } = state.date;
+ const activeString = state.compare.isCompareA ? 'selected' : 'selectedB';
+ const prevDate = getSelectedDate(state);
const maxDate = getMaxActiveLayersDate(state);
- const selectedDate = value > maxDate
- ? maxDate
- : value;
+ const selectedDate = date > maxDate ? maxDate : date;
+ const direction = selectedDate > prevDate ? 'right' : 'left';
+ const directionChange = direction && lastArrowDirection !== direction;
+ if (directionChange || outOfStepChange(state, selectedDate)) {
+ dispatch(clearPreload());
+ }
dispatch({
type: SELECT_DATE,
activeString,
value: selectedDate,
+ lastArrowDirection: direction,
});
};
}
export function changeCustomInterval(delta, customInterval) {
- return {
- type: CHANGE_CUSTOM_INTERVAL,
- value: customInterval,
- delta,
+ return (dispatch, getState) => {
+ dispatch(clearPreload());
+ dispatch({
+ type: CHANGE_CUSTOM_INTERVAL,
+ value: customInterval,
+ delta,
+ });
};
}
export function selectInterval(delta, interval, customSelected) {
- return {
- type: CHANGE_INTERVAL,
- value: interval,
- delta,
- customSelected,
+ return (dispatch, getState) => {
+ dispatch(clearPreload());
+ dispatch({
+ type: CHANGE_INTERVAL,
+ value: interval,
+ delta,
+ customSelected,
+ });
};
}
export function toggleCustomModal(open, toggleBy) {
@@ -94,10 +104,18 @@ export function toggleCustomModal(open, toggleBy) {
toggleBy,
};
}
-export function setArrowDown (value) {
- return {
- type: ARROW_DOWN,
- value,
+export function setArrowDown (direction) {
+ return (dispatch, getState) => {
+ const { date } = getState();
+ const { lastArrowDirection } = date;
+ const directionChange = direction && lastArrowDirection !== direction;
+ if (directionChange) {
+ dispatch(clearPreload());
+ }
+ dispatch({
+ type: ARROW_DOWN,
+ value: direction,
+ });
};
}
export function setArrowUp () {
@@ -112,3 +130,8 @@ export function setPreload (preloaded, lastPreloadDate) {
lastPreloadDate,
};
}
+export function clearPreload () {
+ return {
+ type: CLEAR_PRELOAD,
+ };
+}
diff --git a/web/js/modules/date/constants.js b/web/js/modules/date/constants.js
index 6b59eefd4..af5a27350 100644
--- a/web/js/modules/date/constants.js
+++ b/web/js/modules/date/constants.js
@@ -8,6 +8,7 @@ export const INIT_SECOND_DATE = 'DATE/INIT_SECOND_DATE';
export const ARROW_DOWN = 'DATE/ARROW_DOWN';
export const ARROW_UP = 'DATE/ARROW_UP';
export const SET_PRELOAD = 'DATE/SET_PRELOAD';
+export const CLEAR_PRELOAD = 'DATE/CLEAR_PRELOAD';
export const monthMap = {
1: 'Jan',
diff --git a/web/js/modules/date/reducers.js b/web/js/modules/date/reducers.js
index ff83ae8d0..8dbb64494 100644
--- a/web/js/modules/date/reducers.js
+++ b/web/js/modules/date/reducers.js
@@ -10,11 +10,13 @@ import {
ARROW_DOWN,
ARROW_UP,
SET_PRELOAD,
+ CLEAR_PRELOAD,
} from './constants';
import util from '../../util/util';
export const dateReducerState = {
- arrowDown: false,
+ arrowDown: '',
+ lastArrowDirection: '',
preloaded: false,
lastPreloadDate: undefined,
selectedZoom: 3,
@@ -67,18 +69,20 @@ export function dateReducer(state = dateReducerState, action) {
return {
...state,
[action.activeString]: action.value,
+ lastArrowDirection: action.lastArrowDirection,
};
- case ARROW_DOWN:
+ case ARROW_DOWN: {
+ const { value } = action;
return {
...state,
- arrowDown: action.value,
- preloaded: false,
+ arrowDown: value,
+ lastArrowDirection: value,
};
+ }
case ARROW_UP:
return {
...state,
- arrowDown: false,
- lastPreloadDate: undefined,
+ arrowDown: '',
};
case SET_PRELOAD:
return {
@@ -86,6 +90,13 @@ export function dateReducer(state = dateReducerState, action) {
preloaded: action.preloaded,
lastPreloadDate: action.lastPreloadDate,
};
+ case CLEAR_PRELOAD: {
+ return {
+ ...state,
+ preloaded: null,
+ lastPreloadDate: null,
+ };
+ }
case TOGGLE_CUSTOM_MODAL: {
const timelineToggle = action.toggleBy === customModalType.TIMELINE;
const animationToggle = action.toggleBy === customModalType.ANIMATION;
diff --git a/web/js/modules/date/util.js b/web/js/modules/date/util.js
index 4ed9a4d46..a41f16b0f 100644
--- a/web/js/modules/date/util.js
+++ b/web/js/modules/date/util.js
@@ -281,6 +281,10 @@ export const getNextTimeSelection = (delta, increment, prevDate, minDate, maxDat
return date;
};
+/**
+ * Get the next date when using left/right arrows based on
+ * current interval and delta
+ */
export const getNextDateTime = (state, direction, date) => {
const {
customSelected, customDelta, delta, customInterval, interval,
@@ -292,3 +296,15 @@ export const getNextDateTime = (state, direction, date) => {
: timeScaleFromNumberKey[interval];
return getNextTimeSelection(useDelta * direction, changeUnit, useDate);
};
+
+/**
+ * Determine if the date change was not in sync with the current
+ * interval/delta step (e.g. a time unit was manually changed: 2003 -> 2004)
+ */
+export const outOfStepChange = (state, newDate) => {
+ const date = newDate.toISOString();
+ const previousSelectedDate = getSelectedDate(state);
+ const nextStepDate = getNextDateTime(state, 1, previousSelectedDate).toISOString();
+ const prevStepDate = getNextDateTime(state, -1, previousSelectedDate).toISOString();
+ return date !== nextStepDate && date !== prevStepDate;
+};