MediaWiki:Common.js: Difference between revisions
No edit summary |
No edit summary |
||
| Line 323: | Line 323: | ||
} | } | ||
}() ); | |||
// ── Main page: by-Grantha / by-Author toggle ────────────────── | |||
// Runs only on Main_Page. Attaches click handlers to the two | |||
// <div role="button"> toggles emitted by make_main_page(). | |||
// Using delegated event attachment here (not onclick= in wikitext) | |||
// because MediaWiki strips onclick= attributes for security. | |||
( function () { | |||
function grHomeView( v ) { | |||
var gView = document.getElementById( 'gr-view-grantha' ); | |||
var aView = document.getElementById( 'gr-view-author' ); | |||
var gBtn = document.getElementById( 'gr-toggle-grantha' ); | |||
var aBtn = document.getElementById( 'gr-toggle-author' ); | |||
if ( !gView || !aView || !gBtn || !aBtn ) return; | |||
gView.style.display = ( v === 'grantha' ) ? '' : 'none'; | |||
aView.style.display = ( v === 'author' ) ? '' : 'none'; | |||
gBtn.className = 'gr-toggle-btn' + ( v === 'grantha' ? ' gr-toggle-active' : '' ); | |||
aBtn.className = 'gr-toggle-btn' + ( v === 'author' ? ' gr-toggle-active' : '' ); | |||
try { localStorage.setItem( 'gr_home_view', v ); } catch ( e ) {} | |||
} | |||
function initHomeToggle() { | |||
var gBtn = document.getElementById( 'gr-toggle-grantha' ); | |||
var aBtn = document.getElementById( 'gr-toggle-author' ); | |||
if ( !gBtn || !aBtn ) return; // not on Main_Page | |||
gBtn.addEventListener( 'click', function () { grHomeView( 'grantha' ); } ); | |||
aBtn.addEventListener( 'click', function () { grHomeView( 'author' ); } ); | |||
// Keyboard support | |||
[ gBtn, aBtn ].forEach( function ( btn ) { | |||
btn.addEventListener( 'keydown', function ( e ) { | |||
if ( e.key === 'Enter' || e.key === ' ' ) btn.click(); | |||
} ); | |||
} ); | |||
// Restore persisted preference | |||
var saved; | |||
try { saved = localStorage.getItem( 'gr_home_view' ); } catch ( e ) {} | |||
if ( saved === 'author' ) grHomeView( 'author' ); | |||
} | |||
if ( document.readyState === 'loading' ) { | |||
document.addEventListener( 'DOMContentLoaded', initHomeToggle ); | |||
} else { | |||
initHomeToggle(); | |||
} | |||
}() ); | }() ); | ||