Sha256: 32a37445a774b4497e6705f75b57d2b9bd7eddb09a3bf376dc2c441a0a985510

Contents?: true

Size: 1.91 KB

Versions: 13

Compression:

Stored size: 1.91 KB

Contents

import UrlHelper from "./UrlHelper.mjs";

/// Formats a page for printing
/// For-printing formatting should be done in CSS, if possible.

/// Expands all dropdowns on a page, returns a list of all
/// dropdowns that were opened.
function expandAllDropdowns() {
    let expanded = [];

    for (const dropdown of document.querySelectorAll("details:not([open])")) {
        dropdown.setAttribute("open", true);
        expanded.push(dropdown);
    }

    return expanded;
}

function expandOnPrint() {
    let openedDropdowns = [];

    addEventListener("beforeprint", () => {
        openedDropdowns = expandAllDropdowns();
    });

    addEventListener("afterprint", () => {
        for (const dropdown of openedDropdowns) {
            dropdown.removeAttribute("open");
        }
    });
}

/// Expand all dropdowns containing [elem].
/// [elem] can be either a Node or an Element.
function expandContainingDropdowns(elem) {
    let currentElem = elem;

    // Walk up the DOM tree...
    do {
        // Open all containing details elements.
        if (currentElem.tagName && currentElem.tagName.toLowerCase() == "details") {
            currentElem.setAttribute("open", true);
        }

        currentElem = currentElem.parentElement;
    }
    while (currentElem);
}

function expandBasedOnURL() {
    const doExpansion = (url) => {
        // Determine the hash.
        let hash = UrlHelper.getPageHash();
        if (hash == null) {
            return;
        }

        let targetElem = document.querySelector(hash);
        let currentElem = targetElem;

        expandContainingDropdowns(targetElem);

        targetElem.focus();
    };

    doExpansion(location.href);
    addEventListener("hashchange", ({ newURL }) => {
        doExpansion(newURL);
    });
}

function autoExpandDropdowns() {
    expandOnPrint();
    expandBasedOnURL();
}

export { autoExpandDropdowns, expandContainingDropdowns };
export default autoExpandDropdowns;

Version data entries

13 entries across 13 versions & 1 rubygems

Version Path
hematite-0.1.2 assets/js/dropdownExpander.mjs
hematite-0.0.12 assets/js/dropdownExpander.mjs
hematite-0.0.11 assets/js/dropdownExpander.mjs
hematite-0.0.10 assets/js/dropdownExpander.mjs
hematite-0.0.9 assets/js/dropdownExpander.mjs
hematite-0.0.8 assets/js/dropdownExpander.mjs
hematite-0.0.7 assets/js/dropdownExpander.mjs
hematite-0.0.6 assets/js/dropdownExpander.mjs
hematite-0.0.5 assets/js/dropdownExpander.mjs
hematite-0.0.4 assets/js/dropdownExpander.mjs
hematite-0.0.3 assets/js/dropdownExpander.mjs
hematite-0.0.2 assets/js/dropdownExpander.mjs
hematite-0.0.1 assets/js/dropdownExpander.mjs