-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.js
360 lines (331 loc) · 11.9 KB
/
app.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
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
const $ = document.querySelector.bind(document);
const $$ = document.querySelectorAll.bind(document);
const PLAYER_STORAGE_KEY = 'F8_PLAYER';
const player = $('.player');
const cd = $('.cd');
const heading = $('header h2');
const cdThumb = $('.cd-thumb');
const audio = $('#audio');
const playBtn = $('.btn-toggle-play');
const progress = $('#progress');
const nextBtn = $('.btn-next');
const prevBtn = $('.btn-prev');
const randomBtn = $('.btn-random');
const repeatBtn = $('.btn-repeat');
const playlist = $('.playlist');
const app = {
currentIndex: 0,
isPlaying: false,
isRandom: false,
isRepeat: false,
isChanged: true,
config: JSON.parse(localStorage.getItem(PLAYER_STORAGE_KEY)) || {},
songs: [
{
name: "7 Years",
singer: "Lukas Graham",
path: "music/7Years.mp3",
image: "https://is3-ssl.mzstatic.com/image/thumb/Music69/v4/1f/57/08/1f57082e-ee7e-dcb7-a73e-ba1f3468211d/093624920496.jpg/500x500bb.webp"
},
{
name: "Cheating on You",
singer: "Charlie Puth",
path: "music/Cheating on You Lyrics_320kbps.mp3",
image:
"https://i1.sndcdn.com/artworks-000606602386-58veao-t500x500.jpg"
},
{
name: "Peaches",
singer: "Justin Bieber",
path:
"music/peaches.mp3",
image: "https://i1.sndcdn.com/artworks-boUvTeTEft8V0b2G-8spb0A-t500x500.jpg"
},
{
name: "Girls Like You",
singer: "Maroon 5 ft. Cardi B",
path: "music/girls_like_you.mp3",
image:
"https://i1.sndcdn.com/artworks-Q4YpEfYqUak25yQa-bWezrg-t500x500.jpg"
},
{
name: "Happy For You",
singer: "Lukas Graham ft. Vu",
path: "music/lukas_graham_happy_for_you_feat_vu_performance_video_6462074145644287853.mp3",
image:
"https://i.ytimg.com/vi/mf4upAPwHEo/hqdefault.jpg?"
},
{
name: "MEAN It",
singer: "Lauv & LANY",
path: "music/lauv_lany_mean_it_lyrics.mp3",
image:
"https://avatar-ex-swe.nixcdn.com/song/2019/11/14/e/3/1/6/1573698962727_640.jpg"
},
{
name: "10000 Hours",
singer: "Jungkook (BTS)",
path: "music/bts_jungkook_10000_hours_full_ver_lyrics_-8617717337001492207.mp3",
image:
"https://i.ytimg.com/vi/6-Q36XoHlEI/maxresdefault.jpg"
},
{
name: "Let Her Go",
singer: "Passenger",
path: "music/passenger_let_her_go_lyrics_6445739323343692100.mp3",
image:
"https://i.ytimg.com/vi/EX7oWSbVbGY/hqdefault.jpg?"
},
{
name: "What Are Words",
singer: "Chris Medina",
path: "music/what_are_words_chris_medina_lyrics_-8747535439467037846.mp3",
image:
"https://avatar-ex-swe.nixcdn.com/playlist/2013/11/06/c/c/a/8/1383717171089_500.jpg"
},
{
name: "Closer",
singer: "The Chainsmokers",
path: "music/the_chainsmokers_closer_lyrics_ft_halsey_5382140997956059747.mp3",
image:
"https://upload.wikimedia.org/wikipedia/vi/a/a5/Closer_%28featuring_Halsey%29_%28Official_Single_Cover%29_by_The_Chainsmokers.png"
},
{
name: "We Don't Talk Anymore",
singer: "Charlie Puth ft. Selena Gomez",
path: "music/charlie_puth_we_don_t_talk_anymore_lyrics_feat_selena_gomez_-4202526501292096840.mp3",
image:
"https://thenewsmexico.com/wp-content/uploads/2019/06/image5-12.jpg"
},
{
name: "Totoo",
singer: "Masew",
path: "music/nhac_dam_cuoi_mixi_city_masiu_-4105305789908939866.mp3",
image:
"https://thenewsmexico.com/wp-content/uploads/2019/06/image5-12.jpg"
},
],
setConfig: function (key, value) {
this.config[key] = value;
localStorage.setItem(PLAYER_STORAGE_KEY, JSON.stringify(this.config));
},
render: function () {
const htmls = this.songs.map((song, index) => {
return `
<div class="song ${index === this.currentIndex ? "active" : ""}" data-index="${index}">
<div class="thumb"
style="background-image: url('${song.image}')">
</div>
<div class="body">
<h3 class="title">${song.name}</h3>
<p class="author">${song.singer}</p>
</div>
<div class="option">
<i class="fas fa-ellipsis-h"></i>
</div>
</div>
`;
});
playlist.innerHTML = htmls.join("");
},
defineProperties: function () {
Object.defineProperty(this, "currentSong", {
get: function () {
return this.songs[this.currentIndex];
}
});
},
handleEvents: function () {
const _this = this;
const cdWidth = cd.offsetWidth;
// Xử lý CD quay / dừng
// Handle CD spins / stops
const cdThumbAnimate = cdThumb.animate([{ transform: "rotate(360deg)" }], {
duration: 10000, // 10 seconds
iterations: Infinity
});
cdThumbAnimate.pause();
// Xử lý phóng to / thu nhỏ CD
// Handles CD enlargement / reduction
document.onscroll = function () {
const scrollTop = window.scrollY || document.documentElement.scrollTop;
const newCdWidth = cdWidth - scrollTop;
cd.style.width = newCdWidth > 0 ? newCdWidth + "px" : 0;
cd.style.opacity = newCdWidth / cdWidth;
};
// Xử lý khi click play
// Handle when click play
playBtn.onclick = function () {
if (_this.isPlaying) {
audio.pause();
} else {
audio.play();
}
};
// Khi song được play
// When the song is played
audio.onplay = function () {
_this.isPlaying = true;
player.classList.add("playing");
cdThumbAnimate.play();
};
// Khi song bị pause
// When the song is pause
audio.onpause = function () {
_this.isPlaying = false;
player.classList.remove("playing");
cdThumbAnimate.pause();
};
// Khi tiến độ bài hát thay đổi
// When the song progress changes
audio.ontimeupdate = function () {
if (audio.duration) {
const progressPercent = Math.floor(
(audio.currentTime / audio.duration) * 100
);
if (_this.isChanged) {
progress.value = progressPercent;
}
}
};
// Xử lý khi tua song
// Handling when seek
progress.ontouchstart = function () {
_this.isChanged = false;
}
progress.onmousedown = function () {
_this.isChanged = false;
}
progress.onchange = function (e) {
const seekTime = (audio.duration / 100) * e.target.value;
audio.currentTime = seekTime;
_this.isChanged = true;
};
// Khi next song
// When next song
nextBtn.onclick = function () {
if (_this.isRandom) {
_this.playRandomSong();
} else {
_this.nextSong();
}
audio.play();
_this.render();
_this.scrollToActiveSong();
};
// Khi prev song
// When prev song
prevBtn.onclick = function () {
if (_this.isRandom) {
_this.playRandomSong();
} else {
_this.prevSong();
}
audio.play();
_this.render();
_this.scrollToActiveSong();
};
// Xử lý bật / tắt random song
// Handling on / off random song
randomBtn.onclick = function (e) {
_this.isRandom = !_this.isRandom;
_this.setConfig("isRandom", _this.isRandom);
randomBtn.classList.toggle("active", _this.isRandom);
};
// Xử lý lặp lại một song
// Single-parallel repeat processing
repeatBtn.onclick = function (e) {
_this.isRepeat = !_this.isRepeat;
_this.setConfig("isRepeat", _this.isRepeat);
repeatBtn.classList.toggle("active", _this.isRepeat);
};
// Xử lý next song khi audio ended
// Handle next song when audio ended
audio.onended = function () {
if (_this.isRepeat) {
audio.play();
} else {
nextBtn.click();
}
};
// Lắng nghe hành vi click vào playlist
// Listen to playlist clicks
playlist.onclick = function (e) {
const songNode = e.target.closest(".song:not(.active)");
if (songNode || e.target.closest(".option")) {
// Xử lý khi click vào song
// Handle when clicking on the song
if (songNode) {
_this.currentIndex = Number(songNode.dataset.index);
_this.loadCurrentSong();
_this.render();
audio.play();
}
// Xử lý khi click vào song option
// Handle when clicking on the song option
if (e.target.closest(".option")) {
}
}
};
},
scrollToActiveSong: function () {
setTimeout(() => {
$(".song.active").scrollIntoView({
behavior: "smooth",
block: "nearest"
});
}, 300);
},
loadCurrentSong: function () {
heading.textContent = this.currentSong.name;
cdThumb.style.backgroundImage = `url('${this.currentSong.image}')`;
audio.src = this.currentSong.path;
},
loadConfig: function () {
this.isRandom = this.config.isRandom;
this.isRepeat = this.config.isRepeat;
},
nextSong: function () {
this.currentIndex++;
if (this.currentIndex >= this.songs.length) {
this.currentIndex = 0;
}
this.loadCurrentSong();
},
prevSong: function () {
this.currentIndex--;
if (this.currentIndex < 0) {
this.currentIndex = this.songs.length - 1;
}
this.loadCurrentSong();
},
playRandomSong: function () {
let newIndex;
do {
newIndex = Math.floor(Math.random() * this.songs.length);
} while (newIndex === this.currentIndex);
this.currentIndex = newIndex;
this.loadCurrentSong();
},
start: function () {
// Gán cấu hình từ config vào ứng dụng
// Assign configuration from config to application
this.loadConfig();
// Định nghĩa các thuộc tính cho object
// Defines properties for the object
this.defineProperties();
// Lắng nghe / xử lý các sự kiện (DOM events)
// Listening / handling events (DOM events)
this.handleEvents();
// Tải thông tin bài hát đầu tiên vào UI khi chạy ứng dụng
// Load the first song information into the UI when running the app
this.loadCurrentSong();
// Render playlist
this.render();
// Hiển thị trạng thái ban đầu của button repeat & random
// Display the initial state of the repeat & random button
randomBtn.classList.toggle("active", this.isRandom);
repeatBtn.classList.toggle("active", this.isRepeat);
}
};
app.start();