THDeb​(留言 | 贡献)
Render Minerva sidebar groups from server-generated navigation data
THDeb​(留言 | 贡献)
Use native Vector 2022 navigation and language selection
标签:替换
第1行: 第1行:
( function () {
/* Navigation and language selection use the native skin components. */
'use strict';
 
var topicLabelList = [
'校园总览',
'区域与地点',
'课程与教学',
'组织与社团',
'人物',
'活动与赛事',
'作品与资料',
'待完善内容',
'参与编写',
'工具与浏览',
'语言'
];
var topicLabels = new Set( topicLabelList );
 
function buildMinervaTopicGroups() {
var navigation = document.getElementById( 'p-navigation' );
if ( !navigation || navigation.dataset.tyzxMobileGroupsBuilt === '1' ) {
return;
}
var groups = window.mw && mw.config ? mw.config.get( 'wgTyzxSidebarGroups' ) : null;
if ( !Array.isArray( groups ) || !groups.length ) {
return;
}
var insertionPoint = navigation;
groups.forEach( function ( group, index ) {
var portlet = document.createElement( 'nav' );
var heading = document.createElement( 'h3' );
var content = document.createElement( 'div' );
var list = document.createElement( 'ul' );
portlet.id = 'p-tyzx-mobile-' + index;
portlet.className = 'mw-portlet tyzx-minerva-sidebar-portlet';
heading.textContent = group.label;
content.className = 'mw-portlet-body';
group.links.forEach( function ( link, linkIndex ) {
var item = document.createElement( 'li' );
var anchor = document.createElement( 'a' );
item.id = 'n-tyzx-mobile-' + index + '-' + linkIndex;
item.className = 'mw-list-item ' + ( link.class || '' );
anchor.href = link.href;
anchor.textContent = link.text;
if ( link.lang ) {
anchor.lang = link.lang;
}
if ( link.hreflang ) {
anchor.hreflang = link.hreflang;
}
if ( item.classList.contains( 'tyzx-sidebar-language-current' ) ) {
anchor.setAttribute( 'aria-current', 'true' );
}
item.appendChild( anchor );
list.appendChild( item );
} );
content.appendChild( list );
portlet.appendChild( heading );
portlet.appendChild( content );
insertionPoint.insertAdjacentElement( 'afterend', portlet );
insertionPoint = portlet;
} );
navigation.dataset.tyzxMobileGroupsBuilt = '1';
}
 
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 ) {
return;
}
 
var label = heading.textContent.trim();
if ( portlet.id === 'p-navigation' ) {
portlet.classList.add( 'tyzx-sidebar-fixed' );
portlet.dataset.tyzxSidebarInitialized = '1';
return;
}
if ( !topicLabels.has( label ) ) {
return;
}
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' );
if ( label === '语言' ) {
portlet.classList.add( 'tyzx-sidebar-language' );
var currentLanguage = content.querySelector( '.tyzx-sidebar-language-current a, a.tyzx-sidebar-language-current' );
if ( currentLanguage ) {
currentLanguage.setAttribute( 'aria-current', 'true' );
}
}
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() {
buildMinervaTopicGroups();
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 );
}
}() );