MediaWiki

BibTest.js: Difference between revisions

From Illustrations in German Translations of Mark Twain's Works

No edit summary
No edit summary
Line 1: Line 1:
const activeTags = new Set();
const activeTags = new Set();


const tagButtons = document.querySelectorAll(".tag-filter");
/* TAG CLICK HANDLING */
document.querySelectorAll(".tag").forEach(tag => {


tagButtons.forEach(btn => {
     tag.addEventListener("click", () => {
     btn.addEventListener("click", () => {


         const tag = btn.dataset.tag;
         const value = tag.dataset.tag;


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


         filterEntries();
         filterEntries();
        drawNetwork();


     });
     });
});
});


/* FILTER FUNCTION */
function filterEntries() {
function filterEntries() {


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


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


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


         if (activeTags.size === 0 || match) {
         if (activeTags.size === 0 || matches) {
             entry.style.display = "block";
             entry.style.display = "block";
         } else {
         } else {
Line 37: Line 39:


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

Revision as of 21:45, 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();
            }
        }
    }
}