From 36a344ab07e6060801f36b6c8a08470049616aa7 Mon Sep 17 00:00:00 2001 From: Daan Sprenkels Date: Fri, 23 Nov 2018 15:50:49 +0100 Subject: [PATCH] Change time display to (H:)?MM:SS --- marietje/marietje/static/js/queue.js | 37 ++++++++++++++-------------- 1 file changed, 18 insertions(+), 19 deletions(-) diff --git a/marietje/marietje/static/js/queue.js b/marietje/marietje/static/js/queue.js index a9f0971..6925cc0 100644 --- a/marietje/marietje/static/js/queue.js +++ b/marietje/marietje/static/js/queue.js @@ -162,7 +162,7 @@ function updateTime() iteration = (iteration + 1) % 10; if (secondsLeft >= 0) { - $('.currentsong .time-left').text(secondsLeft.secondsToHHMMSS()); + $('.currentsong .time-left').text(secondsLeft.secondsToMMSS()); refreshSeconds(secondsLeft + timestamp, timestamp); } @@ -185,7 +185,7 @@ function renderQueue(playNextAt, now) var artist = song.song.artist.trim() === '' ? '?' : song.song.artist; var title = song.song.title.trim() === '' ? '?' : song.song.title; - showTime = showTimeToPlay ? (timeToPlay < 0 ? '' : timeToPlay.secondsToHHMMSS()) : (playNextAt < now ? '' : playNextAt.timestampToHHMMSS()) + showTime = showTimeToPlay ? (timeToPlay < 0 ? '' : timeToPlay.secondsToMMSS()) : (playNextAt < now ? '' : playNextAt.timestampToHHMMSS()) $('.queuebody:last-child').append('' + artist + '' + title + '' + requestedBy @@ -250,7 +250,7 @@ function refreshSeconds(playNextAt, now) var timeToPlay = playNextAt - now; times = $('.plays-at'); $.each(queue, function (id, song) { - $(times[id]).text(timeToPlay < 0 ? '' : timeToPlay.secondsToHHMMSS()); + $(times[id]).text(timeToPlay < 0 ? '' : timeToPlay.secondsToMMSS()); timeToPlay += parseInt(song.song.duration); }); } @@ -267,7 +267,7 @@ function getSongs() $.each(songs, function (id, song) { var artist = song.artist.trim() === '' ? '?' : song.artist; var title = song.title.trim() === '' ? '?' : song.title; - $('#request-table tbody:last-child').append('' + artist + '' + title + '' + (song.uploader_name ? song.uploader_name : 'Marietje') + '' + song.duration.secondsToHHMMSS() + ''); + $('#request-table tbody:last-child').append('' + artist + '' + title + '' + (song.uploader_name ? song.uploader_name : 'Marietje') + '' + song.duration.secondsToMMSS() + ''); }); var pageNumSelect = $('.pagenum'); pageNumSelect.empty(); @@ -316,23 +316,22 @@ if (!Date.now) { } } -// Edited from http://stackoverflow.com/a/6313008. -Number.prototype.secondsToHHMMSS = function () { - var sec_num = this; // don't forget the second param - var hours = Math.floor(sec_num / 3600); - var minutes = Math.floor((sec_num - (hours * 3600)) / 60); - var seconds = sec_num - (hours * 3600) - (minutes * 60); +// Edited from https://stackoverflow.com/a/6313008. +Number.prototype.secondsToMMSS = function () { + var hours = Math.floor(this / 3600); + var minutes = Math.floor((this - (hours * 3600)) / 60); + var seconds = this - (hours * 3600) - (minutes * 60); - if (hours < 10 && hours >= 0) { - hours = '0' + hours; + var sep1 = '' + if (hours > 0) { + hours = hours; + sep1 = ':'; + } else { + hours = ''; } - if (minutes < 10) { - minutes = '0' + minutes; - } - if (seconds < 10) { - seconds = '0' + seconds; - } - return hours + ':' + minutes + ':' + seconds; + if (hours > 0 && minutes < 10) minutes = '0' + minutes; + if (seconds < 10) seconds = '0' + seconds; + return hours + sep1 + minutes + ':' + seconds; } Number.prototype.timestampToHHMMSS = function () {