All files / src/events keydown.js

5.88% Statements 2/34
0% Branches 0/22
11.11% Functions 1/9
5.88% Lines 2/34
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 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228                                                                                              31x                                                                                                                                                                                                                                                                                                                                                           31x            
/**
 * Imports the config module
 * @module config
 */
import config from "../config.js";
 
/**
 * Imports the core of AmplitudeJS
 * @module core/Core
 */
import Core from "../core/core.js";
 
/**
 * Imports the Shuffle Utility
 * @module utilities/Shuffle
 */
import Shuffle from "../utilities/shuffler.js";
 
/**
 * Imports the Repeater Utility
 * @module utilities/Repeater
 */
import Repeater from "../utilities/repeater.js";
 
/**
 * Imports the Audio Navigation Utility
 * @module utilities/AudioNavigation
 */
import AudioNavigation from "../utilities/audioNavigation.js";
 
/**
 * Imports the Repeat Elements Visual Handler
 * @module visual/RepeatElements
 */
import RepeatElements from "../visual/repeatElements.js";
 
/**
 * Imports the Play Pause Elements Visual Handler
 * @module visual/PlayPauseElements
 */
import PlayPauseElements from "../visual/playPauseElements.js";
 
/**
 * AmplitudeJS Key Down event handler
 *
 * @module events/KeyDown
 */
let KeyDown = (function() {
  /**
   * When the keydown event is fired, we determine which function should be run
   * based on what was passed in.
   *
   * HANDLER FOR: keydown
   *
   * @access public
   * @prop {object} event The event object being passed in.
   */
  function handle(event) {
    runKeyEvent(event.which);
  }
 
  /**
   * Runs an event on key down
   *
   * @access public
   * @param {number} key 	- The key code the event is bound to.
   */
  function runKeyEvent(key) {
    /*
			Checks to see if the user bound an event to the code pressed.
		*/
    if (config.bindings[key] != undefined) {
      /*
				Determine which event should be run if bound.
			*/
      switch (config.bindings[key]) {
        /*
					Fires a play pause event.
				*/
        case "play_pause":
          runPlayPauseKeyDownEvent();
          break;
 
        /*
					Fires a next event.
				*/
        case "next":
          runNextKeyDownEvent();
          break;
 
        /*
					Fires a previous event.
				*/
        case "prev":
          runPrevKeyDownEvent();
          break;
 
        /*
					Fires a stop event.
				*/
        case "stop":
          runStopKeyDownEvent();
          break;
 
        /*
					Fires a shuffle event.
				*/
        case "shuffle":
          runShuffleKeyDownEvent();
          break;
 
        /*
					Fires a repeat event.
				*/
        case "repeat":
          runRepeatKeyDownEvent();
          break;
      }
    }
  }
 
  /**
   * Runs the play pause method for key down.
   */
  function runPlayPauseKeyDownEvent() {
    /*
      If the song is paused, we play the song. If the song is playing,
      we pause the song.
    */
    if (config.audio.paused) {
      Core.play();
    } else {
      Core.pause();
    }
 
    /*
      Now we sync all the elements to match the state of the audio.
      We don't need to do any checks on new songs or changed playlists
      in the global since it's whatever song is playing.
    */
    PlayPauseElements.sync();
  }
 
  /**
   * Runs the next method for key down.
   */
  function runNextKeyDownEvent() {
    /*
      Check to see if the current state of the player
      is in playlist mode or not playlist mode.
    */
    if (config.active_playlist == "" || config.active_playlist == null) {
      AudioNavigation.setNext();
    } else {
      AudioNavigation.setNextPlaylist(config.active_playlist);
    }
  }
 
  /**
   * Runs the previous method for key down.
   */
  function runPrevKeyDownEvent() {
    /*
      Check to see if the current playlist has been set
      or null and set the previous song.
    */
    if (config.active_playlist == "" || config.active_playlist == null) {
      AudioNavigation.setPrev();
    } else {
      AudioNavigation.setPrevPlaylist(config.active_playlist);
    }
  }
 
  /**
   * Runs the stop method for key down.
   */
  function runStopKeyDownEvent() {
    /*
      Syncs all of the play pause elements to pause.
    */
    PlayPauseElements.syncToPause();
 
    /*
      Stops the active song.
    */
    Core.stop();
  }
 
  /**
   * Runs the shuffle method for key down.
   */
  function runShuffleKeyDownEvent() {
    /*
      Check to see if the current playlist has been set
      or null and set the previous song.
    */
    if (config.active_playlist == "" || config.active_playlist == null) {
      Shuffle.toggleShuffle();
    } else {
      Shuffle.toggleShufflePlaylist(config.active_playlist);
    }
  }
 
  /**
   * Run the repeat method for key down.
   */
  function runRepeatKeyDownEvent() {
    /*
      Toggles the repeat
    */
    Repeater.setRepeat(!config.repeat);
 
    /*
      Visually sync repeat
    */
    RepeatElements.syncRepeat();
  }
 
  /**
   * Returns the public methods for the handler.
   */
  return {
    handle: handle
  };
})();
 
export default KeyDown;