Note: After publishing, you may have to bypass your browser's cache to see the changes.
- Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
- Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
- Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
/* All JavaScript here will be loaded for users of the Citizen skin */
/* Indice d'archivio:
1) dove esiste il rail di Citizen lo mettiamo lì SOPRA la TOC nativa (riuso);
dove il rail non c'è, l'indice resta flottante (.ai-rail) e non sparisce.
2) collassabile (chevron nella testata). Stato iniziale:
- se l'utente ha già scelto, si ricorda (localStorage);
- altrimenti default intelligente: chiuso se la TOC della pagina è lunga.
3) la testata (link alla Dashboard) resta sempre visibile. */
mw.hook( 'wikipage.content' ).add( function () {
var KEY = 'ce-indice-collapsed';
// 1) posizionamento
var floatBox = document.querySelector( '.archivio-indice.ai-rail' );
if ( floatBox ) {
var rail =
document.querySelector( '.citizen-page-sidebar' ) ||
document.querySelector( '.citizen-sidebar' ) ||
document.querySelector( '.citizen-toc-container' );
if ( rail ) {
floatBox.classList.remove( 'ai-rail' );
floatBox.classList.add( 'ai-in-rail' );
rail.prepend( floatBox );
}
}
var idx = document.querySelector( '.archivio-indice' );
if ( !idx ) {
return;
}
// 2) stato iniziale
var saved = null;
try {
saved = localStorage.getItem( KEY );
} catch ( e ) {}
var tocCount = document.querySelectorAll( '.citizen-toc-link' ).length;
var collapsed = ( saved === null ) ? ( tocCount > 8 ) : ( saved === '1' );
if ( collapsed ) {
idx.classList.add( 'ai-collapsed' );
}
// 3) toggle + persistenza
document.querySelectorAll( '.archivio-indice .ai-toggle' ).forEach( function ( t ) {
if ( t.dataset.bound ) {
return;
}
t.dataset.bound = '1';
var act = function () {
var b = t.closest( '.archivio-indice' );
b.classList.toggle( 'ai-collapsed' );
try {
localStorage.setItem( KEY, b.classList.contains( 'ai-collapsed' ) ? '1' : '0' );
} catch ( e ) {}
};
t.addEventListener( 'click', act );
t.addEventListener( 'keydown', function ( e ) {
if ( e.key === 'Enter' || e.key === ' ' ) {
e.preventDefault();
act();
}
} );
} );
} );
/* === [SEZIONI] inizio ===
Sezioni collassabili: Citizen le fornisce già di suo (skin: EnableCollapsibleSections,
.citizen-section + .citizen-section-heading + un solo chevron nativo). Qui NON aggiungiamo
un secondo meccanismo: pilotiamo quello nativo perché le sezioni siano CHIUSE al primo
approdo e lo stato (quali aperte) sia ricordato per-pagina in localStorage. Il toggle e il
chevron restano di Citizen; le àncore/TOC riaprono la sezione da sole grazie a
hidden="until-found". */
mw.hook( 'wikipage.content' ).add( function () {
if ( !document.body.classList.contains( 'citizen-sections-enabled' ) ) {
return;
}
var body = document.querySelector( '#mw-content-text .mw-parser-output' );
if ( !body || body.dataset.ceSecDone ) {
return;
}
var headings = Array.prototype.slice.call(
document.querySelectorAll( '.citizen-section-heading' ) );
if ( !headings.length ) {
return;
}
body.dataset.ceSecDone = '1';
function secOf( h ) {
var s = h.nextElementSibling;
return ( s && s.classList.contains( 'citizen-section' ) ) ? s : null;
}
function idOf( h ) {
var hx = h.querySelector( 'h1,h2,h3,h4,h5,h6' ) || h;
return hx.id || null;
}
var SKEY = 'ce-sections:' + mw.config.get( 'wgPageName' );
var stored = null;
try { stored = localStorage.getItem( SKEY ); } catch ( e ) {}
var openIds = {};
if ( stored ) {
try {
JSON.parse( stored ).forEach( function ( id ) { openIds[ id ] = 1; } );
} catch ( e ) {}
}
// stato iniziale: chiuse, tranne quelle ricordate aperte
headings.forEach( function ( h ) {
var s = secOf( h );
if ( !s ) { return; }
var id = idOf( h );
var open = stored ? !!openIds[ id ] : false;
if ( !open ) { s.hidden = 'until-found'; }
} );
function save() {
var open = [];
headings.forEach( function ( h ) {
var s = secOf( h );
if ( s && !s.hasAttribute( 'hidden' ) ) {
var id = idOf( h );
if ( id ) { open.push( id ); }
}
} );
try { localStorage.setItem( SKEY, JSON.stringify( open ) ); } catch ( e ) {}
}
// registra lo stato dopo i toggle nativi di Citizen e le riaperture via "until-found"
body.addEventListener( 'click', function ( e ) {
if ( e.target.closest( '.citizen-section-heading' ) &&
!e.target.closest( '.mw-editsection' ) ) {
setTimeout( save, 0 );
}
} );
headings.forEach( function ( h ) {
var s = secOf( h );
if ( s ) {
s.addEventListener( 'beforematch', function () { setTimeout( save, 0 ); } );
}
} );
} );
/* === [SEZIONI] fine === */