MediaWiki

ChapterSlides.js: Difference between revisions

From Illustrations in German Translations of Mark Twain's Works

Undo revision 1774 by HMHTEST (talk)
Tag: Undo
No edit summary
 
(11 intermediate revisions by the same user not shown)
Line 53: Line 53:
         idText.indexOf(chapterTag) !== -1
         idText.indexOf(chapterTag) !== -1
       ) {
       ) {
         slides[name].push(idLink.textContent.trim()); // Name ohne .toLowerCase(), weil wir die Original-Dateinamen brauchen
         slides[name].push(idText);
       }
       }
     }
     }
Line 59: Line 59:
     if (slides[name].length === 0) {
     if (slides[name].length === 0) {
       imageElement.src = "https://illus.twainframe.org/images/2/2b/ChapterPlaceholder.png";
       imageElement.src = "https://illus.twainframe.org/images/2/2b/ChapterPlaceholder.png";
      imageElement.alt = ""; // ganz wichtig: leeres alt → kein Hover
       imageElement.title = "No matching images found.";
       imageElement.title = "No matching images found.";
      imageElement.onclick = null;
       updateCounter(name); // Counter aktualisieren
       updateCounter(name); // Counter aktualisieren
     } else {
     } else {
Line 78: Line 78:
   const imageId = slideArray[currentIndex[illustrator]];
   const imageId = slideArray[currentIndex[illustrator]];


  // Bild setzen
   imageElement.src = "/index.php/Special:Redirect/file/" + imageId + ".jpg";
   imageElement.src = "/index.php/Special:Redirect/file/" + imageId + ".jpg";
   imageElement.alt = imageId;
   imageElement.alt = imageId;
   imageElement.title = imageId;
   imageElement.title = imageId;
  imageElement.style.cursor = "pointer";


   // Klick öffnet File-Seite in neuem Tab
   // Klick öffnet File-Seite
   imageElement.onclick = function () {
   imageElement.onclick = function() {
     window.open("/File:" + imageId + ".jpg", "_blank");
     window.open("/File:" + imageId + ".jpg", "_blank");
   };
   };
  // Hover-Popup für MediaInfo
  attachMediaInfoHoverDynamic(imageElement, imageId);


   // Counter aktualisieren
   // Counter aktualisieren
   updateCounter(illustrator);
   updateCounter(illustrator);
  // MediaInfo laden und Infobox füllen
  loadMediaInfo(illustrator, imageId);
}
}


Line 120: Line 118:
}
}


/* --- Hover-Infobox CSS (kann auch direkt ins Common.css) --- */
// ===============================
const style = document.createElement('style');
// MediaInfo Hover Popup (File-Seite, Raw-Text)
style.textContent = `
// ===============================
.slideshow-box { position: relative; }
function attachMediaInfoHoverDynamic(imgElement) {
.mediainfo-box {
    if (!imgElement) return;
  display: none;
 
  position: absolute;
// Wenn Platzhalter → kein Hover-Popup
  top: 5px;
    if (imgElement.alt === "" || imgElement.alt === "placeholder") {
  left: 5px;
        return;
  background: rgba(255,255,255,0.95);
    }
  border: 1px solid #ccc;
 
  padding: 0.5em 1em;
 
  border-radius: 4px;
    let infoBox = document.createElement('div');
  font-size: 0.9em;
    infoBox.style.position = 'absolute';
  z-index: 10;
    infoBox.style.display = 'none';
  max-width: 90%;
    infoBox.style.backgroundColor = "#f9f9f9";
  text-align: left;
    infoBox.style.border = "1px solid #ccc";
    infoBox.style.padding = "0.5em 1em";
    infoBox.style.borderRadius = "4px";
    infoBox.style.fontSize = "0.9em";
    infoBox.style.zIndex = 1000;
    infoBox.style.pointerEvents = "none"; // damit Hover nicht gestört wird
    document.body.appendChild(infoBox);
 
    imgElement.addEventListener('mouseenter', () => {
    // Browser Tooltip deaktivieren
    imgElement.removeAttribute('title');
        const filename = imgElement.alt; // img alt sollte Dateiname ohne .jpg sein
        infoBox.textContent = "Loading ...";
 
        fetch('/index.php?title=File:' + encodeURIComponent(filename) + ".jpg" + '&action=raw')
        .then(resp => resp.text())
        .then(text => {
            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;
        })
        .catch(err => {
            console.error(err);
            infoBox.textContent = "Error";
        });
 
        infoBox.style.display = 'block';
    });
 
    imgElement.addEventListener('mousemove', (e) => {
        infoBox.style.top = (e.pageY + 15) + 'px';
        infoBox.style.left = (e.pageX + 15) + 'px';
    });
 
    imgElement.addEventListener('mouseleave', () => {
        infoBox.style.display = 'none';
    });
 
}
}
.slideshow-box img:hover + .mediainfo-box { display: block; }
`;
document.head.appendChild(style);

