MediaWiki

BibTest.js: Difference between revisions

From Illustrations in German Translations of Mark Twain's Works

Created page with "document.querySelectorAll(".expand-btn").forEach(button => { button.addEventListener("click", () => { const related = button.nextElementSibling; related.classList.toggle("hidden"); button.textContent = related.classList.contains("hidden") ? "Show Related Sources" : "Hide Related Sources"; }); }); document.querySelectorAll(".filter-btn").forEach(button => { button.addEventListener(..."
 
No edit summary
Line 1: Line 1:
document.querySelectorAll(".expand-btn").forEach(button => {
const activeTags = new Set();


    button.addEventListener("click", () => {
const tagButtons = document.querySelectorAll(".tag-filter");


        const related =
tagButtons.forEach(btn => {
            button.nextElementSibling;
    btn.addEventListener("click", () => {


         related.classList.toggle("hidden");
         const tag = btn.dataset.tag;


         button.textContent =
         if (activeTags.has(tag)) {
             related.classList.contains("hidden")
            activeTags.delete(tag);
             ? "Show Related Sources"
             btn.classList.remove("active");
             : "Hide Related Sources";
        } else {
            activeTags.add(tag);
             btn.classList.add("active");
        }
 
        filterEntries();
        drawNetwork();
 
    });
});
 
function filterEntries() {
 
    document.querySelectorAll(".bib-entry").forEach(entry => {
 
        const tags = entry.dataset.tags.split(" ");
 
        const match = [...activeTags].every(t => tags.includes(t));
 
        if (activeTags.size === 0 || match) {
             entry.style.display = "block";
        } else {
            entry.style.display = "none";
        }


     });
     });
}


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


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


    button.addEventListener("click", () => {
        btn.classList.add("active");


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


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


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


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


                const tags =
    });
                    entry.dataset.tags;
});


                if (
// SIMPLE NETWORK GRAPH
                    filter === "all" ||
function drawNetwork() {
                    tags.includes(filter)
                ) {
                    entry.style.display = "block";
                } else {
                    entry.style.display = "none";
                }


            });
    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();
            }
        }
    }
}

Revision as of 21:41, 11 May 2026

const activeTags = new Set();

const tagButtons = document.querySelectorAll(".tag-filter");

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

        const tag = btn.dataset.tag;

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

        filterEntries();
        drawNetwork();

    });
});

function filterEntries() {

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

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

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

        if (activeTags.size === 0 || match) {
            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();
            }
        }
    }
}