All files / src/init playlists.js

91.18% Statements 31/34
43.75% Branches 7/16
100% Functions 9/9
91.18% Lines 31/34
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                                                                      31x                   159x         159x         159x         159x         159x         159x         159x         159x                         159x 477x                             159x       477x       477x       477x 1749x 1749x             1749x                                                       159x       477x         477x       1749x                                                             159x 477x                               159x 477x                               159x 477x                           159x 477x                   31x            
/**
 * Imports the config module
 * @module config
 */
import config from "../config.js";
 
/**
 * AmplitudeJS Debug Module
 * @module utilities/Debug
 */
import Debug from "../utilities/debug.js";
 
/**
 * AmplitudeJS Checks Utility.
 * @module utilities/Checks
 */
import Checks from "../utilities/checks.js";
 
/**
 * AmplitudeJS Visual Meta Data Elements Module
 * @module visual/MetaDataElements
 */
import MetaDataElements from "../visual/metaDataElements.js";
 
/**
 * AmplitudeJS SoundCloud Meta module
 * @module soundcloud/Soundcloud
 */
import SoundCloud from "../soundcloud/soundcloud.js";
 
/**
 * Handles the initialization of the playlists.
 *
 * @module init/PlaylistsInitializer
 */
let PlaylistsInitializer = (function() {
  /**
   * Initializes the playlists for AmplitudeJS
   *
   * @param {Object} playlists - The playlists defined by the user.
   */
  function initialize(playlists) {
    /*
      Copy the playlists over to Amplitude
    */
    config.playlists = playlists;
 
    /*
      Copy songs over from songs array.
    */
    copySongsToPlaylists();
 
    /*
      Grab any SoundCloud Data for the playlist songs if needed.
    */
    grabSoundCloudData();
 
    /*
      Initialize a scoped active index for each playlist.
    */
    initializePlaylistActiveIndexes();
 
    /*
      Initialize the shuffle status of the playlists.
    */
    initializePlaylistShuffleStatuses();
 
    /*
      Initialize the repeat status for the playlits.
    */
    initializePlaylistsRepeatStatuses();
 
    /*
      Initialize temporary place holders for shuffle lists.
    */
    initializePlaylistShuffleLists();
 
    /*
      Initializes the first song in the playlist
    */
    initializeFirstSongInPlaylistMetaData();
  }
 
  /**
   * Initializes a scoped active index for each playlist.
   *
   * @access private
   */
  function initializePlaylistActiveIndexes() {
    /*
  		Iterate over all of the playlists defined by the user
      and add an active index.
		*/
    for (let key in config.playlists) {
      config.playlists[key].active_index = null;
    }
  }
 
  /**
   * Ensures the indexes in the playlists are valid indexes. The song has
   * to exist in the Amplitude config to be played correctly. If the index
   * is an integer, we ensure it exists and coy it to the array.
   *
   * @access private
   */
  function copySongsToPlaylists() {
    /*
      Iterate over all of the config's playlists
    */
    for (let key in config.playlists) {
      /*
        Checks if the playlist key is accurate.
      */
      Eif (config.playlists.hasOwnProperty(key)) {
        /*
          Checks if the playlist has songs.
        */
        Eif (config.playlists[key].songs) {
          /*
            Iterate over all of the songs in the playlist
          */
          for (let i = 0; i < config.playlists[key].songs.length; i++) {
            Eif (Checks.isInt(config.playlists[key].songs[i])) {
              config.playlists[key].songs[i] =
                config.songs[config.playlists[key].songs[i]];
            }
            /*
              Check to see if the index for the song in the playlist
              exists in the songs config.
            */
            Iif (
              Checks.isInt(config.playlists[key].songs[i]) &&
              !config.songs[config.playlists[key].songs[i]]
            ) {
              Debug.writeMessage(
                "The song index: " +
                  config.playlists[key].songs[i] +
                  " in playlist with key: " +
                  key +
                  " is not defined in your songs array!"
              );
            }
          }
        }
      }
    }
  }
 
  /**
   * Grabs the SoundCloud data for any song in the playlist that
   * the user needs to grab data for.
   *
   * @access private
   */
  function grabSoundCloudData() {
    /*
      Iterate over all of the config's playlists
    */
    for (let key in config.playlists) {
      /*
        Checks if the playlist key is accurate.
      */
      Eif (config.playlists.hasOwnProperty(key)) {
        /*
          Iterate over all of the songs in the playlist and see if
          they need to grab the SoundCloud data for the song.
        */
        for (let i = 0; i < config.playlists[key].songs.length; i++) {
          /*
            Only Grab the data if the URL is a SoundCloud URL.
          */
          Iif (SoundCloud.isSoundCloudURL(config.playlists[key].songs[i].url)) {
            /*
              Only grab the data if the SoundCloud data has not already been
              grabbed for the audio. This could happen if the user defined the
              song in the songs array and was grabbed before.
            */
            if (config.playlists[key].songs[i].soundcloud_data == undefined) {
              SoundCloud.resolveIndividualStreamableURL(
                config.playlists[key].songs[i].url,
                key,
                i
              );
            }
          }
        }
      }
    }
  }
 
  /**
   * Initializes the shuffle statuses for each of the playlists. These will
   * be referenced when we shuffle individual playlists.
   *
   * @access private
   */
  function initializePlaylistShuffleStatuses() {
    /*
			Iterate over all of the playlists the user defined adding
			the playlist key to the shuffled playlist array and creating
			and empty object to house the statuses.
		*/
    for (let key in config.playlists) {
      config.playlists[key].shuffle = false;
    }
  }
 
  /**
   * Initializes the repeat statuses for each of the playlists.  These will
   * be referenced when we repeat individual playlits.
   *
   * @access private
   */
  function initializePlaylistsRepeatStatuses() {
    /*
      Iterate over all of the playlists the user defined adding
      the playlist key to the repeated playlist array and creating
      and empty object to house the statuses.
    */
    for (let key in config.playlists) {
      config.playlists[key].repeat = false;
    }
  }
 
  /**
   * Initializes the shuffled playlist placeholders. These will be set for
   * playlists that are shuffled and contain the shuffled songs.
   *
   * @access private
   */
  function initializePlaylistShuffleLists() {
    /*
 			Iterate over all of the playlists the user defined adding
 			the playlist key to the shuffled playlists array and creating
 			and empty object to house the shuffled playlists
 		*/
    for (let key in config.playlists) {
      config.playlists[key].shuffle_list = [];
    }
  }
 
  /**
   * Intializes the display for the first song in the playlist meta data.
   *
   * @access private
   */
  function initializeFirstSongInPlaylistMetaData() {
    /*
			Iterates over all of the playlists setting the meta data for the
			first song.
		*/
    for (let key in config.playlists) {
      MetaDataElements.setFirstSongInPlaylist(
        config.playlists[key].songs[0],
        key
      );
    }
  }
 
  /*
    Returns the public facing methods
  */
  return {
    initialize: initialize
  };
})();
 
export default PlaylistsInitializer;