-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathapp.js
357 lines (311 loc) · 10.1 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
/*
1.Render Song =>OK
2.Scroll top =>OK
3.Play /pause/peek =>OK
4.CD rotate =>OK
5.Next/prev =>OK
6.Random =>Ok
7.Next/Repeat When ended=>Ok
8.Active song=>Ok
9.Scroll active song into view=>Ok
10.Play song onclick=>Ok
*/
const $=document.querySelector.bind(document);
const $$=document.querySelectorAll.bind(document);
const PLAYER_STORAGE_KEY='F8-Office';
const cd=$('.cd');
const playList=$('.playlist');
const heading=$('.music__playing-name');
const cdThumb=$('.cd-thumb');
const audio=$('#audio');
const player=$('.player');
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 app= {
currentSongIndex:0,
isPlaying:false,
isRandomSong:false,
isRepeat:false,
listSongPlayed:[],
config:JSON.parse(this.localStorage.getItem(PLAYER_STORAGE_KEY)) || {},
setConfig:function(key,value) {
this.config[key]=value;
localStorage.setItem(PLAYER_STORAGE_KEY,JSON.stringify(this.config));
},
songs :[
{
name:'Tiếng pháo tiễn người',
singer:'Hùng Quân',
path:'./asset/music/song1.mp3',
image:'./img/song1.jpg'
},
{
name:'Em hát ai nghe',
singer:'Orange',
path:'./asset/music/song2.mp3',
image:'./img/song2.jpg'
},
{
name:'Là do em xui thôi',
singer:'SOFIA',
path:'./asset/music/song3.mp3',
image:'./img/song3.jpg'
},
{
name:'3107-2',
singer:'DuongG-Nâu',
path:'./asset/music/song4.mp3',
image:'./img/song4.jpg'
},
{
name:'Cao ốc 20',
singer:'Bray if Masew',
path:'./asset/music/song5.mp3',
image:'./img/song5.jpg'
},
{
name:'Xin',
singer:'Bray ,Đạt G,Masew',
path:'./asset/music/song6.mp3',
image:'./img/song6.jpg'
},
{
name:'Nếu như là định mệnh',
singer:'Hoài Lâm x BinZ',
path:'./asset/music/song7.mp3',
image:'./img/song7.jpg'
},
{
name:'Tháng 7 của anh',
singer:'Khói if Masew',
path:'./asset/music/song8.mp3',
image:'./img/song8.jpg'
}
],
render: function() {
const htmls=this.songs.map((song,index) => {
return `
<div data-index="${index}" class="song ${index === this.currentSongIndex ? 'active' : ''}">
<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="fa-solid fa-ellipsis"></i></div>
</div>
`
})
playList.innerHTML=htmls.join('')
},
loadConfig: function() {
this.isRandom=this.config.isRandom;
this.isRepeat=this.config.isRepeat;
},
activeSong:function() {
_this=this;
const listSongPlay=$$('.song');
listSongPlay.forEach(function(song,index) {
if(_this.currentSongIndex===index) {
$('.song.active').classList.remove('active');
song.classList.add('active');
}
})
},
handleEvents: function() {
_this=this;
//Play bài hát
playBtn.onclick=function() {
if(_this.isPlaying) {
audio.pause();
}else {
audio.play();
}
}
//Khi song được play
audio.onplay=function() {
_this.isPlaying=true;
player.classList.add('playing');
csThumbAnimate.play();
// $$('.thumb').forEach(function(thumb) {
// const csListThumb=thumb.animate([
// {transform :'rotate(360deg'}
// ],
// {
// duration:10000,
// iterations:Infinity
// })
// csListThumb.play();
// })
}
//Khi song bị pause
audio.onpause=function() {
_this.isPlaying=false;
player.classList.remove('playing');
csThumbAnimate.pause();
}
// Xử lí thu/phóng CD
const cdWidth=cd.offsetWidth;
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í tiến độ bài hát hiện tại
audio.ontimeupdate=function() {
if(audio.currentTime) {
const progressPercent=audio.currentTime/audio.duration *100;
progress.value=progressPercent;
}
}
//Xử lí bài hát khi tua
progress.oninput=function(e) {
const seekTime=audio.duration*e.target.value/100;
audio.currentTime=seekTime;
}
//Quay CD
const csThumbAnimate=cdThumb.animate([
{transform :'rotate(360deg'}
],
{
duration:10000,
iterations:Infinity
})
csThumbAnimate.pause();
//Next Song
nextBtn.onclick =function() {
if(_this.isRandom) {
_this.playRandomSong();
}else {
_this.nextSong();
}
audio.play();
_this.activeSong();
_this.scrollToView();
csThumbAnimate.cancel();
}
//Prev Song
prevBtn.onclick =function() {
if(_this.isRandom) {
_this.playRandomSong();
}else {
_this.prevSong();
}
audio.play();
_this.activeSong();
_this.scrollToView();
csThumbAnimate.cancel();
}
//Random song
randomBtn.onclick=function() {
_this.isRandom=!_this.isRandom;
randomBtn.classList.toggle('active',_this.isRandom);
_this.setConfig('isRandom',_this.isRandom);
}
//Xử lí ended của song
audio.onended= function() {
if(_this.isRepeat) {
audio.play();
}else {
nextBtn.onclick();
}
csThumbAnimate.cancel();
}
//Xử lí Repeat
repeatBtn.onclick=function() {
_this.isRepeat=!_this.isRepeat;
_this.setConfig('isRepeat',_this.isRepeat);
repeatBtn.classList.toggle('active',_this.isRepeat);
}
//Xử lí khi click vào song
playList.onclick=function(e) {
let songNode=e.target.closest('.song:not(.active)');
if(songNode || e.target.closest('.option')) {
//Xử lí khi click vào nodeSong
if(songNode) {
_this.currentSongIndex=Number(songNode.dataset.index);
_this.loadCurrentSong();
audio.play();
console.log('play')
}
//Xử lí khi click vào option và ngăn nổi bọt
$$('.option').forEach(function(option) {
option.onclick=function(e) {
e.stopPropagation();
console.log('option')
}
})
}
}
},
defineProperties:function() {
Object.defineProperty(this,'currentSong',{
get: function() {
return this.songs[this.currentSongIndex];
}
})
},
loadCurrentSong: function() {
heading.innerText=this.currentSong.name;
cdThumb.style.backgroundImage=`url('${this.currentSong.image}')`;
audio.src=`${this.currentSong.path}`;
this.activeSong();
},
nextSong:function() {
this.currentSongIndex++;
if(this.currentSongIndex >= this.songs.length) {
this.currentSongIndex=0;
}
this.loadCurrentSong();
},
prevSong:function() {
this.currentSongIndex--;
if(this.currentSongIndex < 0) {
this.currentSongIndex=this.songs.length-1;
}
this.loadCurrentSong();
},
playRandomSong: function() {
if(this.listSongPlayed.length == this.songs.length) {
this.listSongPlayed=[];
}
let newIndex;
do {
newIndex=Math.floor(Math.random()*this.songs.length);
}while(this.listSongPlayed.includes(newIndex)) ;
this.listSongPlayed.push(newIndex);
this.currentSongIndex=newIndex;
this.loadCurrentSong();
},
scrollToView:function() {
setTimeout(function() {
$('.song.active').scrollIntoView({
behavior:'smooth',
block:'end'
});
},200)
},
start: function() {
//Gán Cấu hình từ config sang app
this.loadConfig();
repeatBtn.classList.toggle('active',this.isRepeat);
randomBtn.classList.toggle('active',this.isRandom);
// Định nghĩa các thuộc tính mới cho Object
this.defineProperties();
// Các sự kiện trong DOM Events
this.handleEvents();
// Tải thông tin bài hát đầu tiên
this.loadCurrentSong();
// Render ra bài hát
this.render();
// Active song
this.activeSong();
}
}
app.start();