Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

break mapLocationToState into pieces to be less confusing #1623

Benjamin Kingcf598b27058e
diff --git a/web/js/location.js b/web/js/location.js
index f910eee94..5505dc148 100644
--- a/web/js/location.js
+++ b/web/js/location.js
@@ -3,19 +3,19 @@ import { encode } from './modules/link/util';
// legacy crutches
// import { getLayersParameterSetup } from './modules/layers/util';
import { getDateParameterSetup } from './modules/date/util';
-import { getAnimationParameterSetup } from './modules/animation/util';
import { checkTourBuildTimestamp } from './modules/tour/util';
import { getMapParameterSetup } from './modules/map/util';
import { eventParse, serializeEvent } from './modules/natural-events/util';
+import { mapLocationToCompareState } from './modules/compare/util';
+import { mapLocationToProjState } from './modules/projection/util';
import {
- layersParse11,
layersParse12,
- serializeLayers
+ serializeLayers,
+ mapLocationToLayerState
} from './modules/layers/util';
-import { initialCompareState } from './modules/compare/reducers';
import { resetLayers } from './modules/layers/selectors';
import { eventsReducerState } from './modules/natural-events/reducers';
-import { loadPalettes } from './modules/palettes/util';
+import { mapLocationToPaletteState } from './modules/palettes/util';
import util from './util/util';
import update from 'immutability-helper';
@@ -24,46 +24,26 @@ export function mapLocationToState(state, location) {
if (location.search) {
let parameters = util.fromQueryString(location.search);
let stateFromLocation = location.query;
- const projId = get(stateFromLocation, 'proj.id');
- if (projId) {
- let selected = get(state, `config.projections.${projId}`) || {};
- stateFromLocation = update(stateFromLocation, {
- proj: { selected: { $set: selected } }
- });
- }
- if (parameters.ca !== undefined) {
- stateFromLocation = update(stateFromLocation, {
- compare: { active: { $set: true } }
- });
- if (!parameters.l1) {
- stateFromLocation = update(stateFromLocation, {
- layers: { activeB: { $set: stateFromLocation.layers.active } }
- });
- }
- } else {
- stateFromLocation = update(stateFromLocation, {
- compare: { $set: initialCompareState }
- });
- }
- // legacy layers permalink
- if (state.parameters.product) {
- stateFromLocation = update(stateFromLocation, {
- layers: {
- active: {
- $set: layersParse11(state.parameters.product, config)
- }
- }
- });
- }
- if (parameters.l1 || parameters.l) {
- stateFromLocation = loadPalettes(
- parameters,
- lodashAssign({}, stateFromLocation, {
- palettes: state.palettes,
- config
- })
- );
- }
+ stateFromLocation = mapLocationToProjState(
+ parameters,
+ stateFromLocation,
+ state
+ );
+ stateFromLocation = mapLocationToLayerState(
+ parameters,
+ stateFromLocation,
+ state,
+ config
+ );
+ stateFromLocation = mapLocationToCompareState(
+ parameters,
+ stateFromLocation
+ );
+ stateFromLocation = mapLocationToPaletteState(
+ parameters,
+ stateFromLocation,
+ state
+ );
// one level deep merge
for (var key in stateFromLocation) {
const obj = lodashAssign({}, state[key], stateFromLocation[key]);
@@ -158,28 +138,12 @@ const getParameters = function(config, parameters) {
},
cm: {
stateKey: 'compare.mode',
- initialState: 'swipe',
- options: {
- parse: str => {
- // if (parameters.ca !== undefined) {
- return str;
- // }
- // return 'swipe';
- }
- }
+ initialState: 'swipe'
},
cv: {
stateKey: 'compare.value',
initialState: 50,
- type: 'number',
- options: {
- parse: num => {
- // if (parameters.ca !== undefined) {
- return num;
- // }
- // return 50;
- }
- }
+ type: 'number'
},
tr: {
stateKey: 'tour.selected',
@@ -215,8 +179,6 @@ export function getParamObject(
legacyState,
errors
) {
- let animationParamObject = {};
-
const mapParamObject = getMapParameterSetup(
parameters,
config,
@@ -224,15 +186,6 @@ export function getParamObject(
legacyState,
errors
);
- if (config.features.animation) {
- // animationParamObject = getAnimationParameterSetup(
- // parameters,
- // config,
- // models,
- // legacyState,
- // errors
- // );
- }
const dateParamObject = getDateParameterSetup(
parameters,
config,
diff --git a/web/js/modules/compare/util.js b/web/js/modules/compare/util.js
index 6d103d030..ac522bdd4 100644
--- a/web/js/modules/compare/util.js
+++ b/web/js/modules/compare/util.js
@@ -1,58 +1,15 @@
-import { cloneDeep as lodashCloneDeep, get } from 'lodash';
+import { initialCompareState } from './reducers';
+import update from 'immutability-helper';
-export function getCompareParameterSetup(
- parameters,
- config,
- models,
- legacyState,
- errors
-) {
- const loadedModel = lodashCloneDeep(models.compare.load(legacyState, errors)); // use legacy parser
- return {
- ca: {
- stateKey: 'legacy.compare.isCompareA',
- initialState: true,
- type: 'bool',
- options: {
- setAsEmptyItem: true,
- serializeNeedsGlobalState: true,
- parse: () => {
- return loadedModel.isCompareA;
- },
- serialize: (currentItemState, state) => {
- const compareIsActive = get(state, 'legacy.compare.active');
- return compareIsActive ? currentItemState : undefined;
- }
- }
- },
- cm: {
- stateKey: 'legacy.compare.mode',
- initialState: 'swipe',
- options: {
- serializeNeedsGlobalState: true,
- parse: () => {
- return loadedModel.mode || 'swipe';
- },
- serialize: (currentItemState, state) => {
- const compareIsActive = get(state, 'legacy.compare.active');
- return compareIsActive ? currentItemState || 'swipe' : undefined;
- }
- }
- },
- cv: {
- stateKey: 'legacy.compare.value',
- initialState: 50,
- type: 'number',
- options: {
- serializeNeedsGlobalState: true,
- parse: () => {
- return loadedModel.value || 50;
- },
- serialize: (currentItemState, state) => {
- const compareIsActive = get(state, 'legacy.compare.active');
- return compareIsActive ? currentItemState || 50 : undefined;
- }
- }
- }
- };
+export function mapLocationToCompareState(parameters, stateFromLocation) {
+ if (parameters.ca !== undefined) {
+ stateFromLocation = update(stateFromLocation, {
+ compare: { active: { $set: true } }
+ });
+ } else {
+ stateFromLocation = update(stateFromLocation, {
+ compare: { $set: initialCompareState }
+ });
+ }
+ return stateFromLocation;
}
diff --git a/web/js/modules/layers/util.js b/web/js/modules/layers/util.js
index e863440fe..47b0032fe 100644
--- a/web/js/modules/layers/util.js
+++ b/web/js/modules/layers/util.js
@@ -6,7 +6,6 @@ import {
findIndex as lodashFindIndex,
each as lodashEach,
isNaN as lodashIsNaN,
- assign as lodashAssign,
isArray
} from 'lodash';
@@ -376,3 +375,26 @@ export function validate(errors, config) {
}
});
}
+export function mapLocationToLayerState(
+ parameters,
+ stateFromLocation,
+ state,
+ config
+) {
+ if (!parameters.l1 && parameters.ca !== undefined) {
+ stateFromLocation = update(stateFromLocation, {
+ layers: { activeB: { $set: stateFromLocation.layers.active } }
+ });
+ }
+ // legacy layers permalink
+ if (state.parameters.product) {
+ stateFromLocation = update(stateFromLocation, {
+ layers: {
+ active: {
+ $set: layersParse11(state.parameters.product, config)
+ }
+ }
+ });
+ }
+ return stateFromLocation;
+}
diff --git a/web/js/modules/palettes/util.js b/web/js/modules/palettes/util.js
index a969e9dac..17a6e1fcb 100644
--- a/web/js/modules/palettes/util.js
+++ b/web/js/modules/palettes/util.js
@@ -311,3 +311,20 @@ export function loadPalettes(permlinkState, state) {
});
return state;
}
+export function mapLocationToPaletteState(
+ parameters,
+ stateFromLocation,
+ state,
+ config
+) {
+ if (parameters.l1 || parameters.l) {
+ stateFromLocation = loadPalettes(
+ parameters,
+ lodashAssign({}, stateFromLocation, {
+ palettes: state.palettes,
+ config
+ })
+ );
+ }
+ return stateFromLocation;
+}
diff --git a/web/js/modules/projection/util.js b/web/js/modules/projection/util.js
index b5b84111f..f4e28fe74 100644
--- a/web/js/modules/projection/util.js
+++ b/web/js/modules/projection/util.js
@@ -1,4 +1,6 @@
import { get as lodashGet } from 'lodash';
+import update from 'immutability-helper';
+
export function getProjInitialState(config) {
const selected = lodashGet(
config,
@@ -9,3 +11,13 @@ export function getProjInitialState(config) {
selected: selected || {}
};
}
+export function mapLocationToProjState(parameters, stateFromLocation, state) {
+ const projId = lodashGet(stateFromLocation, 'proj.id');
+ if (projId) {
+ let selected = lodashGet(state, `config.projections.${projId}`) || {};
+ stateFromLocation = update(stateFromLocation, {
+ proj: { selected: { $set: selected } }
+ });
+ }
+ return stateFromLocation;
+}