All files / src/events songSlider.js

6.9% Statements 2/29
0% Branches 0/34
16.67% Functions 1/6
6.9% Lines 2/29
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 229                                              31x                                                                                                                                                                                                                                                                                                                                                                                                             31x            
/**
 * Imports the config module
 * @module config
 */
import config from "../config.js";
 
/**
 * Imports the time utility
 * @module utilities/Time
 */
import Time from "../utilities/time.js";
 
/**
 * Imports the song slider elements.
 * @module visual/SongSliderElements
 */
import SongSliderElements from "../visual/songSliderElements.js";
 
/**
 * Handles the song slider to event.
 *
 * @module events/SongSlider
 */
let SongSlider = (function() {
  /**
   * Handles a song slider element.
   *
   * HANDLER FOR:       class="amplitude-song-slider"
   *
   * GLOBAL:            class="amplitude-song-slider"
   * PLAYLIST:          class="amplitude-song-slider" amplitude-playlist="playlist_key"
   * SONG:              class="amplitude-song-slider" amplitude-song-index="song_index"
   * SONG IN PLAYLIST:  class="amplitude-song-slider" amplitude-playlist="playlist_key" amplitude-song-index="song_index"
   *
   * @access public
   */
  function handle() {
    /*
			Gets the percentage of the song we will be setting the location for.
		*/
    let locationPercentage = this.value;
 
    /*
      Computes the time in seconds for the current song.
    */
    let computedTime = config.audio.duration * (locationPercentage / 100);
 
    /*
      Gets the attributes for playlist and index for the element.
    */
    let playlist = this.getAttribute("data-amplitude-playlist");
    let song = this.getAttribute("data-amplitude-song-index");
 
    /*
      If no playlist or song is defined, then it's a global song slider.
    */
    if (playlist == null && song == null) {
      handleGlobalSongSlider(computedTime, locationPercentage);
    }
 
    /*
      If a playlist but no song is defined, then it's playlist slider.
    */
    if (playlist != null && song == null) {
      handlePlaylistSongSlider(computedTime, locationPercentage, playlist);
    }
 
    /*
      If no playlist but a song is defined, then it's a song slider.
    */
    if (playlist == null && song != null) {
      handleSongSongSlider(computedTime, locationPercentage, song);
    }
 
    /*
      If playlist and song are defined then it's a song in a playlist
      slider.
    */
    if (playlist != null && song != null) {
      handleSongInPlaylistSongSlider(
        computedTime,
        locationPercentage,
        playlist,
        song
      );
    }
  }
 
  /**
   * Handles a change on a global audio slider
   *
   * @access private
   * @param {integer} computedTime  - The time we will set the audio to.
   * @param {float}   locationPercentage - The percent through the song.
   */
  function handleGlobalSongSlider(computedTime, locationPercentage) {
    /*
			If the active song is not live, set the current time and adjust the slider.
		*/
    if (!config.active_metadata.live) {
      Time.setCurrentTime(computedTime);
 
      /*
        Sync song slider elements.
      */
      SongSliderElements.sync(
        locationPercentage,
        config.active_playlist,
        config.active_index
      );
    }
  }
 
  /**
   * Handles a change on a playlist audio slider
   *
   * @access private
   * @param {integer} computedTime  - The time we will set the audio to.
   * @param {float}   locationPercentage - The percent through the song.
   * @param {string}  playlist = The playlist the song slider belongs to.
   */
  function handlePlaylistSongSlider(
    computedTime,
    locationPercentage,
    playlist
  ) {
    /*
			We don't want to song slide a playlist that's not the
			active placylist.
		*/
    if (config.active_playlist == playlist) {
      /*
  			If the active song is not live, set the current time
  		*/
      if (!config.active_metadata.live) {
        Time.setCurrentTime(computedTime);
 
        /*
          Sync song slider elements.
        */
        SongSliderElements.sync(
          locationPercentage,
          playlist,
          config.active_index
        );
      }
    }
  }
 
  /**
   * Handles a change on a song audio slider
   *
   * @access private
   * @param {integer} computedTime  - The time we will set the audio to.
   * @param {float}   locationPercentage - The percent through the song.
   * @param {integer} songIndex = The song being navigated.
   */
  function handleSongSongSlider(computedTime, locationPercentage, songIndex) {
    /*
      We only want to move the slider if the active song is the
      same as the song being selected.
    */
    if (config.active_index == songIndex && config.active_playlist == null) {
      /*
    		If the active song is not live, set the current time
    	*/
      if (!config.active_metadata.live) {
        Time.setCurrentTime(computedTime);
 
        /*
          Sync song slider elements.
        */
        SongSliderElements.sync(
          locationPercentage,
          config.active_playlist,
          songIndex
        );
      }
    }
  }
 
  /**
   * Handles a change on a song audio slider
   *
   * @access private
   * @param {integer} computedTime  - The time we will set the audio to.
   * @param {float}   locationPercentage - The percent through the song.
   * @param {integer} playlist = The playlist the song belongs to.
   * @param {integer} songIndex = The song being navigated.
   */
  function handleSongInPlaylistSongSlider(
    computedTime,
    locationPercentage,
    playlist,
    songIndex
  ) {
    /*
      We only want to move the slider if the active song is the
      same as the song being selected and the active playlist is the same
      as the playlist selected.
    */
    if (
      config.playlists[playlist].active_index == songIndex &&
      config.active_playlist == playlist
    ) {
      /*
    		If the active song is not live, set the current time
    	*/
      if (!config.active_metadata.live) {
        Time.setCurrentTime(computedTime);
 
        /*
          Sync song slider elements.
        */
        SongSliderElements.sync(locationPercentage, playlist, songIndex);
      }
    }
  }
 
  /*
    Return public facing methods
  */
  return {
    handle: handle
  };
})();
 
export default SongSlider;