All files / src/events skipTo.js

92.59% Statements 25/27
64.29% Branches 9/14
100% Functions 4/4
92.59% Lines 25/27
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                                                                                  31x                               2x       2x 2x 2x           2x                   2x                 2x       2x 1x   1x                                           1x 1x         1x 1x         1x                             1x 1x             1x         1x         1x 1x 1x         1x           31x            
/**
 * Imports the config module
 * @module config
 */
import config from "../config.js";
 
/**
 * Imports AmplitudeJS Debug Utility
 * @module utilities/debug
 */
import Debug from "../utilities/debug.js";
 
/**
 * Imports the AmplitudeJS Audio Navigation Utility
 * @module utilities/AudioNavigation
 */
import AudioNavigation from "../utilities/audioNavigation.js";
 
/**
 * Imports the AmplitudeJS Checks Utility
 * @module utilities/Checks
 */
import Checks from "../utilities/checks.js";
 
/**
 * Imports the AmplitudeJS Core Methods
 * @module core/Core
 */
import Core from "../core/core.js";
 
/**
 * Imports the AmplitudeJS play pause elements.
 * @module visual/PlayPauseElements
 */
import PlayPauseElements from "../visual/playPauseElements.js";
 
/**
 * Handles the skip to event.
 *
 * @module events/SkipTo
 */
let SkipTo = (function() {
  /**
   * Handles an event on a skip to button.
   *
   * HANDLER FOR:       class="amplitude-skip-to"
   *
   * GLOBAL:            class="amplitude-skip-to" amplitude-song-index="song_index" amplitude-location="seconds"
   * PLAYLIST:          class="amplitude-skip-to" amplitude-playlist="playlist_key" amplitude-song-index="song_index" amplitude-location="seconds"
   *
   * @access public
   */
  function handle() {
    /*
      If the touch is moving, we do not want to accidentally touch the play
      pause element and fire an event.
    */
    Eif (!config.is_touch_moving) {
      /*
        Extracts the needed attributes from the element.
      */
      let playlist = this.getAttribute("data-amplitude-playlist");
      let songIndex = this.getAttribute("data-amplitude-song-index");
      let location = this.getAttribute("data-amplitude-location");
 
      /*
        If the location is null, write a message. We can't skip to a location
        that is null
      */
      Iif (location == null) {
        Debug.writeMessage(
          "You must add an 'data-amplitude-location' attribute in seconds to your 'amplitude-skip-to' element."
        );
      }
 
      /*
        If the song index is null, write a debug message. We can't skip to a location
        of a null song.
      */
      Iif (songIndex == null) {
        Debug.writeMessage(
          "You must add an 'data-amplitude-song-index' attribute to your 'amplitude-skip-to' element."
        );
      }
 
      /*
        If the location and song index are set, continue.
      */
      Eif (location != null && songIndex != null) {
        /*
  				Determines if the skip to button is in the scope of a playlist.
  			*/
        if (playlist == null) {
          handleSkipToSong(parseInt(songIndex), parseInt(location));
        } else {
          handleSkipToPlaylist(
            playlist,
            parseInt(songIndex),
            parseInt(location)
          );
        }
      }
    }
  }
 
  /**
   * Handles the skipping to a specific song
   *
   * @access private
   * @param {string} songIndex  - The index of the song being skipped to
   * @param {number} location   - The seconds location of the song in the playlist.
   */
  function handleSkipToSong(songIndex, location) {
    /*
      Changes the song to where it's being skipped and then
      play the song.
    */
    AudioNavigation.changeSong(config.songs[songIndex], songIndex);
    Core.play();
 
    /*
      Syncs all of the play pause buttons now that we've skipped.
    */
    PlayPauseElements.syncGlobal();
    PlayPauseElements.syncSong();
 
    /*
      Skip to the location in the song.
    */
    Core.skipToLocation(location);
  }
 
  /**
   * Handles the skipping to a song that's in a playlist.
   *
   * @access private
   * @param {string} playlist   - The playlist being skipped to
   * @param {string} songIndex  - The index of the song in the playlist
   * @param {number} location   - The seconds location of the song in the playlist.
   */
  function handleSkipToPlaylist(playlist, songIndex, location) {
    /*
      Checks if we are skipping to a new playlist
    */
    Eif (Checks.newPlaylist(playlist)) {
      AudioNavigation.setActivePlaylist(playlist);
    }
 
    /*
      Changes the song to where it's being skipped and then
      play the song.
    */
    AudioNavigation.changeSongPlaylist(
      playlist,
      config.playlists[playlist].songs[songIndex],
      songIndex
    );
    Core.play();
 
    /*
      Sync all of the play pause elements.
    */
    PlayPauseElements.syncGlobal();
    PlayPauseElements.syncPlaylist();
    PlayPauseElements.syncSong();
 
    /*
      Skip to the location in the song.
    */
    Core.skipToLocation(location);
  }
 
  /**
   * Return public facing methods
   */
  return {
    handle: handle
  };
})();
 
export default SkipTo;