All files / src/visual repeatElements.js

100% Statements 24/24
100% Branches 8/8
100% Functions 4/4
100% Lines 24/24
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                        31x                     5x               5x 8x 4x 4x   4x 4x                           5x         5x         8x                 4x 2x 2x   2x 2x                               164x                   164x 8x 2x 2x   6x 6x               31x                
/**
 * Imports the config module
 * @module config
 */
import config from "../config.js";
 
/**
 * Handles all of the visual syncing to the state of the config for the repeat
 * elements.
 *
 * @module visual/RepeatElements
 */
let RepeatElements = (function() {
  /**
   * Syncs repeat for all of the repeat buttons. Users
   * can apply styles to the 'amplitude-repeat-on' and
   * 'amplitude-repeat-off' classes. They represent the state
   * of the player.
   */
  function syncRepeat() {
    /*
			Gets all of the repeat classes
		*/
    let repeatClasses = document.getElementsByClassName("amplitude-repeat");
 
    /*
			Iterate over all of the repeat classes. If repeat is on,
			then add the 'amplitude-repeat-on' class and remove the
			'amplitude-repeat-off' class. If it's off, then do the
			opposite.
		*/
    for (let i = 0; i < repeatClasses.length; i++) {
      if (config.repeat) {
        repeatClasses[i].classList.add("amplitude-repeat-on");
        repeatClasses[i].classList.remove("amplitude-repeat-off");
      } else {
        repeatClasses[i].classList.remove("amplitude-repeat-on");
        repeatClasses[i].classList.add("amplitude-repeat-off");
      }
    }
  }
 
  /**
   * Syncs repeat for all of the playlist repeat buttons. Users
   * can apply styles to the `amplitude-repeat-on` and `amplitude-repeat-off`
   * classes. They repreent the state of the playlist in the player.
   */
  function syncRepeatPlaylist(playlist) {
    /*
			 Gets all of the repeat buttons.
		 */
    let repeatButtons = document.getElementsByClassName("amplitude-repeat");
 
    /*
			 Iterate over all of the repeat buttons
		 */
    for (let i = 0; i < repeatButtons.length; i++) {
      /*
				 Ensure that the repeat button belongs to matches the
				 playlist we are syncing the state for.
			 */
      if (
        repeatButtons[i].getAttribute("data-amplitude-playlist") == playlist
      ) {
        /*
					 If the state of the playlist is shuffled on, true, then
					 we add the 'amplitude-repeat-on' class and remove the
					 'amplitude-repeat-off' class. If the player is not shuffled
					 then we do the opposite.
				 */
        if (config.playlists[playlist].repeat) {
          repeatButtons[i].classList.add("amplitude-repeat-on");
          repeatButtons[i].classList.remove("amplitude-repeat-off");
        } else {
          repeatButtons[i].classList.add("amplitude-repeat-off");
          repeatButtons[i].classList.remove("amplitude-repeat-on");
        }
      }
    }
  }
 
  /**
   * Syncs repeat for all of the repeat song buttons. Users
   * can apply styles to the 'amplitude-repeat-song-on' and
   * 'amplitude-repeat-song-off' classes. They represent the state
   * of the player.
   */
  function syncRepeatSong() {
    /*
			Gets all of the repeat song classes
		*/
    let repeatSongClasses = document.getElementsByClassName(
      "amplitude-repeat-song"
    );
 
    /*
			Iterate over all of the repeat song classes. If repeat is on,
			then add the 'amplitude-repeat-song-on' class and remove the
			'amplitude-repeat-song-off' class. If it's off, then do the
			opposite.
		*/
    for (let i = 0; i < repeatSongClasses.length; i++) {
      if (config.repeat_song) {
        repeatSongClasses[i].classList.add("amplitude-repeat-song-on");
        repeatSongClasses[i].classList.remove("amplitude-repeat-song-off");
      } else {
        repeatSongClasses[i].classList.remove("amplitude-repeat-song-on");
        repeatSongClasses[i].classList.add("amplitude-repeat-song-off");
      }
    }
  }
 
  /*
    Returns the publically available methods.
  */
  return {
    syncRepeat: syncRepeat,
    syncRepeatPlaylist: syncRepeatPlaylist,
    syncRepeatSong: syncRepeatSong
  };
})();
 
export default RepeatElements;