import React, { Component } from 'react'; import PropTypes from 'prop-types'; import { FormattedMessage } from 'react-intl'; import Button from '/imports/ui/components/button/component'; import { styles } from '../styles.scss'; const DELAY_MILLISECONDS = 500; const STEP_TIME = 100; export default class ZoomTool extends Component { static renderAriaLabelsDescs() { return ( ); } constructor(props) { super(props); this.increment = this.increment.bind(this); this.decrement = this.decrement.bind(this); this.mouseDownHandler = this.mouseDownHandler.bind(this); this.mouseUpHandler = this.mouseUpHandler.bind(this); this.execInterval = this.execInterval.bind(this); this.onChanger = this.onChanger.bind(this); this.setInt = 0; this.state = { value: props.value, mouseHolding: false, }; } componentDidUpdate() { const isDifferent = this.props.value !== this.state.value; if (isDifferent) this.onChanger(this.props.value); } onChanger(value) { const { maxBound, minBound, change, } = this.props; let newValue = value; const isDifferent = newValue !== this.state.value; if (newValue <= minBound) { newValue = minBound; } else if (newValue >= maxBound) { newValue = maxBound; } const propsIsDifferente = this.props.value !== newValue; if (isDifferent && propsIsDifferente) { this.setState({ value: newValue }, () => { change(newValue); }); } if (isDifferent && !propsIsDifferente) this.setState({ value: newValue }); } increment() { const { step, } = this.props; const increaseZoom = this.state.value + step; this.onChanger(increaseZoom); } decrement() { const { step, } = this.props; const decreaseZoom = this.state.value - step; this.onChanger(decreaseZoom); } execInterval(inc) { const exec = inc ? this.increment : this.decrement; const interval = () => { clearInterval(this.setInt); this.setInt = setInterval(exec, STEP_TIME); }; setTimeout(() => { if (this.state.mouseHolding) { interval(); } }, DELAY_MILLISECONDS); } mouseDownHandler(bool) { this.setState({ ...this.state, mouseHolding: true, }, () => { this.execInterval(bool); }); } mouseUpHandler() { this.setState({ ...this.state, mouseHolding: false, }, () => clearInterval(this.setInt)); } render() { const { value, minBound, maxBound, } = this.props; return ( [ ZoomTool.renderAriaLabelsDescs(), (