// vim:set sw=4 ts=4 sts=4 ft=javascript expandtab:
import { lufi } from "./lufi.js";
const abortedDOM = document.createElement("div");
abortedDOM.innerHTML = `
${i18n.aborted1}
${i18n.aborted2}
`;
const loadingDOM = document.getElementById("loading");
const passwordDOM = document.getElementById("file_pwd");
const filesizeDOM = document.getElementById("filesize");
document.addEventListener("DOMContentLoaded", () => {
let go = true;
filesizeDOM.innerHTML = filesize(
filesizeDOM.attributes.getNamedItem("data-filesize").value
);
if (isPasswordNeeded()) {
go = false;
passwordDOM.focus();
onPasswordEvents();
}
if (go) {
startDownload();
}
});
const isPasswordNeeded = () =>
document.querySelectorAll("#file_pwd").length === 1;
const startDownload = () => {
warnOnReload();
lufi
.download(window.location, passwordDOM?.value)
.andThen((job) => {
job.onProgress(() => {
updateProgress(job.lufiFile);
});
document.getElementById("abort").onclick = () => {
removeElements(["please-wait", "pbd", "loading", "abort"]);
job.terminate();
filesizeDOM.parentElement.append(abortedDOM);
warnOnReload(false);
document.getElementById("reloadLocation").onclick = (e) => {
e.preventDefault();
window.location.reload();
};
};
return job.waitForCompletion();
})
.mapErr((error) => {
addAlert(error.message);
warnOnReload(false);
removeElements(["abort"]);
})
.andThen((job) => {
notify(i18n.fileDownloaded, job.lufiFile.name);
removeElements(["please-wait", "loading"]);
const pbd = document.getElementById("pbd");
pbd.className = "center-align";
const blobURL = URL.createObjectURL(job.downloadedFile);
let htmlContent = `${i18n.download}
`;
var isZip = filesizeDOM.getAttribute("data-zipped") === "true";
if (job.lufiFile.type.match(/^image\//) !== null) {
htmlContent += `
`;
} else if (job.lufiFile.type.match(/^video\//) !== null) {
htmlContent += ``;
} else if (job.lufiFile.type.match(/^audio\//) !== null) {
htmlContent += ``;
} else if (isZip) {
htmlContent += `${i18n.showZipContent}
`;
}
pbd.innerHTML = htmlContent;
if (isZip) {
showZipContent(job.downloadedFile);
}
document.getElementById("abort").remove();
});
};
/**
* Remove elements using their id
* @param elements
*/
const removeElements = (elements) => {
elements.forEach((id) => {
if (document.getElementById(id)) {
document.getElementById(id).remove();
} else {
console.error(`${id} does not exist`);
}
});
};
const onPasswordEvents = () => {
const callback = () => {
document
.getElementsByClassName("file-progress")[0]
.classList.remove("hide");
document.getElementsByClassName("file-abort")[0].classList.remove("hide");
passwordDOM.parentElement.parentElement.classList.add("hide");
startDownload();
};
document.getElementById("go").onclick = () => {
callback();
};
document.getElementById("file_pwd").onkeydown = (event) => {
if (event.key === "Enter") {
callback();
}
};
};
// Something's wring
const addAlert = (msg) => {
removeElements(["please-wait"]);
let pbd = document.getElementsByClassName("file-progress")[0];
pbd.setAttribute("role", "alert");
pbd.classList.remove("progress");
pbd.innerHTML = ``;
};
const warnOnReload = (toWarn = true) => {
window.onbeforeunload = toWarn ? confirmExit : null;
};
const updateProgress = (lufiFile) => {
// Update loading text
loadingDOM.textContent = i18n.loading.replace(/XX1/, lufiFile.chunksReady);
// Update progress bar
const percent =
Math.round((1000 * lufiFile.chunksReady) / lufiFile.totalChunks) / 10;
const wClass = percent.toString().replace(".", "-");
const pb = document.getElementById("pb");
pb.className = `determinate width-${wClass}`;
pb.attributes.getNamedItem("aria-valuenow").value = percent;
document.getElementById("pbt").innerHTML = `${percent}%`;
};
const showZipContent = (zipFile) => {
const showZipContentDOM = document.getElementById("showZipContent");
const showZipContentDOMListener = () => {
showZipContentDOM.onclick = () => {};
document.body.style.cursor = "wait";
lufi
.decompress(zipFile)
.andThen((job) => job.waitForCompletion())
.andThen((job) => {
const newElement = document.createElement("div");
let innerHTML = `${i18n.zipContent}
`;
job.archiveFiles.forEach((file) => {
innerHTML += `-
${escapeHtml(file.name)}
(${filesize(file.size)})
`;
});
innerHTML += "
";
newElement.innerHTML = innerHTML;
pbd.append(newElement);
document
.querySelectorAll(".download-zip-content")
.forEach((element) => {
const elementListener = (e) => {
e.preventDefault();
const filename = element.getAttribute("download");
const file = job.archiveFiles.find(
(file) => file.name === filename
);
element.removeEventListener("click", elementListener);
element.setAttribute("href", URL.createObjectURL(file));
element.click();
};
element.addEventListener("click", elementListener);
showZipContentDOM.style.display = "none";
document.body.style.cursor = "auto";
});
});
};
showZipContentDOM.onclick = showZipContentDOMListener;
};