MediaWiki:Gadget-CollapsibleToggle.js
外观
注意:保存之后,你必须清除浏览器缓存才能看到做出的更改。Google Chrome、Firefox、Microsoft Edge及Safari:按住⇧ Shift键并单击工具栏的“刷新”按钮。参阅Help:绕过浏览器缓存以获取更多帮助。
/**!
* _________________________________________________________________________________
* | |
* | === WARNING: GLOBAL GADGET FILE === |
* | Changes to this page affect many users. |
* | Please discuss changes on the talk page, [[WP:VPT]] or GitHub before editing. |
* |_________________________________________________________________________________|
*
* CollapsibleToggle.js
* 早期的 NavFrame 替代產品,使用後可以如同 NavFrame 點 Head 展開或關閉
*
* @author [[User:SunAfterRain]]
* @license BSD 3-Clause
*/
// <nowiki>
$(() => {
const HEAD_KEY = 'CollapsibleToggle.head';
const DATA_KEY = 'CollapsibleToggle.data';
const CLASS_SELECTOR = '.collapsible-title';
const IGNORE_TRIGGER_SELECTOR = 'a, button, .mw-collapsible-toggle, .collapsible-toggle-ignore';
function collapsibleGetHead($collapsible) {
let $element;
if ($collapsible.is('table')) {
// If the table has a caption, collapse to the caption
// as opposed to the first row
const $caption = $collapsible.find( '> caption' );
if ($caption.length) {
$element = $caption;
} else {
const $tr = $collapsible.find('tr').first();
if ($tr.is(CLASS_SELECTOR)) {
return $tr;
}
$element = $tr.find('th, td');
}
} else if ($collapsible.is('ul') || $collapsible.is('ol')) {
// The toggle-link will be in the first list-item
$element = $collapsible.find( 'li' ).first();
} else {
$element = $collapsible.children();
}
return $element.filter(CLASS_SELECTOR);
}
function initCollapsibleToggle(element) {
const $element = $(element);
let $head = collapsibleGetHead($element);
if (!$head.length) {
$head = $element.parent(CLASS_SELECTOR);
}
const mwCollapsibleData = $element.data('mwCollapsible');
if (
!$element.hasClass('mw-collapsible')
|| ($element.attr('id') || '').startsWith('mw-customcollapsible-')
|| $element.hasClass('gadget-collapsible-toggle-optout')
|| !$element.data('mwMadeCollapsible')
|| !mwCollapsibleData
|| !$head.length
|| !!$element.data(DATA_KEY)
) {
return;
}
$element.addClass('gadget-collapsible-toggle');
$head
.on('mousedown', (e) => {
// https://commons.wikimedia.org/wiki/MediaWiki:Gadget-CollapsibleTemplates.js?oldid=458731733#L47
const $target = $(e.target);
if ($target.is(IGNORE_TRIGGER_SELECTOR) || $target.parents(IGNORE_TRIGGER_SELECTOR).length) {
return true;
}
mwCollapsibleData.toggle();
return false;
});
$head.data(HEAD_KEY, true);
$element.data(DATA_KEY, {
mwCollapsibleData
});
}
function initCollapsibleToggleWrapper($frame) {
for (const frame of $frame.get()) {
initCollapsibleToggle(frame);
}
}
if (typeof $.fn.makeCollapsible === 'function') {
initCollapsibleToggleWrapper($('.mw-collapsible'));
}
mw.hook('wikipage.collapsibleContent').add(initCollapsibleToggleWrapper);
mw.hook('ext.gadget.CollapsibleToggle').add(initCollapsibleToggleWrapper);
});
// </nowiki>