MediaWiki

ChapterSlidesAlt.js: Difference between revisions

From Illustrations in German Translations of Mark Twain's Works

No edit summary
No edit summary
 
(4 intermediate revisions by the same user not shown)
Line 1: Line 1:
// ALTERNATIV
$(function() {
  const illustrators = ["Kemble","Schroedter","Hirth","Busoni","Kellerer","Harder","Trier","Bebié"];
  let slidesPerBox = {1: [], 2: []};
  let currentIndex = {1: 0, 2: 0};


const illustrators = [
  // Dropdowns befüllen
  "Kemble","Schroedter","Hirth","Busoni","Kellerer","Harder","Trier","Bebié"
  illustrators.forEach(name => {
];
    $("#illustratorSelect1, #illustratorSelect2").append(
      $('<option>').val(name).text(name)
    );
  });
 
  // Button Event
  $("#displayButton").click(loadSlideshowsByChapter);
 
  // Prev/Next Buttons
  $(".prevBtn").click(function() {
    prevSlide($(this).data("box"));
  });
  $(".nextBtn").click(function() {
    nextSlide($(this).data("box"));
  });


let slides = {};
  // Dropdown Change
let currentIndex = {};
  $("#illustratorSelect1, #illustratorSelect2").change(function() {
    const box = $(this).attr("id").replace("illustratorSelect", "");
    refreshGallery(parseInt(box));
  });


illustrators.forEach(name => {
  function loadSlideshowsByChapter() {
  slides[name] = [];
    const chapterNumber = parseInt($("#chapterInput").val(), 10);
  currentIndex[name] = 0;
    if (isNaN(chapterNumber) || chapterNumber < 1 || chapterNumber > 43) {
});
      alert("Please enter a number between 1 and 43.");
      return;
    }
    const chapterTag = "ch" + String(chapterNumber).padStart(3, "0");
    const rows = $("#catalog tbody tr");
    illustrators.forEach(name => { slidesPerBox[name] = []; });


function loadSlideshowsByChapterAlt() {
    rows.each(function() {
  const chapterInput = document.getElementById("chapterInput").value.trim();
      const cells = $(this).find("td");
  const chapterNumber = parseInt(chapterInput, 10);
      if (cells.length < 9) return;
      const illustratorText = cells.eq(2).text().trim().toLowerCase();
      const idLink = cells.eq(8).find("a").text().trim().toLowerCase();
      illustrators.forEach(name => {
        if (illustratorText.includes(name.toLowerCase()) && idLink.includes(chapterTag)) {
          slidesPerBox[name].push(idLink);
        }
      });
    });


  if (isNaN(chapterNumber) || chapterNumber < 1 || chapterNumber > 43) {
    refreshGallery(1);
     alert("Please enter a number between 1 and 43.");
     refreshGallery(2);
    return;
   }
   }


   const chapterTag = "ch" + String(chapterNumber).padStart(3, "0");
   function refreshGallery(boxNumber) {
  const rows = document.querySelectorAll("#catalog tbody tr");
    const select = $("#illustratorSelect" + boxNumber);
    const illustrator = select.val();
    const img = $("#slide" + boxNumber);
    const counter = $("#counter" + boxNumber);
    const infoBox = $("#mediainfo" + boxNumber); // feste Box
    const slideArray = slidesPerBox[illustrator] || [];


  illustrators.forEach(name => {
     if (!slideArray.length) {
     slides[name] = [];
       img.attr("src","https://illus.twainframe.org/images/2/2b/ChapterPlaceholder.png");
    currentIndex[name] = 0;
       currentIndex[boxNumber] = 0;
    for (let i = 0; i < rows.length; i++) {
       counter.text("0/0");
       const row = rows[i];
       img.off("click"); // Klick entfernen
      const illustratorCell = row.cells[2];
       infoBox.html(""); // leere InfoBox
       const idCell = row.cells[8];
    } else {
       const illustratorText = illustratorCell ? illustratorCell.textContent.trim().toLowerCase() : "";
      showSlide(boxNumber, 0);
       const idLink = idCell ? idCell.getElementsByTagName("a")[0] : null;
       const idText = idLink ? idLink.textContent.trim().toLowerCase() : "";
      if (illustratorText.indexOf(name.toLowerCase()) !== -1 && idText.indexOf(chapterTag) !== -1) {
        slides[name].push(idText);
      }
     }
     }
   });
   }
 
  function showSlide(boxNumber, index) {
    const select = $("#illustratorSelect" + boxNumber);
    const illustrator = select.val();
    const slideArray = slidesPerBox[illustrator];
    const img = $("#slide" + boxNumber);
    const counter = $("#counter" + boxNumber);
    const infoBox = $("#mediainfo" + boxNumber); // feste Box
 
    if (!slideArray || !slideArray.length) return;
 
    currentIndex[boxNumber] = (index + slideArray.length) % slideArray.length;
    const imageId = slideArray[currentIndex[boxNumber]];
 
    img.attr("src", "/index.php/Special:Redirect/file/" + imageId + ".jpg")
      .attr("alt", imageId)
      .removeAttr("title"); // Tooltip ausblenden
 
    // Klick öffnet File: Seite in neuem Tab
    img.off("click").on("click", function() {
      const fileUrl = "/File:" + imageId + ".jpg";
      window.open(fileUrl, "_blank");
    });


  // Nach dem Laden direkt beide Boxen refreshen
    // MediaInfo laden
  refreshGallery(1);
    loadMediaInfoBox(boxNumber, imageId);
  refreshGallery(2);
}


function refreshGallery(boxNumber) {
    counter.text((currentIndex[boxNumber]+1) + "/" + slideArray.length);
  const select = document.getElementById("illustratorSelect" + boxNumber);
   }
  const illustrator = select.value;
  const img = document.getElementById("slide" + boxNumber);
   const counter = document.getElementById("counter" + boxNumber);


   if (!slides[illustrator] || slides[illustrator].length === 0) {
   function nextSlide(boxNumber) {
     img.src = "https://illus.twainframe.org/images/2/2b/ChapterPlaceholder.png";
     showSlide(boxNumber, currentIndex[boxNumber] + 1);
    currentIndex[illustrator] = 0;
    counter.textContent = "0/0";
  } else {
    showSlideDropdown(boxNumber, 0);
   }
   }
}


function showSlideDropdown(boxNumber, index) {
  function prevSlide(boxNumber) {
  const select = document.getElementById("illustratorSelect" + boxNumber);
    showSlide(boxNumber, currentIndex[boxNumber] - 1);
  const illustrator = select.value;
   }
  const img = document.getElementById("slide" + boxNumber);
   const counter = document.getElementById("counter" + boxNumber);
  const slideArray = slides[illustrator];


   if (!img || slideArray.length === 0) return;
   // ===============================
  // MediaInfo-Funktion (für feste Box)
  // ===============================
  function loadMediaInfoBox(target, filename) {
    const infoBox = document.getElementById('mediainfo' + target);
    if (!infoBox) return;
    infoBox.textContent = "Loading ...";


  currentIndex[illustrator] = (index + slideArray.length) % slideArray.length;
    // Sicherstellen, dass wir die File-Seite abrufen und .jpg am Ende haben
  const imageId = slideArray[currentIndex[illustrator]];
    const filePage = filename.endsWith('.jpg') ? filename : filename + '.jpg';
  img.src = "/index.php/Special:Redirect/file/" + imageId + ".jpg";
    const url = '/index.php?title=File:' + encodeURIComponent(filePage) + '&action=raw';
  img.alt = imageId;
  img.title = imageId;
  counter.textContent = (currentIndex[illustrator] + 1) + "/" + slideArray.length;
}


function nextSlideDropdown(boxNumber) {
    fetch(url)
  const select = document.getElementById("illustratorSelect" + boxNumber);
        .then(resp => resp.text())
  showSlideDropdown(boxNumber, currentIndex[select.value] + 1);
        .then(text => {
}
            // Nur {{MediaInfo ... }} Block extrahieren, vorherigen Text ignorieren
            const match = text.match(/\{\{MediaInfo([\s\S]*?)\}\}/);
            if (!match) {
                infoBox.textContent = "No Data found.";
                return;
            }


function prevSlideDropdown(boxNumber) {
            const block = match[1];
  const select = document.getElementById("illustratorSelect" + boxNumber);
 
  showSlideDropdown(boxNumber, currentIndex[select.value] - 1);
            const fields = ["title","chapter","illustration","illustrator","year","tags"];
}
            let htmlList = "<ul style='margin:0; padding-left:1em; list-style:none;'>";
 
            fields.forEach(field => {
                const m = block.match(new RegExp("\\|\\s*" + field + "\\s*=([^\\n]*)"));
                if (m) {
                    const label = field.charAt(0).toUpperCase() + field.slice(1);
                    htmlList += `<li><b>${label}:</b> ${m[1].trim()}</li>`;
                }
            });
 
            htmlList += "</ul>";
            infoBox.innerHTML = htmlList;
 
            // Optional: Box stylen
            infoBox.style.backgroundColor = "#f9f9f9";
            infoBox.style.border = "1px solid #ccc";
            infoBox.style.padding = "0.5em 1em";
            infoBox.style.marginTop = "0.5em";
            infoBox.style.borderRadius = "4px";
            infoBox.style.fontSize = "0.9em";
        })
        .catch(err => {
            console.error(err);
            infoBox.textContent = "Error";
        });
  }
}); // <-- Wichtige Klammer zum Schließen von $(function() { ... })

