All files / src/utilities audioNavigation.js

86.82% Statements 112/129
71.43% Branches 50/70
100% Functions 10/10
86.82% Lines 112/129
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 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600                                                                                                                                                    31x                         5x 5x         5x           5x       1x       1x 1x             4x           2x       2x           2x             2x 2x                 2x             5x         5x         5x 5x               5x 5x         5x 1x                             6x 6x         6x         6x       1x       1x 1x           5x         2x             2x           2x           3x       3x                 3x             6x         6x         6x   6x 6x             6x 6x         6x 1x                         6x 6x         6x       1x       1x 1x             5x     5x           5x       2x         3x           6x         6x           6x 6x         6x 1x                             7x 7x         7x       1x       1x 1x             6x 1x   5x           6x       2x         4x             7x         7x         7x         7x 7x         7x 1x                             180x         180x 180x 180x 180x         180x                             30x         30x 30x 30x 30x   30x         30x                         210x         210x 210x 210x 210x         210x 210x                   210x 210x 210x         210x                           32x 19x         19x   19x 17x               31x                        
/**
 * Imports the config module
 * @module config
 */
import config from "../config.js";
 
/**
 * Imports the Core Module
 *
 * @module core/Core
 */
import Core from "../core/core.js";
 
/**
 * Imports the Callbacks Module
 *
 * @module utilities/Callbacks
 */
import Callbacks from "../utilities/callbacks.js";
 
/**
 * Imports the Checks Module
 *
 * @module utilities/Checks
 */
import Checks from "../utilities/checks.js";
 
/**
 * Imports the Play Pause Elements Module
 *
 * @module visual/PlayPauseElements
 */
import PlayPauseElements from "../visual/playPauseElements.js";
 
/**
 * Imports the Song Slider Elements Module
 *
 * @module visual/SongSliderElements
 */
import SongSliderElements from "../visual/songSliderElements.js";
 
/**
 * Imports the Song Played Progress Elements Module
 *
 * @module visual/SongPlayedProgressElements
 */
import SongPlayedProgressElements from "../visual/songPlayedProgressElements.js";
 
/**
 * Imports the Time Elements Module
 *
 * @module visual/TimeElements
 */
import TimeElements from "../visual/timeElements.js";
 
/**
 * Meta Data Elements Module
 *
 * @module visual/MetaDataElements
 */
import MetaDataElements from "../visual/metaDataElements.js";
 
/**
 * Container Elements Module
 *
 * @module visual/ContainerElements
 */
import ContainerElements from "../visual/containerElements.js";
 
/**
 * AmplitudeJS Audio Navigation Utility.
 *
 * @module utilities/AudioNavigation
 */
