(function () { let isPlaying = false; let currentTracks = []; let cursor = 0; let howlerInstance; const retroWaveRu = "https://retrowave.ru"; let titleEl = document.getElementById("track-name"); let coverArtEl = document.getElementsByClassName("music-player")[0]; const modalEl = document.getElementById("intro-modal"); // const uploadInfoEl = document.getElementById("upload-info"); const fileUploadEl = document.getElementById("file-upload"); let volume = 1; openDialog(); getMusic(); listenUploadFileChange(); coverArtEl.addEventListener("click", (event) => { const isNoPause = event.target.classList.contains("no-pause"); if (howlerInstance && !isNoPause) { togglePlay(); } }); // uploadInfoEl.addEventListener("click", () => { // const confirmText = `Do you really want to change your playlist?\nThis will replace all your retrowave music history.\nIf you are sure about this, make sure to upload a valid json file probably downloaded using history link.`; // if (confirm(confirmText)) { // fileUploadEl.click(); // } // }); function listenUploadFileChange() { fileUploadEl.onchange = function () { const selectedFile = fileUploadEl.files[0]; const reader = new FileReader(); reader.readAsText(selectedFile, "UTF-8"); reader.onload = function (event) { try { const uploadedPlaylist = JSON.parse(event.target.result); localStorage.setItem("retrowave-history", event.target.result); } catch (error) { alert("malformed/invalid json file"); } }; }; } document.getElementById("initButton")?.addEventListener("click", () => { modalEl.classList.remove("open"); playMusic(); }); document.getElementById("history").addEventListener("click", () => { downloadHistory(); }); document.addEventListener("keyup", (event) => { const { key } = event; switch (key) { case "x": togglePlay(); break; case "a": break; case "d": break; case "w": volumeUp(); break; case "s": volumeDown(); break; } }); let touchstartX = 0; let touchendX = 0; let touchstartY = 0; let touchendY = 0; const musixPlayerEl = document.getElementsByClassName("music-player")[0]; if (musixPlayerEl) { musixPlayerEl.addEventListener("touchstart", (e) => { touchstartX = e.changedTouches[0].screenX; touchstartY = e.changedTouches[0].screenY; }); musixPlayerEl.addEventListener("touchend", (e) => { touchendX = e.changedTouches[0].screenX; touchendY = e.changedTouches[0].screenY; checkDirection(); }); } function checkDirection() { if (touchendX < touchstartX) { // Swipe Left } if (touchendX > touchstartX) { // Swipe Right } if (touchendY < touchstartY) { volumeUp(); } if (touchendY > touchstartY) { volumeDown(); } } function openDialog() { modalEl.classList.add("open"); } function getMusic() { fetch(`https://retrowave.ru/api/v1/tracks?limit=5&cursor=${cursor}`) .then((res) => res.json()) .then((res) => { const { body: { tracks, cursor: currentCursor }, } = res; cursor = currentCursor; currentTracks = tracks; }); } function initPlayer() { } function playMusic() { const currentTrack = currentTracks[0]; const singleTrack = currentTrack.streamUrl; const fullTrack = `${retroWaveRu}${singleTrack}`; howlerInstance = new Howl({ src: [fullTrack], html5: true, onend: function () { currentTracks.shift(); if (currentTracks.length <= 3) { getMusic(); } playMusic(); }, }); setVolume(); isPlaying = true; updateInfo(currentTrack); addToHistory(currentTrack); howlerInstance.play(); } function volumeDown() { console.log(volume); if(volume>0.1) { volume -= 0.1; } setVolume(); } function volumeUp() { if(volume<1) { volume += 0.1; } setVolume(); } function setVolume() { howlerInstance.volume(volume); } function togglePlay() { isPlaying = !isPlaying; if (isPlaying) { howlerInstance.play(); } else { howlerInstance.pause(); } } function updateInfo(trackDetails) { titleEl.innerText = trackDetails.title; document.title = `Now Playing... ${trackDetails.title}`; coverArtEl.style.backgroundImage = `url("${retroWaveRu}${trackDetails.artworkUrl}")`; } function getHistory() { let localHistoryStore = localStorage.getItem("retrowave-history") || "[]"; let historyArray = JSON.parse(localHistoryStore); return historyArray; } function addToHistory(trackDetails) { let historyArray = getHistory(); historyArray.push(trackDetails); localStorage.setItem("retrowave-history", JSON.stringify(historyArray)); } function downloadHistory() { const historyArray = getHistory(); let element = document.createElement("a"); let playListData = '#EXTM3U'; historyArray.forEach((musicData) => { playListData = `${playListData} #EXTINF:${Math.ceil(musicData.duration/1000)}, ${musicData.title} https://retrowave.ru/${musicData.streamUrl} `; }); element.setAttribute( "href", "data:audio/x-mpegurl;;charset=utf-8," + encodeURIComponent(playListData) ); element.setAttribute("download", "retrowave_playlist.m3u"); element.style.display = "none"; document.body.appendChild(element); element.click(); document.body.removeChild(element); } })();