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 Slideshows
  // --- Huck Slideshow ---
// ===============================
  const huckSlideshow = (function() {
    const tag = 'huck';
    const slides = { A: [], B: [] };
    const currentIndex = { A: 0, B: 0 };


// Helper Factory
    function load(target) {
function createCharacterSlideshow(tag) {
  return {
    tag: tag,
    slides: { A: [], B: [] },
    currentIndex: { A: 0, B: 0 },
 
    load: function(target) {
       const dropdownId = 'huckDropdown' + target;
       const dropdownId = 'huckDropdown' + target;
       const imageId = 'huckSlide' + target;
       const selectedIllustrator = $('#' + dropdownId).val()?.toLowerCase();
      const selectedIllustrator = document.getElementById(dropdownId).value.toLowerCase();
       const img = $('#huckSlide' + target);
       const imageElement = document.getElementById(imageId);


       if (!imageElement) return;
       slides[target] = [];
 
       currentIndex[target] = 0;
      this.slides[target] = [];
       this.currentIndex[target] = 0;


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


       const rows = document.querySelectorAll('#catalog tbody tr');
       $('#catalog tbody tr').each(function() {
      rows.forEach((row) => {
        const row = $(this);
         const illustratorText = row.cells[2]?.textContent.trim().toLowerCase() || '';
         const illustratorText = row.find('td:eq(2)').text().trim().toLowerCase();
         const tagsText = row.cells[7]?.textContent.trim().toLowerCase() || '';
         const tagsText = row.find('td:eq(7)').text().trim().toLowerCase();
         const idLink = row.cells[8]?.getElementsByTagName('a')[0];
         const idText = row.find('td:eq(8) a').text().trim();
        const idText = idLink?.textContent.trim() || '';


         if (illustratorText.includes(selectedIllustrator) && tagsText.includes(this.tag)) {
         if (illustratorText.includes(selectedIllustrator) && tagsText.includes(tag)) {
           this.slides[target].push(idText);
           slides[target].push(idText);
         }
         }
       });
       });


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


       this.show(target, 0);
       show(target, 0);
     },
     }


     show: function(target, index) {
     function show(target, index) {
       const imageElement = document.getElementById('huckSlide' + target);
       if (slides[target].length === 0) return;
       const slideArray = this.slides[target];
      currentIndex[target] = (index + slides[target].length) % slides[target].length;
       if (!imageElement || slideArray.length === 0) return;
       const imageId = slides[target][currentIndex[target]];
       $('#huckSlide' + target)
        .attr('src', '/index.php/Special:Redirect/file/' + imageId + '.jpg')
        .attr('alt', imageId)
        .attr('title', imageId);
    }


      this.currentIndex[target] = (index + slideArray.length) % slideArray.length;
    function next(target) { show(target, currentIndex[target] + 1); }
      const imageId = slideArray[this.currentIndex[target]];
    function prev(target) { show(target, currentIndex[target] - 1); }
      imageElement.src = '/index.php/Special:Redirect/file/' + imageId + '.jpg';
      imageElement.alt = imageId;
      imageElement.title = imageId;
    },


     next: function(target) { this.show(target, this.currentIndex[target] + 1); },
     // Eventlistener für Dropdowns
    prev: function(target) { this.show(target, this.currentIndex[target] - 1); }
    ['A','B'].forEach(target => {
  };
      $('#huckDropdown' + target).on('change', () => load(target));
}
    });


// Huck slideshow object
    return { load, show, next, prev };
const huckSlideshow = createCharacterSlideshow('huck');
  })();


// Event listeners für Dropdowns
   window.huckSlideshow = huckSlideshow; // global verfügbar für onclick Buttons
document.addEventListener("DOMContentLoaded", function() {
   ['A', 'B'].forEach((target) => {
    const dropdown = document.getElementById('huckDropdown' + target);
    if (dropdown) {
      dropdown.addEventListener('change', function() {
        huckSlideshow.load(target);
      });
    }
  });
});
});

Revision as of 22:39, 2 September 2025

$(function() { // jQuery ready
  // --- Huck Slideshow ---
  const huckSlideshow = (function() {
    const tag = 'huck';
    const slides = { A: [], B: [] };
    const currentIndex = { A: 0, B: 0 };

    function load(target) {
      const dropdownId = 'huckDropdown' + target;
      const selectedIllustrator = $('#' + dropdownId).val()?.toLowerCase();
      const img = $('#huckSlide' + target);

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

      if (!selectedIllustrator) {
        img.attr('src', 'https://illus.twainframe.org/images/d/d2/GalleryPlaceholder.png')
           .attr('title', 'No illustrator selected.');
        return;
      }

      $('#catalog tbody tr').each(function() {
        const row = $(this);
        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)) {
          slides[target].push(idText);
        }
      });

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

      show(target, 0);
    }

    function show(target, index) {
      if (slides[target].length === 0) return;
      currentIndex[target] = (index + slides[target].length) % slides[target].length;
      const imageId = slides[target][currentIndex[target]];
      $('#huckSlide' + target)
        .attr('src', '/index.php/Special:Redirect/file/' + imageId + '.jpg')
        .attr('alt', imageId)
        .attr('title', imageId);
    }

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

    // Eventlistener für Dropdowns
    ['A','B'].forEach(target => {
      $('#huckDropdown' + target).on('change', () => load(target));
    });

    return { load, show, next, prev };
  })();

  window.huckSlideshow = huckSlideshow; // global verfügbar für onclick Buttons
});