All files / src/web dom-utils.js

91.3% Statements 21/23
66.66% Branches 4/6
85.71% Functions 6/7
95.23% Lines 20/21

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47          129x       3x 3x 3x   3x 3x       1x 1x 1x             8x       5x 5x 5x 5x 5x 5x 5x 5x         2x 2x    
import { replaceOrAddExtension } from "../archive.js";
 
// Minimal DOM helpers to replace jQuery usage.
 
export function toggle(el, visible) {
    el.style.display = visible ? "" : "none";
}
 
export function fadeIn(el, duration = 400) {
    el.style.display = "";
    el.style.transition = `opacity ${duration}ms`;
    el.style.opacity = "0";
    // Force reflow so the transition triggers.
    void el.offsetHeight;
    el.style.opacity = "1";
}
 
export function fadeOut(el, duration = 400) {
    el.style.transition = `opacity ${duration}ms`;
    el.style.opacity = "0";
    setTimeout(() => {
        if (el.style.opacity === "0") el.style.display = "none";
    }, duration);
}
 
// Safari fetches the blob a task or more after the click, so the URL must outlive it.
// 40s matches FileSaver.js.
const BlobUrlLifetimeMs = 40000;
 
/** Save a blob to the user's downloads under the given file name. */
export function downloadBlob(blob, fileName) {
    const url = URL.createObjectURL(blob);
    const a = document.createElement("a");
    a.href = url;
    a.download = fileName;
    document.body.appendChild(a);
    a.click();
    a.remove();
    setTimeout(() => URL.revokeObjectURL(url), BlobUrlLifetimeMs);
}
 
/** Save raw image bytes under the disc's name with the extension of the format they are in. */
export function downloadDriveData(data, name, extension) {
    const blob = new Blob([data], { type: "application/octet-stream" });
    downloadBlob(blob, replaceOrAddExtension(name, extension));
}