MediaWiki:Common.js: Difference between revisions

m Fix global collapsible controls
m Avoid reinitializing standard collapsible tables
Line 17: Line 17:
(function () {
(function () {
     function normalizeLegacyCollapsibles($root) {
     function normalizeLegacyCollapsibles($root) {
         $root.find('table.collapsible, div.collapsible, ul.collapsible, ol.collapsible').each(function () {
         var $legacy = $root.find('table.collapsible, div.collapsible, ul.collapsible, ol.collapsible')
            .not('.mw-collapsible');
 
        $legacy.each(function () {
             var $element = $(this);
             var $element = $(this);


Line 25: Line 28:
             }
             }
         });
         });
    }
    function prepareCollapsibles($root) {
        normalizeLegacyCollapsibles($root);
        var $items = $root
            .find('.mw-collapsible')
            .add($root.filter('.mw-collapsible'));
        $items.not('.mw-made-collapsible').makeCollapsible();


         return $items.filter('.mw-made-collapsible');
         $legacy.makeCollapsible();
     }
     }


Line 72: Line 65:
     }
     }


     function addCollapsibleControls($content) {
     function attachControls($root) {
        var $root = $content.find('.mw-parser-output').first();
         if ($('#muhro-collapse-controls').length || !findCollapsibles($root).length) {
        var $items;
             return false;
 
        if (!$root.length) {
            $root = $content;
        }
 
        $items = prepareCollapsibles($root);
 
         if ($('#muhro-collapse-controls').length || !$items.length) {
             return;
         }
         }


Line 117: Line 101:
         updateButtons($root, $expand, $collapse);
         updateButtons($root, $expand, $collapse);
         $('body').append($controls);
         $('body').append($controls);
        return true;
    }
    function addCollapsibleControls($content) {
        var $root = $content.find('.mw-parser-output').first();
        var attempts = 0;
        if (!$root.length) {
            $root = $content;
        }
        normalizeLegacyCollapsibles($root);
        function tryAttach() {
            attempts++;
            if (attachControls($root) || attempts >= 20) {
                return;
            }
            window.setTimeout(tryAttach, 250);
        }
        tryAttach();
     }
     }