Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

some cleanup, remove unused functions, reorder

Jason Kentac6168719328
diff --git a/web/js/containers/sidebar/layer-list.js b/web/js/containers/sidebar/layer-list.js
index ab60b1952..effe6ebdf 100644
--- a/web/js/containers/sidebar/layer-list.js
+++ b/web/js/containers/sidebar/layer-list.js
@@ -8,7 +8,7 @@ import {
replaceSubGroup,
getZotsForActiveLayers,
getTitles,
- available,
+ available as availableSelector,
} from '../../modules/layers/selectors';
import { reorderLayers } from '../../modules/layers/actions';
@@ -135,6 +135,7 @@ class LayerList extends React.Component {
zot={zots[object.id] ? zots[object.id].value : null}
names={getNames(object.id)}
checkerBoardPattern={checkerBoardPattern}
+ // TODO probably shouldn't call this function on every render?
isDisabled={!available(object.id)}
isVisible={object.visible}
runningObject={
@@ -179,7 +180,7 @@ function mapStateToProps(state, ownProps) {
layerSplit,
} = ownProps;
const {
- proj, compare, config, map,
+ proj, compare, config, map, date,
} = state;
const { runningLayers } = state.layers;
const { id } = proj;
@@ -199,10 +200,10 @@ function mapStateToProps(state, ownProps) {
projId: id,
checkerBoardPattern,
layerSplit,
- getNames: (layerId) => getTitles(state.config, layerId, id),
- available: (id) => {
- const date = state.date[activeDateString];
- return available(id, date, layers, state.config, state);
+ getNames: (layerId) => getTitles(config, layerId, id),
+ available: (layerId) => {
+ const currentDate = date[activeDateString];
+ return availableSelector(layerId, currentDate, layers, config);
},
};
}
diff --git a/web/js/containers/sidebar/layer.js b/web/js/containers/sidebar/layer.js
index 2793b4f54..a6bc18b33 100644
--- a/web/js/containers/sidebar/layer.js
+++ b/web/js/containers/sidebar/layer.js
@@ -1,3 +1,4 @@
+/* eslint-disable no-nested-ternary */
import React from 'react';
import PropTypes from 'prop-types';
import { Draggable } from 'react-beautiful-dnd';
diff --git a/web/js/containers/timeline/timeline.js b/web/js/containers/timeline/timeline.js
index f80a1d810..67e48660e 100644
--- a/web/js/containers/timeline/timeline.js
+++ b/web/js/containers/timeline/timeline.js
@@ -35,7 +35,6 @@ import {
} from '../../components/timeline/date-util';
import {
hasSubDaily,
- lastDate as layersLastDateTime,
} from '../../modules/layers/selectors';
import {
selectDate,
@@ -1379,7 +1378,7 @@ function mapStateToProps(state) {
customInterval,
} = date;
const { screenWidth, lessThan } = browser;
- const { isCompareA, activeString } = compare;
+ const { isCompareA } = compare;
const isCompareModeActive = compare.active;
const { isDistractionFreeModeActive } = ui;
const isSmallScreen = lessThan.medium;
@@ -1390,7 +1389,9 @@ function mapStateToProps(state) {
const activeLayers = layers[compare.activeString];
const projection = proj.id;
const activeLayersFiltered = activeLayers.filter((layer) => layer.startDate && layer.projections[projection]);
- let hasSubdailyLayers = hasSubDaily(activeLayers);
+ const hasSubdailyLayers = isCompareModeActive
+ ? hasSubDaily(layers.active) || hasSubDaily(layers.activeB)
+ : hasSubDaily(activeLayers);
let updatedInterval = interval;
let updatedCustomInterval = customInterval;
@@ -1408,21 +1409,11 @@ function mapStateToProps(state) {
}
}
- let endTime;
- if (isCompareModeActive) {
- hasSubdailyLayers = hasSubDaily(layers.active) || hasSubDaily(layers.activeB);
- endTime = getEndTime(layers, config);
- } else {
- hasSubdailyLayers = hasSubDaily(activeLayers);
- endTime = layersLastDateTime(layers[activeString], config);
- }
- endTime = appNow;
-
const dimensionsAndOffsetValues = getOffsetValues(
screenWidth,
hasSubdailyLayers,
);
- const timelineEndDateLimit = getISODateFormatted(endTime);
+ const timelineEndDateLimit = getISODateFormatted(appNow);
const selectedDate = isCompareA ? selected : selectedB;
const deltaChangeAmt = customSelected ? customDelta : delta;
@@ -1592,11 +1583,6 @@ const getOffsetValues = (innerWidth, hasSubDaily) => {
return { width, parentOffset };
};
-const getEndTime = (layers, config) => {
- const endDateA = layersLastDateTime(layers.active, config);
- const endDateB = layersLastDateTime(layers.activeB, config);
- return endDateA > endDateB ? endDateA : endDateB;
-};
/**
* @param {Number} delta Date and direction to change
* @param {Number} increment Zoom level of change
diff --git a/web/js/modules/layers/selectors.js b/web/js/modules/layers/selectors.js
index f5ef4a591..d952dcfdc 100644
--- a/web/js/modules/layers/selectors.js
+++ b/web/js/modules/layers/selectors.js
@@ -88,19 +88,14 @@ export function hasSubDaily(layers) {
return false;
}
-export function addLayer(id, spec, layers, layerConfig, overlayLength, projection) {
- layers = lodashCloneDeep(layers);
+export function addLayer(id, spec = {}, layersParam, layerConfig, overlayLength, projection) {
+ let layers = lodashCloneDeep(layersParam);
if (projection) {
layers = layers.filter((layer) => layer.projections[projection]);
}
- if (
- lodashFind(layers, {
- id,
- })
- ) {
+ if (lodashFind(layers, { id })) {
return layers;
}
- spec = spec || {};
const def = lodashCloneDeep(layerConfig[id]);
if (!def) {
throw new Error(`No such layer: ${id}`);
@@ -150,8 +145,9 @@ export function resetLayers(startingLayers, layerConfig) {
*/
export function getTitles(config, layerId, projId) {
try {
- let title; let subtitle; let
- tags;
+ let title;
+ let subtitle;
+ let tags;
const forProj = lodashGet(
config,
`layers.${layerId}.projections.${projId}`,
@@ -182,8 +178,7 @@ export function getTitles(config, layerId, projId) {
* @param {*} spec
* @param {*} state
*/
-export function getLayers(layers, spec, state) {
- spec = spec || {};
+export function getLayers(layers, spec = {}, state) {
const baselayers = forGroup('baselayers', spec, layers, state);
const overlays = forGroup('overlays', spec, layers, state);
@@ -202,8 +197,7 @@ export function getLayers(layers, spec, state) {
return baselayers.concat(overlays);
}
-function forGroup(group, spec, activeLayers, state) {
- spec = spec || {};
+function forGroup(group, spec = {}, activeLayers, state) {
const projId = spec.proj || state.proj.id;
let results = [];
const defs = lodashFilter(activeLayers, {
@@ -237,106 +231,82 @@ function forGroup(group, spec, activeLayers, state) {
return results;
}
-/**
- * Determine if a given layer is available
- * @param {*} id
- * @param {*} date
- * @param {*} layers
- * @param {*} config
- */
-export function available(id, date, layers, config) {
- const range = dateRange({ layer: id }, layers, config);
- if (range && (date < range.start || date > range.end)) {
- return false;
- }
- return true;
-}
-
-export function replaceSubGroup(
- layerId,
- nextLayerId,
- layers,
- subGroup,
- layerSplit,
-) {
- if (nextLayerId) {
- return moveBefore(layerId, nextLayerId, layers);
- }
- return pushToBottom(layerId, layers, layerSplit);
-}
-
/**
* Determine date range for layers
* @param {*} spec
* @param {*} activeLayers
* @param {*} config
*/
-export function dateRange(spec, activeLayers, config) {
- const layers = spec.layer
- ? [lodashFind(activeLayers, { id: spec.layer })]
- : activeLayers;
- const ignoreRange = config.parameters && (config.parameters.debugGIBS || config.parameters.ignoreDateRange);
+function dateRange(id, activeLayers, config) {
+ const ignoreRange = config.parameters
+ && (config.parameters.debugGIBS
+ || config.parameters.ignoreDateRange);
if (ignoreRange) {
return {
start: new Date(Date.UTC(1970, 0, 1)),
end: util.now(),
};
}
+
let min = Number.MAX_VALUE;
let max = 0;
let range = false;
const maxDates = [];
-
// Use the minute ceiling of the current time so that we don't run into an issue where
// seconds value of current appNow time is greater than a layer's available time range
const minuteCeilingCurrentTime = util.now().setSeconds(59);
+ const layers = id ? [lodashFind(activeLayers, { id })] : activeLayers;
- lodashEach(layers, (def) => {
- if (def) {
- if (def.startDate) {
- range = true;
- const start = util.parseDateUTC(def.startDate).getTime();
- min = Math.min(min, start);
- }
- // For now, we assume that any layer with an end date is
- // an ongoing product unless it is marked as inactive.
- if (def.futureLayer && def.endDate) {
- range = true;
- max = util.parseDateUTC(def.endDate).getTime();
+ layers.forEach((def) => {
+ if (!def) {
+ return;
+ }
+ if (def.startDate) {
+ range = true;
+ const start = util.parseDateUTC(def.startDate).getTime();
+ min = Math.min(min, start);
+ }
+
+ // For now, we assume that any layer with an end date is
+ // an ongoing product unless it is marked as inactive.
+ if (def.futureLayer && def.endDate) {
+ range = true;
+ max = util.parseDateUTC(def.endDate).getTime();
+ maxDates.push(new Date(max));
+ } else if (def.inactive && def.endDate) {
+ range = true;
+ const end = util.parseDateUTC(def.endDate).getTime();
+ max = Math.max(max, end);
+ maxDates.push(new Date(max));
+ } else if (def.endDate) {
+ range = true;
+ max = minuteCeilingCurrentTime;
+ maxDates.push(new Date(max));
+ }
+
+ // If there is a start date but no end date, this is a
+ // product that is currently being created each day, set
+ // the max day to today.
+ if (def.futureLayer && def.futureTime && !def.endDate) {
+ // Calculate endDate + parsed futureTime from layer JSON
+ max = new Date();
+ const { futureTime } = def;
+ const dateType = futureTime.slice(-1);
+ const dateInterval = futureTime.slice(0, -1);
+
+ if (dateType === 'D') {
+ max.setDate(max.getDate() + parseInt(dateInterval, 10));
maxDates.push(new Date(max));
- } else if (def.inactive && def.endDate) {
- range = true;
- const end = util.parseDateUTC(def.endDate).getTime();
- max = Math.max(max, end);
+ } else if (dateType === 'M') {
+ max.setMonth(max.getMonth() + parseInt(dateInterval, 10));
maxDates.push(new Date(max));
- } else if (def.endDate) {
- range = true;
- max = minuteCeilingCurrentTime;
- maxDates.push(new Date(max));
- }
- // If there is a start date but no end date, this is a
- // product that is currently being created each day, set
- // the max day to today.
- if (def.futureLayer && def.futureTime && !def.endDate) {
- // Calculate endDate + parsed futureTime from layer JSON
- max = new Date();
- const { futureTime } = def;
- const dateType = futureTime.slice(-1);
- const dateInterval = futureTime.slice(0, -1);
- if (dateType === 'D') {
- max.setDate(max.getDate() + parseInt(dateInterval, 10));
- maxDates.push(new Date(max));
- } else if (dateType === 'M') {
- max.setMonth(max.getMonth() + parseInt(dateInterval, 10));
- maxDates.push(new Date(max));
- } else if (dateType === 'Y') {
- max.setYear(max.getYear() + parseInt(dateInterval, 10));
- maxDates.push(new Date(max));
- }
- } else if (def.startDate && !def.endDate) {
- max = minuteCeilingCurrentTime;
+ } else if (dateType === 'Y') {
+ max.setYear(max.getYear() + parseInt(dateInterval, 10));
maxDates.push(new Date(max));
}
+ } else if (def.startDate && !def.endDate) {
+ max = minuteCeilingCurrentTime;
+ maxDates.push(new Date(max));
}
});
@@ -353,45 +323,19 @@ export function dateRange(spec, activeLayers, config) {
}
}
-export function pushToBottom(id, layers, layerSplit) {
- const decodedId = util.decodeId(id);
- const oldIndex = lodashFindIndex(layers, {
- id: decodedId,
- });
- if (oldIndex < 0) {
- throw new Error(`Layer is not active: ${decodedId}`);
- }
- const def = layers[oldIndex];
- layers.splice(oldIndex, 1);
- if (def.group === 'baselayers') {
- layers.push(def);
- } else {
- layers.splice(layerSplit - 1, 0, def);
- }
- return layers;
-}
-
-export function moveBefore(sourceId, targetId, layers) {
- const decodedId = util.decodeId(sourceId);
- let sourceIndex = lodashFindIndex(layers, {
- id: decodedId,
- });
- if (sourceIndex < 0) {
- throw new Error(`Layer is not active: ${decodedId}`);
- }
- const sourceDef = layers[sourceIndex];
- const targetIndex = lodashFindIndex(layers, {
- id: targetId,
- });
- if (targetIndex < 0) {
- throw new Error(`Layer is not active: ${targetId}`);
- }
- layers.splice(targetIndex, 0, sourceDef);
- if (sourceIndex > targetIndex) {
- sourceIndex += 1;
+/**
+ * Determine if a given layer is available
+ * @param {*} id
+ * @param {*} date
+ * @param {*} layers
+ * @param {*} config
+ */
+export function available(id, date, layers, config) {
+ const range = dateRange(id, layers, config);
+ if (range && (date < range.start || date > range.end)) {
+ return false;
}
- layers.splice(sourceIndex, 1);
- return layers;
+ return true;
}
/**
@@ -434,30 +378,6 @@ export function isRenderable(id, activeLayers, date, state) {
return !obscured;
}
-export function lastDate(activeLayers, config) {
- let endDate;
- const layersDateRange = dateRange({}, activeLayers, config);
- const today = util.today();
- if (layersDateRange && layersDateRange.end > today) {
- endDate = layersDateRange.end;
- } else {
- endDate = today;
- }
- return endDate;
-}
-
-export function lastDateTime(activeLayers, config) {
- let endDate;
- const layersDateRange = dateRange({}, activeLayers, config);
- const now = util.now();
- if (layersDateRange && layersDateRange.end > now) {
- endDate = layersDateRange.end;
- } else {
- endDate = now;
- }
- return endDate;
-}
-
export function activateLayersForEventCategory(activeLayers, state) {
const { layers, compare } = state;
// Turn off all layers in list first
@@ -489,6 +409,60 @@ export function activateLayersForEventCategory(activeLayers, state) {
return newLayers;
}
+export function pushToBottom(id, layers, layerSplit) {
+ const decodedId = util.decodeId(id);
+ const oldIndex = lodashFindIndex(layers, {
+ id: decodedId,
+ });
+ if (oldIndex < 0) {
+ throw new Error(`Layer is not active: ${decodedId}`);
+ }
+ const def = layers[oldIndex];
+ layers.splice(oldIndex, 1);
+ if (def.group === 'baselayers') {
+ layers.push(def);
+ } else {
+ layers.splice(layerSplit - 1, 0, def);
+ }
+ return layers;
+}
+
+export function moveBefore(sourceId, targetId, layers) {
+ const decodedId = util.decodeId(sourceId);
+ let sourceIndex = lodashFindIndex(layers, {
+ id: decodedId,
+ });
+ if (sourceIndex < 0) {
+ throw new Error(`Layer is not active: ${decodedId}`);
+ }
+ const sourceDef = layers[sourceIndex];
+ const targetIndex = lodashFindIndex(layers, {
+ id: targetId,
+ });
+ if (targetIndex < 0) {
+ throw new Error(`Layer is not active: ${targetId}`);
+ }
+ layers.splice(targetIndex, 0, sourceDef);
+ if (sourceIndex > targetIndex) {
+ sourceIndex += 1;
+ }
+ layers.splice(sourceIndex, 1);
+ return layers;
+}
+
+export function replaceSubGroup(
+ layerId,
+ nextLayerId,
+ layers,
+ subGroup,
+ layerSplit,
+) {
+ if (nextLayerId) {
+ return moveBefore(layerId, nextLayerId, layers);
+ }
+ return pushToBottom(layerId, layers, layerSplit);
+}
+
export function getZotsForActiveLayers(config, projection, map, activeLayers) {
const zotObj = {};
const { sources } = config;