All files / src/events play.js

91.89% Statements 34/37
76.67% Branches 23/30
100% Functions 6/6
91.89% Lines 34/37
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                                                                                31x                                   4x               4x 4x         4x 1x           4x 1x           4x 1x           4x 1x     4x                           1x         1x                         1x       1x                 1x             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 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 events
 * @module events/Play
 */
let Play = (function() {
  /**
   * Handles an event on a play button in Amplitude.
   *
   * HANDLER FOR:       class="amplitude-play"
   *
   * GLOBAL:            class="amplitude-play"
   * PLAYLIST:          class="amplitude-play" amplitude-playlist="playlist_key"
   * SONG:              class="amplitude-play" amplitude-song-index="song_index"
   * SONG IN PLAYLIST:  class="amplitude-play" 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) {
      /*
        Gets the attribute for song index so we can check if
        there is a need to change the song.  In some scenarios
        there might be multiple play classes on the page. In that
        case it is possible the user could click a different play
        class and change the song.
      */
      let songIndexAttribute = this.getAttribute("data-amplitude-song-index");
      let playlistAttribute = this.getAttribute("data-amplitude-playlist");
 
      /*
        Handle a global play button.
      */
      if (playlistAttribute == null && songIndexAttribute == null) {
        handleGlobalPlay();
      }
 
      /*
        Handle a playlist play button.
      */
      if (playlistAttribute != null && songIndexAttribute == null) {
        handlePlaylistPlay(playlistAttribute);
      }
 
      /*
        Handle a song play button.
      */
      if (playlistAttribute == null && songIndexAttribute != null) {
        handleSongPlay(songIndexAttribute);
      }
 
      /*
        Handle a song in playlist play button.
      */
      if (playlistAttribute != null && songIndexAttribute != null) {
        handleSongInPlaylistPlay(playlistAttribute, songIndexAttribute);
      }
 
      ConfigState.setPlayerState();
    }
  }
 
  /**
   * Handles global play button which plays whatever song is
   * active.
   *
   * @access private
   */
  function handleGlobalPlay() {
    /*
      Plays the song
    */
    Core.play();
 
    /*
      Sync the play pause elements.
    */
    PlayPauseElements.sync();
  }
 
  /**
   * Handle the playlist play.
   *
   * @access private
   * @param {string} playlist The playlist the play button belongs to.
   */
  function handlePlaylistPlay(playlist) {
    /*
      Checks if we have a new playlist.
    */
    Eif (Checks.newPlaylist(playlist)) {
      /*
        Sets the active playlist to what belongs to the 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
        );
      }
    }
 
    /*
      Plays the song.
    */
    Core.play();
 
    /*
      Syncs the play pause elements since they are dependent upon this state
      of the player.
    */
    PlayPauseElements.sync();
  }
 
  /**
   * Handles the song play button.
   *
   * @access private
   * @param {integer} song The index of the song we are playing.
   */
  function handleSongPlay(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.
    */
    Eif (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.
    */
    Iif (Checks.newSong(null, song)) {
      /*
        The song selected is different, so we change the
        song.
      */
      AudioNavigation.changeSong(config.songs[song], song);
    }
 
    /*
      Plays the song
    */
    Core.play();
 
    /*
      Syncs the play pause elements since they are dependent upon this state
      of the player.
    */
    PlayPauseElements.sync();
  }
 
  /**
   * Handles the song in playlist play.
   *
   * @access private
   * @param {string} playlist The playlist the play button belongs to.
   * @param {integer} song The song the play button belongs to.
   */
  function handleSongInPlaylistPlay(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.
		*/
    Eif (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
      );
    }
 
    /*
      Plays the song
    */
    Core.play();
 
    /*
      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 elements
  */
  return {
    handle: handle
  };
})();
 
export default Play;