From 399225ff9728f6cbccffbcff7895cacb3ac4a04f Mon Sep 17 00:00:00 2001 From: "Alexander \"Arav\" Andreev" Date: Wed, 13 Oct 2021 23:18:04 +0400 Subject: [PATCH] A refactoring of repeatings to eliminate. Not quite smaller the code became. --- files/static/assets/js/main.js | 52 +++++++++++++++------------------- 1 file changed, 23 insertions(+), 29 deletions(-) diff --git a/files/static/assets/js/main.js b/files/static/assets/js/main.js index b631885..e6b27e0 100644 --- a/files/static/assets/js/main.js +++ b/files/static/assets/js/main.js @@ -2,48 +2,42 @@ const video_formats = ["webm", "mp4"]; const audio_formats = ["mp3", "flac", "opus", "ogg", "m4a"]; const image_formats = ["jpg", "jpeg", "gif", "png", "bmp", "webp"]; -let scale = 1; +const overlay = document.getElementById("overlay"); +let scale = 1; function mousescroll(e) { e.preventDefault(); - scale += e.deltaY * -0.001; - scale = Math.min(Math.max(0.25, scale), 4); + scale = Math.min(Math.max(0.25, scale + (e.deltaY * -0.001)), 4); e.target.style.transform = `scale(${scale})`; } +const ext_filter = (ext, pathname) => pathname.toLowerCase().endsWith(ext); + +function to_overlay(eltyp, pathname) { + const el = document.createElement(eltyp); + if (eltyp !== "audio") el.addEventListener('wheel', mousescroll); + if (eltyp !== "img") el.autoplay = el.controls = true; + el.src = pathname; + overlay.appendChild(el); + overlay.style.visibility = "visible"; +} + document.getElementById("overlay").addEventListener("click", e => { e.target.firstChild.remove(); e.target.style.visibility = "hidden"; scale = 1; }); -const file_links = Array.from(document.getElementsByTagName('tr')).slice(2).filter((e) => e.lastChild.innerHTML != "DIR").map(v => v.firstChild.firstChild); +const file_links = Array.from(document.getElementsByTagName('tr')).slice(2).filter(e => e.lastChild.innerHTML != "DIR").map(l => l.firstChild.firstChild); file_links.forEach(f => f.addEventListener('click', e => { - if (video_formats.some(ext => e.target.pathname.toLowerCase().endsWith(ext))) { + const pathname = e.target.pathname; + if (video_formats.some(ext => ext_filter(ext, pathname))) + to_overlay("video", pathname); + else if (audio_formats.some(ext => ext_filter(ext, pathname))) + to_overlay("audio", pathname); + else if (image_formats.some(ext => ext_filter(ext, pathname))) + to_overlay("img", pathname); + if (overlay.firstChild != null) e.preventDefault(); - const video = document.createElement("video"); - video.addEventListener('wheel', mousescroll); - video.autoplay = true; - video.controls = true; - video.src = e.target.pathname; - document.getElementById("overlay").appendChild(video); - document.getElementById("overlay").style.visibility = "visible"; - } else if (audio_formats.some(ext => e.target.pathname.toLowerCase().endsWith(ext))) { - e.preventDefault(); - const audio = document.createElement("audio"); - audio.autoplay = true; - audio.controls = true; - audio.src = e.target.pathname; - document.getElementById("overlay").appendChild(audio); - document.getElementById("overlay").style.visibility = "visible"; - } else if (image_formats.some(ext => e.target.pathname.toLowerCase().endsWith(ext))) { - e.preventDefault(); - const image = document.createElement("img"); - image.addEventListener('wheel', mousescroll); - image.src = e.target.pathname; - document.getElementById("overlay").appendChild(image); - document.getElementById("overlay").style.visibility = "visible"; - } })); -