Interactive interface for browsing global, full-resolution satellite imagery
Use rebux state to manage proj and layer permalinks wip #1623
Benjamin King4fcbe2203ca6
diff --git a/web/js/components/sidebar/products/layer-list.js b/web/js/components/sidebar/products/layer-list.jsindex 41266c736..cd06fb890 100644--- a/web/js/components/sidebar/products/layer-list.js+++ b/web/js/components/sidebar/products/layer-list.js@@ -169,7 +169,7 @@ function mapStateToProps(state, ownProps) {const { proj } = state;const { runningLayers } = state.layers;const { id } = proj;- const zoom = state.legacy.map.getZoom();+ const zoom = state.models.map.getZoom();const zots = getZotsForActiveLayers(layers, zoom) || {};const activeLayers = state.layers[layerGroupName];diff --git a/web/js/containers/projection.js b/web/js/containers/projection.jsindex e3c6496dd..caeeddf58 100644--- a/web/js/containers/projection.js+++ b/web/js/containers/projection.js@@ -29,8 +29,7 @@ const infoArray = [class ProjectionList extends Component {updateProjection(id) {- const { updateProjection, models, config, onCloseModal } = this.props;- models.proj.select(id); // migration crutch+ const { updateProjection, config, onCloseModal } = this.props;updateProjection(id, config);onCloseModal();googleTagManager.pushEvent({@@ -59,7 +58,7 @@ function mapStateToProps(state) {}const mapDispatchToProps = (dispatch, ownProps) => ({updateProjection: (id, config) => {- dispatch(changeProjection(id, config));+ dispatch(changeProjection(id));},onCloseModal: () => {dispatch(onToggle());diff --git a/web/js/location.js b/web/js/location.jsindex 65cb8e089..b7c1fb57d 100644--- a/web/js/location.js+++ b/web/js/location.js@@ -1,26 +1,26 @@-import { merge as lodashMerge, assign, has, set } from 'lodash';+import { assign, has, set, get } from 'lodash';// legacy crutchesimport { getLayersParameterSetup } from './modules/layers/util';import { getDateParameterSetup } from './modules/date/util';import { getNaturalEventsParameterSetup } from './modules/natural-events/util';import { getDataDownloadParameterSetup } from './modules/data/util';-import { getCompareParameterSetup } from './modules/compare/util';import { getAnimationParameterSetup } from './modules/animation/util';import { getTourParameterSetup } from './modules/tour/util';import { getMapParameterSetup } from './modules/map/util';-import util from './util/util';+import update from 'immutability-helper';export function mapLocationToState(state, location) {if (location.search) {- let parameters = util.fromQueryString(location.search);let stateFromLocation = location.query;- if (has(parameters, 'ca')) {- set(stateFromLocation, 'legacy.compare.active', true);- } else {- set(stateFromLocation, 'legacy.compare.active', false);+ const projId = get(stateFromLocation, 'proj.id');+ if (projId) {+ let selected = get(state, `config.projections.${projId}`) || {};+ stateFromLocation = update(stateFromLocation, {+ proj: { selected: { $set: selected } }+ });}- return lodashMerge({}, state, stateFromLocation);+ return update(state, { $merge: stateFromLocation });} else return state;}const simpleParameters = {@@ -29,6 +29,29 @@ const simpleParameters = {initialState: 'geographic'}};+// ca: {+// stateKey: 'compare.isCompareA',+// initialState: true,+// type: 'bool',+// options: {+// setAsEmptyItem: true,+// serializeNeedsGlobalState: true,+// serialize: (currentItemState, state) => {+// const compareIsActive = get(state, 'compare.active');+// return compareIsActive ? currentItemState : undefined;+// }+// }+// },+// cm: {+// stateKey: 'compare.mode',+// initialState: 'swipe'+// },+// cv: {+// stateKey: 'compare.value',+// initialState: 50,+// type: 'number'+// }+// };export function getParamObject(parameters,config,@@ -36,7 +59,6 @@ export function getParamObject(legacyState,errors) {- let compareParamObject = {};let eventParamObject = {};let tourParamObject = {};let animationParamObject = {};@@ -48,15 +70,6 @@ export function getParamObject(legacyState,errors);- if (config.features.compare) {- compareParamObject = getCompareParameterSetup(- parameters,- config,- models,- legacyState,- errors- );- }if (config.features.tour) {tourParamObject = getTourParameterSetup(parameters,@@ -108,15 +121,14 @@ export function getParamObject(const obj = assign({},- simpleParameters, // Once migrated completely to redux most configurations should be heredateParamObject,layersParamObject,animationParamObject,eventParamObject,dataParamObject,- compareParamObject,tourParamObject,- mapParamObject+ mapParamObject,+ simpleParameters);return {global: objdiff --git a/web/js/main.js b/web/js/main.jsindex 59fc6b752..9aac9e53e 100644--- a/web/js/main.js+++ b/web/js/main.js@@ -30,7 +30,7 @@ import { polyfill } from './polyfill';import { debugConfig } from './debug';import { changeProjection } from './modules/projection/actions';-export const history = createBrowserHistory();+export let history = createBrowserHistory();const isDebugMode = typeof DEBUG !== 'undefined';const configURI = Brand.url('config/wv.json');const startTime = new Date().getTime();@@ -90,8 +90,8 @@ const render = (config, parameters, legacyState) => {reducersWithLocation,getInitialState(models, config, parameters),compose(- responsiveStoreEnhancer,- applyMiddleware(...middleware)+ applyMiddleware(...middleware),+ responsiveStoreEnhancer));lodashEach(models, function(component, key) {@@ -104,13 +104,6 @@ const render = (config, parameters, legacyState) => {// sync old and new statecomponent.events.any(dispatchUpdate);});- // Big HACKY sync up of proj state- models.proj.events.on('select', (projObj, id) => {- const state = store.getState();- if (state.proj.id !== id) {- store.dispatch(changeProjection(id, config));- }- });listenForHistoryChange(store, history);elapsed('Render', startTime, parameters);diff --git a/web/js/map/ui.js b/web/js/map/ui.jsindex 662f70986..098158d80 100644--- a/web/js/map/ui.js+++ b/web/js/map/ui.js@@ -484,9 +484,9 @@ export function mapui(models, config, store) {*/var removeLayer = function(action) {const state = store.getState();- const { compare, layers, proj } = state;+ const { compare, proj } = state;const activeLayerStr = compare.isActiveA ? 'active' : 'activeB';- const def = lodashFind(layers[activeLayerStr], { id: action.id });+ const def = action.def;if (isGraticule(def, proj.id)) {removeGraticule(activeLayerStr);diff --git a/web/js/modules/layers/actions.js b/web/js/modules/layers/actions.jsindex b15f4c1c2..55add9b25 100644--- a/web/js/modules/layers/actions.js+++ b/web/js/modules/layers/actions.js@@ -92,10 +92,11 @@ export function toggleVisibility(id, visible) {export function removeLayer(id) {return (dispatch, getState) => {const { layers, compare } = getState();- const activeString = compare.isActiveA ? 'active' : 'activeB';+ const activeString = compare.activeString;const index = lodashFindIndex(layers[activeString], {id: id});+ const def = layers[activeString][index];if (index === -1) {throw new Error('Invalid layer ID: ' + id);}@@ -104,7 +105,8 @@ export function removeLayer(id) {type: REMOVE_LAYER,id,index,- activeString+ activeString,+ def});};}diff --git a/web/js/modules/layers/util.js b/web/js/modules/layers/util.jsindex b0926f568..a4742d527 100644--- a/web/js/modules/layers/util.js+++ b/web/js/modules/layers/util.js@@ -16,6 +16,7 @@ export function getLayersParameterSetup(errors) {const initialState = lodashCloneDeep(models.layers.active);+ console.log(legacyState);models.palettes.load(legacyState, errors);if (models.compare.active) {models.layers.activeB = lodashCloneDeep(initialState);@@ -25,38 +26,35 @@ export function getLayersParameterSetup(return {l: getPermalinkManagementObject(initialState,- 'legacy.layers.active',+ 'layers.active',() => {return layerModelLoaded.active ? layerModelLoaded.active : initialState;},(currentItemState, state) => {- const isActive = lodashGet(models, 'compare.active');- const isCompareA = lodashGet(models, 'compare.isCompareA');- const layersB = lodashGet(models, 'layers.activeB');- const layersA = lodashGet(models, 'layers.active');+ const isActive = lodashGet(state, 'compare.active');+ const isCompareA = lodashGet(state, 'compare.isActiveA');return !isActive && !isCompareA- ? serializeLayers(layersB, models, 'activeB')- : serializeLayers(layersA, models, 'active');+ ? serializeLayers(state, 'activeB')+ : serializeLayers(state, 'active');}),l1: getPermalinkManagementObject(initialState,- 'legacy.layers.activeB',+ 'layers.activeB',() => {return layerModelLoaded.activeB;},(currentItemState, state) => {- const isActive = lodashGet(models, 'compare.active');- const layersB = lodashGet(models, 'layers.activeB');- return isActive- ? serializeLayers(layersB, models, 'activeB')- : undefined;+ const isActive = lodashGet(state, 'compare.active');+ return isActive ? serializeLayers(state, 'activeB') : undefined;})};}-function serializeLayers(layers, models, groupStr) {- return layers.map(def => {+export function serializeLayers(state, groupName) {+ const layers = state.layers[groupName];+ const palettes = state.palettes[groupName];+ return layers.map((def, i) => {var item = {};if (def.id) {@@ -78,32 +76,29 @@ function serializeLayers(layers, models, groupStr) {value: def.opacity});}- let def1 =- def.id && def.palette- ? models.palettes.get(def.id, undefined, groupStr)- : undefined;- if (def1) {- if (def1.custom) {+ let paletteDef = def.id && palettes[def.id] ? palettes[def.id] : undefined;+ if (paletteDef) {+ if (paletteDef.custom) {item.attributes.push({id: 'palette',- value: def1.custom+ value: paletteDef.custom});}- if (def1.min) {- var minValue = def1.entries.values[def1.min];+ if (paletteDef.min) {+ var minValue = paletteDef.entries.values[paletteDef.min];item.attributes.push({id: 'min',value: minValue});}- if (def1.max) {- var maxValue = def1.entries.values[def1.max];+ if (paletteDef.max) {+ var maxValue = paletteDef.entries.values[paletteDef.max];item.attributes.push({id: 'max',value: maxValue});}- if (def1.squash) {+ if (paletteDef.squash) {item.attributes.push({id: 'squash'});diff --git a/web/js/modules/projection/actions.js b/web/js/modules/projection/actions.jsindex 1382b085f..885c9502e 100644--- a/web/js/modules/projection/actions.js+++ b/web/js/modules/projection/actions.js@@ -1,13 +1,17 @@import { CHANGE_PROJECTION } from './constants';-export function changeProjection(id, config) {- const proj = config.projections[id];- if (!proj) {- throw new Error('Invalid projection: ' + id);- }- return {- type: CHANGE_PROJECTION,- id: id,- selected: proj+export function changeProjection(id) {+ return (dispatch, getState) => {+ const { config } = getState();+ const proj = config.projections[id];++ if (!proj) {+ throw new Error('Invalid projection: ' + id);+ }+ dispatch({+ type: CHANGE_PROJECTION,+ id: id,+ selected: proj+ });};}diff --git a/web/js/modules/projection/util.js b/web/js/modules/projection/util.jsindex a1db23b21..b5b84111f 100644--- a/web/js/modules/projection/util.js+++ b/web/js/modules/projection/util.js@@ -1,15 +1,11 @@+import { get as lodashGet } from 'lodash';export function getProjInitialState(config) {- const defaults = config.defaults;- const projections = config.projections;-+ const selected = lodashGet(+ config,+ `projections.${config.defaults.projection}`+ );return {- id: 'geographic',- selected:- defaults &&- projections &&- defaults.projection &&- projections[defaults.projection]- ? projections[defaults.projection]- : {}+ id: selected ? selected.id : 'geographic',+ selected: selected || {}};}