A refactoring of repeatings to eliminate. Not quite smaller the code became.

This commit is contained in:
Alexander Andreev 2021-10-13 23:18:04 +04:00
parent 69a27d1d6f
commit 399225ff97
Signed by: Arav
GPG Key ID: 610DF2574456329F

View File

@ -2,48 +2,42 @@ const video_formats = ["webm", "mp4"];
const audio_formats = ["mp3", "flac", "opus", "ogg", "m4a"]; const audio_formats = ["mp3", "flac", "opus", "ogg", "m4a"];
const image_formats = ["jpg", "jpeg", "gif", "png", "bmp", "webp"]; const image_formats = ["jpg", "jpeg", "gif", "png", "bmp", "webp"];
let scale = 1; const overlay = document.getElementById("overlay");
let scale = 1;
function mousescroll(e) { function mousescroll(e) {
e.preventDefault(); e.preventDefault();
scale += e.deltaY * -0.001; scale = Math.min(Math.max(0.25, scale + (e.deltaY * -0.001)), 4);
scale = Math.min(Math.max(0.25, scale), 4);
e.target.style.transform = `scale(${scale})`; 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 => { document.getElementById("overlay").addEventListener("click", e => {
e.target.firstChild.remove(); e.target.firstChild.remove();
e.target.style.visibility = "hidden"; e.target.style.visibility = "hidden";
scale = 1; 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 => { 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(); 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";
}
})); }));