All files / src/fx visualizations.js

9.68% Statements 9/93
1.75% Branches 2/114
28.57% Functions 4/14
9.68% Lines 9/93
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 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526                                  31x               56x             56x                                                                                                                         56x                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                 295x             295x                                                                           56x       56x                                                                                                                                                                                                           31x                
/**
 * Imports the config module
 * @module config
 */
import config from "../config.js";
 
/**
 * Imports the debug module
 * @module utilities/Debug
 */
import Debug from "../utilities/debug.js";
 
/**
 * Handles the visualizations elements.
 *
 * @module Visualizations
 */
let Visualizations = (function() {
  /**
   * Runs all of the visualizations on the screen.
   */
  function run() {
    /*
      Get all of the visualization elements on the page
    */
    let visualizationElements = document.querySelectorAll(
      ".amplitude-visualization"
    );
 
    /*
      If the web audio API is available, we display the visualizations.
    */
    Iif (config.web_audio_api_available) {
      /*
        If the visualization has not started, there are visualizations available,
        and we have at least one visualization element, then we continue.
      */
      if (
        Object.keys(config.visualizations.available).length > 0 &&
        visualizationElements.length > 0
      ) {
        /*
            Iterate over all of the visualizations on the page and activate the
            ones we need.
          */
        for (let i = 0; i < visualizationElements.length; i++) {
          /*
              Grab the playlist and song attributes from the visualization to
              determine which one we run.
            */
          let playlist = visualizationElements[i].getAttribute(
            "data-amplitude-playlist"
          );
          let song = visualizationElements[i].getAttribute(
            "data-amplitude-song-index"
          );
 
          /*
              If the playlist and song are null, it's a global visualization element.
            */
          if (playlist == null && song == null) {
            runGlobalVisualization(visualizationElements[i]);
          }
 
          /*
              if the playlist is not null and the song is null it's a playlist visualization
              element.
            */
          if (playlist != null && song == null) {
            runPlaylistVisualization(visualizationElements[i], playlist);
          }
 
          /*
              If the playlist is null and the song is not null it's a song visualization element.
            */
          if (playlist == null && song != null) {
            runSongVisualization(visualizationElements[i], song);
          }
 
          /*
              If the playlist and song are not null then it's a song in playlist visualization
              element.
            */
          if (playlist != null && song != null) {
            runSongInPlaylistVisualization(
              visualizationElements[i],
              playlist,
              song
            );
          }
        }
      }
    } else {
      displayBackups();
    }
  }
 
  /**
   * Runs a global visualization
   *
   * @param {Node} element  The container element that handles the visualization.
   */
  function runGlobalVisualization(element) {
    /*
      Gets the global visualization index and the active song visualization indexes
      so we know which visualization to use. The song will override the global
    */
    let globalVisualizationIndex = config.visualization;
    let activeSongVisualizationIndex =
      config.active_index != null
        ? config.songs[config.active_index].visualization
        : config.playlists[config.active_playlist].songs[
            config.playlists[config.active_playlist].active_index
          ].visualization;
 
    /*
      If the active song visualization is defined and the visualization exists,
      use that visualization.
    */
    if (
      activeSongVisualizationIndex != undefined &&
      config.visualizations.available[activeSongVisualizationIndex] != undefined
    ) {
      addToActiveVisualizations(activeSongVisualizationIndex, element);
 
      /*
      If the user defined a global visualization, use that one.
    */
    } else if (
      globalVisualizationIndex != undefined &&
      config.visualizations.available[globalVisualizationIndex] != undefined
    ) {
      addToActiveVisualizations(globalVisualizationIndex, element);
 
      /*
      If the user didn't define a global visualization, use the first visualization
      registered if there is one.
    */
    } else {
      /*
        Grab the first registered visualization. If it exists, use that one.
      */
      let firstVisualization =
        Object.keys(config.visualizations.available).length > 0
          ? Object.keys(config.visualizations.available)[0]
          : null;
 
      if (firstVisualization != null) {
        addToActiveVisualizations(firstVisualization, element);
      }
    }
  }
 
  /**
   * Run a specific playlist visualization.
   *
   * @param {Node} element  The container element that handles the visualization.
   * @param {string} playlist The key of the playlist we are running the visualization for.
   */
  function runPlaylistVisualization(element, playlist) {
    /*
      If the playlist is equal to the active playlist, then we continue.
    */
    if (playlist == config.active_playlist) {
      /*
        Checks if the song has a visualization and that visualization exists,
        run that visualization.
      */
      let activeSongVisualizationIndex =
        config.playlists[config.active_playlist].songs[
          config.playlists[config.active_playlist].active_index
        ].visualization;
      let activePlaylistVisualizationIndex =
        config.playlists[config.active_playlist].visualization;
      let globalVisualizationIndex = config.visualization;
 
      /*
        If the actual song has a visualization, we run that.
      */
      if (
        activeSongVisualizationIndex != undefined &&
        config.visualizations.available[activeSongVisualizationIndex] !=
          undefined
      ) {
        addToActiveVisualizations(activeSongVisualizationIndex, element);
 
        /*
        If the actual playlist has a visualization, run that.
      */
      } else if (
        activePlaylistVisualizationIndex != undefined &&
        config.visualizations.available[activePlaylistVisualizationIndex] !=
          undefined
      ) {
        addToActiveVisualizations(activePlaylistVisualizationIndex, element);
 
        /*
        If a global visualization is defined, run that.
      */
      } else if (
        globalVisualizationIndex != undefined &&
        config.visualizations.available[globalVisualizationIndex] != undefined
      ) {
        addToActiveVisualizations(globalVisualizationIndex, element);
      } else {
        /*
          Grab the first registered visualization. If it exists, use that one.
        */
        let firstVisualization =
          Object.keys(config.visualizations.available).length > 0
            ? Object.keys(config.visualizations.available)[0]
            : null;
 
        if (firstVisualization != null) {
          addToActiveVisualizations(firstVisualization, element);
        }
      }
    }
  }
 
  /**
   * Run a song specific visualization.
   *
   * @param {Node} element The container element that handles the visualization.
   * @param {string} song The song index that we are running the visualization for.
   */
  function runSongVisualization(element, song) {
    /*
      If the song is equal to the active song, then we continue.
    */
    if (song == config.active_index) {
      /*
        Get the indexes of the song
      */
      let activeSongVisualizationIndex =
        config.songs[config.active_index].visualization;
      let globalVisualizationIndex = config.visualization;
 
      /*
        If the song has a visualization, run that.
      */
      if (
        activeSongVisualizationIndex != undefined &&
        config.visualizations.available[activeSongVisualizationIndex] !=
          undefined
      ) {
        addToActiveVisualizations(activeSongVisualizationIndex, element);
 
        /*
        If the global visualization is set, use that.
      */
      } else if (
        globalVisualizationIndex != undefined &&
        config.visualizations.available[globalVisualizationIndex] != undefined
      ) {
        addToActiveVisualizations(globalVisualizationIndex, element);
      } else {
        /*
          Grab the first registered visualization. If it exists, use that one.
        */
        let firstVisualization =
          Object.keys(config.visualizations.available).length > 0
            ? Object.keys(config.visualizations.available)[0]
            : null;
 
        if (firstVisualization != null) {
          addToActiveVisualizations(firstVisualization, element);
        }
      }
    }
  }
 
  /**
   * Run a song in playlist visualization.
   *
   * @param {Node} element - The element containing the visualization.
   * @param {string} playlist - The string of the playlist key.
   * @param {index} song - The index of the song in the playlist.
   */
  function runSongInPlaylistVisualization(element, playlist, song) {
    /*
      If the playlist is the same as the active playlist and the active
      index of the song is the same as the song, then we continue.
    */
    if (
      playlist == config.active_playlist &&
      config.playlists[playlist].active_index == song
    ) {
      /*
        Checks if the song has a visualization and that visualization exists,
        run that visualization.
      */
      let activeSongVisualizationIndex =
        config.playlists[config.active_playlist].songs[
          config.playlists[config.active_playlist].active_index
        ].visualization;
      let activePlaylistVisualizationIndex =
        config.playlists[config.active_playlist].visualization;
      let globalVisualizationIndex = config.visualization;
 
      /*
        If the active song has a visualization, we use that.
      */
      if (
        activeSongVisualizationIndex != undefined &&
        config.visualizations.available[activeSongVisualizationIndex] !=
          undefined
      ) {
        addToActiveVisualizations(activeSongVisualizationIndex, element);
 
        /*
        If the active playlist has a visualization, we use that.
      */
      } else if (
        activePlaylistVisualizationIndex != undefined &&
        config.visualizations.available[activePlaylistVisualizationIndex] !=
          undefined
      ) {
        addToActiveVisualizations(activePlaylistVisualizationIndex, element);
 
        /*
        If the global visualization has been set, we use that.
      */
      } else if (
        globalVisualizationIndex != undefined &&
        config.visualizations.available[globalVisualizationIndex] != undefined
      ) {
        addToActiveVisualizations(globalVisualizationIndex, element);
      } else {
        /*
          Grab the first registered visualization. If it exists, use that one.
        */
        let firstVisualization =
          Object.keys(config.visualizations.available).length > 0
            ? Object.keys(config.visualizations.available)[0]
            : null;
 
        if (firstVisualization != null) {
          addToActiveVisualizations(firstVisualization, element);
        }
      }
    }
  }
 
  /**
   * Add a visualization to the array of active visualizations.
   *
   * @param {string} key - The key of the active visualization.
   * @param {Node} element - The element that the visualization will be applied to.
   */
  function addToActiveVisualizations(key, element) {
    let visualization = new config.visualizations.available[key]["object"]();
    visualization.setPreferences(
      config.visualizations.available[key]["preferences"]
    );
    visualization.startVisualization(element);
    config.visualizations.active.push(visualization);
  }
 
  /**
   * Stops all active visualizations.
   */
  function stop() {
    /*
      Iterates over all of the visualizations and stop the visualization.
    */
    for (let i = 0; i < config.visualizations.active.length; i++) {
      config.visualizations.active[i].stopVisualization();
    }
 
    /*
      Clear the active visualizations.
    */
    config.visualizations.active = [];
  }
 
  /**
   * Registers any visualization we can use.
   *
   * @param {object} visualization The visualization object itself
   * @param {object} preferences User preferences overrides.
   */
  function register(visualization, preferences) {
    /*
      Initialize the new visualization.
    */
    let newVisualization = new visualization();
 
    /*
	    Adds the visualization to the global config so it knows
	    it can be used when playing songs.
 
	    getID is a public function for getting a visualization's id.
	    It becomes the key to access the visualization.
	  */
    config.visualizations.available[newVisualization.getID()] = new Array();
    config.visualizations.available[newVisualization.getID()][
      "object"
    ] = visualization;
    config.visualizations.available[newVisualization.getID()][
      "preferences"
    ] = preferences;
  }
 
  /**
   * Displays the backups for the visualizations.
   */
  function displayBackups() {
    /*
      Get all of the visualization elements on the page
    */
    let visualizationElements = document.querySelectorAll(
      ".amplitude-visualization"
    );
 
    Iif (visualizationElements.length > 0) {
      for (let x = 0; x < visualizationElements.length; x++) {
        /*
          Grab the playlist and song attributes from the visualization to
          determine which one we run.
        */
        let playlist = visualizationElements[x].getAttribute(
          "data-amplitude-playlist"
        );
        let song = visualizationElements[x].getAttribute(
          "data-amplitude-song-index"
        );
 
        /*
          If the playlist and song are null, it's a global visualization element.
        */
        if (playlist == null && song == null) {
          displayGlobalBackup(visualizationElements[x]);
        }
 
        /*
          if the playlist is not null and the song is null it's a playlist visualization
          element.
        */
        if (playlist != null && song == null) {
          displayPlaylistBackup(visualizationElements[x], playlist);
        }
 
        /*
          If the playlist is null and the song is not null it's a song visualization element.
        */
        if (playlist == null && song != null) {
          displaySongBackup(visualizationElements[x], song);
        }
 
        /*
          If the playlist and song are not null then it's a song in playlist visualization
          element.
        */
        if (playlist != null && song != null) {
          displaySongInPlaylistBackup(visualizationElements[x], playlist, song);
        }
      }
    }
  }
 
  /**
   * Displays the global backup which is the cover art of the image in the
   * visualization container.
   *
   * @param {node} element  - The element we are adding the background image to.
   */
  function displayGlobalBackup(element) {
    element.style.backgroundImage =
      "url(" + config.active_metadata.cover_art_url + ")";
  }
 
  /**
   * Displays the playlist backup which is the cover art of the image in the
   * visualization container.
   *
   * @param {node} element  - The element we are adding the background image to.
   */
  function displayPlaylistBackup(element, playlist) {
    if (config.active_playlist == playlist) {
      element.style.backgroundImage =
        "url(" + config.active_metadata.cover_art_url + ")";
    }
  }
 
  /**
   * Displays the song backup which is the cover art of the image in the
   * visualization container.
   *
   * @param {node} element  - The element we are adding the background image to.
   */
  function displaySongBackup(element, song) {
    if (config.active_index == song) {
      element.style.backgroundImage =
        "url(" + config.active_metadata.cover_art_url + ")";
    }
  }
 
  /**
   * Displays the song in playlist backup which is the cover art of the image in the
   * visualization container.
   *
   * @param {node} element  - The element we are adding the background image to.
   */
  function displaySongInPlaylistBackup(element, playlist, song) {
    if (
      config.active_playlist == playlist &&
      config.playlists[active_playlist].active_index == song
    ) {
      element.style.backgroundImage =
        "url(" + config.active_metadata.cover_art_url + ")";
    }
  }
 
  /*
    Returns the public facing methods
  */
  return {
    run: run,
    stop: stop,
    register: register
  };
})();
 
export default Visualizations;