let AudioNavigation = (function() {
  /**
   * Sets the next song
   *
   * @access public
   * @param {boolean} [songEnded=false] If the song ended, this is set to true
   * so we take into effect the repeat setting.
   */
  function setNext(songEnded = false) {
    /*
      Initializes the next index variable. This will be the
      index of the song that is next.
    */
    let nextIndex = null;
    let nextSong = {};
 
    /*
      Ensure we don't loop in the playlist if config.repeat is not true
    */
    let endOfList = false;
 
    /*
      Determines if we are repeating the song or not. If we are repeating,
      the next song will be the same song index.
    */
    if (config.repeat_song) {
      /*
        If the playlist is shuffled, get the now playing index.
      */
      Iif (config.shuffle_on) {
        nextIndex = config.active_index;
        nextSong = config.shuffle_list[nextIndex];
      } else {
        nextIndex = config.active_index;
        nextSong = config.songs[nextIndex];
      }
    } else {
      /*
        If the shuffle is on, we use the shuffled list of
        songs to determine our next song.
      */
      if (config.shuffle_on) {
        /*
          If the active shuffle index + 1 is less than the length, then
          we use the next shuffle otherwise we go to the beginning
          of the shuffle list.
        */
        Eif (parseInt(config.active_index) + 1 < config.shuffle_list.length) {
          /*
            Set the next index to be the index of the song in the shuffle list.
          */
          nextIndex = parseInt(config.active_index) + 1;
        } else {
          nextIndex = 0;
          endOfList = true;
        }
 
        nextSong = config.shuffle_list[nextIndex];
      } else {
        /*
          If the active index + 1 is less than the length of the songs, then
          we use the next song otherwise we go to the beginning of the
          song list.
        */
        Eif (parseInt(config.active_index) + 1 < config.songs.length) {
          nextIndex = parseInt(config.active_index) + 1;
        } else {
          nextIndex = 0;
          endOfList = true;
        }
 
        /*
          Sets the next index.
        */
        nextSong = config.songs[nextIndex];
      }
    }
 
    /*
      Change the song after the next button has been pressed.
    */
    changeSong(nextSong, nextIndex);
 
    /*
   		If it's the end of the list and repeat is not on, do nothing.
   	*/
    Iif (endOfList && !config.repeat) {
    } else {
      /*
   			If the song has ended and repeat is on, play the song.
   		*/
      Eif (!(songEnded && !config.repeat && endOfList)) {
        Core.play();
      }
    }
 
    /*
      Sync the play pause elements and run the
      after next callback.
    */
    PlayPauseElements.sync();
    Callbacks.run("next");
 
    /*
      If we repeated the song, run the repeat song callback.
    */
    if (config.repeat_song) {
      Callbacks.run("song_repeated");
    }
  }
 
  /**
   * Sets the next song in a playlist
   *
   * @param {string} playlist - The playlist being shuffled
   * @param {boolean} [songEnded=false] - If the song ended, this is set to true
   * so we take into effect the repeat setting.
   */
  function setNextPlaylist(playlist, songEnded = false) {
    /*
      Initializes the next index
    */
    let nextIndex = null;
    let nextSong = {};
 
    /*
      Ensure we don't loop in the playlist if config.repeat is not true
    */
    let endOfList = false;
 
    /*
      If we are repeating the song, then we just start the song over.
    */
    if (config.repeat_song) {
      /*
        If the playlist is shuffled, get the now playing index.
      */
      Iif (config.playlists[playlist].shuffle) {
        nextIndex = config.playlists[playlist].active_index;
        nextSong = config.playlists[playlist].shuffle_list[nextIndex];
      } else {
        nextIndex = config.playlists[playlist].active_index;
        nextSong = config.playlists[playlist].songs[nextIndex];
      }
    } else {
      /*
        If the playlist is shuffled we get the next index of the playlist.
      */
      if (config.playlists[playlist].shuffle) {
        /*
          If the active shuffle index + 1 is less than the length of the shuffle list,
          then we use the next shuffle otherwise we go to the beginning of the shuffle list.
        */
        Eif (
          parseInt(config.playlists[playlist].active_index) + 1 <
          config.playlists[playlist].shuffle_list.length
        ) {
          /*
            Set the next index to be the index of the song in the shuffle list.
          */
          nextIndex = config.playlists[playlist].active_index + 1;
        } else {
          nextIndex = 0;
          endOfList = true;
        }
 
        nextSong = config.playlists[playlist].shuffle_list[nextIndex];
      } else {
        /*
          If the active index +1 is less than the length of the songs in the playlist,
          then we use the next song otherwise we go to the beginning of the playlist.
        */
        Eif (
          parseInt(config.playlists[playlist].active_index) + 1 <
          config.playlists[playlist].songs.length
        ) {
          nextIndex = parseInt(config.playlists[playlist].active_index) + 1;
        } else {
          nextIndex = 0;
          endOfList = true;
        }
 
        /*
          Sets the next song.
        */
        nextSong = config.playlists[playlist].songs[nextIndex];
      }
    }
 
    /*
      Sets the active playlist to the playlist we are on.
    */
    setActivePlaylist(playlist);
 
    /*
      Change the song within the playlist.
    */
    changeSongPlaylist(playlist, nextSong, nextIndex);
 
    /*
      If it's the end of the playlist and we aren't repeating, do nothing.
    */
    Iif (endOfList && !config.repeat) {
    } else {
      Eif (!(songEnded && !config.repeat && endOfList)) {
        Core.play();
      }
    }
 
    /*
      Sync the play pause buttons.
    */
    PlayPauseElements.sync();
    Callbacks.run("next");
 
    /*
      Repeat the song.
    */
    if (config.repeat_song) {
      Callbacks.run("song_repeated");
    }
  }
 
  /**
   * Sets the previous song on the global songs array.
   *
   * @access private
   */
  function setPrevious() {
    /*
      Initializes the previous index
    */
    let previousIndex = null;
    let previousSong = {};
 
    /*
      If we are repeating the song, then we just start the song over.
    */
    if (config.repeat_song) {
      /*
        If the config is shuffled, get the now playing index.
      */
      Iif (config.shuffle_on) {
        previousIndex = config.active_index;
        previousSong = config.shuffle_list[previousIndex];
      } else {
        previousIndex = config.active_index;
        previousSong = config.songs[previousIndex];
      }
    } else {
      /*
        Get the previous index. If the previous index will be less than 0, get the
        last song of the array and continue.
      */
      Iif (parseInt(config.active_index) - 1 >= 0) {
        previousIndex = parseInt(config.active_index - 1);
      } else {
        previousIndex = parseInt(config.songs.length - 1);
      }
 
      /*
        If the config is shuffled, we grab the song from the shuffle list
      */
      if (config.shuffle_on) {
        /*
          Grab song from the shuffle list
        */
        previousSong = config.shuffle_list[previousIndex];
      } else {
        /*
          Grab song from the songs array
        */
        previousSong = config.songs[previousIndex];
      }
    }
    /*
      Change the song after the next button has been pressed.
    */
    changeSong(previousSong, previousIndex);
 
    /*
      Play the newest song.
    */
    Core.play();
 
    /*
      Sync the play pause elements and run the
      after next callback.
    */
    PlayPauseElements.sync();
    Callbacks.run("prev");
 
    /*
      If we repeated the song, run the repeat song callback.
    */
    if (config.repeat_song) {
      Callbacks.run("song_repeated");
    }
  }
 
  /**
   * Sets the previous playlist song.
   *
   * @access private
   *
   * @prop {string} playlist  - The playlist we are navigating in.
   */
  function setPreviousPlaylist(playlist) {
    /*
      Initializes the previous index
    */
    let previousIndex = null;
    let previousSong = {};
 
    /*
      If we are repeating the song, then we just start the song over.
    */
    if (config.repeat_song) {
      /*
        If the playlist is shuffled, get the now playing index.
      */
      Iif (config.playlists[playlist].shuffle) {
        previousIndex = config.playlists[playlist].active_index;
        previousSong = config.playlists[playlist].shuffle_list[previousIndex];
      } else {
        previousIndex = config.playlists[playlist].active_index;
        previousSong = config.playlists[playlist].songs[previousIndex];
      }
    } else {
      /*
        Get the previous index. If the previous index will be less than 0, get the
        last song of the array and continue.
      */
      if (parseInt(config.playlists[playlist].active_index) - 1 >= 0) {
        previousIndex = parseInt(config.playlists[playlist].active_index - 1);
      } else {
        previousIndex = parseInt(config.playlists[playlist].songs.length - 1);
      }
 
      /*
        If the playlist is shuffled, we grab the song from the shuffle list
      */
      if (config.playlists[playlist].shuffle) {
        /*
          Grab song from the shuffle list
        */
        previousSong = config.playlists[playlist].shuffle_list[previousIndex];
      } else {
        /*
          Grab song from the songs array
        */
        previousSong = config.playlists[playlist].songs[previousIndex];
      }
    }
 
    /*
      Sets the active playlist to the playlist we are on.
    */
    setActivePlaylist(playlist);
 
    /*
      Change the song within the playlist.
    */
    changeSongPlaylist(playlist, previousSong, previousIndex);
 
    /*
      Plays the song
    */
    Core.play();
 
    /*
      Sync the play pause buttons.
    */
    PlayPauseElements.sync();
    Callbacks.run("prev");
 
    /*
      Repeat the song.
    */
    if (config.repeat_song) {
      Callbacks.run("song_repeated");
    }
  }
 
  /**
   * Change song in the songs array.
   *
   * @access private
   * @prop {object} song  - The song we are changing to.
   * @prop {number} index - The index we are changing to.
   */
  function changeSong(song, index) {
    /*
      Prepare the song change.
    */
    prepareSongChange(song);
 
    /*
      Change the song.
    */
    config.audio.src = song.url;
    config.active_metadata = song;
    config.active_album = song.album;
    config.active_index = parseInt(index);
 
    /*
      Set new information now that the song has changed.
    */
    afterSongChange();
  }
 
  /**
   * Handles a song change in the playlist
   *
   * @access private
   * @prop {string} playlist - The playlist we are changing the song on.
   * @prop {object} song     - The song we are changing to in the playlist.
   * @prop {number} index    - The inded of the song we are changing to in the playlist.
   */
  function changeSongPlaylist(playlist, song, index) {
    /*
      Prepare the song change.
    */
    prepareSongChange(song);
 
    /*
      Change the song.
    */
    config.audio.src = song.url;
    config.active_metadata = song;
    config.active_album = song.album;
    config.active_index = null;
 
    config.playlists[playlist].active_index = parseInt(index);
 
    /*
      Set new information now that the song has changed.
    */
    afterSongChange();
  }
 
  /**
   *  Prepares a song change
   *
   * @access private
   * @prop {object} song  - The song we change to.
   */
  function prepareSongChange(song) {
    /*
      Stop the current song.
    */
    Core.stop();
 
    /*
      Sync all of the elements to a stopped song.
    */
    PlayPauseElements.syncToPause();
    SongSliderElements.resetElements();
    SongPlayedProgressElements.resetElements();
    TimeElements.resetCurrentTimes();
 
    /*
      If an album changes, fire an album change.
    */
    Eif (Checks.newAlbum(song)) {
      Callbacks.run("album_change");
    }
  }
 
  /**
   * Updates data on the display after a song has changed.
   *
   * @access private
   */
  function afterSongChange() {
    MetaDataElements.displayMetaData();
    ContainerElements.setActive();
    TimeElements.resetDurationTimes();
 
    /*
      Run the song change callback.
    */
    Callbacks.run("song_change");
  }
 
  /**
   * Sets the active playlist
   *
   * @access public
   * @param {string} playlist - The string of the playlist being set to active.
   */
  function setActivePlaylist(playlist) {
    /*
      If the active playlist is different than the playlist being set,
      we run the `playlist_changed` callback.
    */
    if (config.active_playlist != playlist) {
      Callbacks.run("playlist_changed");
      /*
        Set the active playlist to the playlist parameter. Only need to
        set if it's different.
      */
      config.active_playlist = playlist;
 
      if (playlist != null) {
        config.playlists[playlist].active_index = 0;
      }
    }
  }
 
  /*
    Return the publically facing methods
  */
  return {
    setNext: setNext,
    setNextPlaylist: setNextPlaylist,
    setPrevious: setPrevious,
    setPreviousPlaylist: setPreviousPlaylist,
    changeSong: changeSong,
    changeSongPlaylist: changeSongPlaylist,
    setActivePlaylist: setActivePlaylist
  };
})();
 
export default AudioNavigation;