playlist randomized

This commit is contained in:
Matt Walsh
2025-03-23 18:42:48 +01:00
parent dc9a08bdc5
commit 06b8dbc959

View File

@@ -29,6 +29,8 @@ const getMedia = async () => {
const enableMediaPlayer = () => {
// see if files are available
if (playlist?.availableFiles?.length > 0) {
// randomize the list
randomizePlaylist();
// enable the icon
const icon = document.getElementById('ToggleMedia');
icon.classList.add('available');
@@ -78,6 +80,20 @@ const stateChanged = () => {
}
};
const randomizePlaylist = () => {
let availableFiles = [...playlist.availableFiles];
const randomPlaylist = [];
while (availableFiles.length > 0) {
// get a randon item from the available files
const i = Math.floor(Math.random() * availableFiles.length);
// add it to the final list
randomPlaylist.push(availableFiles[i]);
// remove the file from the available files
availableFiles = availableFiles.filter((file, index) => index !== i);
}
playlist.availableFiles = randomPlaylist;
};
export {
// eslint-disable-next-line import/prefer-default-export
toggleMedia,