All files / src/visual playPauseElements.js

100% Statements 53/53
100% Branches 28/28
100% Functions 9/9
100% Lines 53/53
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                    31x             66x 66x 66x 66x                       68x         68x               68x       80x     80x             80x         18x   10x 10x   8x 8x                       67x         67x                 67x       35x                 35x         15x   8x 8x   7x 7x                       68x           68x                 68x       25x             25x         16x   9x 9x   7x 7x                         66x     66x                 66x                       66x         14x   8x 8x   6x 6x                           370x         370x 115x                       35x 35x                     143x 143x           31x                      
/**
 * Imports the config module
 * @module config
 */
import config from "../config.js";
 
/**
 * Defines the visual representation of AmplitudeJS play pause elements.
 * @module visual/PlayPauseElements
 */
let PlayPauseElements = (function() {
  /**
   * Syncs all play pause elements.
   *
   * @access public
   */
  function sync() {
    syncGlobal();
    syncPlaylist();
    syncSong();
    syncSongInPlaylist();
  }
 
  /**
   * Syncs the global play pause buttons to the state of the active song.
   *
   * @access public
   */
  function syncGlobal() {
    /*
      Get the active song state.
    */
    let state = config.audio.paused ? "paused" : "playing";
 
    /*
      Get all play pause buttons.
    */
    const playPauseElements = document.querySelectorAll(
      ".amplitude-play-pause"
    );
 
    /*
      Iterate over all of the play pause elements syncing the
      display visually.
    */
    for (let i = 0; i < playPauseElements.length; i++) {
      /*
        Grab the playlist and song attributes from the element.
      */
      let playlist = playPauseElements[i].getAttribute(
        "data-amplitude-playlist"
      );
      let song = playPauseElements[i].getAttribute("data-amplitude-song-index");
 
      /*
        This method is responsible for only the global elements,
        so we make sure there are no playlist or songs defined on
        the element.
      */
      if (playlist == null && song == null) {
        /*
          Determines what classes we should add and remove
          from the elements.
        */
        switch (state) {
          case "playing":
            setElementPlay(playPauseElements[i]);
            break;
          case "paused":
            setElementPause(playPauseElements[i]);
            break;
        }
      }
    }
  }
 
  /**
   * Syncs the main playlist play pause buttons to the state of the active song.
   *
   * @access public
   */
  function syncPlaylist() {
    let state = config.audio.paused ? "paused" : "playing";
 
    /*
      Get all of the main playlist play pause elements
    */
    const playlistPlayPauseElements = document.querySelectorAll(
      '.amplitude-play-pause[data-amplitude-playlist="' +
        config.active_playlist +
        '"]'
    );
 
    /*
      Iterate over the play pause elements, syncing the state accordingly.
    */
    for (let i = 0; i < playlistPlayPauseElements.length; i++) {
      /*
        Grab the song attributes from the element.
      */
      let song = playlistPlayPauseElements[i].getAttribute(
        "data-amplitude-song-index"
      );
 
      /*
        We want only the play pause elements for the main on a
        playlist nothing else. We have another method for the
        song in playlist play pause method.
      */
      if (song == null) {
        /*
          Determines what classes we should add and remove
          from the elements.
        */
        switch (state) {
          case "playing":
            setElementPlay(playlistPlayPauseElements[i]);
            break;
          case "paused":
            setElementPause(playlistPlayPauseElements[i]);
            break;
        }
      }
    }
  }
 
  /**
   * Syncs the song play pause buttons to the state of the active song.
   *
   * @access public
   */
  function syncSong() {
    let state = config.audio.paused ? "paused" : "playing";
 
    /*
      Get all of the individual song play pause buttons. These have an
      amplitude-song-index that matches the active index attribute.
    */
    let songPlayPauseElements = document.querySelectorAll(
      '.amplitude-play-pause[data-amplitude-song-index="' +
        config.active_index +
        '"]'
    );
 
    /*
      Iterate over all of the song play pause elements
    */
    for (let i = 0; i < songPlayPauseElements.length; i++) {
      /*
        Grab the playlist attributes from the element.
      */
      let playlist = songPlayPauseElements[i].getAttribute(
        "data-amplitude-playlist"
      );
 
      /*
        We want only the song play pause buttons, not ones scoped in a playlist.
      */
      if (playlist == null) {
        /*
          Determines what classes we should add and remove
          from the elements.
        */
        switch (state) {
          case "playing":
            setElementPlay(songPlayPauseElements[i]);
            break;
          case "paused":
            setElementPause(songPlayPauseElements[i]);
            break;
        }
      }
    }
  }
 
  /**
   * Syncs the song in playlist play pause buttons to the state of
   * the active song.
   *
   * @access public
   */
  function syncSongInPlaylist() {
    let state = config.audio.paused ? "paused" : "playing";
 
    let activePlaylistIndex =
      config.active_playlist != "" && config.active_playlist != null
        ? config.playlists[config.active_playlist].active_index
        : null;
 
    /*
      Get all of the individual song play pause buttons. These have an
      amplitude-song-index attribute. Some have amplitude-playlist which
      means they are individual songs within a playlist.
    */
    let songInPlaylistPlayPauseElements = document.querySelectorAll(
      '.amplitude-play-pause[data-amplitude-song-index="' +
        activePlaylistIndex +
        '"][data-amplitude-playlist="' +
        config.active_playlist +
        '"]'
    );
 
    /*
      Iterate over all of the individual play pause elements for songs inspect
      a playlist.
    */
    for (let i = 0; i < songInPlaylistPlayPauseElements.length; i++) {
      /*
        Determines what classes we should add and remove
        from the elements.
      */
      switch (state) {
        case "playing":
          setElementPlay(songInPlaylistPlayPauseElements[i]);
          break;
        case "paused":
          setElementPause(songInPlaylistPlayPauseElements[i]);
          break;
      }
    }
  }
 
  /**
   * Sets all of the play pause buttons to paused.
   *
   * @access public
   */
  function syncToPause() {
    /*
      Gets all of the play pause elements
    */
    let playPauseElements = document.querySelectorAll(".amplitude-play-pause");
 
    /*
      Sets all of the elements to pause
    */
    for (let i = 0; i < playPauseElements.length; i++) {
      setElementPause(playPauseElements[i]);
    }
  }
 
  /**
   * Sets an element to be playing by removing the 'amplitude-paused' class
   * and adding the 'amplitude-playing' class
   *
   * @access public
   * @param {element} element 	- The element getting the playing class added.
   */
  function setElementPlay(element) {
    element.classList.add("amplitude-playing");
    element.classList.remove("amplitude-paused");
  }
 
  /**
   * Sets an element to be paused by adding the 'amplitude-paused' class
   * and removing the 'amplitude-playing' class
   *
   * @access public
   * @param {element} element 	- The element getting the paused class added.
   */
  function setElementPause(element) {
    element.classList.remove("amplitude-playing");
    element.classList.add("amplitude-paused");
  }
 
  /**
   * Returns the public facing methods
   */
  return {
    sync: sync,
    syncGlobal: syncGlobal,
    syncPlaylist: syncPlaylist,
    syncSong: syncSong,
    syncSongInPlaylist: syncSongInPlaylist,
    syncToPause: syncToPause
  };
})();
 
export default PlayPauseElements;