MediaWiki:Common.js

THDeb​(留言 | 贡献)2026年8月10日 (一) 22:42的版本 (Add accessible persistent collapsible topic sidebar behavior)
(差异) ←上一版本 | 最后版本 (差异) | 下一版本→ (差异)

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

  • Firefox或Safari:按住Shift的同时单击刷新,或按Ctrl-F5或Ctrl-R(Mac为⌘-R)
  • Google Chrome:按Ctrl-Shift-R(Mac为⌘-Shift-R)
  • Edge:按住Ctrl的同时单击刷新,或按Ctrl-F5。
( function () {
	'use strict';

	var topicLabels = new Set( [
		'校园总览',
		'区域与地点',
		'课程与教学',
		'组织与社团',
		'人物',
		'活动与赛事',
		'作品与资料',
		'待完善内容',
		'参与编写',
		'工具与浏览'
	] );

	function storageKey( portlet ) {
		return 'tyzx.sidebar.' + ( portlet.id || 'topic' );
	}

	function readOpenPreference( portlet ) {
		try {
			return window.localStorage.getItem( storageKey( portlet ) ) === 'open';
		} catch ( error ) {
			return false;
		}
	}

	function writeOpenPreference( portlet, open ) {
		try {
			window.localStorage.setItem( storageKey( portlet ), open ? 'open' : 'closed' );
		} catch ( error ) {
			// Storage can be unavailable in private or restricted browser contexts.
		}
	}

	function uniqueContentId( portlet, content, index ) {
		if ( content.id ) {
			return content.id;
		}
		var base = ( portlet.id || 'tyzx-sidebar-topic-' + index ) + '-content';
		var candidate = base;
		var suffix = 2;
		while ( document.getElementById( candidate ) ) {
			candidate = base + '-' + suffix;
			suffix++;
		}
		content.id = candidate;
		return candidate;
	}

	function setExpanded( portlet, button, content, expanded, persist ) {
		button.setAttribute( 'aria-expanded', expanded ? 'true' : 'false' );
		content.setAttribute( 'aria-hidden', expanded ? 'false' : 'true' );
		portlet.dataset.tyzxCollapsed = expanded ? 'false' : 'true';
		if ( expanded ) {
			content.removeAttribute( 'inert' );
		} else {
			content.setAttribute( 'inert', '' );
		}
		if ( persist ) {
			writeOpenPreference( portlet, expanded );
		}
	}

	function initializePortlet( portlet, index ) {
		if ( portlet.dataset.tyzxSidebarInitialized === '1' ) {
			return;
		}
		var heading = portlet.querySelector( ':scope > .vector-menu-heading, :scope > h3' );
		var content = portlet.querySelector(
			':scope > .vector-menu-content, :scope > .pBody, :scope > .mw-portlet-body'
		);
		if ( !heading || !content || !topicLabels.has( heading.textContent.trim() ) ) {
			return;
		}

		var label = heading.textContent.trim();
		var contentId = uniqueContentId( portlet, content, index );
		var button = document.createElement( 'button' );
		button.type = 'button';
		button.className = 'tyzx-sidebar-toggle';
		button.setAttribute( 'aria-controls', contentId );
		button.appendChild( document.createTextNode( label ) );
		heading.replaceChildren( button );
		content.classList.add( 'tyzx-sidebar-topic-content' );
		portlet.classList.add( 'tyzx-sidebar-topic' );
		portlet.dataset.tyzxSidebarInitialized = '1';
		setExpanded( portlet, button, content, readOpenPreference( portlet ), false );

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

	function initializeSidebar() {
		document.querySelectorAll( '.mw-portlet, .portlet.mw-portlet' )
			.forEach( initializePortlet );
	}

	function scheduleInitialization() {
		window.requestAnimationFrame( initializeSidebar );
	}

	if ( document.readyState === 'loading' ) {
		document.addEventListener( 'DOMContentLoaded', initializeSidebar, { once: true } );
	} else {
		initializeSidebar();
	}

	if ( window.mw && mw.hook ) {
		mw.hook( 'wikipage.content' ).add( scheduleInitialization );
	}
}() );