All files / src/visual shuffleElements.js

100% Statements 17/17
100% Branches 6/6
100% Functions 3/3
100% Lines 17/17
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                    31x                   177x         177x         16x             8x 2x 2x   6x 6x                               11x             11x             4x 2x 2x   2x 2x               31x              
/**
 * Imports the config module
 * @module config
 */
import config from "../config.js";
 
/**
 * Handles all of the shuffle elements
 * @module visual/ShuffleElements
 */
let ShuffleElements = (function() {
  /**
   * Syncs the global shuffle button visual state.
   *
   * @access public
   */
  function syncMain() {
    /*
			Gets the shuffle buttons.
		*/
    let shuffleButtons = document.getElementsByClassName("amplitude-shuffle");
 
    /*
			Iterate over all of the shuffle buttons.
		*/
    for (let i = 0; i < shuffleButtons.length; i++) {
      /*
				Ensure the shuffle button doesn't belong to a playlist. We have
				a separate method for that.
			*/
      if (shuffleButtons[i].getAttribute("data-amplitude-playlist") == null) {
        /*
					If the state of the player is shuffled on, true, then
					we add the 'amplitude-shuffle-on' class and remove the
					'amplitude-shuffle-off' class. If the player is not shuffled
					then we do the opposite.
				*/
        if (config.shuffle_on) {
          shuffleButtons[i].classList.add("amplitude-shuffle-on");
          shuffleButtons[i].classList.remove("amplitude-shuffle-off");
        } else {
          shuffleButtons[i].classList.add("amplitude-shuffle-off");
          shuffleButtons[i].classList.remove("amplitude-shuffle-on");
        }
      }
    }
  }
 
  /**
   * Syncs the playlist shuffle button visual state.
   *
   * @access public
   * @param {string} playlist - The playlist string the shuffle button belongs to.
   */
  function syncPlaylist(playlist) {
    /*
			Gets all of the shuffle buttons.
		*/
    let shuffleButtons = document.querySelectorAll(
      '.amplitude-shuffle[data-amplitude-playlist="' + playlist + '"]'
    );
 
    /*
			Iterate over all of the shuffle buttons
		*/
    for (let i = 0; i < shuffleButtons.length; i++) {
      /*
				If the state of the playlist is shuffled on, true, then
				we add the 'amplitude-shuffle-on' class and remove the
				'amplitude-shuffle-off' class. If the player is not shuffled
				then we do the opposite.
			*/
      if (config.playlists[playlist].shuffle) {
        shuffleButtons[i].classList.add("amplitude-shuffle-on");
        shuffleButtons[i].classList.remove("amplitude-shuffle-off");
      } else {
        shuffleButtons[i].classList.add("amplitude-shuffle-off");
        shuffleButtons[i].classList.remove("amplitude-shuffle-on");
      }
    }
  }
 
  /**
   * Returns public facing methods
   */
  return {
    syncMain: syncMain,
    syncPlaylist: syncPlaylist
  };
})();
 
export default ShuffleElements;