All files / src/visual metaDataElements.js

41.54% Statements 27/65
20.83% Branches 10/48
100% Functions 5/5
41.54% Lines 27/65
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 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355                      31x                           213x                 213x                 213x         2x         2x     2x                     2x                                                                                               162x         162x                 162x                                                                                                               477x                 477x                 477x                                                                                                                     161x                 161x                 161x 2x     2x       2x 2x         2x         2x           2x               2x                                                   161x           31x                  
/**
 * Imports the config module
 * @module config
 */
import config from "../config.js";
 
/**
 * These methods help display the audio's meta data
 *
 * @module visual/MetaDataElements
 */
let MetaDataElements = (function() {
  /**
   * Displays the active song's metadata. This is called after a song has
   * been changed. This method takes the active song and displays the
   * metadata. So once the new active song is set, we update all of the
   * screen elements.
   *
   * @access public
   */
  function displayMetaData() {
    /*
			Define the image meta data keys. These are managed separately
			since we aren't actually changing the inner HTML of these elements.
		*/
    let imageMetaDataKeys = [
      "cover_art_url",
      "station_art_url",
      "podcast_episode_cover_art_url"
    ];
 
    /*
			Get all of the song info elements
		*/
    let songInfoElements = document.querySelectorAll(
      "[data-amplitude-song-info]"
    );
 
    /*
			Iterate over all of the song info elements. We will either
			set these to the new values, or clear them if the active song
			doesn't have the info set.
		*/
    for (let i = 0; i < songInfoElements.length; i++) {
      /*
				Get the info so we can check if the active meta data has the
				key.
			*/
      let info = songInfoElements[i].getAttribute("data-amplitude-song-info");
 
      /*
				Grab the playlist and song index.
			*/
      let playlist = songInfoElements[i].getAttribute(
        "data-amplitude-playlist"
      );
      let songIndex = songInfoElements[i].getAttribute(
        "data-amplitude-song-index"
      );
 
      /*
				Ensure that we don't set any individual elements now. We set this with the
				sync meta data method. The reason we don't set them here is because
				all individual songs would get the now playing artwork. If the playlists
				match or the element is a main element meaning it doesn't
				belong to a playlist or a song, then we set the song info.
			*/
      Iif (
        songIndex == null &&
        (config.active_playlist == playlist ||
          (playlist == null && songIndex == null))
      ) {
        /*
					If the active metadata has the key, then we set it,
					otherwise we clear it. If it's an image element then
					we default it to the default info if needed.
				*/
        if (config.active_metadata[info] != undefined) {
          if (imageMetaDataKeys.indexOf(info) >= 0) {
            songInfoElements[i].setAttribute(
              "src",
              config.active_metadata[info]
            );
          } else {
            songInfoElements[i].innerHTML = config.active_metadata[info];
          }
        } else {
          /*
						We look for the default album art because
						the actual key didn't exist. If the default album
						art doesn't exist then we set the src attribute
						to null.
					*/
          if (imageMetaDataKeys.indexOf(info) >= 0) {
            if (config.default_album_art != "") {
              songInfoElements[i].setAttribute("src", config.default_album_art);
            } else {
              songInfoElements[i].setAttribute("src", "");
            }
          } else {
            songInfoElements[i].innerHTML = "";
          }
        }
      }
    }
  }
 
  /**
   * Displays the playlist meta data.
   */
  function displayPlaylistMetaData() {
    /*
			Define the image meta data keys. These are managed separately
			since we aren't actually changing the inner HTML of these elements.
		*/
    let imageMetaDataKeys = ["image_url"];
 
    /*
			Get all of the playlist info elements
		*/
    let playlistInfoElements = document.querySelectorAll(
      "[data-amplitude-playlist-info]"
    );
 
    /*
			Iterate over all of the playlist info elements. We will either
			set these to the new values, or clear them if the active song
			doesn't have the info set.
		*/
    for (let i = 0; i < playlistInfoElements.length; i++) {
      /*
				Get the info so we can check if the active meta data has the
				key.
			*/
      let info = playlistInfoElements[i].getAttribute(
        "data-amplitude-playlist-info"
      );
      let playlist = playlistInfoElements[i].getAttribute(
        "data-amplitude-playlist"
      );
 
      if (config.playlists[playlist][info] != undefined) {
        if (imageMetaDataKeys.indexOf(info) >= 0) {
          playlistInfoElements[i].setAttribute(
            "src",
            config.playlists[playlist][info]
          );
        } else {
          playlistInfoElements[i].innerHTML = config.playlists[playlist][info];
        }
      } else {
        /*
					We look for the default album art because
					the actual key didn't exist. If the default album
					art doesn't exist then we set the src attribute
					to null.
				*/
        if (imageMetaDataKeys.indexOf(info) >= 0) {
          if (config.default_playlist_art != "") {
            playlistInfoElements[i].setAttribute(
              "src",
              config.default_playlist_art
            );
          } else {
            playlistInfoElements[i].setAttribute("src", "");
          }
        } else {
          playlistInfoElements[i].innerHTML = "";
        }
      }
    }
  }
 
  /**
   * Sets the first song in the playlist. This is used to fill in the meta
   * data in the playlist
   *
   * @param {object} song 			- The song we are setting to be the first song in the playlist
   * @param {string} playlist 	- Key of the playlist we are setting the first song in
   */
  function setFirstSongInPlaylist(song, playlist) {
    /*
      Define the image meta data keys. These are managed separately
      since we aren't actually changing the inner HTML of these elements.
    */
    let imageMetaDataKeys = [
      "cover_art_url",
      "station_art_url",
      "podcast_episode_cover_art_url"
    ];
 
    /*
      Get all of the song info elements
    */
    let songInfoElements = document.querySelectorAll(
      '[data-amplitude-song-info][data-amplitude-playlist="' + playlist + '"]'
    );
 
    /*
      Iterate over all of the song info elements. We will either
      set these to the new values, or clear them if the active song
      doesn't have the info set.
    */
    for (let i = 0; i < songInfoElements.length; i++) {
      /*
        Get the info so we can check if the active meta data has the
        key.
      */
      let info = songInfoElements[i].getAttribute("data-amplitude-song-info");
 
      /*
        Get the song info element playlist.
      */
      let elementPlaylist = songInfoElements[i].getAttribute(
        "data-amplitude-playlist"
      );
 
      /*
        If the playlists match or the element is a main element, then
        we set the song info.
      */
      if (elementPlaylist == playlist) {
        /*
          If the active metadata has the key, then we set it,
          otherwise we clear it. If it's an image element then
          we default it to the default info if needed.
        */
        if (song[info] != undefined) {
          if (imageMetaDataKeys.indexOf(info) >= 0) {
            songInfoElements[i].setAttribute("src", song[info]);
          } else {
            songInfoElements[i].innerHTML = song[info];
          }
        } else {
          /*
            We look for the default album art because
            the actual key didn't exist. If the default album
            art doesn't exist then we set the src attribute
            to null.
          */
          if (imageMetaDataKeys.indexOf(info) >= 0) {
            if (song.default_album_art != "") {
              songInfoElements[i].setAttribute("src", song.default_album_art);
            } else {
              songInfoElements[i].setAttribute("src", "");
            }
          } else {
            songInfoElements[i].innerHTML = "";
          }
        }
      }
    }
  }
 
  /**
   * Sets the meta data for songs loaded in the songs array
   */
  function syncMetaData() {
    /*
		 Define the image meta data keys. These are managed separately
		 since we aren't actually changing the inner HTML of these elements.
	 */
    let imageMetaDataKeys = [
      "cover_art_url",
      "station_art_url",
      "podcast_episode_cover_art_url"
    ];
 
    /*
		 Get all of the song info elements
	 */
    let songInfoElements = document.querySelectorAll(
      "[data-amplitude-song-info]"
    );
 
    /*
		 Iterate over all of the song info elements. We will either
		 set these to the new values, or clear them if the active song
		 doesn't have the info set.
	 */
    for (let i = 0; i < songInfoElements.length; i++) {
      let songIndex = songInfoElements[i].getAttribute(
        "data-amplitude-song-index"
      );
      let playlist = songInfoElements[i].getAttribute(
        "data-amplitude-playlist"
      );
 
      Eif (songIndex != null && playlist == null) {
        let info = songInfoElements[i].getAttribute("data-amplitude-song-info");
 
        /*
				 Make sure that the song index they are referencing is defined.
			 */
        Eif (config.songs[songIndex][info] != undefined) {
          /*
					 If it's an image meta data key, then we set the src attribute of
					 the element. Otherwise we set the inner HTML of the element.
				 */
          Iif (imageMetaDataKeys.indexOf(info) >= 0) {
            songInfoElements[i].setAttribute(
              "src",
              config.songs[songIndex][info]
            );
          } else {
            songInfoElements[i].innerHTML = config.songs[songIndex][info];
          }
        }
      }
 
      /*
        If the song index and playlist are not null, continue.
      */
      Iif (songIndex != null && playlist != null) {
        /*
          Get the info we are displaying.
        */
        let info = songInfoElements[i].getAttribute("data-amplitude-song-info");
 
        /*
          Set the meta data accordingly.
        */
        if (config.playlists[playlist].songs[songIndex][info] != undefined) {
          if (imageMetaDataKeys.indexOf(info) >= 0) {
            songInfoElements[i].setAttribute(
              "src",
              config.playlists[playlist].songs[songIndex][info]
            );
          } else {
            songInfoElements[i].innerHTML =
              config.playlists[playlist].songs[songIndex][info];
          }
        }
      }
    }
 
    /*
      Display the playlist meta data.
    */
    displayPlaylistMetaData();
  }
 
  /**
   * Returns publically facing methods
   */
  return {
    displayMetaData: displayMetaData,
    setFirstSongInPlaylist: setFirstSongInPlaylist,
    syncMetaData: syncMetaData,
    displayPlaylistMetaData: displayPlaylistMetaData
  };
})();
 
export default MetaDataElements;