mckabue commited on
Commit
8ca9883
·
1 Parent(s): 4ac214c

.playlist tbody tr

Browse files
Files changed (1) hide show
  1. index.html +53 -33
index.html CHANGED
@@ -14,6 +14,10 @@
14
  width: 100%;
15
  height: 100%;
16
  }
 
 
 
 
17
  </style>
18
  </head>
19
 
@@ -56,7 +60,7 @@
56
  const playlistTable = document.querySelector('.playlist');
57
  const uploaForm = document.querySelector('.upload-form');
58
  let songsPlaylist = []
59
- let videoIdGenerator = generateVideoId();
60
 
61
  uploaForm.addEventListener('input', e => {
62
  e.preventDefault();
@@ -66,26 +70,40 @@
66
 
67
  parseCsv(e.target.files[0], playlistTable);
68
  });
69
-
70
- async function* generateVideoId() {
71
- for (const song of songsPlaylist) {
72
- const response = await (await fetch(
73
- '/video_id',
74
- {
75
- headers: { 'Content-Type': 'application/json' },
76
- method: 'POST',
77
- body: JSON.stringify({ title: song.Title, artist: song.Artist })
78
- })).text();
79
- yield response
80
  }
 
 
 
 
 
 
 
 
 
81
  }
82
- async function nextVideo(callback) {
83
- let video_id = (await videoIdGenerator.next()).value;
84
- if (!video_id) {
85
- videoIdGenerator = generateVideoId();
86
- video_id = (await videoIdGenerator.next()).value;
87
- }
 
 
 
 
 
 
 
 
 
88
  callback(video_id);
 
89
  }
90
  async function parseCsv(file, playlistTable) {
91
  try {
@@ -94,9 +112,9 @@
94
  songsPlaylist = await (await fetch('/parse_csv', { method: 'POST', body: formData })).json();
95
 
96
  tableBody = songsPlaylist.map((i, index) => `
97
- <tr>
98
  <th>${index + 1}</th>
99
- <th class="">${i.Title}</th>
100
  <th>${i.Artist}</th>
101
  </tr>`
102
  ).join('')
@@ -115,9 +133,10 @@
115
  playlistTable.innerHTML = error;
116
  }
117
  }
 
118
  function onYouTubeIframeAPIReady() {
119
  const youtubePlayerElement = document.querySelector('.youtube-player');
120
- const youtubePlayer = window.youtubePlayer = new YT.Player(youtubePlayerElement, {
121
  height: '100%',
122
  width: '100%',
123
  playerVars: { autoplay: 1 },
@@ -127,31 +146,32 @@
127
  },
128
  'onStateChange': function (event) {
129
  if (event.data === YT.PlayerState.ENDED) {
130
- onContinue(event?.target || youtubePlayer);
131
  }
132
  },
133
  'onError': function(event) {
134
- onContinue(event?.target || youtubePlayer);
135
  }
136
  }
137
  });
138
 
139
- function onContinue(player) {
140
- if (songsPlaylist.length > 0) {
141
- nextVideo((value) => {
142
- player.loadVideoById(value);
143
- player.playVideo();
144
- });
145
- }
146
- }
147
-
148
  const setIntervalId = setInterval(() => {
149
  if (songsPlaylist.length > 0) {
150
  clearInterval(setIntervalId);
151
- onContinue(youtubePlayer);
152
  }
153
  }, 500);
154
  };
 
 
 
 
 
 
 
 
 
 
155
  </script>
156
  </body>
157
 
 
14
  width: 100%;
15
  height: 100%;
16
  }
17
+
18
+ .playlist tbody tr {
19
+ cursor: pointer;
20
+ }
21
  </style>
22
  </head>
23
 
 
60
  const playlistTable = document.querySelector('.playlist');
61
  const uploaForm = document.querySelector('.upload-form');
62
  let songsPlaylist = []
63
+ let videoIndex = 0;
64
 
65
  uploaForm.addEventListener('input', e => {
66
  e.preventDefault();
 
70
 
71
  parseCsv(e.target.files[0], playlistTable);
72
  });
73
+ playlistTable.addEventListener('click', e => {
74
+ e.preventDefault();
75
+ const row = event.target.closest('tr');
76
+ if (row) {
77
+ const index = row.dataset.index ? Number(row.dataset.index) : undefined;
78
+ onContinue(undefined, index);
 
 
 
 
 
79
  }
80
+ });
81
+ function resetCurrentPlayingBackground() {
82
+ playlistTable.querySelectorAll('tbody tr').forEach(row => {
83
+ if (Number(row.dataset.index) == videoIndex) {
84
+ row.classList.add('bg-warning');
85
+ } else {
86
+ row.classList.remove('bg-warning');
87
+ }
88
+ })
89
  }
90
+
91
+ async function getVideoId(song) {
92
+ const response = await fetch(
93
+ '/video_id',
94
+ {
95
+ headers: { 'Content-Type': 'application/json' },
96
+ method: 'POST',
97
+ body: JSON.stringify({ title: song.Title, artist: song.Artist })
98
+ });
99
+ return await response.text()
100
+ }
101
+ async function nextVideo(callback, newIndex = undefined) {
102
+ newIndex = (newIndex || (videoIndex + 1))
103
+ videoIndex = newIndex < songsPlaylist.length ? newIndex : 0;
104
+ let video_id = await getVideoId(songsPlaylist[videoIndex]);
105
  callback(video_id);
106
+ resetCurrentPlayingBackground();
107
  }
108
  async function parseCsv(file, playlistTable) {
109
  try {
 
112
  songsPlaylist = await (await fetch('/parse_csv', { method: 'POST', body: formData })).json();
113
 
114
  tableBody = songsPlaylist.map((i, index) => `
115
+ <tr data-index="${index}">
116
  <th>${index + 1}</th>
117
+ <th>${i.Title}</th>
118
  <th>${i.Artist}</th>
119
  </tr>`
120
  ).join('')
 
133
  playlistTable.innerHTML = error;
134
  }
135
  }
136
+ let youtubePlayer;
137
  function onYouTubeIframeAPIReady() {
138
  const youtubePlayerElement = document.querySelector('.youtube-player');
139
+ youtubePlayer = window.youtubePlayer = new YT.Player(youtubePlayerElement, {
140
  height: '100%',
141
  width: '100%',
142
  playerVars: { autoplay: 1 },
 
146
  },
147
  'onStateChange': function (event) {
148
  if (event.data === YT.PlayerState.ENDED) {
149
+ onContinue(event?.target);
150
  }
151
  },
152
  'onError': function(event) {
153
+ onContinue(event?.target);
154
  }
155
  }
156
  });
157
 
 
 
 
 
 
 
 
 
 
158
  const setIntervalId = setInterval(() => {
159
  if (songsPlaylist.length > 0) {
160
  clearInterval(setIntervalId);
161
+ onContinue();
162
  }
163
  }, 500);
164
  };
165
+
166
+ function onContinue(player = undefined, newIndex = undefined) {
167
+ if (songsPlaylist.length > 0) {
168
+ nextVideo((value) => {
169
+ player = player || youtubePlayer
170
+ player.loadVideoById(value);
171
+ player.playVideo();
172
+ }, newIndex);
173
+ }
174
+ }
175
  </script>
176
  </body>
177