-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathmain.js
More file actions
80 lines (62 loc) · 2.42 KB
/
main.js
File metadata and controls
80 lines (62 loc) · 2.42 KB
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
const $videoList = document.getElementById('videoList')
const $playlists = document.querySelector('#playLists > .topics')
const youtubeVideoOptions = {
theme: 'light',
color: 'white',
showinfo: false
}
function createVideoWidget({ videoUrl, title, desc }, youtubeVideoOptions) {
// create all elements
const $videoWidget = document.createElement('div')
const $videoContainer = document.createElement('iframe')
const $videoDetailsContainer = document.createElement('div')
const $videoTitle = document.createElement('div')
const $videoDesc = document.createElement('div')
// populate all elements with class names
$videoWidget.className = 'video__widget'
$videoContainer.className = 'video__container'
$videoDetailsContainer.className = 'details__containers'
$videoTitle.className = 'details__title'
$videoDesc.className = 'details__desc'
let iframeSrc = `${videoUrl}?`
Object
.keys(youtubeVideoOptions)
.forEach(option => iframeSrc += `${option}=${youtubeVideoOptions[option]}&`)
$videoContainer.setAttribute('src', iframeSrc)
$videoContainer.setAttribute('width', 550)
$videoContainer.setAttribute('height', 310)
$videoContainer.setAttribute('frameborder', 0)
$videoTitle.innerHTML = title
$videoDesc.innerHTML = desc
// append elements to create widget
$videoDetailsContainer.appendChild($videoTitle)
$videoDetailsContainer.appendChild($videoDesc)
$videoWidget.appendChild($videoContainer)
$videoWidget.appendChild($videoDetailsContainer)
return $videoWidget;
}
function createPlaylistItem(playlist) {
const $link = document.createElement('a')
$link.innerHTML = playlist.title
$link.href = `#${playlist.id}`
return $link;
}
function renderVideoList() {
const { hash } = window.location
let playlistId = hash && hash.length > 1 ? hash.slice(1) : $playlists.firstChild.href.split('#')[1]
$videoList.innerHTML = null
fetch(`http://api.pakistanjs.com/${playlistId}/list`)
.then(res => res.json())
.then(videos => {
videos.forEach(video => $videoList.appendChild(createVideoWidget(video, youtubeVideoOptions)));
})
}
document.addEventListener('DOMContentLoaded', () => {
fetch('http://api.pakistanjs.com/playlists')
.then(res => res.json())
.then(playlists => {
playlists.forEach(playlist => $playlists.appendChild(createPlaylistItem(playlist)));
setTimeout(renderVideoList, 100)
})
window.onhashchange = renderVideoList;
})