All files / src/events playPause.js

91.11% Statements 41/45
89.47% Branches 34/38
100% Functions 6/6
91.11% Lines 41/45
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 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320                                                                                31x                                   18x         18x 18x         18x 4x           18x 4x           18x 5x           18x 5x     18x                         4x 2x   2x               4x                           4x       2x                 2x             2x                       4x 2x   2x               4x                                     5x                                     5x         3x             5x 3x   2x               5x                                         5x         3x         3x                         5x                               5x 3x   2x               5x           31x            
/**
 * Imports the config module
 * @module config
 */
import config from "../config.js";
 
/**
 * Import the config state utility.
 * @module utilities/configState
 */
import ConfigState from "../utilities/configState.js";
 
/**
 * Imports the AmplitudeJS Core Methods
 * @module core/Core
 */
import Core from "../core/core.js";
 
/**
 * Imports the AmplitudeJS Checks Utility
 * @module utilities/Checks
 */
import Checks from "../utilities/checks.js";
 
/**
 * Imports the AmplitudeJS Audio Navigation Utility
 * @module utilities/AudioNavigation
 */
import AudioNavigation from "../utilities/audioNavigation.js";
 
/**
 * Imports the AmplitudeJS Play Pause Elements
 * @module visual/PlayPauseElements
 */
import PlayPauseElements from "../visual/playPauseElements.js";
 
/**
 * Handles all of the play pause events
 * @module events/PlayPause
 */
let PlayPause = (function() {
  /**
   * Handles an event on a play/pause button
   *
   * HANDLER FOR:       class="amplitude-play-pause"
   *
   * GLOBAL:            class="amplitude-play-pause"
   * PLAYLIST:          class="amplitude-play-pause" amplitude-playlist="playlist_key"
   * SONG:              class="amplitude-play-pause" amplitude-song-index="song_index"
   * SONG IN PLAYLIST:  class="amplitude-play-pause" amplitude-playlist="playlist-key" amplitude-song-index="playlist_index"
   *
   * @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) {
      /*
        Get the playlist and song from the element. It's alright if these
        are null.
      */
      let playlist = this.getAttribute("data-amplitude-playlist");
      let song = this.getAttribute("data-amplitude-song-index");
 
      /*
        Handle a global play pause button
      */
      if (playlist == null && song == null) {
        handleGlobalPlayPause();
      }
 
      /*
        Handle a playlist play pause button
      */
      if (playlist != null && song == null) {
        handlePlaylistPlayPause(playlist);
      }
 
      /*
        Handle a song play pause button
      */
      if (playlist == null && song != null) {
        handleSongPlayPause(song);
      }
 
      /*
        Handle a song in playlist play pause button
      */
      if (playlist != null && song != null) {
        handleSongInPlaylistPlayPause(playlist, song);
      }
 
      ConfigState.setPlayerState();
    }
  }
 
  /**
   * Sets the main play pause buttons to the current state of the song.
   * @access private
   */
  function handleGlobalPlayPause() {
    /*
      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();
  }
 
  /**
   * Sets the playlist main play pause buttons to the current state of the song.
   * @access private
   * @param {string} playlist The playlist the main play pause button controls
   */
  function handlePlaylistPlayPause(playlist) {
    /*
      The only thing that can change when you click a playlist
      play pause is the playlist. Main play pauses have no change
      in song, song play pauses can change playlist and song.
    */
    if (Checks.newPlaylist(playlist)) {
      /*
        If there's a new playlist, then we set the new playlist.
      */
      AudioNavigation.setActivePlaylist(playlist);
 
      /*
        Play first song in the playlist since we just
        switched playlists, we start from the first song.
 
        If the user has shuffle on for the playlist, then
        we go from the first song in the shuffle playlist array.
      */
      Iif (config.playlists[playlist].shuffle) {
        AudioNavigation.changeSongPlaylist(
          playlist,
          config.playlists[playlist].shuffle_list[0],
          0
        );
      } else {
        AudioNavigation.changeSongPlaylist(
          playlist,
          config.playlists[playlist].songs[0],
          0
        );
      }
    }
 
    /*
      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();
  }
 
  /**
   * Sets the playlist main play pause buttons to the current state of the song.
   * @access private
   * @param {string} song The index of the song being played/paused
   */
  function handleSongPlayPause(song) {
    /*
			There can be multiple playlists on the page and there can be
			multiple songs on the page AND there can be songs in multiple
			playlists, so we have some checking to do.
		*/
 
    /*
			Check to see if the playlist has changed. Essentially, if we are moving
      out of a playlist context.
		*/
    Iif (Checks.newPlaylist(null)) {
      /*
        We've moved out of the playlist context, so we set the active playlist
        to null
      */
      AudioNavigation.setActivePlaylist(null);
 
      /*
				We then change the song to the index selected.
			*/
      AudioNavigation.changeSong(config.songs[song], song);
    }
 
    /*
			Check to see if the song has changed. If it has,
			set the active song. If it was in a playlist, the
			song wouldn't change here, since we already set the
			song when we checked for a playlist.
		*/
    if (Checks.newSong(null, song)) {
      /*
				The song selected is different, so we change the
				song.
			*/
      AudioNavigation.changeSong(config.songs[song], song);
    }
 
    /*
      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();
  }
 
  /**
   * Sets the song in playlist play pause buttons to the current
   * state of the song.
   * @access private
   * @param {string} playlist The playlist the song is a part of
   * @param {number} song The index of the song being played/paused
   */
  function handleSongInPlaylistPlayPause(playlist, song) {
    /*
			There can be multiple playlists on the page and there can be
			multiple songs on the page AND there can be songs in multiple
			playlists, so we have some checking to do.
		*/
 
    /*
			Check to see if the playlist has changed. Essentially, if we are moving
      out of a playlist context.
		*/
    if (Checks.newPlaylist(playlist)) {
      /*
        We've moved out of the playlist context, so we set the active playlist
        to null
      */
      AudioNavigation.setActivePlaylist(playlist);
 
      /*
				We then change the song to the index selected.
			*/
      AudioNavigation.changeSongPlaylist(
        playlist,
        config.playlists[playlist].songs[song],
        song
      );
    }
 
    /*
			Check to see if the song has changed. If it has,
			set the active song. If it was in a playlist, the
			song wouldn't change here, since we already set the
			song when we checked for a playlist.
		*/
    Iif (Checks.newSong(playlist, song)) {
      /*
				The song selected is different, so we change the
				song.
			*/
      AudioNavigation.changeSongPlaylist(
        playlist,
        config.playlists[playlist].songs[song],
        song
      );
    }
 
    /*
      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();
  }
 
  /**
   * Returns the public facing methods
   */
  return {
    handle: handle
  };
})();
 
export default PlayPause;