MediaWiki:Gadget-InstantCollapsible.js

From [N8]
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)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/** <nowiki>
 * Removes fade animations on mw-collapsible tables
 * Does not affect enhanced recent changes
 *
 * @author  Cqm
 */

(function ($) {
    'use strict';

    function noFade() {
            // use th to avoid affecting enhanced rc
        var $toggle = $('th > .mw-collapsible-toggle'),
            $table = $toggle.parent().parent().parent().parent();

        // prevent normal mw-collapsible behaviour
        $toggle.unbind('click');

        $table.find('td > table.navbox-subgroup > tbody > tr[style*="display"]')
            .css('display', '');
        $table.find('td > table > tbody > tr[style*="display"] > th.navbox-title')
            .parent()
            .css('display', '');

        $toggle.click(function (e) {
            // stop scrolling to the top of the page
            e.preventDefault();

            var $this = $(this),
                // move one level at a time to avoid selecting nested tables
                $table = $this.parent().parent().parent().parent(),
                // check for defined expand/collapse text
                // normally these are defined by [[MediaWiki:Collapsible-expand]] and [[MediaWiki:Collapsible-collapse]] respectively
                expand = $table.attr('data-expandtext') || mw.msg( 'collapsible-expand' ) || 'Expand',
                collapse = $table.attr('data-collapsetext') || mw.msg( 'collapsible-collapse' ) || 'Collapse',
                $tr, hasHeader = false;

            if ($table.children('thead').length) {
                // there seems to be a bug with mw-collapsible hiding thead rows?
                // possibly caused by sortable script loading after collapsible script
                $tr = $table.children('tbody').children();
                hasHeader = true;
            } else {
                $tr = $table.children().children();
            }

            if ($table.hasClass('mw-collapsed')) {
                $table.removeClass('mw-collapsed');
                $this.children('a').text(collapse);

                $this.addClass('mw-collapsible-toggle-expanded')
                    .removeClass('mw-collapsible-toggle-collapsed');

                $tr.each(function (i) {
                    // ignoring first row if no headers
                    if (hasHeader) {
                    	$(this).css('display', 'table-row');
                    } else if (i !== 0) {
                    	$(this).css('display', 'table-row');
                    }
                });
            } else {
                $table.addClass('mw-collapsed');

                // this is only added by default if already collapsed
                if (!$table.hasClass('mw-made-collapsible')) {
                    $table.addClass('mw-made-collapsible');
                }

                $this.children('a').text(expand);
                $this.addClass('mw-collapsible-toggle-collapsed')
                    .removeClass('mw-collapsible-toggle-expanded');

                $tr.each(function (i) {
                    // ignoring first row if no headers
                    if (hasHeader) {
                    	$(this).css('display', 'none');
                    } else if (i !== 0) {
                    	$(this).css('display', 'none');
                    }
                });
            }
        });
    }

    $(function () {
        if ($('.mw-collapsible').length) {
        	mw.hook('wikipage.collapsibleContent').add(noFade);
        }
    });

}(this.jQuery));

/* </nowiki> */