跳转到内容

天一学子免费网站托管计划: 面向全体天一在校学生及校友,提供免费网站托管、二级域名分发及付费制作服务。详见 公告页面。

立即参与贡献🎉 天一中学维基现在严重缺少词条,希望大家踊跃参与编辑。

警告⚠️: 严禁公开他人的私人信息,违者永久封杀

成为管理员: 若你是图灵社社员或是对天一维基作出重大贡献,便有机会成为管理员

MediaWiki:Common.js

来自江苏省天一中学非官方维基
THDeb​(留言 | 贡献)2026年8月17日 (一) 21:36的版本 (Use language-neutral native sidebar disclosure behavior)

注意:在发布之后,您可能需要清除浏览器缓存才能看到所作出的更改的影响。

  • Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5或Ctrl-R(Mac为⌘-R)
  • Google Chrome:按Ctrl-Shift-R(Mac为⌘-Shift-R)
  • Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5。
/* Keep Vector's native sidebar and add accessible disclosure behaviour to custom groups. */
( function () {
	'use strict';

	var excludedPortlets = [ 'p-navigation', 'p-tb', 'p-lang', 'p-search' ];

	function isCurrentPortlet( content ) {
		var current = new URL( window.location.href );

		if ( content.querySelector( '.mw-list-item.selected, .mw-list-item.active, a[aria-current="page"]' ) ) {
			return true;
		}

		return Array.prototype.some.call( content.querySelectorAll( 'a[href]' ), function ( link ) {
			var target = new URL( link.href, window.location.href );
			return target.origin === current.origin &&
				target.pathname === current.pathname &&
				target.search === current.search;
		} );
	}

	function enhancePortlet( portlet ) {
		var heading;
		var content;
		var button;
		var icon;
		var label;
		var contentId;

		if ( excludedPortlets.indexOf( portlet.id ) !== -1 ||
			portlet.dataset.tyzxCollapsible === '1' ) {
			return;
		}

		heading = portlet.querySelector( ':scope > .vector-menu-heading' );
		content = portlet.querySelector( ':scope > .vector-menu-content' );
		if ( !heading || !content || !content.querySelector( 'li' ) ) {
			return;
		}

		label = heading.textContent.trim();
		if ( !label ) {
			return;
		}

		contentId = content.id || portlet.id + '-content';
		content.id = contentId;

		button = document.createElement( 'button' );
		button.type = 'button';
		button.className = 'tyzx-sidebar-toggle';
		button.setAttribute( 'aria-controls', contentId );

		icon = document.createElement( 'span' );
		icon.className = 'tyzx-sidebar-toggle__icon';
		icon.setAttribute( 'aria-hidden', 'true' );
		button.appendChild( icon );
		button.appendChild( document.createTextNode( label ) );

		heading.textContent = '';
		heading.appendChild( button );
		portlet.classList.add( 'tyzx-sidebar-collapsible' );
		portlet.dataset.tyzxCollapsible = '1';

		function setOpen( open ) {
			button.setAttribute( 'aria-expanded', open ? 'true' : 'false' );
			content.hidden = !open;
			portlet.classList.toggle( 'is-open', open );
		}

		setOpen( isCurrentPortlet( content ) );

		button.addEventListener( 'click', function () {
			setOpen( button.getAttribute( 'aria-expanded' ) !== 'true' );
		} );

		button.addEventListener( 'keydown', function ( event ) {
			if ( event.key === 'Escape' ) {
				setOpen( false );
				button.focus();
			}
		} );
	}

	function initSidebar() {
		Array.prototype.forEach.call(
			document.querySelectorAll( '#vector-main-menu .mw-portlet' ),
			enhancePortlet
		);
	}

	if ( document.readyState === 'loading' ) {
		document.addEventListener( 'DOMContentLoaded', initSidebar );
	} else {
		initSidebar();
	}
}() );