Latest revision as of 16:18, 16 September 2025

$(function() {
  const illustrators = ["Kemble","Schroedter","Hirth","Busoni","Kellerer","Harder","Trier","Bebié"];
  let slidesPerBox = {1: [], 2: []};
  let currentIndex = {1: 0, 2: 0};

  // Dropdowns befüllen
  illustrators.forEach(name => {
    $("#illustratorSelect1, #illustratorSelect2").append(
      $('<option>').val(name).text(name)
    );
  });

  // Button Event
  $("#displayButton").click(loadSlideshowsByChapter);

  // Prev/Next Buttons
  $(".prevBtn").click(function() {
    prevSlide($(this).data("box"));
  });
  $(".nextBtn").click(function() {
    nextSlide($(this).data("box"));
  });

  // Dropdown Change
  $("#illustratorSelect1, #illustratorSelect2").change(function() {
    const box = $(this).attr("id").replace("illustratorSelect", "");
    refreshGallery(parseInt(box));
  });

  function loadSlideshowsByChapter() {
    const chapterNumber = parseInt($("#chapterInput").val(), 10);
    if (isNaN(chapterNumber) || chapterNumber < 1 || chapterNumber > 43) {
      alert("Please enter a number between 1 and 43.");
      return;
    }
    const chapterTag = "ch" + String(chapterNumber).padStart(3, "0");
    const rows = $("#catalog tbody tr");
    illustrators.forEach(name => { slidesPerBox[name] = []; });

    rows.each(function() {
      const cells = $(this).find("td");
      if (cells.length < 9) return;
      const illustratorText = cells.eq(2).text().trim().toLowerCase();
      const idLink = cells.eq(8).find("a").text().trim().toLowerCase();
      illustrators.forEach(name => {
        if (illustratorText.includes(name.toLowerCase()) && idLink.includes(chapterTag)) {
          slidesPerBox[name].push(idLink);
        }
      });
    });

    refreshGallery(1);
    refreshGallery(2);
  }

  function refreshGallery(boxNumber) {
    const select = $("#illustratorSelect" + boxNumber);
    const illustrator = select.val();
    const img = $("#slide" + boxNumber);
    const counter = $("#counter" + boxNumber);
    const infoBox = $("#mediainfo" + boxNumber); // feste Box
    const slideArray = slidesPerBox[illustrator] || [];

    if (!slideArray.length) {
      img.attr("src","https://illus.twainframe.org/images/2/2b/ChapterPlaceholder.png");
      currentIndex[boxNumber] = 0;
      counter.text("0/0");
      img.off("click"); // Klick entfernen
      infoBox.html(""); // leere InfoBox
    } else {
      showSlide(boxNumber, 0);
    }
  }

  function showSlide(boxNumber, index) {
    const select = $("#illustratorSelect" + boxNumber);
    const illustrator = select.val();
    const slideArray = slidesPerBox[illustrator];
    const img = $("#slide" + boxNumber);
    const counter = $("#counter" + boxNumber);
    const infoBox = $("#mediainfo" + boxNumber); // feste Box

    if (!slideArray || !slideArray.length) return;

    currentIndex[boxNumber] = (index + slideArray.length) % slideArray.length;
    const imageId = slideArray[currentIndex[boxNumber]];

    img.attr("src", "/index.php/Special:Redirect/file/" + imageId + ".jpg")
       .attr("alt", imageId)
       .removeAttr("title"); // Tooltip ausblenden

    // Klick öffnet File: Seite in neuem Tab
    img.off("click").on("click", function() {
      const fileUrl = "/File:" + imageId + ".jpg";
      window.open(fileUrl, "_blank");
    });

    // MediaInfo laden
    loadMediaInfoBox(boxNumber, imageId);

    counter.text((currentIndex[boxNumber]+1) + "/" + slideArray.length);
  }

  function nextSlide(boxNumber) {
    showSlide(boxNumber, currentIndex[boxNumber] + 1);
  }

  function prevSlide(boxNumber) {
    showSlide(boxNumber, currentIndex[boxNumber] - 1);
  }

  // ===============================
  // MediaInfo-Funktion (für feste Box)
  // ===============================
  function loadMediaInfoBox(target, filename) {
    const infoBox = document.getElementById('mediainfo' + target);
    if (!infoBox) return;
    infoBox.textContent = "Loading ...";

    // Sicherstellen, dass wir die File-Seite abrufen und .jpg am Ende haben
    const filePage = filename.endsWith('.jpg') ? filename : filename + '.jpg';
    const url = '/index.php?title=File:' + encodeURIComponent(filePage) + '&action=raw';

    fetch(url)
        .then(resp => resp.text())
        .then(text => {
            // Nur {{MediaInfo ... }} Block extrahieren, vorherigen Text ignorieren
            const match = text.match(/\{\{MediaInfo([\s\S]*?)\}\}/);
            if (!match) {
                infoBox.textContent = "No Data found.";
                return;
            }

            const block = match[1];

            const fields = ["title","chapter","illustration","illustrator","year","tags"];
            let htmlList = "<ul style='margin:0; padding-left:1em; list-style:none;'>";

            fields.forEach(field => {
                const m = block.match(new RegExp("\\|\\s*" + field + "\\s*=([^\\n]*)"));
                if (m) {
                    const label = field.charAt(0).toUpperCase() + field.slice(1);
                    htmlList += `<li><b>${label}:</b> ${m[1].trim()}</li>`;
                }
            });

            htmlList += "</ul>";
            infoBox.innerHTML = htmlList;

            // Optional: Box stylen
            infoBox.style.backgroundColor = "#f9f9f9";
            infoBox.style.border = "1px solid #ccc";
            infoBox.style.padding = "0.5em 1em";
            infoBox.style.marginTop = "0.5em";
            infoBox.style.borderRadius = "4px";
            infoBox.style.fontSize = "0.9em";
        })
        .catch(err => {
            console.error(err);
            infoBox.textContent = "Error";
        });
  }
}); // <-- Wichtige Klammer zum Schließen von $(function() { ... })