Latest revision as of 15:38, 6 October 2025

// Acht feste Illustratoren definieren
const illustrators = [
  "Kemble",
  "Schroedter",
  "Hirth",
  "Busoni",
  "Kellerer",
  "Harder",
  "Trier",
  "Bebié"
];

// Bildlisten und aktuelle Indizes
let slides = {};
let currentIndex = {};

illustrators.forEach(name => {
  slides[name] = [];
  currentIndex[name] = 0;
});

// Eingabe verarbeiten
function loadSlideshowsByChapter() {
  const chapterInput = document.getElementById("chapterInput").value.trim();
  const chapterNumber = parseInt(chapterInput, 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");

  // Tabelle durchsuchen
  const rows = document.querySelectorAll("#catalog tbody tr");

  illustrators.forEach(name => {
    slides[name] = [];
    currentIndex[name] = 0;
    const imageElement = document.getElementById("slide-" + name);

    for (let i = 0; i < rows.length; i++) {
      const row = rows[i];
      const illustratorCell = row.cells[2];
      const idCell = row.cells[8];

      const illustratorText = illustratorCell ? illustratorCell.textContent.trim().toLowerCase() : "";
      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);
      }
    }

    if (slides[name].length === 0) {
      imageElement.src = "https://illus.twainframe.org/images/2/2b/ChapterPlaceholder.png";
      imageElement.alt = ""; // ganz wichtig: leeres alt → kein Hover
      imageElement.title = "No matching images found.";
      updateCounter(name); // Counter aktualisieren
    } else {
      showSlide(name, 0);
    }
  });
}

// Ein Bild anzeigen
function showSlide(illustrator, index) {
  const imageElement = document.getElementById("slide-" + illustrator);
  const slideArray = slides[illustrator];

  if (!imageElement || slideArray.length === 0) return;

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

  imageElement.src = "/index.php/Special:Redirect/file/" + imageId + ".jpg";
  imageElement.alt = imageId;
  imageElement.title = imageId;

  // Klick öffnet File-Seite
  imageElement.onclick = function() {
    window.open("/File:" + imageId + ".jpg", "_blank");
  };

  // Hover-Popup für MediaInfo
  attachMediaInfoHoverDynamic(imageElement, imageId);

  // Counter aktualisieren
  updateCounter(illustrator);
}

// Counter aktualisieren
function updateCounter(illustrator) {
  const counterElement = document.getElementById("counter-" + illustrator);
  const slideArray = slides[illustrator];
  const index = currentIndex[illustrator];

  if (counterElement) {
    if (slideArray.length === 0) {
      counterElement.textContent = "0/0";
    } else {
      counterElement.textContent = (index + 1) + "/" + slideArray.length;
    }
  }
}

// Buttons
function nextSlide(illustrator) {
  showSlide(illustrator, currentIndex[illustrator] + 1);
}

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

// ===============================
// MediaInfo Hover Popup (File-Seite, Raw-Text)
// ===============================
function attachMediaInfoHoverDynamic(imgElement) {
    if (!imgElement) return;

// Wenn Platzhalter → kein Hover-Popup
    if (imgElement.alt === "" || imgElement.alt === "placeholder") {
        return;
    }


    let infoBox = document.createElement('div');
    infoBox.style.position = 'absolute';
    infoBox.style.display = 'none';
    infoBox.style.backgroundColor = "#f9f9f9";
    infoBox.style.border = "1px solid #ccc";
    infoBox.style.padding = "0.5em 1em";
    infoBox.style.borderRadius = "4px";
    infoBox.style.fontSize = "0.9em";
    infoBox.style.zIndex = 1000;
    infoBox.style.pointerEvents = "none"; // damit Hover nicht gestört wird
    document.body.appendChild(infoBox);

    imgElement.addEventListener('mouseenter', () => {
    	 // Browser Tooltip deaktivieren
    imgElement.removeAttribute('title');
        const filename = imgElement.alt; // img alt sollte Dateiname ohne .jpg sein
        infoBox.textContent = "Loading ...";

        fetch('/index.php?title=File:' + encodeURIComponent(filename) + ".jpg" + '&action=raw')
        .then(resp => resp.text())
        .then(text => {
            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;
        })
        .catch(err => {
            console.error(err);
            infoBox.textContent = "Error";
        });

        infoBox.style.display = 'block';
    });

    imgElement.addEventListener('mousemove', (e) => {
        infoBox.style.top = (e.pageY + 15) + 'px';
        infoBox.style.left = (e.pageX + 15) + 'px';
    });

    imgElement.addEventListener('mouseleave', () => {
        infoBox.style.display = 'none';
    });

}