All files / src/visual songPlayedProgressElements.js

9.76% Statements 4/41
0% Branches 0/26
28.57% Functions 2/7
9.76% Lines 4/41
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                      31x                                                                                                                                                                                                                                                                                                                                                         211x       211x         31x              
/**
 * Imports the config module
 * @module config
 */
import config from "../config.js";
 
/**
 * Handles the syncing of the song played progress elements.
 *
 * @module visual/SongPlayedProgressElements
 */
let SongPlayedProgressElements = (function() {
  /**
   * Syncs the song played progress bars. These are HTML5 progress elements.
   *
   * @access private
   * @param {number} songPlayedPercentage  	- The percentage of the song that has been played.
   */
  function sync(songPlayedPercentage) {
    syncGlobal(songPlayedPercentage);
    syncPlaylist(songPlayedPercentage);
    syncSong(songPlayedPercentage);
    syncSongInPlaylist(songPlayedPercentage);
  }
 
  /**
   * Sync how much has been played with a progress bar. This is the global progress bar.
   *
   * @access private
   * @param {number} songPlayedPercentage 	- The percent of the song completed.
   */
  function syncGlobal(percentage) {
    /*
			Ensure that the song completion percentage is a number
		*/
    if (!isNaN(percentage)) {
      /*
				Get all of the song progress bars
			*/
      let songPlayedProgressBars = document.querySelectorAll(
        ".amplitude-song-played-progress"
      );
 
      for (let i = 0; i < songPlayedProgressBars.length; i++) {
        let playlist = songPlayedProgressBars[i].getAttribute(
          "data-amplitude-playlist"
        );
        let songIndex = songPlayedProgressBars[i].getAttribute(
          "data-amplitude-song-index"
        );
 
        if (playlist == null && songIndex == null) {
          let max = songPlayedProgressBars[i].max;
 
          songPlayedProgressBars[i].value = (percentage / 100) * max;
        }
      }
    }
  }
 
  /**
   * Sync how much has been played with a progress bar. This is the playlist progress bar.
   *
   * @access public
   * @param {number} songPlayedPercentage 	- The percent of the song completed.
   */
  function syncPlaylist(percentage) {
    /*
			Ensure that the song completion percentage is a number
		*/
    if (!isNaN(percentage)) {
      /*
				Get all of the song progress bars
			*/
      let songPlayedProgressBars = document.querySelectorAll(
        '.amplitude-song-played-progress[data-amplitude-playlist="' +
          config.active_playlist +
          '"]'
      );
 
      for (let i = 0; i < songPlayedProgressBars.length; i++) {
        let song = songPlayedProgressBars[i].getAttribute(
          "data-amplitude-song-index"
        );
 
        if (song == null) {
          let max = songPlayedProgressBars[i].max;
 
          songPlayedProgressBars[i].value = (percentage / 100) * max;
        }
      }
    }
  }
 
  /**
   * Sync how much has been played with a progress bar. This is for an individual song.
   *
   * @access private
   * @param {number} songPlayedPercentage 	- The percent of the song completed.
   */
  function syncSong(percentage) {
    if (config.active_playlist == null) {
      /*
				Ensure that the song completion percentage is a number
			*/
      if (!isNaN(percentage)) {
        /*
					Get all of the song progress bars
				*/
        let songPlayedProgressBars = document.querySelectorAll(
          '.amplitude-song-played-progress[data-amplitude-song-index="' +
            config.active_index +
            '"]'
        );
 
        for (let i = 0; i < songPlayedProgressBars.length; i++) {
          let playlist = songPlayedProgressBars[i].getAttribute(
            "data-amplitude-playlist"
          );
 
          if (playlist == null) {
            let max = songPlayedProgressBars[i].max;
 
            songPlayedProgressBars[i].value = (percentage / 100) * max;
          }
        }
      }
    }
  }
 
  /**
   * Sync how much has been played with a progress bar. This is for an individual song in playlist.
   *
   * @access private
   * @param {number} songPlayedPercentage 	- The percent of the song completed.
   */
  function syncSongInPlaylist(percentage) {
    /*
			Ensure that the song completion percentage is a number
		*/
    if (!isNaN(percentage)) {
      let activePlaylistIndex =
        config.active_playlist != "" && config.active_playlist != null
          ? config.playlists[config.active_playlist].active_index
          : null;
 
      /*
				Get all of the song progress bars
			*/
      let songPlayedProgressBars = document.querySelectorAll(
        '.amplitude-song-played-progress[data-amplitude-playlist="' +
          config.active_playlist +
          '"][data-amplitude-song-index="' +
          activePlaylistIndex +
          '"]'
      );
 
      /*
        Iterates over all of the song played progress elements
        and sets them accordingly.
      */
      for (let i = 0; i < songPlayedProgressBars.length; i++) {
        let playlist = songPlayedProgressBars[i].getAttribute(
          "data-amplitude-playlist"
        );
        let songIndex = songPlayedProgressBars[i].getAttribute(
          "data-amplitude-song-index"
        );
 
        if (playlist != null && songIndex != null) {
          let max = songPlayedProgressBars[i].max;
 
          songPlayedProgressBars[i].value = (percentage / 100) * max;
        }
      }
    }
  }
 
  /**
   * Sets all of the song played progress bars to 0
   *
   * @access public
   */
  function resetElements() {
    let songPlayedProgressBars = document.getElementsByClassName(
      "amplitude-song-played-progress"
    );
 
    for (let i = 0; i < songPlayedProgressBars.length; i++) {
      songPlayedProgressBars[i].value = 0;
    }
  }
 
  return {
    sync: sync,
    resetElements: resetElements
  };
})();
 
export default SongPlayedProgressElements;