Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

Fix animation and tour -- redux

Benjamin King7b362994a800
diff --git a/web/js/components/animation-widget/play-queue.js b/web/js/components/animation-widget/play-queue.js
index c56963a1e..753733d63 100644
--- a/web/js/components/animation-widget/play-queue.js
+++ b/web/js/components/animation-widget/play-queue.js
@@ -205,14 +205,21 @@ class PlayAnimation extends React.Component {
*
*/
checkQueue(bufferLength, index) {
- const { startDate, endDate, hasCustomPalettes, canPreloadAll } = this.props;
+ const {
+ startDate,
+ endDate,
+ hasCustomPalettes,
+ canPreloadAll,
+ queueLength,
+ maxQueueLength
+ } = this.props;
var currentDate = util.parseDateUTC(index);
var lastToQueue = this.getLastBufferDateStr(
currentDate,
startDate,
endDate
);
-
+ var nextDate = this.nextDate(currentDate);
if (!this.preloadedArray[0] && !this.inQueueObject[index]) {
this.initialPreload(currentDate, startDate, endDate, lastToQueue);
} else if (
@@ -223,7 +230,12 @@ class PlayAnimation extends React.Component {
) {
// if last preload date doesn't exist
this.addItemToQueue(currentDate, startDate, endDate);
- } else if (hasCustomPalettes && this.preloadedArray[0]) {
+ } else if (
+ hasCustomPalettes &&
+ this.preloadedArray[0] &&
+ !this.inQueueObject[nextDate] &&
+ queueLength > maxQueueLength
+ ) {
this.customQueuer(currentDate, startDate, endDate);
}
}
@@ -240,6 +252,7 @@ class PlayAnimation extends React.Component {
clearCache() {
this.preloadObject = {};
this.preloadedArray = [];
+ this.inQueueObject = {};
}
/*
@@ -280,13 +293,11 @@ class PlayAnimation extends React.Component {
if (nextDate > endDate) {
nextDate = startDate;
}
-
nextDateStr = util.toISOStringSeconds(nextDate);
-
if (
- !this.preloadedArray[nextDateStr] &&
+ !this.preloadObject[nextDateStr] &&
!this.inQueueObject[nextDateStr] &&
- !this.state.isplaying
+ !this.state.isPlaying
) {
this.clearCache();
this.checkQueue(this.props.queueLength, this.currentPlayingDate);
@@ -348,9 +359,8 @@ class PlayAnimation extends React.Component {
*
*/
getNextBufferDate(currentDate, startDate, endDate) {
- var lastInBuffer = util.parseDateUTC(
- this.preloadedArray[this.preloadedArray.length - 1]
- );
+ const strDate = this.preloadedArray[this.preloadedArray.length - 1];
+ var lastInBuffer = util.parseDateUTC(strDate);
var nextDate = this.nextDate(lastInBuffer);
if (lastInBuffer >= endDate || nextDate > endDate) {
return startDate;
@@ -443,7 +453,6 @@ class PlayAnimation extends React.Component {
return promiseImageryForTime(date, activeLayers);
})
.then(date => {
- console.log('playing: ' + this.props.isPlaying);
if (this.props.isPlaying) {
this.preloadObject[strDate] = date;
delete this.inQueueObject[strDate];
@@ -492,7 +501,9 @@ class PlayAnimation extends React.Component {
var player = () => {
this.shiftCache();
this.checkQueue(queueLength, currentPlayingDate);
- selectDate(util.parseDateUTC(currentPlayingDate));
+ if (this.props.isPlaying) {
+ selectDate(util.parseDateUTC(currentPlayingDate));
+ }
this.pastDates[currentPlayingDate] = util.parseDateUTC(
currentPlayingDate
); // played record
@@ -523,7 +534,7 @@ class PlayAnimation extends React.Component {
renderSpinner() {
const { onClose } = this.props;
return (
- <Modal isOpen={true} toggle={onClose}>
+ <Modal isOpen={true} toggle={onClose} size="sm" backdrop={false}>
<ModalHeader toggle={onClose}> Preloading imagery </ModalHeader>
<ModalBody>
<div style={{ minHeight: 50 }}>
diff --git a/web/js/containers/sidebar/data.js b/web/js/containers/sidebar/data.js
index 1d84143eb..e4aff574d 100644
--- a/web/js/containers/sidebar/data.js
+++ b/web/js/containers/sidebar/data.js
@@ -13,6 +13,7 @@ import { selectProduct } from '../../modules/data/actions';
class Data extends React.Component {
render() {
const {
+ isActive,
height,
findProductToSelect,
counts,
@@ -25,7 +26,7 @@ class Data extends React.Component {
} = this.props;
if (!tabTypes.download) return null;
const dataArray = Object.entries(products);
- if (dataArray.length > 0 && !selectedProduct) {
+ if (dataArray.length > 0 && !selectedProduct && isActive) {
findProductToSelect(activeLayers);
}
return (
@@ -82,7 +83,7 @@ const mapDispatchToProps = dispatch => ({
});
function mapStateToProps(state, ownProps) {
const { tabTypes } = ownProps;
- const { layers, proj, data, config, compare } = state;
+ const { layers, proj, data, config, compare, sidebar } = state;
const { selectedProduct, selectedGranules } = data;
const activeString = compare.activeString;
const activeLayers = layers[activeString];
@@ -98,7 +99,8 @@ function mapStateToProps(state, ownProps) {
selectedProduct,
products,
tabTypes,
- activeLayers
+ activeLayers,
+ isActive: sidebar.activeTab === 'download'
};
}
export default connect(
diff --git a/web/js/containers/tour.js b/web/js/containers/tour.js
index c418fa993..646718660 100644
--- a/web/js/containers/tour.js
+++ b/web/js/containers/tour.js
@@ -3,14 +3,22 @@ import PropTypes from 'prop-types';
import TourStart from '../components/tour/modal-tour-start';
import TourInProgress from '../components/tour/modal-tour-in-progress';
import TourComplete from '../components/tour/modal-tour-complete';
+import {
+ preloadPalettes,
+ hasCustomTypePalette
+} from '../modules/palettes/util';
+import { BULK_PALETTE_RENDERING_SUCCESS } from '../modules/palettes/constants';
+import { stop as stopAnimation } from '../modules/animation/actions';
import { connect } from 'react-redux';
import googleTagManager from 'googleTagManager';
+import { layersParse12 } from '../modules/layers/util';
import { endTour, selectStory, startTour } from '../modules/tour/actions';
-import { findIndex as lodashFindIndex, get as lodashGet } from 'lodash';
+import { findIndex as lodashFindIndex, get as lodashGet, uniqBy } from 'lodash';
import ErrorBoundary from './error-boundary';
import update from 'immutability-helper';
import { history } from '../main';
import util from '../util/util';
+import { parse } from '../parse';
const DEFAULT_STATE = {
modalStart: true,
@@ -81,7 +89,9 @@ class Tour extends React.Component {
storyStep.transition.action
);
this.props.processStepLink(
- storyStep['stepLink'] + '&tr=' + currentStoryId + transition
+ storyStep['stepLink'] + '&tr=' + currentStoryId + transition,
+ this.props.config,
+ this.props.renderedPalettes
);
}
fetchMetadata(currentStory, stepIndex) {
@@ -170,7 +180,9 @@ class Tour extends React.Component {
currentStory.steps[newStep - 1]['stepLink'] +
'&tr=' +
currentStoryId +
- transition
+ transition,
+ this.props.config,
+ this.props.renderedPalettes
);
}
if (currentStep + 1 === totalSteps + 1) {
@@ -194,7 +206,9 @@ class Tour extends React.Component {
currentStory.steps[newStep - 1]['stepLink'] +
'&tr=' +
currentStoryId +
- transition
+ transition,
+ this.props.config,
+ this.props.renderedPalettes
);
} else {
this.setState({
@@ -322,12 +336,34 @@ Tour.propTypes = {
showTour: PropTypes.func.isRequired
};
const mapDispatchToProps = dispatch => ({
- processStepLink: search => {
+ processStepLink: (search, config, rendered) => {
search = search.split('/?').pop();
const location = update(history.location, {
search: { $set: search }
});
- dispatch({ type: 'REDUX-LOCATION-POP-ACTION', payload: location });
+ let parameters = util.fromQueryString(search);
+ let layers = [];
+ if (
+ (parameters.l && hasCustomTypePalette(parameters.l)) ||
+ (parameters.l1 && hasCustomTypePalette(parameters.l1))
+ ) {
+ layers = layersParse12(parameters.l, config);
+ if (parameters.l1 && hasCustomTypePalette(parameters.l1)) {
+ layers.push(layersParse12(parameters.l1, config));
+ }
+ layers = uniqBy(layers, 'id');
+ dispatch(stopAnimation());
+ preloadPalettes(layers, rendered, true).then(obj => {
+ dispatch({
+ type: BULK_PALETTE_RENDERING_SUCCESS,
+ rendered: obj.rendered
+ });
+ dispatch({ type: 'REDUX-LOCATION-POP-ACTION', payload: location });
+ });
+ } else {
+ console.log('promised_none');
+ dispatch({ type: 'REDUX-LOCATION-POP-ACTION', payload: location });
+ }
},
startTour: () => {
dispatch(startTour());
@@ -345,7 +381,7 @@ const mapDispatchToProps = dispatch => ({
hideTour: hideTour
});
function mapStateToProps(state) {
- const { browser, config, tour } = state;
+ const { browser, config, tour, palettes } = state;
const { screenWidth, screenHeight } = browser;
return {
@@ -359,7 +395,8 @@ function mapStateToProps(state) {
hideTour,
screenWidth,
screenHeight,
- showTour
+ showTour,
+ renderedPalettes: palettes.rendered
};
}
const getTransitionAttr = function(el, action) {
diff --git a/web/js/location.js b/web/js/location.js
index e3078c1f9..542a37247 100644
--- a/web/js/location.js
+++ b/web/js/location.js
@@ -15,7 +15,10 @@ import {
} from './modules/layers/util';
import { resetLayers, hasSubDaily } from './modules/layers/selectors';
import { eventsReducerState } from './modules/natural-events/reducers';
-import { mapLocationToPaletteState } from './modules/palettes/util';
+import {
+ mapLocationToPaletteState,
+ preloadPalettes
+} from './modules/palettes/util';
import { mapLocationToAnimationState } from './modules/animation/util';
import { mapLocationToSidebarState } from './modules/sidebar/util';
import util from './util/util';
@@ -29,7 +32,7 @@ import update from 'immutability-helper';
* @param {Object} state | Default state object
* @param {Object} location | Redux-location-state Location object
*/
-export function mapLocationToState(state, location) {
+export const mapLocationToState = (state, location) => {
const config = state.config;
if (location.search) {
let parameters = util.fromQueryString(location.search);
@@ -90,7 +93,7 @@ export function mapLocationToState(state, location) {
}
return state;
}
-}
+};
const getParameters = function(config, parameters) {
const now = config.now;
diff --git a/web/js/main.js b/web/js/main.js
index 452531ae9..55c3cc0fc 100644
--- a/web/js/main.js
+++ b/web/js/main.js
@@ -22,10 +22,14 @@ import Brand from './brand';
import { combineModels } from './combine-models';
import { parse } from './parse';
import { combineUi } from './combine-ui';
-import { requirements as paletteRequirements } from './modules/palettes/util';
-import { validate as layerValidate } from './modules/layers/util';
+import { preloadPalettes, hasCustomTypePalette } from './modules/palettes/util';
+import {
+ validate as layerValidate,
+ layersParse12
+} from './modules/layers/util';
import { polyfill } from './polyfill';
import { debugConfig } from './debug';
+import { uniqBy } from 'lodash';
export let history = createBrowserHistory();
const isDebugMode = typeof DEBUG !== 'undefined';
@@ -39,6 +43,7 @@ const startTime = new Date().getTime();
let parameters = util.fromQueryString(location.search);
let elapsed = util.elapsed;
let errors = [];
+let loadCustoms = false;
// Document ready function
window.onload = () => {
@@ -53,13 +58,31 @@ window.onload = () => {
promise
.done(config => {
config.now = new Date();
- elapsed('Config loaded', startTime, parameters);
+ config.palettes = {
+ rendered: {},
+ custom: {}
+ };
+ elapsed('Config loaded', config.now, parameters);
+ let layers = [];
+ if (
+ (parameters.l && hasCustomTypePalette(parameters.l)) ||
+ (parameters.l1 && hasCustomTypePalette(parameters.l1))
+ ) {
+ layers = layersParse12(parameters.l, config);
+ if (parameters.l1 && hasCustomTypePalette(parameters.l1)) {
+ layers.push(layersParse12(parameters.l1, config));
+ }
+ layers = uniqBy(layers, 'id');
+ }
let legacyState = parse(parameters, config, errors);
layerValidate(errors, config);
- let requirements = [paletteRequirements(legacyState, config, true)];
- $.when
- .apply(null, requirements)
- .then(() => render(config, parameters, legacyState));
+ preloadPalettes(layers, {}, false).then(obj => {
+ config.palettes = {
+ custom: obj.custom,
+ rendered: obj.rendered
+ };
+ render(config, parameters, legacyState);
+ });
})
.fail(util.error);
};
diff --git a/web/js/map/ui.js b/web/js/map/ui.js
index ca4d57af8..fa2fb5146 100644
--- a/web/js/map/ui.js
+++ b/web/js/map/ui.js
@@ -99,8 +99,6 @@ export function mapui(models, config, store, ui) {
if (newState.v && !newState.e && extent) {
flyToNewExtent(extent, rotation);
}
- // if (newState.l || newState.l1 || newState.ca || newState.p) {
-
// ''}
return;
case layerConstants.REMOVE_LAYER:
@@ -307,7 +305,6 @@ export function mapui(models, config, store, ui) {
const compareState = state.compare;
var layerGroupStr = compareState.activeString;
var activeLayers = layers[layerGroupStr];
-
if (!config.features.compare || !compareState.active) {
if (!compareState.active && compareMapUi.active) {
compareMapUi.destroy();
@@ -672,12 +669,12 @@ export function mapui(models, config, store, ui) {
layerGroup = layerGroup || self.selected;
var layers = layerGroup.getLayers().getArray();
- var layer = lodashFindIndex(layers, {
+ var index = lodashFindIndex(layers, {
wv: {
id: def.id
}
});
- return layer;
+ return index;
};
/*
diff --git a/web/js/modules/animation/reducers.js b/web/js/modules/animation/reducers.js
index 9eb67837c..d7c8e6ec8 100644
--- a/web/js/modules/animation/reducers.js
+++ b/web/js/modules/animation/reducers.js
@@ -53,11 +53,13 @@ export function animationReducer(state = defaultState, action) {
});
case UPDATE_START_DATE:
return lodashAssign({}, state, {
- startDate: action.value
+ startDate: action.value,
+ isPlaying: false
});
case UPDATE_END_DATE:
return lodashAssign({}, state, {
- endDate: action.value
+ endDate: action.value,
+ isPlaying: false
});
case TOGGLE_LOOPING:
return lodashAssign({}, state, {
@@ -65,7 +67,8 @@ export function animationReducer(state = defaultState, action) {
});
case TOGGLE_GIF:
return lodashAssign({}, state, {
- gifActive: !state.gifActive
+ gifActive: !state.gifActive,
+ isPlaying: false
});
case 'KEY_PRESS_ACTION':
if (action.keyCode === 32 && state.isActive) {
diff --git a/web/js/modules/combine-reducers.js b/web/js/modules/combine-reducers.js
index 963e10fdd..1acd2a3fe 100644
--- a/web/js/modules/combine-reducers.js
+++ b/web/js/modules/combine-reducers.js
@@ -40,6 +40,7 @@ import { get as lodashGet } from 'lodash';
function lastAction(state = null, action) {
return action;
}
+
const responsiveStateReducer = createResponsiveStateReducer(
{
extraSmall: 500,
@@ -104,5 +105,11 @@ const reducers = {
notificationsRequest,
lastAction: lastAction
};
+const appReducer = combineReducers(reducers);
+export default (state, action) => {
+ if (action.type === 'APP_RESET') {
+ state = undefined;
+ }
-export default combineReducers(reducers);
+ return appReducer(state, action);
+};
diff --git a/web/js/modules/palettes/constants.js b/web/js/modules/palettes/constants.js
index 20f6822df..1423db467 100644
--- a/web/js/modules/palettes/constants.js
+++ b/web/js/modules/palettes/constants.js
@@ -8,3 +8,12 @@ export const SET_RANGE_AND_SQUASH = 'PALETTES/SET_RANGE_AND_SQUASH';
export const CLEAR_CUSTOM = 'PALETTES/CLEAR_CUSTOM';
export const SET_CUSTOM = 'PALETTES/SET_CUSTOM';
export const LOADED_CUSTOM_PALETTES = 'PALETTES/LOADED_CUSTOM_PALETTES';
+export const BULK_PALETTE_RENDERING_SUCCESS =
+ 'PALETTES/BULK_PALETTE_RENDERING_SUCCESS';
+
+export const PALETTE_STRINGS_PERMALINK_ARRAY = [
+ 'palette',
+ 'squash',
+ 'min',
+ 'max'
+];
diff --git a/web/js/modules/palettes/reducers.js b/web/js/modules/palettes/reducers.js
index 9b98e26b3..f74e534c7 100644
--- a/web/js/modules/palettes/reducers.js
+++ b/web/js/modules/palettes/reducers.js
@@ -4,7 +4,8 @@ import {
SET_CUSTOM,
REQUEST_PALETTE_START,
SET_RANGE_AND_SQUASH,
- LOADED_CUSTOM_PALETTES
+ LOADED_CUSTOM_PALETTES,
+ BULK_PALETTE_RENDERING_SUCCESS
} from './constants';
import update from 'immutability-helper';
import util from '../../util/util';
@@ -35,6 +36,10 @@ export function paletteReducer(state = defaultPaletteState, action) {
[action.id]: { $set: true }
})
});
+ case BULK_PALETTE_RENDERING_SUCCESS:
+ return lodashAssign({}, state, {
+ rendered: action.rendered
+ });
case REQUEST_PALETTE_SUCCESS:
let isLoading = update(state.isLoading, { $unset: [action.id] });
return lodashAssign({}, state, {
diff --git a/web/js/modules/palettes/util.js b/web/js/modules/palettes/util.js
index ab3adfb88..2b691c382 100644
--- a/web/js/modules/palettes/util.js
+++ b/web/js/modules/palettes/util.js
@@ -3,8 +3,10 @@ import {
each as lodashEach,
find as lodashFind,
assign as lodashAssign,
+ isEmpty as lodashIsEmpty,
isArray
} from 'lodash';
+import { PALETTE_STRINGS_PERMALINK_ARRAY } from './constants';
import {
setCustom as setCustomSelector,
getCount,
@@ -12,6 +14,8 @@ import {
findIndex as findPaletteExtremeIndex
} from './selectors';
import util from '../../util/util';
+import Promise from 'bluebird';
+import { setFromArray } from 'ol/transform';
export function getCheckerboard() {
var size = 2;
@@ -328,33 +332,59 @@ export function mapLocationToPaletteState(
}
return stateFromLocation;
}
-// TODO replace without jQuery
-export function requirements(state, config, startup) {
- var promises = [];
- if (startup || !state.tr) {
- config.palettes = {
- rendered: {},
- custom: {}
- };
- }
- lodashEach(state.l, function(qsLayer) {
- var layerId = qsLayer.id;
- if (config.layers[layerId] && config.layers[layerId].palette) {
- promises.push(loadRenderedPalette(config, layerId));
- }
- var custom = lodashFind(qsLayer.attributes, {
- id: 'palette'
+export function preloadPalettes(layersArray, renderedPalettes, customLoaded) {
+ let rendered = renderedPalettes || {};
+ customLoaded = customLoaded || false;
+ let preloadedCustom = false;
+ let requestArray = [];
+ let custom = {};
+ let loading = {};
+ if (layersArray) {
+ layersArray.forEach(obj => {
+ if (
+ obj &&
+ obj.palette &&
+ !renderedPalettes[obj.palette.id] &&
+ !loading[obj.palette.id]
+ ) {
+ const paletteId = obj.palette.id;
+ const location = 'config/palettes/' + paletteId + '.json';
+ const promise = util.fetch(location, 'application/json');
+ loading[paletteId] = true;
+ requestArray.push(promise);
+ promise.then(data => {
+ rendered[paletteId] = data;
+ });
+ }
+ if (obj.custom && !customLoaded && !preloadedCustom) {
+ let customPromise = util.fetch(
+ 'config/palettes-custom.json',
+ 'application/json'
+ );
+ preloadedCustom = true;
+ requestArray.push(customPromise);
+ customPromise.then(data => {
+ custom = data;
+ });
+ }
});
- if (custom) {
- promises.push(loadCustom(config));
- }
- });
- if (promises.length > 0) {
- var promise = $.Deferred();
- $.when
- .apply(null, promises)
- .then(promise.resolve)
- .fail(promise.reject);
- return promise;
+ return new Promise((resolve, reject) => {
+ Promise.all(requestArray)
+ .then(() => {
+ resolve({ custom, rendered });
+ })
+ .catch(error => {
+ reject(error);
+ });
+ });
+ } else {
+ return Promise.resolve({ custom, rendered });
}
}
+export function hasCustomTypePalette(str) {
+ let bool = false;
+ PALETTE_STRINGS_PERMALINK_ARRAY.forEach(element => {
+ if (str.includes(element)) bool = true;
+ });
+ return bool;
+}
diff --git a/web/js/parse.js b/web/js/parse.js
index 1e4ef6a9f..62a1a5fbc 100644
--- a/web/js/parse.js
+++ b/web/js/parse.js
@@ -4,24 +4,16 @@ import { mapParser } from './map/map';
// import { parse as projectionParser } from './projection/projection';
// import { parse as layerParser } from './layers/layers';
// import { parse as animationParser } from './animation/anim';
-// import palettes from './palettes/palettes';
+import { parsePalettes } from './modules/palettes/util';
// import { dataParser } from './map/data/data';
export function parse(parameters, config, errors) {
let state = parameters;
let parsers = [
- // projectionParser,
- // layerParser,
- // dateParser,
- mapParser
- // palettes.parse
+ mapParser,
+ parsePalettes // legacy palette parser e.g.state.palette in permalink
];
- // if (config.features.dataDownload) {
- // parsers.push(dataParser);
- // }
- // if (config.features.animation) {
- // parsers.push(animationParser);
- // }
+
lodashEach(parsers, function(parser) {
parser(state, errors, config);
});
diff --git a/web/js/util/util.js b/web/js/util/util.js
index 58627eb6b..6acac4a8d 100644
--- a/web/js/util/util.js
+++ b/web/js/util/util.js
@@ -792,6 +792,22 @@ export default (function (self) {
// calculate differences in 3D Space
return Math.sqrt(Math.pow((r1 - r2), 2) + Math.pow((g1 - g2), 2) + Math.pow((b1 - b2), 2));
};
+ self.fetch = function(url, mimeType) {
+ return new Promise((resolve, reject) => {
+ return fetch(url)
+ .then(function(response) {
+ return mimeType === 'application/json'
+ ? response.json()
+ : response.text();
+ })
+ .then(function(data) {
+ resolve(data);
+ })
+ .catch(function(error) {
+ reject(error);
+ });
+ });
+ }
/**
* Submits an AJAX request or retreives the result from the cache.