-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapp.js
More file actions
544 lines (466 loc) · 19.1 KB
/
app.js
File metadata and controls
544 lines (466 loc) · 19.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
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
// متغیرهای سراسری
let songs = [];
let currentSongIndex = 0;
let isPlaying = false;
let audio = new Audio();
// تنظیم رویدادهای صفحه در هنگام بارگذاری
document.addEventListener('DOMContentLoaded', function() {
// بارگذاری آهنگها از دیتابیس
loadSongs();
// تنظیم رویدادهای دکمههای پخش
document.getElementById('play-btn').addEventListener('click', togglePlay);
document.getElementById('prev-btn').addEventListener('click', playPrevious);
document.getElementById('next-btn').addEventListener('click', playNext);
// تنظیم رویداد دکمه افزودن آهنگ
document.getElementById('add-song-btn').addEventListener('click', showAddSongForm);
// تنظیم رویداد جستجو
document.getElementById('search-input').addEventListener('input', searchSongs);
document.getElementById('search-input-mobile').addEventListener('input', searchSongs);
// تنظیم رویدادهای صوتی
audio.addEventListener('timeupdate', updateProgress);
audio.addEventListener('ended', playNext);
// تنظیم رویداد نوار پیشرفت
document.querySelector('.progress-container').addEventListener('click', setProgress);
});
// بارگذاری آهنگها از دیتابیس
function loadSongs() {
fetch('api.php?action=get_songs')
.then(response => response.json())
.then(data => {
if (data.success && data.songs) {
songs = data.songs;
renderSongList();
if (songs.length > 0) {
loadSong(0);
}
} else {
showMessage('خطا در بارگذاری آهنگها: ' + (data.error || 'خطای نامشخص'), 'error');
}
})
.catch(error => {
showMessage('خطا در ارتباط با سرور: ' + error.message, 'error');
});
}
// نمایش لیست آهنگها
function renderSongList() {
const songListContainer = document.querySelector('.song-list');
songListContainer.innerHTML = '';
if (songs.length === 0) {
songListContainer.innerHTML = '<div class="text-center p-4">هیچ آهنگی یافت نشد</div>';
return;
}
songs.forEach((song, index) => {
const songItem = document.createElement('div');
songItem.className = `song-item ${index === currentSongIndex ? 'active' : ''}`;
songItem.dataset.index = index;
const imageUrl = song.image_url || 'default-album.jpg';
songItem.innerHTML = `
<img src="${imageUrl}" alt="${song.title}" class="song-cover" onerror="this.src='default-album.jpg'">
<div class="song-info">
<div class="song-title">${song.title}</div>
<div class="song-artist">${song.artist}</div>
</div>
<div class="song-actions">
<button class="play-song" data-index="${index}" title="پخش">
<i class="fas fa-play"></i>
</button>
<button class="delete-song" data-id="${song.id}" title="حذف">
<i class="fas fa-trash"></i>
</button>
<button class="download-song" data-url="${song.audio_url}" title="دانلود">
<i class="fas fa-download"></i>
</button>
</div>
`;
songListContainer.appendChild(songItem);
});
// تنظیم رویدادهای دکمههای آهنگها
document.querySelectorAll('.play-song').forEach(button => {
button.addEventListener('click', function() {
const index = parseInt(this.dataset.index);
loadSong(index);
playAudio();
});
});
document.querySelectorAll('.delete-song').forEach(button => {
button.addEventListener('click', function() {
const songId = parseInt(this.dataset.id);
deleteSong(songId);
});
});
document.querySelectorAll('.download-song').forEach(button => {
button.addEventListener('click', function(e) {
e.stopPropagation();
const audioUrl = this.dataset.url;
if (audioUrl) {
// ایجاد یک لینک موقت برای دانلود
const a = document.createElement('a');
a.href = audioUrl;
a.download = audioUrl.split('/').pop(); // استخراج نام فایل از URL
document.body.appendChild(a);
a.click();
document.body.removeChild(a);
} else {
showMessage('آدرس فایل صوتی نامعتبر است', 'error');
}
});
});
// تنظیم رویداد کلیک روی آیتم آهنگ
document.querySelectorAll('.song-item').forEach(item => {
item.addEventListener('click', function(e) {
// اگر روی دکمههای عملیات کلیک نشده باشد
if (!e.target.closest('.song-actions')) {
const index = parseInt(this.dataset.index);
loadSong(index);
playAudio();
}
});
});
}
// بارگذاری آهنگ
function loadSong(index) {
if (index < 0 || index >= songs.length) return;
currentSongIndex = index;
const song = songs[index];
// بهروزرسانی اطلاعات آهنگ فعلی
document.querySelector('.current-song-title').textContent = song.title;
document.querySelector('.current-song-artist').textContent = song.artist;
const albumArt = document.querySelector('.current-song-cover');
albumArt.src = song.image_url || 'default-album.jpg';
albumArt.alt = song.title;
// اضافه کردن رویداد برای زمانی که تصویر بارگذاری نشود
albumArt.onerror = function() {
this.src = 'default-album.jpg';
};
// تنظیم منبع صوتی
audio.src = song.audio_url;
audio.load();
// بهروزرسانی کلاس فعال در لیست آهنگها
document.querySelectorAll('.song-item').forEach((item, i) => {
item.classList.toggle('active', i === index);
});
// بهروزرسانی آیکون دکمه پخش/توقف
updatePlayPauseIcon();
}
// پخش/توقف آهنگ
function togglePlay() {
if (songs.length === 0) return;
if (isPlaying) {
pauseAudio();
} else {
playAudio();
}
}
// پخش آهنگ
function playAudio() {
audio.play();
isPlaying = true;
updatePlayPauseIcon();
}
// توقف آهنگ
function pauseAudio() {
audio.pause();
isPlaying = false;
updatePlayPauseIcon();
}
// بهروزرسانی آیکون دکمه پخش/توقف
function updatePlayPauseIcon() {
const playBtn = document.getElementById('play-btn');
playBtn.innerHTML = isPlaying ? '<i class="fas fa-pause"></i>' : '<i class="fas fa-play"></i>';
}
// پخش آهنگ قبلی
function playPrevious() {
if (songs.length === 0) return;
let prevIndex = currentSongIndex - 1;
if (prevIndex < 0) prevIndex = songs.length - 1;
loadSong(prevIndex);
if (isPlaying) playAudio();
}
// پخش آهنگ بعدی
function playNext() {
if (songs.length === 0) return;
let nextIndex = currentSongIndex + 1;
if (nextIndex >= songs.length) nextIndex = 0;
loadSong(nextIndex);
if (isPlaying) playAudio();
}
// بهروزرسانی نوار پیشرفت
function updateProgress() {
const duration = audio.duration;
const currentTime = audio.currentTime;
if (duration) {
// بهروزرسانی نوار پیشرفت
const progressPercent = (currentTime / duration) * 100;
document.querySelector('.progress-bar').style.width = `${progressPercent}%`;
// بهروزرسانی زمانها
document.querySelector('.current-time').textContent = formatTime(currentTime);
document.querySelector('.duration').textContent = formatTime(duration);
} else {
document.querySelector('.current-time').textContent = '0:00';
document.querySelector('.duration').textContent = '0:00';
}
}
// تنظیم پیشرفت پخش با کلیک روی نوار
function setProgress(e) {
const width = this.clientWidth;
const clickX = e.offsetX;
const duration = audio.duration;
if (duration) {
audio.currentTime = (clickX / width) * duration;
}
}
// تبدیل ثانیه به فرمت mm:ss
function formatTime(seconds) {
const minutes = Math.floor(seconds / 60);
const secs = Math.floor(seconds % 60);
return `${minutes}:${secs < 10 ? '0' : ''}${secs}`;
}
// جستجوی آهنگها
function searchSongs(e) {
const searchTerm = e.target.value.toLowerCase();
if (searchTerm === '') {
// نمایش همه آهنگها
document.querySelectorAll('.song-item').forEach(item => {
item.style.display = 'flex';
});
return;
}
// فیلتر کردن آهنگها بر اساس عبارت جستجو
document.querySelectorAll('.song-item').forEach(item => {
const index = parseInt(item.dataset.index);
const song = songs[index];
if (song.title.toLowerCase().includes(searchTerm) ||
song.artist.toLowerCase().includes(searchTerm) ||
(song.album && song.album.toLowerCase().includes(searchTerm)) ||
(song.genre && song.genre.toLowerCase().includes(searchTerm))) {
item.style.display = 'flex';
} else {
item.style.display = 'none';
}
});
}
// نمایش فرم افزودن آهنگ
function showAddSongForm() {
// بررسی وجود فرم قبلی و حذف آن
const existingForm = document.querySelector('.add-song-form');
if (existingForm) {
existingForm.remove();
return;
}
const formContainer = document.createElement('div');
formContainer.className = 'add-song-form';
formContainer.innerHTML = `
<div class="edit-form-header">
<h5 class="edit-form-title">افزودن آهنگ جدید</h5>
<button type="button" class="edit-form-close">
<i class="fas fa-times"></i>
</button>
</div>
<form id="add-song-form">
<div class="form-group mb-3">
<label for="title">عنوان آهنگ</label>
<input type="text" class="form-control" id="title" name="title" required>
</div>
<div class="form-group mb-3">
<label for="artist">خواننده</label>
<input type="text" class="form-control" id="artist" name="artist" required>
</div>
<div class="form-group mb-3">
<label for="image_url">آدرس تصویر</label>
<input type="text" class="form-control" id="image_url" name="image_url">
</div>
<div class="form-group mb-3">
<label for="audio_url">آدرس فایل صوتی</label>
<input type="text" class="form-control" id="audio_url" name="audio_url" required>
</div>
<div class="form-group mb-3">
<label for="album">آلبوم</label>
<input type="text" class="form-control" id="album" name="album">
</div>
<div class="form-group mb-3">
<label for="genre">سبک</label>
<input type="text" class="form-control" id="genre" name="genre">
</div>
<div class="edit-buttons">
<button type="submit" class="btn btn-save">ذخیره</button>
<button type="button" class="btn btn-cancel">انصراف</button>
</div>
</form>
`;
// افزودن فرم به صفحه
document.querySelector('.music-player').appendChild(formContainer);
// تنظیم رویدادها
document.querySelector('.edit-form-close').addEventListener('click', () => {
formContainer.remove();
});
document.querySelector('.btn-cancel').addEventListener('click', () => {
formContainer.remove();
});
document.getElementById('add-song-form').addEventListener('submit', function(e) {
e.preventDefault();
addSong(this);
});
}
// افزودن آهنگ جدید
function addSong(form) {
const formData = new FormData(form);
fetch('api.php?action=add_song', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
if (data.success) {
showMessage(data.message, 'success');
// افزودن آهنگ جدید به آرایه آهنگها
songs.push(data.song);
renderSongList();
// حذف فرم
document.querySelector('.add-song-form').remove();
} else {
showMessage(data.error || 'خطا در افزودن آهنگ', 'error');
}
})
.catch(error => {
showMessage('خطا در ارتباط با سرور: ' + error.message, 'error');
});
}
// حذف آهنگ
function deleteSong(songId) {
if (!confirm('آیا از حذف این آهنگ اطمینان دارید؟')) {
return;
}
const formData = new FormData();
formData.append('id', songId);
fetch('api.php?action=delete_song', {
method: 'POST',
body: formData
})
.then(response => response.json())
.then(data => {
if (data.success) {
showMessage(data.message, 'success');
// حذف آهنگ از آرایه آهنگها
const index = songs.findIndex(s => s.id === songId);
if (index !== -1) {
songs.splice(index, 1);
// اگر آهنگ در حال پخش حذف شده است
if (index === currentSongIndex) {
if (songs.length > 0) {
// بارگذاری آهنگ بعدی یا قبلی
const newIndex = index < songs.length ? index : index - 1;
loadSong(newIndex >= 0 ? newIndex : 0);
} else {
// اگر هیچ آهنگی باقی نمانده است
audio.pause();
isPlaying = false;
document.querySelector('.current-song-title').textContent = 'بدون آهنگ';
document.querySelector('.current-song-artist').textContent = '';
document.querySelector('.current-song-cover').src = 'default-album.jpg';
document.querySelector('.progress-bar').style.width = '0%';
document.querySelector('.current-time').textContent = '0:00';
document.querySelector('.duration').textContent = '0:00';
updatePlayPauseIcon();
}
} else if (index < currentSongIndex) {
// اگر آهنگ حذف شده قبل از آهنگ در حال پخش بوده است
currentSongIndex--;
}
renderSongList();
}
} else {
showMessage(data.error || 'خطا در حذف آهنگ', 'error');
}
})
.catch(error => {
showMessage('خطا در ارتباط با سرور: ' + error.message, 'error');
});
}
// نمایش پیام
function showMessage(message, type = 'info') {
// بررسی وجود المان پیام قبلی و حذف آن
const existingMessage = document.querySelector('.message-container');
if (existingMessage) {
existingMessage.remove();
}
// ایجاد المان پیام
const messageContainer = document.createElement('div');
messageContainer.className = `message-container message-${type}`;
messageContainer.innerHTML = `
<div class="message-content">
<i class="message-icon fas ${type === 'success' ? 'fa-check-circle' : type === 'error' ? 'fa-exclamation-circle' : 'fa-info-circle'}"></i>
<span class="message-text">${message}</span>
</div>
<button class="message-close">
<i class="fas fa-times"></i>
</button>
`;
// افزودن استایل
const style = document.createElement('style');
style.textContent = `
.message-container {
position: fixed;
top: 80px;
left: 50%;
transform: translateX(-50%);
z-index: 9999;
min-width: 300px;
max-width: 80%;
padding: 15px 20px;
border-radius: 8px;
box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
display: flex;
justify-content: space-between;
align-items: center;
animation: slideDown 0.3s ease;
}
.message-success {
background-color: rgba(46, 204, 113, 0.9);
color: white;
}
.message-error {
background-color: rgba(231, 76, 60, 0.9);
color: white;
}
.message-info {
background-color: rgba(52, 152, 219, 0.9);
color: white;
}
.message-content {
display: flex;
align-items: center;
gap: 10px;
}
.message-icon {
font-size: 1.2rem;
}
.message-close {
background: none;
border: none;
color: white;
opacity: 0.7;
cursor: pointer;
transition: opacity 0.2s ease;
}
.message-close:hover {
opacity: 1;
}
@keyframes slideDown {
from { transform: translate(-50%, -20px); opacity: 0; }
to { transform: translate(-50%, 0); opacity: 1; }
}
`;
// افزودن به صفحه
document.head.appendChild(style);
document.body.appendChild(messageContainer);
// تنظیم رویداد دکمه بستن
messageContainer.querySelector('.message-close').addEventListener('click', () => {
messageContainer.remove();
});
// حذف خودکار پیام بعد از 5 ثانیه
setTimeout(() => {
if (messageContainer.parentNode) {
messageContainer.remove();
}
}, 5000);
}