MediaWiki:Common.js: Difference between revisions
Jump to navigation
Jump to search
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: | ||
} | } | ||
}); | }); | ||
$legacy.makeCollapsible(); | |||
} | } | ||
| Line 72: | Line 65: | ||
} | } | ||
function | function attachControls($root) { | ||
if ($('#muhro-collapse-controls').length || !findCollapsibles($root).length) { | |||
return false; | |||
if ($('#muhro-collapse-controls').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(); | |||
} | } | ||
Revision as of 14:14, 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 normalizeLegacyCollapsibles($root) {
var $legacy = $root.find('table.collapsible, div.collapsible, ul.collapsible, ol.collapsible')
.not('.mw-collapsible');
$legacy.each(function () {
var $element = $(this);
$element.addClass('mw-collapsible');
if ($element.hasClass('collapsed')) {
$element.addClass('mw-collapsed');
}
});
$legacy.makeCollapsible();
}
function findCollapsibles($root) {
return $root
.find('.mw-collapsible.mw-made-collapsible')
.add($root.filter('.mw-collapsible.mw-made-collapsible'));
}
function updateButtons($root, $expand, $collapse) {
var $items = findCollapsibles($root);
var collapsedCount = $items.filter('.mw-collapsed').length;
$expand.prop('disabled', $items.length === 0 || collapsedCount === 0);
$collapse.prop('disabled', $items.length === 0 || collapsedCount === $items.length);
}
function setAll($root, collapse, $expand, $collapse) {
findCollapsibles($root).each(function () {
var $element = $(this);
var api = $element.data('mw-collapsible');
if (!api || $element.hasClass('mw-collapsed') === collapse) {
return;
}
if (collapse) {
api.collapse();
} else {
api.expand();
}
});
updateButtons($root, $expand, $collapse);
}
function attachControls($root) {
if ($('#muhro-collapse-controls').length || !findCollapsibles($root).length) {
return false;
}
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',
role: 'group',
'aria-label': 'Collapsible content 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 keydown', '.mw-collapsible-toggle', function () {
window.setTimeout(function () {
updateButtons($root, $expand, $collapse);
}, 0);
});
updateButtons($root, $expand, $collapse);
$('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();
}
mw.loader.using('jquery.makeCollapsible').then(function () {
mw.hook('wikipage.content').add(addCollapsibleControls);
});
}());