Toggle menu
Toggle preferences menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

MediaWiki:Common.js: Difference between revisions

MediaWiki interface page
No edit summary
No edit summary
Line 2: Line 2:


$(document).ready(function() {
$(document).ready(function() {
     // True accordion behavior: collapse others when one is expanded
     // Wait for collapsible elements to be initialized by MediaWiki
     $('.accordion-section').on('click', function(e) {
     mw.hook('wikipage.content').add(function() {
         var $this = $(this);
         var $sections = $('.accordion-section');
        // If clicking the header (not a link inside content), toggle it
        if (e.target.tagName !== 'A' && !$this.hasClass('mw-collapsed')) {
            $this.addClass('mw-collapsed');
        } else if (e.target.tagName !== 'A') {
            $('.accordion-section').not(this).addClass('mw-collapsed'); // Collapse all others
            $this.removeClass('mw-collapsed'); // Expand this one
        }
    });


    // Disable link clicks in collapsed sections
        // Initialize: disable links in collapsed sections
    $('.accordion-section.mw-collapsed a').on('click', function(e) {
        $sections.each(function() {
         e.preventDefault(); // Prevent link from working when collapsed
            if ($(this).hasClass('mw-collapsed')) {
    });
                $(this).find('.mw-headline a').css('pointer-events', 'none');
            }
         });


    // Re-enable links when expanded
        // Handle clicks on headers
    $('.accordion-section').on('click', function() {
        $sections.on('click', '.mw-headline', function(e) {
        if (!$(this).hasClass('mw-collapsed')) {
            var $section = $(this).closest('.accordion-section');
            $(this).find('a').off('click'); // Remove the preventDefault when expanded
            var isCollapsed = $section.hasClass('mw-collapsed');
         }
 
            if (isCollapsed) {
                // Collapse all other sections
                $sections.not($section).addClass('mw-collapsed');
                // Expand this section
                $section.removeClass('mw-collapsed');
                // Enable links in this section
                $section.find('.mw-headline a').css('pointer-events', 'auto');
                // Disable links in all other sections
                $sections.not($section).find('.mw-headline a').css('pointer-events', 'none');
            } else {
                // Collapse this section
                $section.addClass('mw-collapsed');
                // Disable links in this section
                $section.find('.mw-headline a').css('pointer-events', 'none');
            }
 
            // Prevent link click from propagating if collapsed
            if (e.target.tagName === 'A' && $section.hasClass('mw-collapsed')) {
                e.preventDefault();
            }
         });
     });
     });
});
});

Revision as of 07:47, 29 March 2025

/* Any JavaScript here will be loaded for all users on every page load. */

$(document).ready(function() {
    // Wait for collapsible elements to be initialized by MediaWiki
    mw.hook('wikipage.content').add(function() {
        var $sections = $('.accordion-section');

        // Initialize: disable links in collapsed sections
        $sections.each(function() {
            if ($(this).hasClass('mw-collapsed')) {
                $(this).find('.mw-headline a').css('pointer-events', 'none');
            }
        });

        // Handle clicks on headers
        $sections.on('click', '.mw-headline', function(e) {
            var $section = $(this).closest('.accordion-section');
            var isCollapsed = $section.hasClass('mw-collapsed');

            if (isCollapsed) {
                // Collapse all other sections
                $sections.not($section).addClass('mw-collapsed');
                // Expand this section
                $section.removeClass('mw-collapsed');
                // Enable links in this section
                $section.find('.mw-headline a').css('pointer-events', 'auto');
                // Disable links in all other sections
                $sections.not($section).find('.mw-headline a').css('pointer-events', 'none');
            } else {
                // Collapse this section
                $section.addClass('mw-collapsed');
                // Disable links in this section
                $section.find('.mw-headline a').css('pointer-events', 'none');
            }

            // Prevent link click from propagating if collapsed
            if (e.target.tagName === 'A' && $section.hasClass('mw-collapsed')) {
                e.preventDefault();
            }
        });
    });
});