MediaWiki:Common.js: Difference between revisions

From MuhRO
Jump to navigation Jump to search
No edit summary
m Add global collapsible expand/collapse controls
Line 14: Line 14:


});
});
(function () {
    function findCollapsibles($root) {
        return $root
            .find('.mw-collapsible.mw-made-collapsible')
            .add($root.filter('.mw-collapsible.mw-made-collapsible'))
            .filter(function () {
                return !$(this).closest('#muhro-collapse-controls').length;
            });
    }
    function findToggle($element) {
        var $toggle = $element
            .find('> .mw-collapsible-toggle, > caption .mw-collapsible-toggle, > tbody > tr:first-child .mw-collapsible-toggle, > tr:first-child .mw-collapsible-toggle')
            .first();
        return $toggle.length ? $toggle : $element.find('.mw-collapsible-toggle').first();
    }
    function updateButtons($root, $expand, $collapse) {
        var $items = findCollapsibles($root);
        var collapsedCount = $items.filter('.mw-collapsed').length;
        $expand.prop('disabled', collapsedCount === 0);
        $collapse.prop('disabled', collapsedCount === $items.length);
    }
    function setAll($root, collapse, $expand, $collapse) {
        findCollapsibles($root).each(function () {
            var $element = $(this);
            if ($element.hasClass('mw-collapsed') === collapse) {
                return;
            }
            findToggle($element).trigger('click');
        });
        window.setTimeout(function () {
            updateButtons($root, $expand, $collapse);
        }, 0);
    }
    function addCollapsibleControls($content) {
        var $root = $content.find('.mw-parser-output').first();
        if (!$root.length) {
            $root = $content;
        }
        if ($root.find('#muhro-collapse-controls').length || !findCollapsibles($root).length) {
            return;
        }
        var $expand = $('<button>')
            .attr('type', 'button')
            .addClass('muhro-collapse-button')
            .text('Expand all');
        var $collapse = $('<button>')
            .attr('type', 'button')
            .addClass('muhro-collapse-button')
            .text('Collapse all');
        var $controls = $('<div>')
            .attr('id', 'muhro-collapse-controls')
            .addClass('muhro-collapse-controls noprint')
            .append($expand, $collapse);
        $expand.on('click', function () {
            setAll($root, false, $expand, $collapse);
        });
        $collapse.on('click', function () {
            setAll($root, true, $expand, $collapse);
        });
        $root.on('click', '.mw-collapsible-toggle', function () {
            window.setTimeout(function () {
                updateButtons($root, $expand, $collapse);
            }, 0);
        });
        updateButtons($root, $expand, $collapse);
        $root.prepend($controls);
    }
    mw.loader.using('jquery.makeCollapsible').then(function () {
        mw.hook('wikipage.content').add(addCollapsibleControls);
    });
}());

Revision as of 13:58, 27 May 2026

/* Any JavaScript here will be loaded for all users on every page load. */
$('.copy-link').on('click', function() {

    var link    = $(this);
    var copy_id = $(this).attr('id');
    var text    = $(this).text();
    var clipboard = new ClipboardJS('#' + copy_id );
    clipboard.on('success', function(e) {
        link.text('Copied!');
        setTimeout(function() {
            link.text(''+text);
        }, 2000);
    });

});

(function () {
    function findCollapsibles($root) {
        return $root
            .find('.mw-collapsible.mw-made-collapsible')
            .add($root.filter('.mw-collapsible.mw-made-collapsible'))
            .filter(function () {
                return !$(this).closest('#muhro-collapse-controls').length;
            });
    }

    function findToggle($element) {
        var $toggle = $element
            .find('> .mw-collapsible-toggle, > caption .mw-collapsible-toggle, > tbody > tr:first-child .mw-collapsible-toggle, > tr:first-child .mw-collapsible-toggle')
            .first();

        return $toggle.length ? $toggle : $element.find('.mw-collapsible-toggle').first();
    }

    function updateButtons($root, $expand, $collapse) {
        var $items = findCollapsibles($root);
        var collapsedCount = $items.filter('.mw-collapsed').length;

        $expand.prop('disabled', collapsedCount === 0);
        $collapse.prop('disabled', collapsedCount === $items.length);
    }

    function setAll($root, collapse, $expand, $collapse) {
        findCollapsibles($root).each(function () {
            var $element = $(this);

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

            findToggle($element).trigger('click');
        });

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

    function addCollapsibleControls($content) {
        var $root = $content.find('.mw-parser-output').first();

        if (!$root.length) {
            $root = $content;
        }

        if ($root.find('#muhro-collapse-controls').length || !findCollapsibles($root).length) {
            return;
        }

        var $expand = $('<button>')
            .attr('type', 'button')
            .addClass('muhro-collapse-button')
            .text('Expand all');
        var $collapse = $('<button>')
            .attr('type', 'button')
            .addClass('muhro-collapse-button')
            .text('Collapse all');
        var $controls = $('<div>')
            .attr('id', 'muhro-collapse-controls')
            .addClass('muhro-collapse-controls noprint')
            .append($expand, $collapse);

        $expand.on('click', function () {
            setAll($root, false, $expand, $collapse);
        });
        $collapse.on('click', function () {
            setAll($root, true, $expand, $collapse);
        });
        $root.on('click', '.mw-collapsible-toggle', function () {
            window.setTimeout(function () {
                updateButtons($root, $expand, $collapse);
            }, 0);
        });

        updateButtons($root, $expand, $collapse);
        $root.prepend($controls);
    }

    mw.loader.using('jquery.makeCollapsible').then(function () {
        mw.hook('wikipage.content').add(addCollapsibleControls);
    });
}());