all files / src/ KeyboardHandler.js

88.24% Statements 15/17
90% Branches 9/10
80% Functions 4/5
88.24% Lines 15/17
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60                                                                                         
/**
 * Set up a keyboard event listener to detect chart navigation keypresses.
 * @private
 * @param {HTMLElement} container
 *	- The element (usually a <code>&lt;div&gt;</code>) containing the chart
 * @param {Player} player - AudioChart Player object
 * @todo mark up the DIV properly
 * @todo check what sort of element we get given? no; could be button?
 */
class KeyboardHandler {
	constructor(container, player) {
		if (!container) {
			throw Error('No container given')
		}
		if (!player) {
			throw Error('No Player given')
		}
 
		container.setAttribute('tabindex', '0')
		container.addEventListener('keydown', this.keypressHandler.bind(this))
		this.player = player
	}
 
	/**
	 * Handle keypresses
	 *
	 * Note: This is bound to the {@link KeyboardHandler} so that it can call
	 *       the right handler methods.
	 *
	 * @param {KeyboardEvent} event - the KeyboardEvent that occured
	 * @todo make link work
	 */
	keypressHandler(event) {
		event.preventDefault()  // TODO should this be here or later? check for defaultPrevented?
 
		if (event.key === 'ArrowRight') {
			this.handleRight()
		} else Iif (event.key === 'ArrowLeft' ) {
			this.handleLeft()
		} else if (event.key === ' ') {
			this.handleSpace()
		}
	}
 
	/** Handle a left arrow being pressed */
	handleLeft() {
		this.player.stepBackward()
	}
 
	/** Handle a right arrow being pressed */
	handleRight() {
		this.player.stepForward()
	}
 
	/** Handle the space key being pressed */
	handleSpace() {
		this.player.playPause()
	}
}