MediaWiki:Common.js

From Innocent Witches
Jump to navigation Jump to search

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.
//------------------кнопка collapse all-------------------------//

$(document).ready(function() {
    var allExpanded = true; // Track the state of collapsibles

    // Function to toggle all collapsibles
    function toggleAllCollapsibles() {
        if (allExpanded) {
            // Collapse all
            $('.mw-collapsible .mw-collapsible-toggle a').each(function() {
                var $parent = $(this).closest('.mw-collapsible');
                if (!$parent.hasClass('mw-collapsed')) {
                    $(this).click();
                }
            });
            allExpanded = false;
        } else {
            // Expand all
            $('.mw-collapsible .mw-collapsible-toggle a').each(function() {
                var $parent = $(this).closest('.mw-collapsible');
                if ($parent.hasClass('mw-collapsed')) {
                    $(this).click();
                }
            });
            allExpanded = true;
        }
    }

    // Attach the toggle function to the span with the unique class 'toggle-all-button'
    $('.toggle-all-button').on('click', function() {
        toggleAllCollapsibles();
    });
});


//------------------таббер-------------------------//

mw.loader.using('mediawiki.util').then(function() {

function zselector($content, prefix) {
    var ActiveID = '';
    $(function () {
        $('[class|="' + prefix + 'cc"]').click(function () {
            var cn = $(this).attr('class');
            if (typeof cn !== 'undefined') {
                ZContent(cn, '0');
            }
        });
        $('[class|="' + prefix + 'hh"]').mouseenter(function () {
            var cn = $(this).attr('class');
            if (typeof cn !== 'undefined') {
                ZContent(cn, '1');
            }
        });
        $('[class|="' + prefix + 'hh"]').mouseleave(function () {
            var cn = $(this).attr('class');
            if (typeof cn !== 'undefined') {
                ZContent(cn, '2');
            }
        });
        $('[class|="' + prefix + 'zz"]').each(function (i, elem) {
            if ($(this).css('display') == 'none') {
                $(this).css('opacity', 0);
            }
        });
    });
    
    function ZContent(classValue, effect) {
        if (classValue.split) {
            var ID = '';
            var elemClasses = classValue.split(' ');
            for (var i = 0; i < elemClasses.length; i++) {
                var elemClass = elemClasses[i];
                if (elemClass.substring(0, 3 + prefix.length) == prefix + 'hh-' || elemClass.substring(0, 3 + prefix.length) == prefix + 'cc-') {
                    ID = elemClass.substring(3 + prefix.length);
                    if (effect == '0') {
                        ActiveID = ID;
                        ZEffect(ID);
                        SelectElem(prefix + 'cc', ID);
                        break;
                    } else if (effect == '1') {
                        ActiveID = ID;
                        ZEffect(ID);
                        SelectElem(prefix + 'hh', ID);
                        break;
                    } else if (effect == '2') {
                        ZEffect(ActiveID);
                        SelectElem(prefix + 'hh', ID);
                        break;
                    }
                }
            }
        }
    }
    
    function ZEffect(ID) {
        $('[class|="' + prefix + 'zz"]').each(function (i, elem) {
            if ($(this).hasClass(prefix + 'zz-' + ID)) {
                $(this).css('display', 'block');
                $(this).stop();
                $(this).animate({
                    opacity: 1,
                    queue: false
                }, 700);
                triggerLazyLoad($(this));
            } else {
                $(this).css('display', 'none');
                $(this).stop();
                $(this).animate({
                    opacity: 0,
                    queue: false
                }, 0);
            }
        });
    }
    
    function SelectElem(type, ID) {
        $('[class|="' + prefix + 'cc"],[class|="' + prefix + 'hh"]').each(function (i, elem) {
            if ($(this).hasClass(type + '-' + ID)) {
                $(this).removeClass('sn');
                $(this).addClass('sy');
            } else {
                $(this).removeClass('sy');
                $(this).addClass('sn');
            }
        });
    }
    
    function triggerLazyLoad($element) {
        $element.find('img').each(function() {
            if (typeof this.loading === 'undefined' || this.loading === 'lazy') {
                this.loading = 'eager'; // Force image to load
                this.src = this.src; // Trigger the load
            }
        });
    }
}

mw.hook('wikipage.content').add(function($content) {
    zselector($content, '');  // Default prefix
    zselector($content, 'l-'); // First tabber
    zselector($content, 'm-'); // Second tabber, can use more like this
    // Add more prefixes as needed (e.g., 'n-', 'o-', etc.)
});
});

