-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.js
More file actions
398 lines (335 loc) · 14.1 KB
/
main.js
File metadata and controls
398 lines (335 loc) · 14.1 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
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
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
import { extractColorsFromImage, getTopColors } from "./modules/colors";
import { getDetails, getUser } from "./modules/http.request"
import { navHeader, header, aside, mainFooter, footer } from "./modules/layouts";
import { asidePlaylists, reloadRecentlyPlayed, reloadSpotiPlaylist } from "./modules/reload";
import { scrollToX } from "./modules/scrollFunction";
import { token } from "./modules/token";
import { usersCurrentTime } from "./modules/usersCurrentTime";
usersCurrentTime()
let header_place = document.querySelector("header")
let aside_place = document.querySelector("aside")
let nav_header_place = document.querySelector(".nav_header")
let main_footer = document.querySelector(".main_footer")
let footer_place = document.querySelector("footer")
header(header_place)
aside(aside_place)
navHeader(nav_header_place)
mainFooter(main_footer)
footer(footer_place)
const allScopes = [
'user-read-playback-state',
'user-modify-playback-state',
'user-read-currently-playing',
'user-library-read',
'user-library-modify',
'playlist-read-private',
'playlist-modify-public',
'playlist-modify-private',
'user-top-read',
'user-follow-read',
'user-follow-modify',
'user-read-email',
// Добавьте сюда другие скоупы, если они вам нужны
];
let playlists = document.querySelector(".playlists")
let recently_played = document.querySelector(".recently_played")
let spotify_playlists = document.querySelector(".spotify_playlists")
let focus = document.querySelector(".focus")
let container = document.querySelector(".container")
let canvas = document.querySelector(".content_after")
scrollToX(focus)
scrollToX(spotify_playlists)
const initialColor = [0, 0, 0, 0.0]; // Начальный цвет с прозрачностью [R, G, B, A]
const targetColor = [18, 18, 18, 1.0]; // Целевой цвет с прозрачностью [R, G, B, A]
container.addEventListener('scroll', () => {
const currentScrollTop = container.scrollTop;
if (currentScrollTop > 50) {
const progress = Math.min((currentScrollTop - 50) / 100, 1); // Прогресс от 0 до 1
const newColor = blendColors(initialColor, targetColor, progress);
nav_header_place.style.background = `rgba(${newColor[0]}, ${newColor[1]}, ${newColor[2]}, ${newColor[3]})`;
} else {
nav_header_place.style.background = `rgba(${initialColor[0]}, ${initialColor[1]}, ${initialColor[2]}, ${initialColor[3]})`;
}
});
// Функция для плавного перехода между цветами
function blendColors(startColor, endColor, progress) {
const blendedColor = [];
for (let i = 0; i < 4; i++) {
blendedColor[i] = startColor[i] + (endColor[i] - startColor[i]) * progress;
}
return blendedColor;
}
if (!token) {
location.assign(`${import.meta.env.VITE_AUTH_ENDPOINT}?client_id=${import.meta.env.VITE_CLIENT_ID}&redirect_uri=${import.meta.env.VITE_REDIRECT_URI}&response_type=${import.meta.env.VITE_RESPONSE_TYPE}&scope=${allScopes.join('%20')}`)
localStorage.setItem("token", JSON.stringify(location.href.split('access_token=').at(-1)))
}
let audio = document.getElementById("audio");
let progress = document.querySelector(".progress");
let line = document.querySelector(".parent_line");
let volume = document.querySelector(".volume img");
let liked = document.querySelector(".player_left .actions button img");
let volume_line = document.querySelector(".volume_parent");
let volume_progressed = document.querySelector(".volume_progressed");
let btnPlay = document.querySelector(".controls .play img");
let btnPrev = document.querySelector(".prev");
let btnNext = document.querySelector(".next");
let currentTime = document.querySelector(".currentTime");
let duration = document.querySelector(".duration");
let track_name = document.querySelector(".track_name");
let track_author = document.querySelector(".track_author");
let track_img = document.querySelector(".track_img");
let isPlaying = false
let playlist = [];
liked.onclick = () => {
liked.src = "/icons/heart_active.svg"
}
duration.innerHTML = "00:30"
let treck; // Переменная с индексом трека
// Событие перед загрузкой страницы
window.onload = function () {
treck = 0; // Присваиваем переменной ноль
}
function switchTreck(numTreck) {
track_name.innerHTML = playlist[numTreck].name;
track_author.innerHTML = playlist[numTreck].artists[0].name;
track_img.src = playlist[numTreck].album.images.at(-1).url;
console.log(playlist[numTreck]);
// Меняем значение атрибута src
audio.src = playlist[numTreck].preview_url;
// Назначаем время песни ноль
audio.currentTime = 0;
// Включаем песню
audio.play();
}
btnPlay.onclick = () => {
isPlaying = !isPlaying
if (isPlaying) {
audio.play()
btnPlay.src = "/icons/pause.svg"
}
else {
audio.pause()
btnPlay.src = "/icons/play.svg"
}
}
audio.ontimeupdate = (e) => {
currentTime.innerHTML = `${Math.ceil(audio.currentTime)}`.length < 2 ? "00:0" + Math.ceil(audio.currentTime) : "00:" + Math.ceil(audio.currentTime)
progress.style.width = (audio.currentTime * 100) / audio.duration + '%';
if ((audio.currentTime * 100) / audio.duration === 100) {
if (treck < playlist.length - 1) { // Если да, то
treck++; // Увеличиваем её на один
switchTreck(treck); // Меняем песню
} else { // Иначе
treck = 0; // Присваиваем ей ноль
switchTreck(treck); // Меняем песню
}
}
}
let isMouseDown = false;
let volumeisMouseDown = false;
line.addEventListener('mousedown', (e) => {
isMouseDown = true;
progress.style.width = e.offsetX + "px";
audio.currentTime = e.offsetX / line.offsetWidth * audio.duration
});
line.addEventListener('mouseup', () => {
audio.play()
isMouseDown = false;
});
line.addEventListener('mousemove', (e) => {
if (!isMouseDown) return;
e.preventDefault();
progress.style.width = e.offsetX + "px";
audio.currentTime = e.offsetX / line.offsetWidth * audio.duration
audio.pause()
});
let currentVolume = 1
volume_line.addEventListener('mousedown', (e) => {
volumeisMouseDown = true;
volume_progressed.style.width = e.offsetX + "px";
audio.volume = e.offsetX / 100
currentVolume = audio.volume
console.log(currentVolume);
if (currentVolume < 0.50) {
volume.src = "/icons/low_volume.svg"
} else {
volume.src = "/icons/high_volume.svg"
}
});
volume_line.addEventListener('mouseup', () => {
volumeisMouseDown = false;
});
volume_line.addEventListener('mousemove', (e) => {
if (!volumeisMouseDown) return;
e.preventDefault();
volume_progressed.style.width = e.offsetX + "px";
audio.volume = e.offsetX / 100
currentVolume = audio.volume
if (currentVolume < 0.50) {
volume.src = "/icons/low_volume.svg"
} else {
volume.src = "/icons/high_volume.svg"
}
});
volume_line.addEventListener('mouseleave', (e) => {
volumeisMouseDown = false;
});
volume.onclick = () => {
if (audio.volume !== 0) {
audio.volume = 0
volume_progressed.style.width = 0 + "px";
volume.src = "/icons/mute_volume.svg"
} else {
audio.volume = currentVolume
volume_progressed.style.width = currentVolume * 100 + "px";
if (currentVolume < 0.50) {
volume.src = "/icons/low_volume.svg"
} else {
volume.src = "/icons/high_volume.svg"
}
}
}
btnPrev.addEventListener("click", function () {
btnPlay.src = "/icons/pause.svg"
isPlaying = true
// Проверяем что переменная treck больше нуля
if (treck > 0) {
treck--; // Если верно, то уменьшаем переменную на один
switchTreck(treck); // Меняем песню.
} else { // Иначе
treck = playlist.length - 1; // Присваиваем три
switchTreck(treck); // Меняем песню
}
});
btnNext.addEventListener("click", function () {
btnPlay.src = "/icons/pause.svg"
isPlaying = true
// Проверяем что переменная treck больше трёх
if (treck < playlist.length - 1) { // Если да, то
treck++; // Увеличиваем её на один
switchTreck(treck); // Меняем песню
} else { // Иначе
treck = 0; // Присваиваем ей ноль
switchTreck(treck); // Меняем песню
}
});
getUser("/me")
.then(res => {
if (!res) {
location.assign(`${import.meta.env.VITE_AUTH_ENDPOINT}?client_id=${import.meta.env.VITE_CLIENT_ID}&redirect_uri=${import.meta.env.VITE_REDIRECT_URI}&response_type=${import.meta.env.VITE_RESPONSE_TYPE}&scope=user-library-read%20user-read-recently-played`)
}
console.log(res.data);
})
getDetails("/browse/featured-playlists")
.then(res => {
if (!res) {
location.assign(`${import.meta.env.VITE_AUTH_ENDPOINT}?client_id=${import.meta.env.VITE_CLIENT_ID}&redirect_uri=${import.meta.env.VITE_REDIRECT_URI}&response_type=${import.meta.env.VITE_RESPONSE_TYPE}&scope=user-library-read%20user-read-recently-played`)
localStorage.setItem("token", JSON.stringify(location.href.split('access_token=').at(-1)))
}
reloadSpotiPlaylist(res.data.playlists.items, spotify_playlists)
let btns = document.querySelectorAll(".spotify_playlists .item_img .play")
btns.forEach(btn => {
btn.onclick = (e) => {
e.stopPropagation()
console.log(btn);
isPlaying = true
btnPlay.src = "/icons/pause.svg"
playlist.length = 0
getDetails(`/playlists/${btn.parentElement.id.split("/").at(-2)}/tracks`)
.then(res => {
for (let track of res.data.items) {
if (track.track.preview_url) playlist.push(track.track)
}
treck = 0
switchTreck(treck); // Меняем песню.
console.log(playlist);
})
}
})
})
const playlistIds = [
"37i9dQZF1DX4sWSpwq3LiO",
"37i9dQZF1DWZeKCadgRdKQ",
"37i9dQZF1DX9sIqqvKsjG8",
"37i9dQZF1DWZZbwlv3Vmtr",
"37i9dQZF1DXa2SPUyWl8Y5",
"37i9dQZF1DWUWUfWSLE7dn",
"37i9dQZF1DX3DZBe6wPMXo",
"37i9dQZF1DX7KrTMVQnM02"
];
// Массив для хранения результатов запросов
const playlistData = [];
// Выполните запросы для каждого плейлиста
const axiosPromises = playlistIds.map(playlistId => getDetails(`/playlists/${playlistId}`)
.then(res => {
playlistData.push(res.data); // Добавляем результат в массив
})
.catch(error => {
console.error(`Error fetching playlist ${playlistId}:`, error);
})
);
// Дождитесь завершения всех запросов
Promise.all(axiosPromises)
.then(() => {
reloadSpotiPlaylist(playlistData, focus);
let btns = document.querySelectorAll(".focus .item_img .play")
btns.forEach(btn => {
btn.onclick = () => {
isPlaying = true
btnPlay.src = "/icons/pause.svg"
playlist.length = 0
getDetails(`/playlists/${btn.parentElement.id.split("/").at(-2)}/tracks`)
.then(res => {
for (let track of res.data.items) {
if (track.track.preview_url) playlist.push(track.track)
}
treck = 0
switchTreck(treck); // Меняем песню.
console.log(playlist);
})
}
})
});
getDetails("/me/player/recently-played")
.then(res => {
// if (!res) {
// location.assign(`${import.meta.env.VITE_AUTH_ENDPOINT}?client_id=${import.meta.env.VITE_CLIENT_ID}&redirect_uri=${import.meta.env.VITE_REDIRECT_URI}&response_type=${import.meta.env.VITE_RESPONSE_TYPE}&scope=user-library-read%20user-read-recently-played`)
// }
playlist.length = 0
for (let track of res.data.items) {
if (track.track.preview_url) playlist.push(track.track)
}
reloadRecentlyPlayed(playlist.slice(0, 6), recently_played)
asidePlaylists(playlist, playlists)
let btns = document.querySelectorAll(".recently_played .play")
btns.forEach((btn, i) => {
btn.onclick = () => {
isPlaying = true
btnPlay.src = "/icons/pause.svg"
treck = i
audio.src = btn.id;
audio.play()
track_name.innerHTML = playlist[i].name;
track_author.innerHTML = playlist[i].artists[0].name;
track_img.src = playlist[i].album.images.at(-1).url;
}
btn.parentElement.onmouseenter = () => {
extractColorsFromImage(playlist[i].album.images.at(-1).url)
.then(colors => {
const topColors = getTopColors(colors, 1)[0];
canvas.style.background = `rgba(${topColors}, 0.643)`
// Здесь можно использовать полученные цвета в вашем приложении
})
.catch(error => {
console.error('Ошибка:', error);
});
}
btn.parentElement.onmouseleave = () => {
usersCurrentTime()
}
})
audio.src = playlist[0].preview_url;
track_name.innerHTML = playlist[0].name;
track_author.innerHTML = playlist[0].artists[0].name;
track_img.src = playlist[0].album.images.at(-1).url;
})