MediaWiki:Common.js

From Mor Afgin Website
Revision as of 18:41, 21 October 2024 by Mor Afgin (talk | contribs)

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Internet Explorer / Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5
  • Opera: Press Ctrl-F5.
/* Any JavaScript here will be loaded for all users on every page load. */

// Load Fancybox CSS
$('<link>')
  .appendTo('head')
  .attr({
    type: 'text/css',
    rel: 'stylesheet',
    href: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css'
  });

// Load Fancybox JS
$.getScript('https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js', function() {
    // Apply Fancybox to all images inside the gallery
    $('.fancybox-gallery a').each(function() {
        var $link = $(this);
        var $image = $link.find('img');  // Find the image element inside the link

        // Get the thumbnail URL
        var thumbnailUrl = $image.attr('src');

        // Log the original thumbnail URL for debugging
        console.log('Thumbnail URL:', thumbnailUrl);

        // Convert the thumbnail URL to the full image URL by removing "/thumb/" and the size part
        var fullImageUrl = thumbnailUrl.replace('/thumb', '').replace(/\/\d+px-.+$/, '');

        // Log the constructed full image URL for debugging
        console.log('Constructed Full Image URL:', fullImageUrl);

        // Update the link to point to the full image URL
        $link.attr('href', fullImageUrl);

        // Log the href being set on the link to make sure it's correct
        console.log('Final href set on link:', $link.attr('href'));

        // Enable Fancybox for this link
        $link.attr('data-fancybox', 'gallery');
    });

    // Initialize Fancybox with additional debugging
    $('[data-fancybox="gallery"]').fancybox({
        buttons: [
            'zoom',
            'slideShow',
            'fullScreen',
            'thumbs',
            'close'
        ],
        protect: true,
        loop: true,
        maxWidth: '90%',
        maxHeight: '90%',
        width: 'auto',
        height: 'auto',
        responsive: true,
        transitionEffect: 'fade',
        mobile: {
            clickSlide: 'close',  // Customize for mobile if needed
            clickOutside: 'close'
        },
        // Additional logging to catch errors
        afterLoad: function(instance, current) {
            console.log('Fancybox loaded:', current);
        },
        afterShow: function(instance, current) {
            console.log('Fancybox showing image:', current.src);
        },
        onInit: function(instance) {
            console.log('Fancybox initialized');
        }
    });
});