//------------Динамическая кнопка "back to top"---------------------//


$(document).ready(function() {
    // Create the back to top button element
    var backButton = $('<button id="back-to-top" title="Back to top"> </button>');
    
    // Append the button to the body
    $('body').append(backButton);
    
    // Hide the button initially
    backButton.hide();
    
    // Show or hide the button based on the scroll position
    $(window).scroll(function() {
        if ($(this).scrollTop() > 500) {
            backButton.fadeIn();
        } else {
            backButton.fadeOut();
        }
    });
    
    // Smooth scroll to top when the button is clicked
    backButton.click(function() {
        $('html, body').animate({scrollTop: 0}, 800);
        return false;
    });
});


//----------------кликабельные изображения-------------------//

$(document).ready(function() {
  $('body').append('<div id="lightbox" style="display:none;position:fixed;top:0;left:0;width:100%;height:100%;background:rgba(0,0,0,0.6);text-align:center;z-index:9999"></div>');
  $('a.image').click(function(e) {
    e.preventDefault();
    var src = $(this).find('img').attr('src');
    if (src.match('/thumb/')) {
      src = src.replace('/thumb', '').replace(/\/[\-_.%\w]*$/, '');
    }
    if (!src.startsWith('http')) {
      src = window.location.protocol + src;
    }
    $('#lightbox')
      .html('<img src="'+src+'" style="background:#fff0;max-width:70%;vertical-align:middle;cursor:pointer;" />')
      .css('line-height', $(window).height()+'px')
      .show()
      .on('click', function() { $(this).hide(); });
  });
  $(document).keyup(function(e) {
    if (e.key === "Escape") { // "Escape" key maps to keycode 27
      $('#lightbox').hide();
    }
  });
});

//----------------прекл-------------------//

$(document).ready(function() {
    var clock = $('#digital-clock');
    var image = $('#countdown-image');
    var countdownStarted = false;

    // Function to format the time as MM:SS
    function formatTime(seconds) {
        var minutes = Math.floor(seconds / 60);
        var remainingSeconds = seconds % 60;
        return (minutes < 10 ? '0' : '') + minutes + ':' + (remainingSeconds < 10 ? '0' : '') + remainingSeconds;
    }

    // Function to start the countdown
    function startCountdown() {
        if (countdownStarted) return; // Do not restart the countdown if it's already started
        countdownStarted = true;

        var count = 10; // 10 seconds
        clock.text(formatTime(count));

        var interval = setInterval(function() {
            count--;
            clock.text(formatTime(count));
            if (count === 0) {
                clearInterval(interval);
                clock.hide(); // Hide the clock when countdown is over
                image.show(); // Show the image when countdown is over
            }
        }, 1000); // 1000ms per count, 10 counts in 10 seconds
    }

    // Start the countdown on hover
    clock.hover(function() {
        startCountdown();
    });
});

// Load jQuery if it's not already loaded
if (typeof jQuery === 'undefined') {
    var script = document.createElement('script');
    script.src = 'https://code.jquery.com/jquery-3.6.0.min.js';
    document.head.appendChild(script);
}

$(document).ready(function() {
    var isExpanded = false; // Track the panel state

    $('#expand-wardrobe-button').on('click', function() {
        var $wardrobePanel = $('#wardrobe-panel');

        if (!isExpanded) {
            // Expand the panel to 590px and show the content
            $wardrobePanel.css('width', '590px').addClass('expanded');
            setTimeout(function() {
                $('#wardrobe-panel-content').css('opacity', 1);
            }, 500); // Delay content appearance until the sliding finishes
        } else {
            // Collapse the panel to 20px and hide the content
            $('#wardrobe-panel-content').css('opacity', 0);
            setTimeout(function() {
                $wardrobePanel.css('width', '20px').removeClass('expanded');
            }, 500); // Delay collapsing to ensure content is hidden first
        }

        isExpanded = !isExpanded;
    });
});