Interactive interface for browsing global, full-resolution satellite imagery
Remove layer state mutations #1314
Benjamin King624757582e48
diff --git a/web/js/map/layerbuilder.js b/web/js/map/layerbuilder.jsindex e1e52595b..63e931dfd 100644--- a/web/js/map/layerbuilder.js+++ b/web/js/map/layerbuilder.js@@ -88,7 +88,8 @@ export function mapLayerBuilder(models, config, cache, ui, store) {const activeDateStr = state.compare.isCompareA ? 'selected' : 'selectedB';options = options || {};const group = options.group || null;- let date = self.closestDate(def, options);+ const { closestDate, nextDate, previousDate } = self.getRequestDates(def, options);+ let date = closestDate;if (date) {options.date = date;}@@ -106,7 +107,9 @@ export function mapLayerBuilder(models, config, cache, ui, store) {date,proj: proj.id,def,- group+ group,+ nextDate,+ previousDate};def = lodashCloneDeep(def);lodashMerge(def, def.projections[proj.id]);@@ -140,30 +143,39 @@ export function mapLayerBuilder(models, config, cache, ui, store) {* @param {object} options Layer options* @return {object} Closest date*/- self.closestDate = function(def, options) {+ self.getRequestDates = function(def, options) {const state = store.getState();const activeDateStr = state.compare.isCompareA ? 'selected' : 'selectedB';const stateCurrentDate = new Date(state.date[activeDateStr]);+ const previousLayer = options.previousLayer || {};let date = options.date || stateCurrentDate;let previousDateFromRange;+ let previousLayerDate = previousLayer.previousDate;+ let nextLayerDate = previousLayer.nextDate;if (!state.animation.isPlaying) {// need to get previous available date to prevent unecessary requestslet dateRange;// let previousDateFromRange;- if (def.previousDate && def.nextDate) {+ if (previousLayer.previousDate && previousLayer.nextDate) {const dateTime = date.getTime();- const previousDateTime = def.previousDate.getTime();- const nextDateTime = def.nextDate.getTime();+ const previousDateTime = previousLayer.previousDate.getTime();+ const nextDateTime = previousLayer.nextDate.getTime();// if current date is outside previous and next dates avaiable, recheck rangeif (dateTime <= previousDateTime || dateTime >= nextDateTime) {dateRange = datesinDateRanges(def, date);- previousDateFromRange = prevDateInDateRange(def, date, dateRange);+ const { next, previous } = prevDateInDateRange(def, date, dateRange);+ previousDateFromRange = previous;+ previousLayerDate = previous;+ nextLayerDate = next;} else {previousDateFromRange = def.previousDate;}} else {dateRange = datesinDateRanges(def, date);- previousDateFromRange = prevDateInDateRange(def, date, dateRange);+ const { next, previous } = prevDateInDateRange(def, date, dateRange);+ previousDateFromRange = previous;+ previousLayerDate = previous;+ nextLayerDate = next;}}@@ -179,7 +191,7 @@ export function mapLayerBuilder(models, config, cache, ui, store) {}}- return date;+ return { closestDate: date, previousDate: previousLayerDate, nextDate: nextLayerDate };};/**@@ -204,7 +216,7 @@ export function mapLayerBuilder(models, config, cache, ui, store) {// every date.if (def.period) {date = util.toISOStringSeconds(- util.roundTimeOneMinute(self.closestDate(def, options))+ util.roundTimeOneMinute(self.getRequestDates(def, options).closestDate));}if (isPaletteActive(def.id, activeGroupStr, state)) {@@ -293,7 +305,7 @@ export function mapLayerBuilder(models, config, cache, ui, store) {}let date = options.date || state.date[activeDateStr];if (def.period === 'subdaily') {- date = self.closestDate(def, options);+ date = self.getRequestDates(def, options).closestDate;date = new Date(date.getTime());}if (day && def.period !== 'subdaily') {diff --git a/web/js/map/ui.js b/web/js/map/ui.jsindex 9550ba5d0..93ae7f8ed 100644--- a/web/js/map/ui.js+++ b/web/js/map/ui.js@@ -48,7 +48,6 @@ import {getLayers,isRenderable as isRenderableLayer} from '../modules/layers/selectors';-import { datesinDateRanges } from '../modules/layers/util';import { CLEAR_ROTATE, RENDERED, UPDATE_MAP_UI, FITTED_TO_LEADING_EXTENT } from '../modules/map/constants';import { getLeadingExtent } from '../modules/map/util';@@ -99,7 +98,7 @@ export function mapui(models, config, store, ui) {const subscribeToStore = function(action) {switch (action.type) {case layerConstants.ADD_LAYER: {- const def = lodashFind(action.layers, { id: action.id });+ const def = lodashCloneDeep(lodashFind(action.layers, { id: action.id }));return addLayer(def);}case CLEAR_ROTATE:@@ -539,7 +538,6 @@ export function mapui(models, config, store, ui) {if (isGraticule(def, proj.id)) {addGraticule(def.opacity, activeLayerStr);} else {- def.availableDates = datesinDateRanges(def, date);if (firstLayer && firstLayer.get('group')) {// Find which map layer-group is the active LayerGroup// and add layer to layerGroup in correct location@@ -634,18 +632,21 @@ export function mapui(models, config, store, ui) {if (compare && compare.active) {if (layerGroup && layerGroup.getLayers().getArray().length) {const index = findLayerIndex(def, layerGroup);+ const layerValue = self.selected.getLayers().getArray()[index];layerGroup.getLayers().setAt(index,createLayer(def, {group: activeLayerStr,- date: state.date[activeDate]+ date: state.date[activeDate],+ previousLayer: layerValue ? layerValue.wv : null}));compareMapUi.update(activeLayerStr);}} else {const index = findLayerIndex(def);- self.selected.getLayers().setAt(index, createLayer(def));+ const layerValue = self.selected.getLayers().getArray()[index];+ self.selected.getLayers().setAt(index, createLayer(def, { previousLayer: layerValue ? layerValue.wv : null }));}if (config.vectorStyles && def.vectorStyle && def.vectorStyle.id) {var vectorStyles = config.vectorStyles;diff --git a/web/js/modules/layers/util.js b/web/js/modules/layers/util.jsindex d50af89c3..f1ba19e68 100644--- a/web/js/modules/layers/util.js+++ b/web/js/modules/layers/util.js@@ -67,8 +67,8 @@ export function prevDateInDateRange(def, date, dateArray) {const currentDateOffsetCheck = new Date(currentDate.getTime() + (currentDate.getTimezoneOffset() * 60000));if (!dateArray ||- (def.period === 'monthly' && isFirstDayOfMonth(currentDateOffsetCheck)) ||- (def.period === 'yearly' && (currentDateOffsetCheck.getDate() === 1 && currentDateOffsetCheck.getMonth() === 0))) {+ (def.period === 'monthly' && isFirstDayOfMonth(currentDateOffsetCheck)) ||+ (def.period === 'yearly' && (currentDateOffsetCheck.getDate() === 1 && currentDateOffsetCheck.getMonth() === 0))) {return date;}@@ -81,13 +81,10 @@ export function prevDateInDateRange(def, date, dateArray) {// use closest date index to find closest date in filtered closestAvailableDatesconst closestDateIndex = closestIndexTo(currentDate, closestAvailableDates);const closestDate = closestAvailableDates[closestDateIndex];- def.previousDate = closestDate || null;// check for potential next date in function passed dateArrayconst nextClosestDate = dateArray[closestDateIndex + 1];- def.nextDate = nextClosestDate || null;-- return closestDate ? new Date(closestDate.getTime()) : date;+ return { previous: closestDate ? new Date(closestDate.getTime()) : date, next: nextClosestDate || null };};/**@@ -131,7 +128,7 @@ export function datesinDateRanges(def, date) {year = new Date(year.getTime() - (year.getTimezoneOffset() * 60000));dateArray.push(year);}- // Monthly layers+ // Monthly layers} else if (def.period === 'monthly') {const maxMonthDate = new Date(maxYear, maxMonth + dateInterval, maxDay);if (currentDate >= minDate && currentDate <= maxMonthDate) {@@ -142,7 +139,7 @@ export function datesinDateRanges(def, date) {month = new Date(month.getTime() - (month.getTimezoneOffset() * 60000));dateArray.push(month);}- // Daily layers+ // Daily layers} else if (def.period === 'daily') {const maxDayDate = new Date(maxYear, maxMonth, maxDay + dateInterval);if (currentDate >= minDate && currentDate <= maxDayDate) {@@ -155,7 +152,7 @@ export function datesinDateRanges(def, date) {day = new Date(day.getTime() - (day.getTimezoneOffset() * 60000));dateArray.push(day);}- // Subdaily layers+ // Subdaily layers} else if (def.period === 'subdaily') {const maxHours = maxDate.getUTCHours();const maxMinutes = maxDate.getUTCMinutes();@@ -300,7 +297,7 @@ export function dateOverlap(period, dateRanges) {previousEnd = new Date(previousEnd.setTime(previousEnd.getTime() +- (previous.dateInterval * 86400000 - 86400000)+ (previous.dateInterval * 86400000 - 86400000)));}