riddhiman commited on
Commit
2de827e
·
verified ·
1 Parent(s): 6486919

Update public/script.js

Browse files
Files changed (1) hide show
  1. public/script.js +39 -8
public/script.js CHANGED
@@ -2,13 +2,32 @@ let isLooping = false; // Flag to manage loop state
2
  let isLoadingMusic = false; // Flag to manage loading state
3
  let currentPlaying = ''; // Store the filename of the currently playing music
4
  let trackList = []; // Store currently displayed track filenames
 
5
 
6
  window.onload = () => {
 
 
 
 
7
  loadMusicList();
8
- document.getElementById('search-bar').addEventListener('keyup', loadMusicList);
 
 
 
9
  document.getElementById('audio-player').style.display = 'none';
 
10
  };
11
 
 
 
 
 
 
 
 
 
 
 
12
  function toggleLoop() {
13
  isLooping = !isLooping;
14
  document.getElementById('loop-toggle').textContent = `Loop: ${isLooping ? 'On' : 'Off'}`;
@@ -18,17 +37,32 @@ function toggleLoop() {
18
 
19
  function loadMusicList() {
20
  const searchQuery = document.getElementById('search-bar').value.toLowerCase();
 
21
 
22
- fetch('/tracks')
23
  .then(response => response.json())
24
- .then(tracks => {
 
25
  const musicList = document.getElementById('music-list');
26
  musicList.innerHTML = '';
27
  trackList = [];
 
28
 
 
 
 
 
29
  tracks.forEach(track => {
30
  if (track.filename.toLowerCase().includes(searchQuery)) {
31
- trackList.push(track.filename); // Populate trackList
 
 
 
 
 
 
 
 
32
  const musicItem = document.createElement('div');
33
  musicItem.classList.add('music-item');
34
  musicItem.setAttribute('data-filename', track.filename); // Add this line
@@ -47,10 +81,7 @@ function loadMusicList() {
47
  fileNameSpan.textContent = track.filename;
48
 
49
  musicItem.append(playButton, artworkImg, fileNameSpan);
50
- musicList.appendChild(musicItem);
51
- }
52
- });
53
- });
54
  }
55
 
56
  function updatePlayingIndicator() {
 
2
  let isLoadingMusic = false; // Flag to manage loading state
3
  let currentPlaying = ''; // Store the filename of the currently playing music
4
  let trackList = []; // Store currently displayed track filenames
5
+ let currentPage = 1;
6
 
7
  window.onload = () => {
8
+ // Attach click handlers for paging
9
+ document.getElementById('prev-page').addEventListener('click', () => changePage(-1));
10
+ document.getElementById('next-page').addEventListener('click', () => changePage(1));
11
+
12
  loadMusicList();
13
+ document.getElementById('search-bar').addEventListener('keyup', () => {
14
+ currentPage = 1; // Reset to first page on new search
15
+ loadMusicList();
16
+ });
17
  document.getElementById('audio-player').style.display = 'none';
18
+ updatePageIndicator();
19
  };
20
 
21
+ function changePage(direction) {
22
+ currentPage += direction;
23
+ loadMusicList();
24
+ updatePageIndicator();
25
+ }
26
+
27
+ function updatePageIndicator() {
28
+ document.getElementById('page-indicator').textContent = currentPage.toString();
29
+ }
30
+
31
  function toggleLoop() {
32
  isLooping = !isLooping;
33
  document.getElementById('loop-toggle').textContent = `Loop: ${isLooping ? 'On' : 'Off'}`;
 
37
 
38
  function loadMusicList() {
39
  const searchQuery = document.getElementById('search-bar').value.toLowerCase();
40
+ const params = new URLSearchParams({page: currentPage}); // Include current page in request
41
 
42
+ fetch(`/tracks?${params}`)
43
  .then(response => response.json())
44
+ .then(data => {
45
+ const { tracks, total } = data;
46
  const musicList = document.getElementById('music-list');
47
  musicList.innerHTML = '';
48
  trackList = [];
49
+ const totalPages = Math.ceil(total / 5);
50
 
51
+ // Disable or enable pagination buttons based on the current page
52
+ document.getElementById('prev-page').disabled = currentPage === 1;
53
+ document.getElementById('next-page').disabled = currentPage === totalPages;
54
+
55
  tracks.forEach(track => {
56
  if (track.filename.toLowerCase().includes(searchQuery)) {
57
+ trackList.push(track.filename);
58
+ const musicItem = createMusicItem(track);
59
+ musicList.appendChild(musicItem);
60
+ }
61
+ });
62
+ });
63
+ }
64
+
65
+ function createMusicItem(track) {
66
  const musicItem = document.createElement('div');
67
  musicItem.classList.add('music-item');
68
  musicItem.setAttribute('data-filename', track.filename); // Add this line
 
81
  fileNameSpan.textContent = track.filename;
82
 
83
  musicItem.append(playButton, artworkImg, fileNameSpan);
84
+ return musicItem;
 
 
 
85
  }
86
 
87
  function updatePlayingIndicator() {