MediaWiki

CharacterFilter.js: Difference between revisions

From Illustrations in German Translations of Mark Twain's Works

No edit summary
Tag: Reverted
No edit summary
Tag: Reverted
Line 1: Line 1:
$(function() { // jQuery ready
// ===============================
  // --- Huck Slideshow ---
// Huck Slideshow – Schnellere Version
  const huckSlideshow = (function() {
// ===============================
    const tag = 'huck';
    const slides = { A: [], B: [] };
    const currentIndex = { A: 0, B: 0 };


    function load(target) {
var huckSlideshow = {
      const dropdownId = 'huckDropdown' + target;
  slides: { A: [], B: [] },
      const selectedIllustrator = $('#' + dropdownId).val()?.toLowerCase();
  currentIndex: { A: 0, B: 0 },
      const img = $('#huckSlide' + target);
  indexBuilt: false,
  characterIndex: {},


       slides[target] = [];
  // Tabelle einmalig indexieren
       currentIndex[target] = 0;
  buildIndex: function() {
    this.characterIndex = {};
    const rows = document.querySelectorAll('#catalog tbody tr');
    rows.forEach(row => {
       const illustr = row.cells[2]?.textContent.trim().toLowerCase();
      const tagsText = row.cells[7]?.textContent.trim().toLowerCase();
       const idLink = row.cells[8]?.getElementsByTagName('a')[0];
      const idText = idLink?.textContent.trim().toLowerCase();
      if (!idText) return;


       if (!selectedIllustrator) {
       tagsText.split(/\s+/).forEach(tag => {
         img.attr('src', 'https://illus.twainframe.org/images/d/d2/GalleryPlaceholder.png')
        if (!this.characterIndex[tag]) this.characterIndex[tag] = {};
          .attr('title', 'No illustrator selected.');
         if (!this.characterIndex[tag][illustr]) this.characterIndex[tag][illustr] = [];
        return;
        this.characterIndex[tag][illustr].push(idText);
      }
      });
    });
    this.indexBuilt = true;
  },


      $('#catalog tbody tr').each(function() {
  load: function(target) {
        const row = $(this);
    if (!this.indexBuilt) this.buildIndex();
        const illustratorText = row.find('td:eq(2)').text().trim().toLowerCase();
        const tagsText = row.find('td:eq(7)').text().trim().toLowerCase();
        const idText = row.find('td:eq(8) a').text().trim();


        if (illustratorText.includes(selectedIllustrator) && tagsText.includes(tag)) {
    const dropdownId = 'illustratorDropdown' + target;
          slides[target].push(idText);
    const selectedIllustrator = document.getElementById(dropdownId).value.toLowerCase();
        }
    const imageElement = document.getElementById('slide' + target);
      });
    if (!imageElement) return;


      if (slides[target].length === 0) {
    this.slides[target] = [];
        img.attr('src', 'https://illus.twainframe.org/images/d/d2/GalleryPlaceholder.png')
    this.currentIndex[target] = 0;
          .attr('title', 'No matching images.');
        return;
      }


      show(target, 0);
    if (!selectedIllustrator) {
      imageElement.src = "https://illus.twainframe.org/images/d/d2/GalleryPlaceholder.png";
      imageElement.title = "No illustrator selected.";
      return;
     }
     }


     function show(target, index) {
     const ids = this.characterIndex['huck']?.[selectedIllustrator] || [];
      if (slides[target].length === 0) return;
    if (ids.length === 0) {
      currentIndex[target] = (index + slides[target].length) % slides[target].length;
      imageElement.src = "https://illus.twainframe.org/images/d/d2/GalleryPlaceholder.png";
      const imageId = slides[target][currentIndex[target]];
      imageElement.title = "No matching images.";
      $('#huckSlide' + target)
      return;
        .attr('src', '/index.php/Special:Redirect/file/' + imageId + '.jpg')
        .attr('alt', imageId)
        .attr('title', imageId);
     }
     }


     function next(target) { show(target, currentIndex[target] + 1); }
     this.slides[target] = ids;
     function prev(target) { show(target, currentIndex[target] - 1); }
     this.show(target, 0);
  },


    // Eventlistener für Dropdowns
  show: function(target, index) {
    ['A','B'].forEach(target => {
    const imageElement = document.getElementById('slide' + target);
      $('#huckDropdown' + target).on('change', () => load(target));
    const slideArray = this.slides[target];
     });
    if (!imageElement || slideArray.length === 0) return;
 
    this.currentIndex[target] = (index + slideArray.length) % slideArray.length;
    const imageId = slideArray[this.currentIndex[target]];
    imageElement.src = '/index.php/Special:Redirect/file/' + imageId + '.jpg';
    imageElement.alt = imageId;
     imageElement.title = imageId;


     return { load, show, next, prev };
     // preload next
  })();
    const nextImg = new Image();
    nextImg.src = '/index.php/Special:Redirect/file/' + slideArray[(this.currentIndex[target]+1)%slideArray.length] + '.jpg';
  },


   window.huckSlideshow = huckSlideshow; // global verfügbar für onclick Buttons
   next: function(target) { this.show(target, this.currentIndex[target] + 1); },
});
  prev: function(target) { this.show(target, this.currentIndex[target] - 1); }
};

