Skip to content
This repository has been archived by the owner on May 7, 2024. It is now read-only.

Commit

Permalink
Player UI #9 - Track Control
Browse files Browse the repository at this point in the history
  • Loading branch information
theADAMJR committed Dec 21, 2020
1 parent 5213c48 commit 8d53028
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 4 deletions.
37 changes: 35 additions & 2 deletions dashboard/assets/js/music/html-music-wrapper.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
class HTMLMusicWrapper {
#music;

get currentTimestamp() {
const position = this.#music.position;

const minutes = Math.floor(position / 60).toString().padStart(2, '0');
const seconds = Math.floor(position - (minutes * 60)).toString().padStart(2, '0');
return `${minutes}:${seconds}`;
}

set apiError(error) {
if (!error)
return $('#musicAPIError').addClass('d-none');
Expand All @@ -11,13 +19,33 @@ class HTMLMusicWrapper {

constructor(musicClient) {
this.#music = musicClient;

setInterval(() => this.#updateSeeker(), 1000);
}

#updateSeeker() {
if (!this.#music.isPlaying || this.#music.isPaused) return;

this.#music.position++;

$('#seekTrack input').val(this.#music.position);
$('.current').text(this.currentTimestamp);
}

updateList() {
$('.now-playing').html(this.#nowPlaying());

const track = (this.#music.isPlaying) ? this.#music.list[0] : null;
if (track) {
$('.current').text(this.currentTimestamp);
$('.duration').text(track.duration.timestamp);
$('#seekTrack input').attr('max', track.duration.seconds);
} else {
$('.current, .duration').text(`00:00`);
}

$('.track-list').html(
(this.#music.list.length <= 0)
(!this.#music.isPlaying)
? '<p>No tracks here.</p>'
: this.#music.list
.map(this.#htmlTrack)
Expand All @@ -30,8 +58,13 @@ class HTMLMusicWrapper {
});
}

toggle() {
$('#toggleTrack i').toggleClass('fa-pause');
$('#toggleTrack i').toggleClass('fa-play');
}

#nowPlaying() {
if (this.#music.list.length <= 0) return ``;
if (!this.#music.isPlaying) return ``;

const track = this.#music.list[0];
return `
Expand Down
40 changes: 40 additions & 0 deletions dashboard/assets/js/music/music-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ class MusicWrapper {
#endpoint = `/api/guilds/${guildId}/music`;
#html = new HTMLMusicWrapper(this);

isPaused = $('#toggleTrack i').hasClass('fa-play');
list = [];
position = +$('#seekTrack input').val();

get isPlaying() {
return this.list.length > 0;
}

async #fetch(action) {
try {
Expand Down Expand Up @@ -32,10 +38,44 @@ class MusicWrapper {
try {
await this.#fetch(`stop`);
this.#html.apiError = null;
this.position = 0;
} catch {}
await this.updateList();
}

async toggle() {
try {
await this.#fetch(`toggle`);
this.#html.apiError = null;
} catch {}
}

async setVolume(value) {
try {
await this.#fetch(`volume?v=${value}`);
this.#html.apiError = null;
} catch {}
}

async seek(to) {
try {
await this.#fetch(`seek?to=${to}`);
this.position = to;

this.#html.apiError = null;
} catch {}
}

async toggle() {
try {
await this.#fetch(`toggle`);
this.isPaused = !this.isPaused;

this.#html.apiError = null;
this.#html.toggle();
} catch {}
}

async remove(index) {
try {
const list = await this.#fetch(`remove?i=${index}`);
Expand Down
10 changes: 10 additions & 0 deletions dashboard/assets/js/music/music.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,20 @@ $(async () => {
await music.updateList();

$('#skipTrack').on('click', () => music.skip());
$('#toggleTrack').on('click', () => music.toggle());
$('#shuffleList').on('click', () => music.shuffle());
$('#stopTrack').on('click', () => music.stop());
$('#trackSearch').on('click', async () => {
const query = $('.q-control input').val();
await music.play(query);
});

$('#seekTrack input').on('input', async function() {
const to = +$(this).val();
await music.seek(to);
});
$('#volume input').on('input', async function() {
const value = +$(this).val();
await music.setVolume(value);
});
});
33 changes: 33 additions & 0 deletions dashboard/routes/music-routes.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,39 @@ router.get('/stop', async (req, res) => {
}
});

router.get('/toggle', async (req, res) => {
try {
const player = res.locals.player;
(player.isPaused)
? await player.resume()
: await player.pause();

res.json({ message: 'Success' });
} catch (error) {
sendError(res, error);
}
});

router.get('/volume', async (req, res) => {
try {
await res.locals.player.setVolume(+req.query.v);

res.json({ message: 'Success' });
} catch (error) {
sendError(res, error);
}
});

router.get('/seek', async (req, res) => {
try {
await res.locals.player.seek(+req.query.to);

res.json({ message: 'Success' });
} catch (error) {
sendError(res, error);
}
});

router.get('/list', async (req, res) => {
try {
res.json(res.locals.player.q.items);
Expand Down
4 changes: 2 additions & 2 deletions dashboard/views/dashboard/modules/music.pug
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,9 @@ section#musicModule.module.container.px-5
button#shuffleList.btn.text-gradient #[i.fas.fa-random]
#volume.col-3
i.fas.fa-volume-up.text-gradient
input.form-control(type='range', value='0')
input.form-control(type='range', value='0', max='1', step='0.1')
#seekTrack
input.form-control(type='range', value=(player.position || 0) / 1000)
input.form-control(type='range', value=(Math.floor(player.position || 0) / 1000))
.position
span.current 00:00
span.text-muted.px-1 /
Expand Down

0 comments on commit 8d53028

Please sign in to comment.