Skip to content

Interactive interface for browsing global, full-resolution satellite imagery

move keyboard arrow handling to arrow component

Jason Kenteef7726b0e46
diff --git a/web/js/components/timeline/timeline-controls/date-change-arrows.js b/web/js/components/timeline/timeline-controls/date-change-arrows.js
index 7bd837496..36fd848cd 100644
--- a/web/js/components/timeline/timeline-controls/date-change-arrows.js
+++ b/web/js/components/timeline/timeline-controls/date-change-arrows.js
@@ -7,10 +7,10 @@ import {
setArrowUp as setArrowUpAction,
} from '../../../modules/date/actions';
-const ANIMATION_DELAY = 500; // interval firing to trigger parent level arrow change
-const CLICK_HOLD_DELAY = 300; // wait before click is considered a hold
+const ANIMATION_DELAY = 600; // interval for timestep
+const CLICK_HOLD_DELAY = 200; // wait before click is considered a hold
-let mouseHoldCheckTimer = null;
+let arrowDownCheckTimer = null;
// left/right arrow intervals
const intervals = {
left: 0,
@@ -24,16 +24,18 @@ class DateChangeArrows extends PureComponent {
left: props.leftArrowDown,
right: props.rightArrowDown,
};
- this.arrowUpMap = {
- left: props.leftArrowUp,
- right: props.rightArrowUp,
- };
+ }
+
+ componentDidMount() {
+ document.addEventListener('keydown', this.handleKeyDown);
+ document.addEventListener('keyup', this.handleKeyUp);
}
componentDidUpdate (prevProps) {
const { tilesPreloaded, arrowDown } = this.props;
- const finishedPreload = !prevProps.tilesPreloaded && tilesPreloaded;
- if (finishedPreload && arrowDown) {
+ const notAnimating = !intervals.left && !intervals.right;
+
+ if (tilesPreloaded && arrowDown && notAnimating) {
// set interval for holding arrow down
intervals[arrowDown] = setInterval(this.arrowDownMap[arrowDown], ANIMATION_DELAY);
}
@@ -42,28 +44,50 @@ class DateChangeArrows extends PureComponent {
componentWillUnmount() {
clearInterval(intervals.left);
clearInterval(intervals.right);
+ document.removeEventListener('keydown', this.handleKeyDown);
+ document.removeEventListener('keyup', this.handleKeyUp);
}
clickAndHold = (direction) => {
const { setArrowDown } = this.props;
setArrowDown(direction);
- mouseHoldCheckTimer = null;
+ arrowDownCheckTimer = null;
}
- arrowDown = (direction) => {
+ handleKeyDown = (e) => {
+ const { arrowDown } = this.props;
+ const direction = e.keyCode === 37 ? 'left' : e.keyCode === 39 ? 'right' : null;
+ if (e.target.tagName === 'INPUT' || e.target.className === 'rc-slider-handle' || e.ctrlKey || e.metaKey) {
+ return;
+ }
+ if (direction) {
+ e.preventDefault();
+ if (!arrowDown) this.onArrowDown(direction);
+ }
+ }
+
+ handleKeyUp = (e) => {
+ const direction = e.keyCode === 37 ? 'left' : e.keyCode === 39 ? 'right' : null;
+ if (direction) {
+ e.preventDefault();
+ this.onArrowUp(direction);
+ }
+ };
+
+ onArrowDown = (direction) => {
this.arrowDownMap[direction]();
- mouseHoldCheckTimer = setTimeout(() => {
+ arrowDownCheckTimer = setTimeout(() => {
this.clickAndHold(direction);
}, CLICK_HOLD_DELAY);
}
- arrowUp = (direction) => {
+ onArrowUp = (direction) => {
const { setArrowUp, arrowDown } = this.props;
- if (mouseHoldCheckTimer) {
- clearTimeout(mouseHoldCheckTimer);
+ if (arrowDownCheckTimer) {
+ clearTimeout(arrowDownCheckTimer);
}
clearInterval(intervals[direction]);
- this.arrowUpMap[direction]();
+ intervals[direction] = 0;
if (arrowDown) setArrowUp();
}
@@ -76,10 +100,10 @@ class DateChangeArrows extends PureComponent {
rightArrowDisabled,
} = this.props;
- const leftArrowDown = () => this.arrowDown('left');
- const rightArrowDown = () => this.arrowDown('right');
- const leftArrowUp = () => this.arrowUp('left');
- const rightArrowUp = () => this.arrowUp('right');
+ const leftArrowDown = () => this.onArrowDown('left');
+ const rightArrowDown = () => this.onArrowDown('right');
+ const leftArrowUp = () => this.onArrowUp('left');
+ const rightArrowUp = () => this.onArrowUp('right');
return (
<div>
@@ -176,12 +200,10 @@ DateChangeArrows.propTypes = {
arrowDown: PropTypes.string,
leftArrowDisabled: PropTypes.bool,
leftArrowDown: PropTypes.func,
- leftArrowUp: PropTypes.func,
isMobile: PropTypes.bool,
nowButtonDisabled: PropTypes.bool,
rightArrowDisabled: PropTypes.bool,
rightArrowDown: PropTypes.func,
- rightArrowUp: PropTypes.func,
setArrowDown: PropTypes.func,
setArrowUp: PropTypes.func,
tilesPreloaded: PropTypes.bool,
diff --git a/web/js/containers/timeline/timeline.js b/web/js/containers/timeline/timeline.js
index f75b2562b..4db9b10ef 100644
--- a/web/js/containers/timeline/timeline.js
+++ b/web/js/containers/timeline/timeline.js
@@ -69,7 +69,6 @@ import util from '../../util/util';
import MobileComparisonToggle from '../../components/compare/mobile-toggle';
-const ANIMATION_DELAY = 500;
const preventDefaultFunc = (e) => {
e.preventDefault();
};
@@ -121,16 +120,6 @@ class Timeline extends React.Component {
const throttleSettings = { leading: true, trailing: false };
this.throttleSelectDate = lodashThrottle(selectDate, 100, throttleSettings);
this.debounceDraggerDateUpdate = lodashDebounce(this.onDateChange, 200);
- this.throttleDecrementDate = lodashThrottle(
- this.handleArrowDateChange.bind(this, -1),
- ANIMATION_DELAY,
- throttleSettings,
- );
- this.throttleIncrementDate = lodashThrottle(
- this.handleArrowDateChange.bind(this, 1),
- ANIMATION_DELAY,
- throttleSettings,
- );
// animation dragger updates
this.debounceOnUpdateStartDate = lodashDebounce(onUpdateStartDate, 30);
@@ -493,22 +482,6 @@ class Timeline extends React.Component {
}
}
- /**
- * @desc stop animation from left arrows - clear throttle invocation
- * @returns {void}
- */
- stopLeftArrow = () => {
- this.throttleDecrementDate.cancel();
- }
-
- /**
- * @desc stop animation from right arrows - clear throttle invocation
- * @returns {void}
- */
- stopRightArrow = () => {
- this.throttleIncrementDate.cancel();
- }
-
/**
* @desc handles left/right arrow down to decrement/increment date
* @param {Event} mouse event
@@ -521,16 +494,7 @@ class Timeline extends React.Component {
if (e.target.tagName !== 'INPUT' && e.target.className !== 'rc-slider-handle' && !e.ctrlKey && !e.metaKey && !isTimelineDragging) {
const timeScaleNumber = Number(timeScaleToNumberKey[timeScale]);
const maxTimeScaleNumber = hasSubdailyLayers ? 5 : 3;
- // left arrow
- if (e.keyCode === 37) {
- e.preventDefault();
- this.throttleDecrementDate();
- // right arrow
- } else if (e.keyCode === 39) {
- e.preventDefault();
- this.throttleIncrementDate();
- // up arrow
- } else if (e.keyCode === 38) {
+ if (e.keyCode === 38) {
e.preventDefault();
if (timeScaleNumber > 1) {
this.changeTimeScale(timeScaleNumber - 1);
@@ -545,23 +509,6 @@ class Timeline extends React.Component {
}
};
- /**
- * @desc handles stopping change date in process and to allow faster key downs
- * @param {Event} mouse event
- * @returns {void}
- */
- handleKeyUp = (e) => {
- // left arrow
- if (e.keyCode === 37) {
- e.preventDefault();
- this.stopLeftArrow();
- // right arrow
- } else if (e.keyCode === 39) {
- e.preventDefault();
- this.stopRightArrow();
- }
- };
-
/**
* @desc show/hide custom interval modal
* @param {Boolean} isOpen
@@ -1077,6 +1024,19 @@ class Timeline extends React.Component {
const containerDisplayStyle = {
display: isDistractionFreeModeActive ? 'none' : 'block',
};
+
+ const renderDateChangeArrows = () => (
+ <DateChangeArrows
+ leftArrowDown={() => this.handleArrowDateChange(-1)}
+ leftArrowDisabled={leftArrowDisabled}
+ isMobile={isMobile}
+ rightArrowDown={() => this.handleArrowDateChange(1)}
+ rightArrowDisabled={rightArrowDisabled}
+ nowButtonDisabled={nowButtonDisabled}
+ handleSelectNowButton={this.handleSelectNowButton}
+ />
+ );
+
return (
<>
<div
@@ -1104,17 +1064,7 @@ class Timeline extends React.Component {
style={this.getMobileDateButtonStyle()}
>
<div id="zoom-buttons-group">
- <DateChangeArrows
- handleSelectNowButton={this.handleSelectNowButton}
- isMobile={isMobile}
- leftArrowDisabled={leftArrowDisabled}
- leftArrowDown={this.throttleDecrementDate}
- leftArrowUp={this.stopLeftArrow}
- nowButtonDisabled={nowButtonDisabled}
- rightArrowDisabled={rightArrowDisabled}
- rightArrowDown={this.throttleIncrementDate}
- rightArrowUp={this.stopRightArrow}
- />
+ {renderDateChangeArrows()}
</div>
</div>
</div>
@@ -1149,17 +1099,7 @@ class Timeline extends React.Component {
timeScaleChangeUnit={timeScaleChangeUnit}
hasSubdailyLayers={hasSubdailyLayers}
/>
- <DateChangeArrows
- handleSelectNowButton={this.handleSelectNowButton}
- isMobile={isMobile}
- leftArrowDisabled={leftArrowDisabled}
- leftArrowDown={this.throttleDecrementDate}
- leftArrowUp={this.stopLeftArrow}
- nowButtonDisabled={nowButtonDisabled}
- rightArrowDisabled={rightArrowDisabled}
- rightArrowDown={this.throttleIncrementDate}
- rightArrowUp={this.stopRightArrow}
- />
+ {renderDateChangeArrows()}
</div>
<AnimationButton
clickAnimationButton={this.clickAnimationButton}