Interactive interface for browsing global, full-resolution satellite imagery
handle tab cycling, break out some validation code
Jason Kentb91d80085eb8
diff --git a/web/js/components/date-selector/date-input-column.js b/web/js/components/date-selector/date-input-column.jsindex 2c84a15f5..1914ae7b2 100644--- a/web/js/components/date-selector/date-input-column.js+++ b/web/js/components/date-selector/date-input-column.js@@ -2,6 +2,7 @@ import React, { Component } from 'react';import PropTypes from 'prop-types';import Arrow from '../util/arrow';import util from '../../util/util';+import { validateBasedOnType, monthStringArray } from './util';/** DateInputColumn used in DateSelector within@@ -17,7 +18,11 @@ class DateInputColumn extends Component {value: '',selected: false,size: null,- lastPosition: subDailyMode ? type === 'minute' : type === 'day',+ isLastPosition: subDailyMode ? type === 'minute' : type === 'day',+ isFirstPosition: type === 'year',+ unitCycle: subDailyMode+ ? ['year', 'month', 'day', 'hour', 'minute']+ : ['year', 'month', 'day'],};}@@ -49,20 +54,11 @@ class DateInputColumn extends Component {});}- /**- * Handle changing to next input position when user hits enter, or- * return to first input when hitting tab/enter on the last input- */- nextTab = () => {- const { lastPosition } = this.state;+ nextInput = () => {+ const { isLastPosition, unitCycle } = this.state;const {- type, subDailyMode, idSuffix, isEndDate, isStartDate,+ type, idSuffix, isEndDate, isStartDate,} = this.props;- const unitCycle = ['year', 'month', 'day'];- if (subDailyMode) {- unitCycle.push('hour');- unitCycle.push('minute');- }const currentPosition = unitCycle.indexOf(type);const nextPosition = currentPosition === unitCycle.length - 1 ? 0 : currentPosition + 1;const nextUnit = unitCycle[nextPosition];@@ -71,12 +67,12 @@ class DateInputColumn extends Component {if (isStartDate) {currentTarget = `${currentTarget}-start`;- nextTarget = lastPosition+ nextTarget = isLastPosition? `#year-${idSuffix}-end`: `${nextTarget}-start`;} else if (isEndDate) {currentTarget = `${currentTarget}-end`;- nextTarget = lastPosition+ nextTarget = isLastPosition? `#year-${idSuffix}-start`: `${nextTarget}-end`;}@@ -87,6 +83,39 @@ class DateInputColumn extends Component {}, 10);}+ prevInput = () => {+ const { isFirstPosition, unitCycle } = this.state;+ const {+ type, subDailyMode, idSuffix, isEndDate, isStartDate,+ } = this.props;+ const currentPosition = unitCycle.indexOf(type);+ const prevPosition = currentPosition === 0 ? unitCycle.length - 1 : currentPosition - 1;+ const prevUnit = unitCycle[prevPosition];+ const rollOverPosition = subDailyMode ? 'minute' : 'day';++ let currentTarget = `#${type}-${idSuffix}`;+ let prevTarget = isFirstPosition+ ? `#${rollOverPosition}-${idSuffix}`+ : `#${prevUnit}-${idSuffix}`;++ if (isStartDate) {+ currentTarget = `${currentTarget}-start`;+ prevTarget = isFirstPosition+ ? `#${rollOverPosition}-${idSuffix}-end`+ : `${prevTarget}-start`;+ } else if (isEndDate) {+ currentTarget = `${currentTarget}-end`;+ prevTarget = isFirstPosition+ ? `#${rollOverPosition}-${idSuffix}-start`+ : `${prevTarget}-end`;+ }++ document.querySelector(currentTarget).blur();+ setTimeout(() => {+ document.querySelector(prevTarget).focus();+ }, 10);+ }+updateValue = () => {const { value } = this.props;this.setState({ value });@@ -95,7 +124,10 @@ class DateInputColumn extends Component {onKeyPress = (e) => {const { keyCode } = e;const entered = keyCode === 13;- if (entered) {+ const tabbed = keyCode === 9;+ const shiftTab = e.shiftKey && keyCode === 9;++ if (entered || tabbed || shiftTab) {e.preventDefault();e.stopPropagation();}@@ -108,6 +140,8 @@ class DateInputColumn extends Component {const tabbed = keyCode === 9;const backspace = keyCode === 8;const numKeys = keyCode >= 48 && keyCode <= 57;+ const shiftTab = e.shiftKey && keyCode === 9;+ const validKeycodes = numKeys || entered || backspace || tabbed;if (keyCode === 38) {// up@@ -122,51 +156,24 @@ class DateInputColumn extends Component {return;}if (e.type === 'focusout' || entered || tabbed) {- if (type === 'year' || type === 'day') {- if (!(numKeys || entered || backspace)) {- return;- }+ if ((type === 'year' || type === 'day') && !validKeycodes) {+ return;+ }+ if (shiftTab) {+ this.prevInput();+ return;}- if (entered) {- // entered or tabbed - move forward- this.nextTab();+ if (entered || tabbed) {+ this.nextInput();}}}- validateBasedOnType = (value) => {- const { type, updateTimeUnitInput } = this.props;- let newDate;- switch (type) {- case 'year':- newDate = this.yearValidation(value);- break;- case 'month':- newDate = this.monthValidation(value);- // transform month number to string (e.g., 3 -> 'MAR')- // eslint-disable-next-line no-restricted-globals- if (newDate !== null && !isNaN(value)) {- value = util.monthStringArray[value - 1];- }- break;- case 'day':- newDate = this.dayValidation(value);- break;- case 'hour':- newDate = this.hourValidation(value);- break;- case 'minute':- newDate = this.minuteValidation(value);- break;- default:- break;- }-- // add leading '0' to single string number- if (newDate !== null && value.length === 1) {- value = `0${value}`;- }-+ sanitizeInput = (value) => {+ const {+ date, type, minDate, maxDate, updateTimeUnitInput,+ } = this.props;+ const newDate = validateBasedOnType(type, value, date, minDate, maxDate);// update parent level time unit type valueif (newDate !== null) {updateTimeUnitInput(type, value);@@ -174,88 +181,6 @@ class DateInputColumn extends Component {return newDate;}- yearValidation = (input) => {- let { date } = this.props;- date = new Date(date);- if (input > 1000 && input < 9999) {- const newDate = new Date(date.setUTCFullYear(input));- return this.validateDate(newDate);- }- return null;- }-- monthValidation = (input) => {- let { date } = this.props;- date = new Date(date);- let newDate;- // eslint-disable-next-line no-restricted-globals- if (!isNaN(input) && input < 13 && input > 0) {- newDate = new Date(date.setUTCMonth(input - 1));- if (newDate) {- return this.validateDate(newDate);- }- return null;- }- const realMonth = util.stringInArray(util.monthStringArray, input);- if (realMonth !== false) {- const day = date.getUTCDate();- const zeroDay = new Date(date.setUTCDate(1));-- const zeroAddMonth = new Date(zeroDay.setUTCMonth(realMonth));- const zeroAddedMonthNumber = zeroAddMonth.getUTCMonth();-- const addDay = new Date(zeroAddMonth.setUTCDate(day));- const addedDayMonthNumber = addDay.getUTCMonth();-- if (addedDayMonthNumber !== zeroAddedMonthNumber) {- return false;- }- return this.validateDate(addDay);- }- return null;- }-- dayValidation = (input) => {- let { date } = this.props;- date = new Date(date);- const standardMaxDateForMonth = 31;-- if (input > 0 && input <= standardMaxDateForMonth) {- const actualMaxDateForMonth = new Date(- date.getYear(),- date.getMonth() + 1,- 0,- ).getDate();-- if (input > actualMaxDateForMonth) {- return false;- }- const newDate = new Date(date.setUTCDate(input));- return this.validateDate(newDate);- }- return null;- }-- hourValidation = (input) => {- let { date } = this.props;- date = new Date(date);- if (input >= 0 && input <= 23) {- const newDate = new Date(date.setUTCHours(input));- return this.validateDate(newDate);- }- return null;- }-- minuteValidation = (input) => {- let { date } = this.props;- date = new Date(date);- if (input >= 0 && input <= 59) {- const newDate = new Date(date.setUTCMinutes(input));- return this.validateDate(newDate);- }- return null;- }-rollDate = (amt) => {const {date, minDate, maxDate, type, updateDate,@@ -286,14 +211,14 @@ class DateInputColumn extends Component {const { type, value } = this.props;// check for valid date on blurconst inputValue = e.target.value;- const newDate = this.validateBasedOnType(inputValue);+ const newDate = this.sanitizeInput(inputValue);let newValue = newDate === null? value: inputValue;// eslint-disable-next-line no-restricted-globalsif (type === 'month' && !isNaN(newValue)) {- newValue = util.monthStringArray[newValue - 1];+ newValue = monthStringArray[newValue - 1];} else if (newValue.length === 1) {newValue = `0${newValue}`;}@@ -310,14 +235,6 @@ class DateInputColumn extends Component {});}- validateDate = (date) => {- const { minDate, maxDate } = this.props;- if (date > minDate && date <= maxDate) {- return date;- }- return false;- }-render() {const {fontSize,diff --git a/web/js/components/date-selector/date-range-selector.js b/web/js/components/date-selector/date-range-selector.jsindex 9e07f5e2d..be222a99e 100644--- a/web/js/components/date-selector/date-range-selector.js+++ b/web/js/components/date-selector/date-range-selector.js@@ -7,10 +7,10 @@ export default function DateRangeSelector (props) {startDate, endDate, setDateRange, minDate, maxDate, subDailyMode, idSuffix,} = props;- const setStartDate = (newStart, id) => {+ const setStartDate = (newStart) => {setDateRange([newStart, endDate]);};- const setEndDate = (newEnd, id) => {+ const setEndDate = (newEnd) => {setDateRange([startDate, newEnd]);};return (@@ -20,7 +20,7 @@ export default function DateRangeSelector (props) {date={startDate}onDateChange={setStartDate}minDate={minDate}- maxDate={maxDate}+ maxDate={endDate}subDailyMode={subDailyMode}isStartDate/>diff --git a/web/js/components/date-selector/date-selector.js b/web/js/components/date-selector/date-selector.jsindex 5ec9c35c6..316d501ed 100644--- a/web/js/components/date-selector/date-selector.js+++ b/web/js/components/date-selector/date-selector.js@@ -3,6 +3,7 @@ import React, { Component } from 'react';import PropTypes from 'prop-types';import DateInputColumn from './date-input-column';import util from '../../util/util';+import { monthStringArray } from './util';/** DateSelector used within Timeline and AnimationWidget.@@ -67,9 +68,10 @@ class DateSelector extends Component {componentDidUpdate(prevProps) {const {date,- id,minDate,maxDate,+ isStartDate,+ isEndDate,} = this.props;const {year,@@ -84,8 +86,8 @@ class DateSelector extends Component {this.clearTimeValuesAndValidation();}// handle animation start/end date limit changes and pending invalid -> valid dates- const minDateChangeEndUpdate = id === 'end' && prevProps.minDate.getTime() !== minDate.getTime();- const maxDateChangeStartUpdate = id === 'start' && prevProps.maxDate.getTime() !== maxDate.getTime();+ const minDateChangeEndUpdate = isEndDate && prevProps.minDate.getTime() !== minDate.getTime();+ const maxDateChangeStartUpdate = isStartDate && prevProps.maxDate.getTime() !== maxDate.getTime();const anyPendingTimeUnits = year || month || day || hour || minute;if ((minDateChangeEndUpdate || maxDateChangeStartUpdate) && anyPendingTimeUnits) {this.updateDate();@@ -166,7 +168,7 @@ class DateSelector extends Component {}if (month) {- const realMonth = util.stringInArray(util.monthStringArray, month);+ const realMonth = util.stringInArray(monthStringArray, month);const maxDatePrev = new Date(date.getUTCFullYear(),date.getUTCMonth() + 1,@@ -219,7 +221,7 @@ class DateSelector extends Component {let dateCheck;if (day <= maxDayDate) {- const realMonth = util.stringInArray(util.monthStringArray, month);+ const realMonth = util.stringInArray(monthStringArray, month);date = new Date(new Date(date).setUTCDate(day));dateCheck = new Date(inputDate);dateCheck = new Date(new Date(date).setUTCDate(1));@@ -300,11 +302,11 @@ class DateSelector extends Component {*/// eslint-disable-next-line react/destructuring-assignmentupdateDate = (date = this.props.date, isRollDate = false) => {- const { id, onDateChange } = this.props;+ const { onDateChange } = this.props;const newDate = this.updateDateCheck(date, isRollDate);if (newDate) {- onDateChange(newDate, id);+ onDateChange(newDate);// clear the pending timeunit inputs and reset validationthis.clearTimeValuesAndValidation();}@@ -377,7 +379,7 @@ class DateSelector extends Component {<DateInputColumn{...sharedProps}type="month"- value={month || util.monthStringArray[date.getUTCMonth()]}+ value={month || monthStringArray[date.getUTCMonth()]}isValid={monthValid}/><DateInputColumn@@ -414,7 +416,6 @@ DateSelector.defaultProps = {DateSelector.propTypes = {date: PropTypes.object,fontSize: PropTypes.number,- id: PropTypes.string,idSuffix: PropTypes.string,isStartDate: PropTypes.bool,isEndDate: PropTypes.bool,diff --git a/web/js/components/date-selector/util.js b/web/js/components/date-selector/util.jsnew file mode 100644index 000000000..98f5f8133--- /dev/null+++ b/web/js/components/date-selector/util.js@@ -0,0 +1,168 @@+import util from '../../util/util';++let minDate;+let maxDate;++export const monthStringArray = [+ 'JAN',+ 'FEB',+ 'MAR',+ 'APR',+ 'MAY',+ 'JUN',+ 'JUL',+ 'AUG',+ 'SEP',+ 'OCT',+ 'NOV',+ 'DEC',+];++export function validateBasedOnType(type, value, date, min, max) {+ let newDate;+ minDate = min;+ maxDate = max;+ switch (type) {+ case 'year':+ newDate = yearValidation(value, date);+ break;+ case 'month':+ newDate = monthValidation(value, date);+ // transform month number to string (e.g., 3 -> 'MAR')+ // eslint-disable-next-line no-restricted-globals+ if (newDate !== null && !isNaN(value)) {+ value = monthStringArray[value - 1];+ }+ break;+ case 'day':+ newDate = dayValidation(value, date);+ break;+ case 'hour':+ newDate = hourValidation(value, date);+ break;+ case 'minute':+ newDate = minuteValidation(value, date);+ break;+ default:+ break;+ }+ // add leading '0' to single string number+ if (newDate !== null && value.length === 1) {+ value = `0${value}`;+ }+ return newDate;+}++/**+ *+ * @param {*} date+ * @returns+ */+const validateDate = (date) => date > minDate && date <= maxDate && date;++/**+ *+ * @param {*} value+ * @param {*} date+ * @returns+ */+const yearValidation = (value, dateParam) => {+ const date = new Date(dateParam);+ if (value > 1000 && value < 9999) {+ const newDate = new Date(date.setUTCFullYear(value));+ return validateDate(newDate);+ }+ return null;+};++/**+ *+ * @param {*} value+ * @param {*} date+ * @returns+ */+const monthValidation = (value, dateParam) => {+ const date = new Date(dateParam);+ let newDate;+ // eslint-disable-next-line no-restricted-globals+ if (!isNaN(value) && value < 13 && value > 0) {+ newDate = new Date(date.setUTCMonth(value - 1));+ if (newDate) {+ return validateDate(newDate);+ }+ return null;+ }+ const realMonth = util.stringInArray(monthStringArray, value);+ if (realMonth !== false) {+ const day = date.getUTCDate();+ const zeroDay = new Date(date.setUTCDate(1));++ const zeroAddMonth = new Date(zeroDay.setUTCMonth(realMonth));+ const zeroAddedMonthNumber = zeroAddMonth.getUTCMonth();++ const addDay = new Date(zeroAddMonth.setUTCDate(day));+ const addedDayMonthNumber = addDay.getUTCMonth();++ if (addedDayMonthNumber !== zeroAddedMonthNumber) {+ return false;+ }+ return validateDate(addDay);+ }+ return null;+};++/**+ *+ * @param {*} value+ * @param {*} date+ * @returns+ */+const dayValidation = (value, dateParam) => {+ const date = new Date(dateParam);+ const standardMaxDateForMonth = 31;++ if (value > 0 && value <= standardMaxDateForMonth) {+ const actualMaxDateForMonth = new Date(+ date.getYear(),+ date.getMonth() + 1,+ 0,+ ).getDate();++ if (value > actualMaxDateForMonth) {+ return false;+ }+ const newDate = new Date(date.setUTCDate(value));+ return validateDate(newDate);+ }+ return null;+};++/**+ *+ * @param {*} value+ * @param {*} date+ * @returns+ */+const hourValidation = (value, dateParam) => {+ const date = new Date(dateParam);+ if (value >= 0 && value <= 23) {+ const newDate = new Date(date.setUTCHours(value));+ return validateDate(newDate);+ }+ return null;+};++/**+ *+ * @param {*} value+ * @param {*} date+ * @returns+ */+const minuteValidation = (value, dateParam) => {+ const date = new Date(dateParam);+ if (value >= 0 && value <= 59) {+ const newDate = new Date(date.setUTCMinutes(value));+ return validateDate(newDate);+ }+ return null;+};diff --git a/web/js/containers/animation-widget.js b/web/js/containers/animation-widget.jsindex 0dbfe28ae..63d8ade1d 100644--- a/web/js/containers/animation-widget.js+++ b/web/js/containers/animation-widget.js@@ -243,10 +243,10 @@ class AnimationWidget extends React.Component {onUpdateStartDate, onUpdateEndDate, startDate, endDate,} = this.props;if (newStartDate !== startDate) {- onUpdateStartDate(startDate);+ onUpdateStartDate(newStartDate);}if (newEndDate !== endDate) {- onUpdateEndDate(endDate);+ onUpdateEndDate(newEndDate);}}diff --git a/web/js/util/util.js b/web/js/util/util.jsindex 31be0b4cf..3012e9807 100644--- a/web/js/util/util.js+++ b/web/js/util/util.js@@ -17,20 +17,6 @@ export default (function(self) {self.browser = browser;self.events = events;self.load = load;- self.monthStringArray = [- 'JAN',- 'FEB',- 'MAR',- 'APR',- 'MAY',- 'JUN',- 'JUL',- 'AUG',- 'SEP',- 'OCT',- 'NOV',- 'DEC',- ];self.repeat = function(value, length) {let result = '';