Would it be possible or wise to now remove config from the code?

Continuing the discussion from Removing code that is not needed:

After removing code that is not needed now, would it make sense to remove config from the code, or is it still needed?

const config = {};

https://jsfiddle.net/2w19gmjo/

1 Like

What do you think and why?

2 Likes

Removing config I was able to do this: https://jsfiddle.net/7f5qLxcd/

If I did this correctly without error.

const videoPlayer = (function makeVideoPlayer() {
  let playlist = "";
  let player;

  function loadIframeScript() {
    const tag = document.createElement("script");
    tag.src = "https://www.youtube.com/iframe_api";
    const firstScriptTag = document.getElementsByTagName("script")[0];
    firstScriptTag.parentNode.insertBefore(tag, firstScriptTag);
  }

  function onYouTubeIframeAPIReady() {
    const cover = document.querySelector(".play");
    const wrapper = cover.parentElement;
    const frameContainer = wrapper.querySelector(".video");
    player = addPlayer(frameContainer, playlist);
  }

  function isPlaylist(videoIds) {
    return Array.isArray(videoIds) && videoIds.length;
  }

  function shufflePlaylist(player) {
    player.setShuffle(true);
    player.playVideoAt(0);
    player.stopVideo();
  }

  function onPlayerReady(event) {
    player = event.target;
    player.setVolume(100);
    shufflePlaylist(player);
  }

  function createPlaylist(videoIds) {
    return videoIds.join();
  }

  function createOptions(videoIds) {
    const options = {
      height: 360,
      host: "https://www.youtube-nocookie.com",
      width: 640
    };
    options.playerVars = {
      autoplay: 0,
      cc_load_policy: 0,
      controls: 1,
      disablekb: 1,
      fs: 0,
      iv_load_policy: 3,
      rel: 0
    };
    options.events = {
      "onReady": onPlayerReady
    };

    if (isPlaylist(videoIds)) {
      options.playerVars.loop = 1;
      options.playerVars.playlist = createPlaylist(videoIds);
    } else {
      options.videoId = videoIds;
    }
    return options;
  }

  function isVideoId(videoIds) {
    return typeof videoIds === "string";
  }

  function getVideoId(video, videoIds) {
    const videoId = video.dataset.id;
    if (isVideoId(videoIds)) {
      return videoIds;
    }
    return videoId;
  }

  function getVideoIds(video, ids) {
    if (isPlaylist(ids)) {
      return ids;
    } else {
      const videoId = getVideoId(video, ids);
      return videoId;
    }
  }

  function createVideoOptions(video, ids) {
    const videoIds = getVideoIds(video, ids);
    const options = createOptions(videoIds);
    return options;
  }

  function addPlayer(video, ids) {
    const options = createVideoOptions(video, ids);
    player = new YT.Player(video, options);
    return player;
  }

  function play() {
    if (player && player.playVideo) {
      player.playVideo();
    }
  }

  function init(videoIds) {
    player = null;
    loadIframeScript();
    window.onYouTubeIframeAPIReady = onYouTubeIframeAPIReady;
    playlist = videoIds;
    return play;
  }

  return {
    init,
    play
  };
}());

That doesn’t answer my question. Why do you think it can be removed?

Because it is not needed anywhere in the code, as demonstrated in me removing it.

If you know, why do you ask?