MediaWiki

BibTest.js: Difference between revisions

From Illustrations in German Translations of Mark Twain's Works

No edit summary
No edit summary
Line 107: Line 107:
     }
     }
}
}
document.querySelectorAll(".expand-btn").forEach(btn => {
    btn.addEventListener("click", () => {
        const abstract =
            btn.nextElementSibling;
        abstract.classList.toggle("hidden");
        btn.textContent =
            abstract.classList.contains("hidden")
            ? "Show Abstract"
            : "Hide Abstract";
    });
});

Revision as of 21:49, 11 May 2026

const activeTags = new Set();

/* TAG CLICK HANDLING */
document.querySelectorAll(".tag").forEach(tag => {

    tag.addEventListener("click", () => {

        const value = tag.dataset.tag;

        if (activeTags.has(value)) {
            activeTags.delete(value);
            tag.classList.remove("active");
        } else {
            activeTags.add(value);
            tag.classList.add("active");
        }

        filterEntries();

    });

});

/* FILTER FUNCTION */
function filterEntries() {

    document.querySelectorAll(".bib-entry").forEach(entry => {

        const entryTags = entry.dataset.tags.split(" ");

        const matches =
            [...activeTags].every(t => entryTags.includes(t));

        if (activeTags.size === 0 || matches) {
            entry.style.display = "block";
        } else {
            entry.style.display = "none";
        }

    });

}
document.querySelectorAll(".view-btn").forEach(btn => {
    btn.addEventListener("click", () => {

        document.querySelectorAll(".view-btn")
            .forEach(b => b.classList.remove("active"));

        btn.classList.add("active");

        const view = btn.dataset.view;

        document.getElementById("listView").style.display =
            view === "list" ? "block" : "none";

        document.getElementById("networkCanvas").style.display =
            view === "network" ? "block" : "none";

        drawNetwork();

    });
});

// SIMPLE NETWORK GRAPH
function drawNetwork() {

    const canvas = document.getElementById("networkCanvas");
    const ctx = canvas.getContext("2d");

    ctx.clearRect(0,0,canvas.width,canvas.height);

    const entries = [...document.querySelectorAll(".bib-entry")];

    const nodes = entries.map((e, i) => ({
        id: i,
        label: e.querySelector("h3").innerText,
        tags: e.dataset.tags.split(" "),
        x: Math.random() * 800,
        y: Math.random() * 500
    }));

    // draw nodes
    nodes.forEach(n => {
        ctx.beginPath();
        ctx.arc(n.x, n.y, 6, 0, Math.PI * 2);
        ctx.fillStyle = "#008CBA";
        ctx.fill();
    });

    // draw edges if shared tags
    for (let i = 0; i < nodes.length; i++) {
        for (let j = i+1; j < nodes.length; j++) {

            const shared =
                nodes[i].tags.filter(t =>
                    nodes[j].tags.includes(t)
                );

            if (shared.length > 0) {
                ctx.beginPath();
                ctx.moveTo(nodes[i].x, nodes[i].y);
                ctx.lineTo(nodes[j].x, nodes[j].y);
                ctx.strokeStyle = "rgba(0,140,186,0.2)";
                ctx.stroke();
            }
        }
    }
}
document.querySelectorAll(".expand-btn").forEach(btn => {

    btn.addEventListener("click", () => {

        const abstract =
            btn.nextElementSibling;

        abstract.classList.toggle("hidden");

        btn.textContent =
            abstract.classList.contains("hidden")
            ? "Show Abstract"
            : "Hide Abstract";

    });

});