THDeb​(留言 | 贡献)
Add accessible persistent collapsible topic sidebar behavior
 
THDeb​(留言 | 贡献)
Move Vector navigation behavior into upgrade-safe ResourceLoader module
标签:替换
 
(未显示同一用户的6个中间版本)
第1行: 第1行:
( function () {
/* Vector navigation behavior moved to the upgrade-safe ext.tyzxSiteUI module. */
'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 );
}
}() );