Revision as of 22:43, 2 September 2025

// ===============================
// Huck Slideshow – Schnellere Version
// ===============================

var huckSlideshow = {
  slides: { A: [], B: [] },
  currentIndex: { A: 0, B: 0 },
  indexBuilt: false,
  characterIndex: {},

  // Tabelle einmalig indexieren
  buildIndex: function() {
    this.characterIndex = {};
    const rows = document.querySelectorAll('#catalog tbody tr');
    rows.forEach(row => {
      const illustr = row.cells[2]?.textContent.trim().toLowerCase();
      const tagsText = row.cells[7]?.textContent.trim().toLowerCase();
      const idLink = row.cells[8]?.getElementsByTagName('a')[0];
      const idText = idLink?.textContent.trim().toLowerCase();
      if (!idText) return;

      tagsText.split(/\s+/).forEach(tag => {
        if (!this.characterIndex[tag]) this.characterIndex[tag] = {};
        if (!this.characterIndex[tag][illustr]) this.characterIndex[tag][illustr] = [];
        this.characterIndex[tag][illustr].push(idText);
      });
    });
    this.indexBuilt = true;
  },

  load: function(target) {
    if (!this.indexBuilt) this.buildIndex();

    const dropdownId = 'illustratorDropdown' + target;
    const selectedIllustrator = document.getElementById(dropdownId).value.toLowerCase();
    const imageElement = document.getElementById('slide' + target);
    if (!imageElement) return;

    this.slides[target] = [];
    this.currentIndex[target] = 0;

    if (!selectedIllustrator) {
      imageElement.src = "https://illus.twainframe.org/images/d/d2/GalleryPlaceholder.png";
      imageElement.title = "No illustrator selected.";
      return;
    }

    const ids = this.characterIndex['huck']?.[selectedIllustrator] || [];
    if (ids.length === 0) {
      imageElement.src = "https://illus.twainframe.org/images/d/d2/GalleryPlaceholder.png";
      imageElement.title = "No matching images.";
      return;
    }

    this.slides[target] = ids;
    this.show(target, 0);
  },

  show: function(target, index) {
    const imageElement = document.getElementById('slide' + target);
    const slideArray = this.slides[target];
    if (!imageElement || slideArray.length === 0) return;

    this.currentIndex[target] = (index + slideArray.length) % slideArray.length;
    const imageId = slideArray[this.currentIndex[target]];
    imageElement.src = '/index.php/Special:Redirect/file/' + imageId + '.jpg';
    imageElement.alt = imageId;
    imageElement.title = imageId;

    // preload next
    const nextImg = new Image();
    nextImg.src = '/index.php/Special:Redirect/file/' + slideArray[(this.currentIndex[target]+1)%slideArray.length] + '.jpg';
  },

  next: function(target) { this.show(target, this.currentIndex[target] + 1); },
  prev: function(target) { this.show(target, this.currentIndex[target] - 1); }
};