All files / src/visual/time durationCountDownTimeElements.js

9.3% Statements 4/43
0% Branches 0/24
25% Functions 2/8
9.3% Lines 4/43
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                      31x                                                                                                                                                                                                                                       211x       211x                                                                                                                         31x              
/**
 * Imports the config module
 * @module config
 */
import config from "../../config.js";
 
/**
 * Handles all of the duration countdown elements.
 *
 * @module visual/time/DurationCountDownTimeElements.
 */
let DurationCountDownTimeElements = (function() {
  /**
   * Syncs all of the countdown time elements.
   *
   * @param {object} countDownTime - The current time of the audio.
   * @param {object} songDuration - The song duration of the audio.
   */
  function sync(countDownTime, songDuration) {
    let timeRemaining = computeTimeRemaining(countDownTime, songDuration);
 
    syncGlobal(timeRemaining);
    syncPlaylist(timeRemaining);
    syncSong(timeRemaining);
    syncSongInPlaylist(timeRemaining);
  }
 
  /**
   * Syncs the global count down time elements.
   *
   * @param {string} timeRemaining - The time remaining for the audio.
   */
  function syncGlobal(timeRemaining) {
    let durationTimeRemainingSelectors = document.querySelectorAll(
      ".amplitude-time-remaining"
    );
 
    for (let i = 0; i < durationTimeRemainingSelectors.length; i++) {
      let playlist = durationTimeRemainingSelectors[i].getAttribute(
        "data-amplitude-playlist"
      );
      let songIndex = durationTimeRemainingSelectors[i].getAttribute(
        "data-amplitude-song-index"
      );
 
      if (playlist == null && songIndex == null) {
        durationTimeRemainingSelectors[i].innerHTML = timeRemaining;
      }
    }
  }
 
  /**
   * Syncs the playlist count down time elements.
   *
   * @param {string} timeRemaining - The time remaining for the audio.
   */
  function syncPlaylist(timeRemaining) {
    let durationTimeRemainingSelectors = document.querySelectorAll(
      '.amplitude-time-remaining[data-amplitude-playlist="' +
        config.active_playlist +
        '"]'
    );
 
    for (let i = 0; i < durationTimeRemainingSelectors.length; i++) {
      let songIndex = durationTimeRemainingSelectors[i].getAttribute(
        "data-amplitude-song-index"
      );
 
      if (songIndex == null) {
        durationTimeRemainingSelectors[i].innerHTML = timeRemaining;
      }
    }
  }
 
  /**
   * Syncs the song count down time elements.
   *
   * @param {string} timeRemaining - The time remaining for the audio.
   */
  function syncSong(timeRemaining) {
    if (config.active_playlist == null) {
      let durationTimeRemainingSelectors = document.querySelectorAll(
        '.amplitude-time-remaining[data-amplitude-song-index="' +
          config.active_index +
          '"]'
      );
 
      for (let i = 0; i < durationTimeRemainingSelectors.length; i++) {
        let playlist = durationTimeRemainingSelectors[i].getAttribute(
          "data-amplitude-playlist"
        );
 
        if (playlist == null) {
          durationTimeRemainingSelectors[i].innerHTML = timeRemaining;
        }
      }
    }
  }
 
  /**
   * Syncs the song in playlist count down time elements.
   *
   * @param {string} timeRemaining - The time remaining for the audio.
   */
  function syncSongInPlaylist(timeRemaining) {
    let activePlaylistIndex =
      config.active_playlist != "" && config.active_playlist != null
        ? config.playlists[config.active_playlist].active_index
        : null;
 
    let durationTimeRemainingSelectors = document.querySelectorAll(
      '.amplitude-time-remaining[data-amplitude-playlist="' +
        config.active_playlist +
        '"][data-amplitude-song-index="' +
        activePlaylistIndex +
        '"]'
    );
 
    for (let i = 0; i < durationTimeRemainingSelectors.length; i++) {
      durationTimeRemainingSelectors[i].innerHTML = timeRemaining;
    }
  }
 
  /**
   * Resets the count down times.
   */
  function resetTimes() {
    let durationTimeRemainingSelectors = document.querySelectorAll(
      ".amplitude-time-remaining"
    );
 
    for (let i = 0; i < durationTimeRemainingSelectors.length; i++) {
      durationTimeRemainingSelectors[i].innerHTML = "00";
    }
  }
 
  /**
   * Computes the time remaining for the audio.
   *
   * @param {object} currentTime - The current time of the audio.
   * @param {object} songDuration - The duration of the audio.
   */
  function computeTimeRemaining(currentTime, songDuration) {
    let timeRemaining = "00:00";
 
    /*
      Initialize the total current seconds and total duration seconds
    */
    let totalCurrentSeconds =
      parseInt(currentTime.seconds) +
      parseInt(currentTime.minutes) * 60 +
      parseInt(currentTime.hours) * 60 * 60;
    let totalDurationSeconds =
      parseInt(songDuration.seconds) +
      parseInt(songDuration.minutes) * 60 +
      parseInt(songDuration.hours) * 60 * 60;
 
    /*
      If the two variables are numbers we continue the computing.
    */
    if (!isNaN(totalCurrentSeconds) && !isNaN(totalDurationSeconds)) {
      /*
        Find the total remaining seconds.
      */
      let timeRemainingTotalSeconds =
        totalDurationSeconds - totalCurrentSeconds;
 
      var remainingHours = Math.floor(timeRemainingTotalSeconds / 3600);
      var remainingMinutes = Math.floor(
        (timeRemainingTotalSeconds - remainingHours * 3600) / 60
      );
      var remainingSeconds =
        timeRemainingTotalSeconds -
        remainingHours * 3600 -
        remainingMinutes * 60;
 
      timeRemaining =
        (remainingMinutes < 10 ? "0" + remainingMinutes : remainingMinutes) +
        ":" +
        (remainingSeconds < 10 ? "0" + remainingSeconds : remainingSeconds);
 
      if (remainingHours > 0) {
        timeRemaining = remainingHours + ":" + timeRemaining;
      }
    }
 
    return timeRemaining;
  }
 
  /**
   * Returns the publically facing methods.
   */
  return {
    sync: sync,
    resetTimes: resetTimes
  };
})();
 
export default DurationCountDownTimeElements;