-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
275 lines (267 loc) · 8.33 KB
/
index.js
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
import SPOTIFY_API_KEY from './apikey.js'
const nav = document.querySelector('nav')
const ranges = document.querySelectorAll('input[type=range]')
const navItems = document.querySelectorAll('nav li')
const audio = document.querySelector('audio')
const playBtn = document.querySelector('#play')
const container = document.querySelector('.new-releases__container')
const currMusicImg = document.querySelector('aside > img')
const currMusicTitle = document.querySelector('.text__title')
const currMusicArtist = document.querySelector('.text__artist')
const repeatBtn = document.querySelector('#repeat')
const shuffleBtn = document.querySelector('#shuffle')
const nextBtn = document.querySelector('#next')
const previousBtn = document.querySelector('#previous')
const topCharts = document.querySelectorAll('.top-chart')
const searchBar = document.querySelector('#search')
const searchContainer = document.querySelector('.your-search__container')
let seekInterval
let currSong
document.querySelectorAll('.harmburger-toggle').forEach(el => {
el.addEventListener('click', function () {
nav.classList.toggle('active')
})
})
navItems.forEach((item, idx) => {
item.addEventListener('click', function () {
if (idx === 0) window.location = 'index.html'
if (idx === 1) window.location = 'album.html'
if (idx === 2) window.location = 'collections.html'
})
})
ranges.forEach((el, idx) => {
el.addEventListener('input', function () {
el.parentElement.style.setProperty('--progress', `${el.value}%`)
if (idx === 0) {
audio.volume = el.value / 100
return
}
audio.currentTime = (el.value / 100) * audio.duration
})
})
function pause() {
this.classList.remove('fa-circle-pause')
this.classList.add('fa-circle-play')
this.setAttribute('data-playboolean', 'true')
clearInterval(seekInterval)
audio.pause()
}
function play() {
this.classList.remove('fa-circle-play')
this.classList.add('fa-circle-pause')
this.setAttribute('data-playboolean', 'false')
audio.play()
}
playBtn.addEventListener('click', function () {
if (Number.isNaN(audio.duration)) return
;(this.dataset.playboolean !== 'true' ? pause : play).call(this)
})
let inter = new IntersectionObserver(
entries => {
if (!entries[0].isIntersecting) {
document.body.style.overflowY = 'unset'
document.querySelector('html').style.overflowY = 'unset'
return
}
if (window.innerWidth >= 735) return
document.body.style.overflowY = 'hidden'
document.querySelector('html').style.overflowY = 'hidden'
document.body.scrollIntoView()
},
{
threshold: 0.1,
}
)
inter.observe(nav)
const lazyLoader = new IntersectionObserver(
(entries, lazyLoader) => {
entries.forEach(entry => {
if (!entry.isIntersecting) return
entry.target.addEventListener('load', function () {
this.style.width = '100%'
this.style.filter = 'unset'
})
entry.target.src = entry.target.dataset.src
})
},
{
threshold: 0.6,
}
)
const options = {
method: 'GET',
headers: {
'X-RapidAPI-Key': SPOTIFY_API_KEY,
'X-RapidAPI-Host': 'spotify23.p.rapidapi.com',
},
}
async function getMusicPlaylist(id, container) {
try {
let res = await fetch(
`https://spotify23.p.rapidapi.com/playlist_tracks/?id=${id}&offset=0&limit=100`,
options
)
if (!res.ok) throw new Error('Too many requests, try again later')
let data = await res.json()
let { items } = data
container.innerHTML = ''
items.forEach(({ track }) => {
if (!track) return
let {
album: {
images: [{ url }, , { url: url1 }],
name: artist,
},
name,
preview_url,
} = track
container.innerHTML += `<div class="new-release" data-audio="${preview_url}">
<img src="${url1}" alt="" style="border-radius: 25px; filter: blur(2px);" data-src="${url}" class="ger"/>
<p title='${name}'>${
name.length > 15 ? name.slice(0, 16).padEnd(18, '...') : name
}</p>
<p class="sub-text" title='${artist}'>${
artist.length > 15 ? artist.slice(0, 16).padEnd(18, '...') : artist
}</p>
</div>`
})
Array.from(container.children).forEach(child => {
child.firstElementChild.style.width = '153px'
lazyLoader.observe(child.firstElementChild)
})
} catch (err) {
console.error(err.message)
window.alert(err)
}
}
getMusicPlaylist('4XuvWxk9sq6YpvUAYwZKbw', container)
function chooseSong(releaseContainer, src, title, artist) {
if (audio.src === releaseContainer.dataset.audio) return
currSong = releaseContainer
pause.call(playBtn)
audio.src = releaseContainer.dataset.audio
ranges[1].parentElement.style.setProperty('--progress', `0%`)
ranges[1].value = 0
ranges[0].parentElement.style.setProperty(
'--progress',
`${audio.volume * 100}%`
)
ranges[0].value = audio.volume * 100
currMusicImg.src = src || releaseContainer.firstElementChild.src
currMusicTitle.textContent = title || releaseContainer.children[1].textContent
currMusicArtist.textContent =
artist || releaseContainer.lastElementChild.textContent
}
document.body.addEventListener('click', function (e) {
if (!e.target.closest('.new-release')) return
chooseSong(e.target.closest('.new-release'))
})
audio.addEventListener('play', function () {
let duration = audio.duration
play.call(playBtn)
seekInterval = setInterval(() => {
let progress = (audio.currentTime / duration) * 100
ranges[1].parentElement.style.setProperty('--progress', `${progress}%`)
ranges[1].value = progress
}, 500)
})
audio.addEventListener('pause', function () {
pause.call(playBtn)
})
audio.addEventListener('ended', function () {
repeatBtn.classList.contains('active') ? audio.play() : null
if (shuffleBtn.classList.contains('active')) {
let randomChild = [...currSong.parentElement.children][
Math.floor(Math.random() * currSong.parentElement.children.length)
]
chooseSong(randomChild)
audio.addEventListener('durationchange', () => {
play.call(playBtn)
})
}
})
repeatBtn.addEventListener('click', function () {
this.classList.toggle('active')
shuffleBtn.classList.remove('active')
})
shuffleBtn.addEventListener('click', function () {
this.classList.toggle('active')
repeatBtn.classList.remove('active')
})
nextBtn.addEventListener('click', function () {
let nextSong = currSong.nextElementSibling
if (!nextSong) return
if (nextSong.classList.contains('top-chart')) {
let children = nextSong.children
chooseSong(
nextSong,
children[3].src,
children[0].textContent,
children[1].textContent
)
return
}
chooseSong(nextSong)
})
previousBtn.addEventListener('click', function () {
let previousSong = currSong.previousElementSibling
if (!previousSong) return
if (previousSong.classList.contains('top-chart')) {
let children = previousSong.children
console.log(children)
chooseSong(
previousSong,
children[3].src,
children[0].textContent,
children[1].textContent
)
return
}
chooseSong(previousSong)
})
topCharts.forEach(chart => {
chart.addEventListener('click', function () {
let chartChildren = [...chart.children]
console.log(chartChildren[3].src)
chooseSong(
chart,
chartChildren[3].src,
chartChildren[0].textContent,
chartChildren[1].textContent
)
})
})
searchBar.addEventListener('keydown', async function (e) {
if (e.key !== 'Enter' || this.value.trim() === '') return
try {
const res = await fetch(
`https://spotify23.p.rapidapi.com/search/?q=${this.value}&type=multi&offset=0&limit=10&numberOfTopResults=5`,
options
)
if (!res.ok) {
if (res.status === 429)
throw new Error(
'Error 429: Too many requests!! Unable to fetch album, please try again later'
)
}
const resData = await res.json()
if (resData.playlists.totalCount === 0)
throw new Error(
`${this.value} could not be found, please search for another song`
)
const {
playlists: {
items: [
{
data: { uri },
},
],
},
} = resData
let id = uri.match(/:[0-9a-zA-Z]+$/g)
getMusicPlaylist(id[0].slice(1), searchContainer)
} catch (err) {
console.error(err)
window.alert(err)
}
})