MediaWiki:Common.js: Difference between revisions

m Add searchable floating contents panel
m Restore floating expand collapse controls
 
(One intermediate revision by the same user not shown)
Line 14: Line 14:


});
});
(function () {
    window.muhroGetFloatingTools = function () {
        var $tools = $('#muhro-floating-tools');
        if (!$tools.length) {
            $tools = $('<div>')
                .attr('id', 'muhro-floating-tools')
                .addClass('muhro-floating-tools noprint');
            $('body').append($tools);
        }
        return $tools;
    };
}());


(function () {
(function () {
Line 34: Line 49:
     function findCollapsibles($root) {
     function findCollapsibles($root) {
         return $root
         return $root
             .find('.mw-collapsible.mw-made-collapsible')
             .find('.mw-collapsible')
             .add($root.filter('.mw-collapsible.mw-made-collapsible'));
             .add($root.filter('.mw-collapsible'));
     }
     }


Line 44: Line 59:
         $expand.prop('disabled', $items.length === 0 || collapsedCount === 0);
         $expand.prop('disabled', $items.length === 0 || collapsedCount === 0);
         $collapse.prop('disabled', $items.length === 0 || collapsedCount === $items.length);
         $collapse.prop('disabled', $items.length === 0 || collapsedCount === $items.length);
    }
    function triggerFallback($element, collapse) {
        if ($element.hasClass('mw-collapsed') === collapse) {
            return;
        }
        $element.find('> .muhro-early-toggle, .muhro-early-toggle').first().trigger('click');
     }
     }


Line 51: Line 74:
             var api = $element.data('mw-collapsible');
             var api = $element.data('mw-collapsible');


             if (!api || $element.hasClass('mw-collapsed') === collapse) {
             if ($element.hasClass('mw-collapsed') === collapse) {
                 return;
                 return;
             }
             }


             if (collapse) {
             if (api) {
                 api.collapse();
                if (collapse) {
                    api.collapse();
                 } else {
                    api.expand();
                }
             } else {
             } else {
                 api.expand();
                 triggerFallback($element, collapse);
             }
             }
         });
         });


         updateButtons($root, $expand, $collapse);
         window.setTimeout(function () {
            updateButtons($root, $expand, $collapse);
        }, 0);
     }
     }


Line 84: Line 113:
                 'aria-label': 'Collapsible content controls'
                 'aria-label': 'Collapsible content controls'
             })
             })
             .addClass('muhro-collapse-controls noprint')
             .addClass('muhro-collapse-controls')
             .append($expand, $collapse);
             .append($expand, $collapse);


Line 100: Line 129:


         updateButtons($root, $expand, $collapse);
         updateButtons($root, $expand, $collapse);
         $('body').append($controls);
         window.muhroGetFloatingTools().prepend($controls);
         return true;
         return true;
     }
     }
Line 116: Line 145:
         function tryAttach() {
         function tryAttach() {
             attempts++;
             attempts++;
             if (attachControls($root) || attempts >= 20) {
             if (attachControls($root) || attempts >= 40) {
                 return;
                 return;
             }
             }
Line 218: Line 247:
         $wrapper = $('<div>')
         $wrapper = $('<div>')
             .attr('id', 'muhro-floating-toc')
             .attr('id', 'muhro-floating-toc')
             .addClass('muhro-floating-toc noprint')
             .addClass('muhro-floating-toc')
             .append($button, $panel);
             .append($button, $panel);


Line 243: Line 272:
         });
         });


         $('body').append($wrapper);
         window.muhroGetFloatingTools().append($wrapper);
     }
     }


     mw.hook('wikipage.content').add(addFloatingToc);
     mw.hook('wikipage.content').add(addFloatingToc);
}());
}());