Aktionen

MediaWiki

Common.js: Unterschied zwischen den Versionen

Aus exmediawiki

Die Seite wurde neu angelegt: „// MediaWiki Slideshow Gallery - PAGE SPECIFIC ONLY // Only loads on pages with slideshow-gallery-page class // Add this to MediaWiki:Common.js // Check if cu…“
 
Keine Bearbeitungszusammenfassung
Zeile 1: Zeile 1:
// MediaWiki Slideshow Gallery - PAGE SPECIFIC ONLY
// MediaWiki Slideshow Gallery - PAGE SPECIFIC ONLY
// Only loads on pages with slideshow-gallery-page class
// Add this to MediaWiki:Common.js
// Add this to MediaWiki:Common.js


// Check if current page should have slideshow functionality
(function() {
function shouldLoadSlideshow() {
    'use strict';
    return document.body.classList.contains('slideshow-gallery-page') ||  
   
          document.querySelector('.slideshow-gallery-page') !== null ||
    // Check if current page should have slideshow functionality
          document.querySelector('.slideshow-container') !== null;
    function shouldLoadSlideshow() {
}
        return document.body && (
            document.body.classList.contains('slideshow-gallery-page') ||  
            document.querySelector('.slideshow-gallery-page') !== null ||
            document.querySelector('.slideshow-container') !== null
        );
    }
 
    // Only load slideshow code if needed
    if (!shouldLoadSlideshow()) {
        return;
    }


// Only load slideshow code if needed
if (shouldLoadSlideshow()) {
     console.log('Loading slideshow gallery for this page');
     console.log('Loading slideshow gallery for this page');
   
    // Wait for MediaWiki to fully load
    mw.hook('wikipage.content').add(function($content) {
        initSlideshows($content);
    });


     $(document).ready(function() {
     // Wait for MediaWiki and jQuery to be ready
        initSlideshows($(document));
    function initWhenReady() {
    });
        if (typeof mw === 'undefined' || typeof $ === 'undefined') {
            setTimeout(initWhenReady, 100);
            return;
        }
       
        // MediaWiki hook
        mw.hook('wikipage.content').add(function($content) {
            initSlideshows($content);
        });
 
        // jQuery ready
        $(document).ready(function() {
            initSlideshows($(document));
        });
    }


     function initSlideshows($context) {
     function initSlideshows($context) {
Zeile 27: Zeile 43:
          
          
         // Only initialize slideshow containers on pages with the specific class
         // Only initialize slideshow containers on pages with the specific class
         const $galleryPages = $context.find('.slideshow-gallery-page, body.slideshow-gallery-page');
         var $galleryPages = $context.find('.slideshow-gallery-page, body.slideshow-gallery-page');
         if ($galleryPages.length === 0 && !$context.hasClass('slideshow-gallery-page')) {
         if ($galleryPages.length === 0 && !$context.hasClass('slideshow-gallery-page')) {
             // Also check for direct slideshow containers as fallback
             // Also check for direct slideshow containers as fallback
Zeile 45: Zeile 61:


     function initSingleSlideshow(container) {
     function initSingleSlideshow(container) {
         const slides = container.querySelectorAll('.mySlides');
         var slides = container.querySelectorAll('.mySlides');
         const thumbnails = container.querySelectorAll('.demo');
         var thumbnails = container.querySelectorAll('.demo');
         const captionText = container.querySelector('#caption, .caption');
         var captionText = container.querySelector('#caption, .caption');
         let slideIndex = 1;
         var slideIndex = 1;
          
          
         console.log('Initializing slideshow with', slides.length, 'slides');
         console.log('Initializing slideshow with', slides.length, 'slides');
Zeile 61: Zeile 77:
          
          
         // Add click handlers to navigation buttons
         // Add click handlers to navigation buttons
         const prevBtn = container.querySelector('.prev');
         var prevBtn = container.querySelector('.prev');
         const nextBtn = container.querySelector('.next');
         var nextBtn = container.querySelector('.next');
          
          
         if (prevBtn) {
         if (prevBtn) {
Zeile 79: Zeile 95:
          
          
         // Add click handlers to thumbnails
         // Add click handlers to thumbnails
         thumbnails.forEach(function(thumb, index) {
         for (var i = 0; i < thumbnails.length; i++) {
            thumb.addEventListener('click', function() {
            (function(index) {
                currentSlide(index + 1);
                thumbnails[index].addEventListener('click', function() {
             });
                    currentSlide(index + 1);
         });
                });
             })(i);
         }
          
          
         // Keyboard navigation
         // Keyboard navigation
Zeile 100: Zeile 118:
          
          
         // Touch/swipe support
         // Touch/swipe support
         let startX = 0;
         var startX = 0;
         let endX = 0;
         var endX = 0;
          
          
         container.addEventListener('touchstart', function(e) {
         container.addEventListener('touchstart', function(e) {
Zeile 113: Zeile 131:
         container.addEventListener('touchend', function(e) {
         container.addEventListener('touchend', function(e) {
             endX = e.changedTouches[0].clientX;
             endX = e.changedTouches[0].clientX;
             const diffX = startX - endX;
             var diffX = startX - endX;
              
              
             if (Math.abs(diffX) > 50) {
             if (Math.abs(diffX) > 50) {
Zeile 125: Zeile 143:
          
          
         // Auto-play functionality (if data-autoplay attribute is set)
         // Auto-play functionality (if data-autoplay attribute is set)
         let autoPlayInterval;
         var autoPlayInterval;
         const autoPlayDelay = container.getAttribute('data-autoplay');
         var autoPlayDelay = container.getAttribute('data-autoplay');
          
          
         if (autoPlayDelay) {
         if (autoPlayDelay) {
Zeile 159: Zeile 177:
          
          
         function showSlides(n) {
         function showSlides(n) {
             let i;
             var i;
              
              
             if (n > slides.length) { slideIndex = 1; }
             if (n > slides.length) {  
             if (n < 1) { slideIndex = slides.length; }
                slideIndex = 1;  
            }
             if (n < 1) {  
                slideIndex = slides.length;  
            }
              
              
             // Hide all slides
             // Hide all slides
Zeile 194: Zeile 216:
         }
         }
     }
     }
} else {
 
     console.log('Slideshow gallery not needed on this page');
     // Start initialization
}
    initWhenReady();
 
})();

Version vom 24. Juli 2025, 11:12 Uhr

// MediaWiki Slideshow Gallery - PAGE SPECIFIC ONLY
// Add this to MediaWiki:Common.js

(function() {
    'use strict';
    
    // Check if current page should have slideshow functionality
    function shouldLoadSlideshow() {
        return document.body && (
            document.body.classList.contains('slideshow-gallery-page') || 
            document.querySelector('.slideshow-gallery-page') !== null ||
            document.querySelector('.slideshow-container') !== null
        );
    }

    // Only load slideshow code if needed
    if (!shouldLoadSlideshow()) {
        return;
    }

    console.log('Loading slideshow gallery for this page');

    // Wait for MediaWiki and jQuery to be ready
    function initWhenReady() {
        if (typeof mw === 'undefined' || typeof $ === 'undefined') {
            setTimeout(initWhenReady, 100);
            return;
        }
        
        // MediaWiki hook
        mw.hook('wikipage.content').add(function($content) {
            initSlideshows($content);
        });

        // jQuery ready
        $(document).ready(function() {
            initSlideshows($(document));
        });
    }

    function initSlideshows($context) {
        $context = $context || $(document);
        
        // Only initialize slideshow containers on pages with the specific class
        var $galleryPages = $context.find('.slideshow-gallery-page, body.slideshow-gallery-page');
        if ($galleryPages.length === 0 && !$context.hasClass('slideshow-gallery-page')) {
            // Also check for direct slideshow containers as fallback
            if ($context.find('.slideshow-container').length === 0) {
                return;
            }
        }
        
        // Initialize each slideshow container
        $context.find('.slideshow-container').each(function() {
            if (!$(this).data('slideshow-initialized')) {
                $(this).data('slideshow-initialized', true);
                initSingleSlideshow(this);
            }
        });
    }

    function initSingleSlideshow(container) {
        var slides = container.querySelectorAll('.mySlides');
        var thumbnails = container.querySelectorAll('.demo');
        var captionText = container.querySelector('#caption, .caption');
        var slideIndex = 1;
        
        console.log('Initializing slideshow with', slides.length, 'slides');
        
        if (slides.length === 0) {
            console.log('No slides found in slideshow container');
            return;
        }
        
        // Show first slide initially
        showSlides(slideIndex);
        
        // Add click handlers to navigation buttons
        var prevBtn = container.querySelector('.prev');
        var nextBtn = container.querySelector('.next');
        
        if (prevBtn) {
            prevBtn.addEventListener('click', function(e) {
                e.preventDefault();
                plusSlides(-1);
            });
        }
        
        if (nextBtn) {
            nextBtn.addEventListener('click', function(e) {
                e.preventDefault();
                plusSlides(1);
            });
        }
        
        // Add click handlers to thumbnails
        for (var i = 0; i < thumbnails.length; i++) {
            (function(index) {
                thumbnails[index].addEventListener('click', function() {
                    currentSlide(index + 1);
                });
            })(i);
        }
        
        // Keyboard navigation
        container.addEventListener('keydown', function(e) {
            if (e.keyCode === 37) { // Left arrow
                plusSlides(-1);
                e.preventDefault();
            } else if (e.keyCode === 39) { // Right arrow
                plusSlides(1);
                e.preventDefault();
            }
        });
        
        // Make container focusable for keyboard navigation
        container.setAttribute('tabindex', '0');
        
        // Touch/swipe support
        var startX = 0;
        var endX = 0;
        
        container.addEventListener('touchstart', function(e) {
            startX = e.touches[0].clientX;
        });
        
        container.addEventListener('touchmove', function(e) {
            e.preventDefault();
        });
        
        container.addEventListener('touchend', function(e) {
            endX = e.changedTouches[0].clientX;
            var diffX = startX - endX;
            
            if (Math.abs(diffX) > 50) {
                if (diffX > 0) {
                    plusSlides(1); // Swipe left = next
                } else {
                    plusSlides(-1); // Swipe right = previous
                }
            }
        });
        
        // Auto-play functionality (if data-autoplay attribute is set)
        var autoPlayInterval;
        var autoPlayDelay = container.getAttribute('data-autoplay');
        
        if (autoPlayDelay) {
            function startAutoPlay() {
                autoPlayInterval = setInterval(function() {
                    plusSlides(1);
                }, parseInt(autoPlayDelay));
            }
            
            function stopAutoPlay() {
                if (autoPlayInterval) {
                    clearInterval(autoPlayInterval);
                    autoPlayInterval = null;
                }
            }
            
            container.addEventListener('mouseenter', stopAutoPlay);
            container.addEventListener('mouseleave', startAutoPlay);
            
            startAutoPlay();
        }
        
        // Next/previous controls
        function plusSlides(n) {
            showSlides(slideIndex += n);
        }
        
        // Thumbnail image controls
        function currentSlide(n) {
            showSlides(slideIndex = n);
        }
        
        function showSlides(n) {
            var i;
            
            if (n > slides.length) { 
                slideIndex = 1; 
            }
            if (n < 1) { 
                slideIndex = slides.length; 
            }
            
            // Hide all slides
            for (i = 0; i < slides.length; i++) {
                slides[i].style.display = "none";
                slides[i].classList.remove('active');
            }
            
            // Remove active class from all thumbnails
            for (i = 0; i < thumbnails.length; i++) {
                thumbnails[i].classList.remove('active-thumb');
            }
            
            // Show current slide
            if (slides[slideIndex - 1]) {
                slides[slideIndex - 1].style.display = "block";
                slides[slideIndex - 1].classList.add('active');
            }
            
            // Highlight current thumbnail
            if (thumbnails[slideIndex - 1]) {
                thumbnails[slideIndex - 1].classList.add('active-thumb');
            }
            
            // Update caption
            if (captionText && thumbnails[slideIndex - 1]) {
                captionText.innerHTML = thumbnails[slideIndex - 1].getAttribute('alt') || '';
            }
            
            console.log('Showing slide', slideIndex, 'of', slides.length);
        }
    }

    // Start initialization
    initWhenReady();

})();