All files / src/events prev.js

100% Statements 14/14
91.67% Branches 11/12
100% Functions 4/4
100% Lines 14/14
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                                              31x                               4x       4x         4x 2x           4x 2x                               2x 1x   1x                             2x 1x   1x                 31x            
/**
 * Imports the config module
 * @module config
 */
import config from "../config.js";
 
/**
 * Imports the Amplitude Audio Navigation Utility
 * @module utilities/AudioNavigation
 */
import AudioNavigation from "../utilities/audioNavigation.js";
 
/**
 * AmplitudeJS Debug Module
 * @module utilities/Debug
 */
import Debug from "../utilities/debug.js";
 
/**
 * AmplitudeJS Prev Event Handler
 *
 * @module events/Prev
 */
let Prev = (function() {
  /**
   * Handles an event on the previous button
   *
   * HANDLER FOR:       class="amplitude-prev"
   *
   * GLOBAL:            class="amplitude-prev"
   * PLAYLIST:          class="amplitude-prev" amplitude-playlist="playlist_key"
   *
   * @access public
   */
  function handle() {
    /*
      We don't fire this if the user is touching the screen and it's moving.
      This could lead to a mis-fire
    */
    Eif (!config.is_touch_moving) {
      /*
        Gets the playlist attribute from the element.
      */
      let playlist = this.getAttribute("data-amplitude-playlist");
 
      /*
        If the playlist is null, we handle the global prev.
      */
      if (playlist == null) {
        handleGlobalPrev();
      }
 
      /*
        If the playlist is set, we handle the playlist prev.
      */
      if (playlist != null) {
        handlePlaylistPrev(playlist);
      }
    }
  }
 
  /**
   * Handles an event on a global previous button.
   *
   * @access private
   */
  function handleGlobalPrev() {
    /*
      Check to see if the current state of the player
      is in playlist mode or not playlist mode. If we are in playlist mode,
      we set prev on the playlist.
    */
    if (config.active_playlist == "" || config.active_playlist == null) {
      AudioNavigation.setPrevious();
    } else {
      AudioNavigation.setPreviousPlaylist(config.active_playlist);
    }
  }
 
  /**
   * Handles an event on a previous playlist button.
   *
   * @access private
   * @prop {string} playlist  - The playlist we are handling the previous for.
   */
  function handlePlaylistPrev(playlist) {
    /*
      Ensure the playlist is the same as the active playlist. To get to change
      the scope to a new playlist, you need to play that playlist.
    */
    if (playlist == config.active_playlist) {
      AudioNavigation.setPreviousPlaylist(config.active_playlist);
    } else {
      Debug.writeMessage(
        "You can not go to the previous song on a playlist that is not being played!"
      );
    }
  }
 
  /*
    Returns the public facing methods.
  */
  return {
    handle: handle
  };
})();
 
export default Prev;