MediaWiki:Common.js: Difference between revisions
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
/* MediaWiki:Common.js — grantha.io | /* MediaWiki:Common.js — grantha.io (v6) | ||
* | * | ||
* | * Changes vs v5: | ||
* 1. | * 1. TOC active highlight fix: Vector 2022 sets the active class on the | ||
* | * <li.vector-toc-list-item>, but Common.js wraps all text nodes inside | ||
* | * .vector-toc-text spans in <span data-deva="…">. The CSS colour rule | ||
* | * was targeting .vector-toc-list-item-active > a which never matched | ||
* because the <a> sits deeper and its text is now inside a <span>. | |||
* | * Fix: MutationObserver watches the <li> for class changes and directly | ||
* | * applies/removes the orange colour via inline style on the .vector-toc-link | ||
* | * ancestor, bypassing the CSS specificity war entirely. | ||
* 2. All other behaviour identical to v5. | |||
* the | |||
* | |||
*/ | */ | ||
| Line 125: | Line 122: | ||
function tagTextNodes() { | function tagTextNodes() { | ||
var content = document.querySelector( '.mw-parser-output' ); | var content = document.querySelector( '.mw-parser-output' ); | ||
if ( content ) { | if ( content ) { | ||
| Line 137: | Line 133: | ||
if ( p.hasAttribute && p.hasAttribute( 'data-deva' ) ) return; | if ( p.hasAttribute && p.hasAttribute( 'data-deva' ) ) return; | ||
if ( p.closest ) { | if ( p.closest ) { | ||
if ( p.closest( '.gr-controls' ) | if ( p.closest( '.gr-controls' ) || p.closest( '.mw-editsection' ) ) return; | ||
} | } | ||
var orig = node.textContent; | var orig = node.textContent; | ||
| Line 150: | Line 145: | ||
} | } | ||
// | // Tag .vector-toc-text spans for TOC transliteration | ||
document.querySelectorAll( '.vector-toc .vector-toc-text' ).forEach( function ( span ) { | document.querySelectorAll( '.vector-toc .vector-toc-text' ).forEach( function ( span ) { | ||
if ( span.hasAttribute( 'data-deva' ) ) return; | if ( span.hasAttribute( 'data-deva' ) ) return; | ||
var orig = span.textContent; | var orig = span.textContent; | ||
if ( !orig.trim() ) return; | if ( !orig.trim() ) return; | ||
| Line 169: | Line 159: | ||
currentScript = script; | currentScript = script; | ||
translatableSpans.forEach( function ( span ) { | translatableSpans.forEach( function ( span ) { | ||
if ( !span.parentNode ) return; | if ( !span.parentNode ) return; | ||
var orig = span.getAttribute( 'data-deva' ); | var orig = span.getAttribute( 'data-deva' ); | ||
if ( !orig ) return; | if ( !orig ) return; | ||
| Line 239: | Line 229: | ||
setLinkActive( li, isActive ); | setLinkActive( li, isActive ); | ||
/* Scroll active item into view | /* Scroll active item into view within the TOC container — | ||
* but ONLY when the TOC sidebar is actually visible and expanded. | |||
* If the TOC is collapsed or hidden, scrollIntoView scrolls the | |||
* whole page instead of just the TOC, which hijacks the reading | |||
* position. */ | |||
if ( isActive ) { | if ( isActive ) { | ||
var container = document.querySelector( '.vector-sticky-pinned-container' ); | var container = document.querySelector( '.vector-sticky-pinned-container' ); | ||
if ( container ) { | if ( container ) { | ||
var liRect | /* Check TOC is visible: the container must have nonzero height | ||
* and must not be hidden by the Vector "pinned/unpinned" toggle */ | |||
var tocVisible = container.offsetHeight > 0 && | |||
container.offsetParent !== null && | |||
window.getComputedStyle( container ).display !== 'none' && | |||
window.getComputedStyle( container ).visibility !== 'hidden'; | |||
if ( tocVisible ) { | |||
var liRect = li.getBoundingClientRect(); | |||
var cRect = container.getBoundingClientRect(); | |||
if ( liRect.top < cRect.top + 8 || liRect.bottom > cRect.bottom - 8 ) { | |||
/* Scroll only inside the TOC container, not the page */ | |||
var scrollParent = container.querySelector( '.vector-toc' ) || container; | |||
var offset = liRect.top - cRect.top; | |||
if ( offset < 8 || offset > cRect.height - 48 ) { | |||
scrollParent.scrollTop += offset - ( cRect.height / 2 ); | |||
} | |||
} | |||
} | } | ||
} | } | ||
| Line 289: | Line 296: | ||
structObserver.observe( toc, { childList: true, subtree: true } ); | structObserver.observe( toc, { childList: true, subtree: true } ); | ||
/* On initial load, | /* On initial load, colour the already-active item. | ||
* scrollIntoView is intentionally skipped here — calling it while the | |||
* TOC sidebar might be collapsed causes the PAGE to scroll to the element | |||
* rather than scrolling within the TOC container. The liObserver handles | |||
* scrolling the TOC as the user scrolls the page content. */ | |||
setTimeout( function () { | setTimeout( function () { | ||
var active = toc.querySelector( '.vector-toc-list-item-active' ); | var active = toc.querySelector( '.vector-toc-list-item-active' ); | ||
if ( active ) { | if ( active ) { | ||
setLinkActive( active, true ); | setLinkActive( active, true ); | ||
} | } | ||
}, 300 ); | }, 300 ); | ||
| Line 366: | Line 376: | ||
}() ); | }() ); | ||
// ── Main page: by-Grantha / by-Author toggle ────────────────── | // ── Main page: by-Grantha / by-Author toggle ────────────────── | ||
( function () { | ( function () { | ||
function grHomeView( v ) { | function grHomeView( v ) { | ||
var gView | var gView = document.getElementById( 'gr-view-grantha' ); | ||
var aView | var aView = document.getElementById( 'gr-view-author' ); | ||
var gBtn | var gBtn = document.getElementById( 'gr-toggle-grantha' ); | ||
var aBtn | var aBtn = document.getElementById( 'gr-toggle-author' ); | ||
if ( !gView || !aView || !gBtn || !aBtn ) return; | if ( !gView || !aView || !gBtn || !aBtn ) return; | ||
gView.style.display = ( v === 'grantha' ) ? '' : 'none'; | gView.style.display = ( v === 'grantha' ) ? '' : 'none'; | ||
aView.style.display = ( v === 'author' ) ? '' : 'none'; | aView.style.display = ( v === 'author' ) ? '' : 'none'; | ||
| Line 385: | Line 393: | ||
try { localStorage.setItem( 'gr_home_view', v ); } catch ( e ) {} | try { localStorage.setItem( 'gr_home_view', v ); } catch ( e ) {} | ||
} | } | ||
function initHomeToggle() { | function initHomeToggle() { | ||
var gBtn = document.getElementById( 'gr-toggle-grantha' ); | var gBtn = document.getElementById( 'gr-toggle-grantha' ); | ||
var aBtn = document.getElementById( 'gr-toggle-author' ); | var aBtn = document.getElementById( 'gr-toggle-author' ); | ||
if ( !gBtn || !aBtn ) return; | if ( !gBtn || !aBtn ) return; | ||
gBtn.addEventListener( 'click', function () { grHomeView( 'grantha' ); } ); | gBtn.addEventListener( 'click', function () { grHomeView( 'grantha' ); } ); | ||
aBtn.addEventListener( 'click', function () { grHomeView( 'author' ); } ); | aBtn.addEventListener( 'click', function () { grHomeView( 'author' ); } ); | ||
[ gBtn, aBtn ].forEach( function ( btn ) { | [ gBtn, aBtn ].forEach( function ( btn ) { | ||
btn.addEventListener( 'keydown', function ( e ) { | btn.addEventListener( 'keydown', function ( e ) { | ||
| Line 399: | Line 406: | ||
} ); | } ); | ||
} ); | } ); | ||
var saved; | var saved; | ||
try { saved = localStorage.getItem( 'gr_home_view' ); } catch ( e ) {} | try { saved = localStorage.getItem( 'gr_home_view' ); } catch ( e ) {} | ||
if ( saved === 'author' ) grHomeView( 'author' ); | if ( saved === 'author' ) grHomeView( 'author' ); | ||
} | } | ||
if ( document.readyState === 'loading' ) { | if ( document.readyState === 'loading' ) { | ||
document.addEventListener( 'DOMContentLoaded', initHomeToggle ); | document.addEventListener( 'DOMContentLoaded', initHomeToggle ); | ||
Revision as of 09:39, 24 April 2026
/* MediaWiki:Common.js — grantha.io (v6)
*
* Changes vs v5:
* 1. TOC active highlight fix: Vector 2022 sets the active class on the
* <li.vector-toc-list-item>, but Common.js wraps all text nodes inside
* .vector-toc-text spans in <span data-deva="…">. The CSS colour rule
* was targeting .vector-toc-list-item-active > a which never matched
* because the <a> sits deeper and its text is now inside a <span>.
* Fix: MutationObserver watches the <li> for class changes and directly
* applies/removes the orange colour via inline style on the .vector-toc-link
* ancestor, bypassing the CSS specificity war entirely.
* 2. All other behaviour identical to v5.
*/
( function () {
var LS_SCRIPT_KEY = 'grantha_reader_script';
var currentScript = 'deva';
// ── IAST transliteration ────────────────────────────────────────
function devanagariToIAST( text ) {
var CONSONANTS = {
'क':'k','ख':'kh','ग':'g','घ':'gh','ङ':'ṅ',
'च':'c','छ':'ch','ज':'j','झ':'jh','ञ':'ñ',
'ट':'ṭ','ठ':'ṭh','ड':'ḍ','ढ':'ḍh','ण':'ṇ',
'त':'t','थ':'th','द':'d','ध':'dh','न':'n',
'प':'p','फ':'ph','ब':'b','भ':'bh','म':'m',
'य':'y','र':'r','ल':'l','ळ':'ḷ','व':'v',
'श':'ś','ष':'ṣ','स':'s','ह':'h'
};
var DIACRITICS = {
'ा':'ā','ि':'i','ी':'ī','ु':'u','ू':'ū',
'ृ':'ṛ','ॄ':'ṝ','े':'e','ै':'ai','ो':'o','ौ':'au'
};
var VOWELS = {
'अ':'a','आ':'ā','इ':'i','ई':'ī','उ':'u','ऊ':'ū',
'ऋ':'ṛ','ॠ':'ṝ','ए':'e','ऐ':'ai','ओ':'o','औ':'au','ऽ':"'"
};
var MISC = {
'ं':'ṃ','ः':'ḥ','ँ':'m̐','ॐ':'oṃ',
'०':'0','१':'1','२':'2','३':'3','४':'4',
'५':'5','६':'6','७':'7','८':'8','९':'9'
};
var HALANTA = '्';
var chars = Array.from( text );
var result = '';
var i = 0;
while ( i < chars.length ) {
var ch = chars[ i ];
var next = chars[ i + 1 ];
if ( CONSONANTS[ ch ] ) {
var base = CONSONANTS[ ch ];
if ( next === HALANTA ) { result += base; i += 2; }
else if ( DIACRITICS[ next ] ) { result += base + DIACRITICS[ next ]; i += 2; }
else if ( next === 'ं' || next === 'ः' ) { result += base + 'a' + MISC[ next ]; i += 2; }
else { result += base + 'a'; i++; }
} else if ( VOWELS[ ch ] ) { result += VOWELS[ ch ]; i++; }
else if ( DIACRITICS[ ch ] ) { result += DIACRITICS[ ch ]; i++; }
else if ( MISC[ ch ] ) { result += MISC[ ch ]; i++; }
else { result += ch; i++; }
}
return result;
}
// ── Character maps for Kannada / Tamil ─────────────────────────
var SCRIPT_MAP = {
kn: {
'अ':'ಅ','आ':'ಆ','इ':'ಇ','ई':'ಈ','उ':'ಉ','ऊ':'ಊ','ऋ':'ಋ',
'ए':'ಏ','ऐ':'ಐ','ओ':'ಓ','औ':'ಔ','ऽ':'ಽ',
'क':'ಕ','ख':'ಖ','ग':'ಗ','घ':'ಘ','ङ':'ಙ',
'च':'ಚ','छ':'ಛ','ज':'ಜ','झ':'ಝ','ञ':'ಞ',
'ट':'ಟ','ठ':'ಠ','ड':'ಡ','ढ':'ಢ','ण':'ಣ',
'त':'ತ','थ':'ಥ','द':'ದ','ध':'ಧ','न':'ನ',
'प':'ಪ','फ':'ಫ','ब':'ಬ','भ':'ಭ','म':'ಮ',
'य':'ಯ','र':'ರ','ल':'ಲ','व':'ವ',
'श':'ಶ','ष':'ಷ','स':'ಸ','ह':'ಹ',
'ा':'ಾ','ि':'ಿ','ी':'ೀ','ु':'ು','ू':'ೂ',
'ृ':'ೃ','े':'ೇ','ै':'ೈ','ो':'ೋ','ौ':'ೌ',
'ं':'ಂ','ः':'ಃ','्':'್',
'०':'೦','१':'೧','२':'೨','३':'೩','४':'೪',
'५':'೫','६':'೬','७':'೭','८':'೮','९':'೯'
},
ta: {
'अ':'அ','आ':'ஆ','इ':'இ','ई':'ஈ','उ':'உ','ऊ':'ஊ',
'ऋ':'ரு','ॠ':'ரூ',
'ए':'ஏ','ऐ':'ஐ','ओ':'ஓ','औ':'ஔ',
'क':'க','ख':'க','ग':'க','घ':'க','ङ':'ங',
'च':'ச','छ':'ச','ज':'ஜ','झ':'ஜ','ञ':'ஞ',
'ट':'ட','ठ':'ட','ड':'ட','ढ':'ட','ण':'ண',
'त':'த','थ':'த','द':'த','ध':'த','न':'ந',
'प':'ப','फ':'ப','ब':'ப','भ':'ப','म':'ம',
'य':'ய','र':'ர','ल':'ல','ळ':'ழ','व':'வ',
'श':'ஶ','ष':'ஷ','स':'ஸ','ह':'ஹ',
'ा':'ா','ि':'ி','ी':'ீ','ु':'ு','ू':'ூ',
'ृ':'ு','ॄ':'ூ',
'े':'ே','ै':'ை','ो':'ோ','ौ':'ௌ',
'ं':'ம்','ः':':','ँ':'ம்','्':'்','ॐ':'ௐ','ऽ':'ௗ',
'०':'0','१':'1','२':'2','३':'3','४':'4',
'५':'5','६':'6','७':'7','८':'8','९':'9'
}
};
var PRE = [
[ /ङ्क/g, 'ंक' ], [ /ङ्ख/g, 'ंख' ], [ /ङ्ग/g, 'ंग' ], [ /ङ्घ/g, 'ंघ' ],
[ /ञ्च/g, 'ंच' ], [ /ञ्ज/g, 'ंज' ], [ /ण्ट/g, 'ंट' ], [ /ण्ड/g, 'ंड' ],
[ /न्त/g, 'ंत' ], [ /न्द/g, 'ंद' ], [ /म्ब/g, 'ंब' ], [ /म्भ/g, 'ंभ' ]
];
function transliterateText( text, script ) {
if ( script === 'en' ) return devanagariToIAST( text );
var map = SCRIPT_MAP[ script ];
if ( !map ) return text;
var t = text;
PRE.forEach( function ( p ) { t = t.replace( p[ 0 ], p[ 1 ] ); } );
return Array.from( t ).map( function ( ch ) {
return map[ ch ] !== undefined ? map[ ch ] : ch;
} ).join( '' );
}
// ── Tag all transliteratable text nodes once per page ───────────
var translatableSpans = [];
function tagTextNodes() {
var content = document.querySelector( '.mw-parser-output' );
if ( content ) {
var walker = document.createTreeWalker( content, NodeFilter.SHOW_TEXT );
var nodes = [];
while ( walker.nextNode() ) nodes.push( walker.currentNode );
nodes.forEach( function ( node ) {
var p = node.parentNode;
if ( !p ) return;
if ( p.hasAttribute && p.hasAttribute( 'data-deva' ) ) return;
if ( p.closest ) {
if ( p.closest( '.gr-controls' ) || p.closest( '.mw-editsection' ) ) return;
}
var orig = node.textContent;
if ( !orig.trim() ) return;
var span = document.createElement( 'span' );
span.setAttribute( 'data-deva', orig );
span.textContent = orig;
p.replaceChild( span, node );
translatableSpans.push( span );
} );
}
// Tag .vector-toc-text spans for TOC transliteration
document.querySelectorAll( '.vector-toc .vector-toc-text' ).forEach( function ( span ) {
if ( span.hasAttribute( 'data-deva' ) ) return;
var orig = span.textContent;
if ( !orig.trim() ) return;
span.setAttribute( 'data-deva', orig );
translatableSpans.push( span );
} );
}
// ── Apply a script to all tagged spans ─────────────────────────
function applyScript( script ) {
currentScript = script;
translatableSpans.forEach( function ( span ) {
if ( !span.parentNode ) return;
var orig = span.getAttribute( 'data-deva' );
if ( !orig ) return;
span.textContent = ( script === 'deva' )
? orig
: transliterateText( orig, script );
} );
}
// ── TOC active-item highlight ────────────────────────────────────
// FIX: Instead of relying on CSS :active selectors (which fail because
// Common.js wraps text nodes in <span data-deva>, making > a or > .link
// selectors not match the coloured text), we use a MutationObserver to
// watch each <li> for class changes and imperatively apply/remove the
// orange colour via inline style on the .vector-toc-link inside it.
// This is immune to DOM depth and span nesting.
function watchTocActive() {
var toc = document.querySelector( '.vector-toc' );
if ( !toc ) return;
if ( !window.MutationObserver ) return;
/* Guard only the structObserver — attach it once per toc element.
Per-<li> attachment is already guarded by _grHighlightAttached,
so calling watchTocActive() multiple times is safe and picks up
any <li> items that weren't in the DOM on the first call. */
if ( toc._grObserved ) {
/* Re-scan for any <li> items that arrived after first call */
toc.querySelectorAll( '.vector-toc-list-item' ).forEach( attachHighlight );
return;
}
toc._grObserved = true;
var ACTIVE_COLOR = '#f57c00';
var ACTIVE_WEIGHT = '700';
function setLinkActive( li, active ) {
var link = li.querySelector( '.vector-toc-link' ) || li.querySelector( 'a' );
if ( !link ) return;
if ( active ) {
link.style.setProperty( 'color', ACTIVE_COLOR, 'important' );
link.style.setProperty( 'font-weight', ACTIVE_WEIGHT, 'important' );
/* Also colour nested spans (e.g. data-deva spans from transliteration) */
link.querySelectorAll( 'span' ).forEach( function ( s ) {
s.style.setProperty( 'color', ACTIVE_COLOR, 'important' );
} );
} else {
link.style.removeProperty( 'color' );
link.style.removeProperty( 'font-weight' );
link.querySelectorAll( 'span' ).forEach( function ( s ) {
s.style.removeProperty( 'color' );
} );
}
}
function attachHighlight( li ) {
if ( li._grHighlightAttached ) return;
li._grHighlightAttached = true;
liObserver.observe( li, { attributes: true, attributeFilter: [ 'class' ] } );
/* Apply immediately if already active on attachment */
if ( li.classList.contains( 'vector-toc-list-item-active' ) ) {
setLinkActive( li, true );
}
}
var liObserver = new MutationObserver( function ( mutations ) {
mutations.forEach( function ( m ) {
if ( m.attributeName !== 'class' ) return;
var li = m.target;
var isActive = li.classList.contains( 'vector-toc-list-item-active' );
setLinkActive( li, isActive );
/* Scroll active item into view within the TOC container —
* but ONLY when the TOC sidebar is actually visible and expanded.
* If the TOC is collapsed or hidden, scrollIntoView scrolls the
* whole page instead of just the TOC, which hijacks the reading
* position. */
if ( isActive ) {
var container = document.querySelector( '.vector-sticky-pinned-container' );
if ( container ) {
/* Check TOC is visible: the container must have nonzero height
* and must not be hidden by the Vector "pinned/unpinned" toggle */
var tocVisible = container.offsetHeight > 0 &&
container.offsetParent !== null &&
window.getComputedStyle( container ).display !== 'none' &&
window.getComputedStyle( container ).visibility !== 'hidden';
if ( tocVisible ) {
var liRect = li.getBoundingClientRect();
var cRect = container.getBoundingClientRect();
if ( liRect.top < cRect.top + 8 || liRect.bottom > cRect.bottom - 8 ) {
/* Scroll only inside the TOC container, not the page */
var scrollParent = container.querySelector( '.vector-toc' ) || container;
var offset = liRect.top - cRect.top;
if ( offset < 8 || offset > cRect.height - 48 ) {
scrollParent.scrollTop += offset - ( cRect.height / 2 );
}
}
}
}
}
} );
} );
var structObserver = new MutationObserver( function ( mutations ) {
mutations.forEach( function ( m ) {
if ( m.type !== 'childList' ) return;
m.addedNodes.forEach( function ( n ) {
if ( n.nodeType !== 1 ) return;
/* Attach highlight observer to newly added list items */
if ( n.classList && n.classList.contains( 'vector-toc-list-item' ) ) {
attachHighlight( n );
}
if ( n.querySelectorAll ) {
n.querySelectorAll( '.vector-toc-list-item' ).forEach( attachHighlight );
}
/* Tag any new .vector-toc-text spans for transliteration */
var newSpans = [];
if ( n.classList && n.classList.contains( 'vector-toc-text' ) ) newSpans.push( n );
if ( n.querySelectorAll ) {
n.querySelectorAll( '.vector-toc-text' ).forEach( function ( s ) { newSpans.push( s ); } );
}
newSpans.forEach( function ( span ) {
if ( span.hasAttribute( 'data-deva' ) ) return;
var orig = span.textContent;
if ( !orig.trim() ) return;
span.setAttribute( 'data-deva', orig );
if ( currentScript !== 'deva' ) {
span.textContent = transliterateText( orig, currentScript );
}
translatableSpans.push( span );
} );
} );
} );
} );
toc.querySelectorAll( '.vector-toc-list-item' ).forEach( attachHighlight );
structObserver.observe( toc, { childList: true, subtree: true } );
/* On initial load, colour the already-active item.
* scrollIntoView is intentionally skipped here — calling it while the
* TOC sidebar might be collapsed causes the PAGE to scroll to the element
* rather than scrolling within the TOC container. The liObserver handles
* scrolling the TOC as the user scrolls the page content. */
setTimeout( function () {
var active = toc.querySelector( '.vector-toc-list-item-active' );
if ( active ) {
setLinkActive( active, true );
}
}, 300 );
}
// ── Init ────────────────────────────────────────────────────────
function init() {
var content = document.querySelector( '.mw-parser-output' );
var alreadyTagged = content && content.querySelector( '[data-deva]' );
if ( !alreadyTagged ) {
translatableSpans = [];
tagTextNodes();
} else {
document.querySelectorAll( '.vector-toc .vector-toc-text:not([data-deva])' ).forEach( function ( span ) {
var orig = span.textContent;
if ( !orig.trim() ) return;
span.setAttribute( 'data-deva', orig );
translatableSpans.push( span );
} );
}
var saved = ( function () {
try { return localStorage.getItem( LS_SCRIPT_KEY ); } catch ( e ) { return null; }
}() );
if ( saved && saved !== 'deva' ) {
applyScript( saved );
} else {
currentScript = 'deva';
}
watchTocActive();
/* Retry for Vector 2022 TOC which renders after DOMContentLoaded */
setTimeout( watchTocActive, 300 );
setTimeout( watchTocActive, 800 );
}
// ── React to toolbar dropdown changes ──────────────────────────
window.addEventListener( 'gr-script-change', function ( e ) {
var script = e && e.detail && e.detail.script;
if ( script ) applyScript( script );
} );
// ── MediaWiki hook (SPA-style navigation) ───────────────────────
if ( window.mw ) {
mw.hook( 'wikipage.content' ).add( function () {
setTimeout( function () {
var content = document.querySelector( '.mw-parser-output' );
var alreadyTagged = content && content.querySelector( '[data-deva]' );
if ( !alreadyTagged ) {
translatableSpans = [];
tagTextNodes();
} else {
document.querySelectorAll( '.vector-toc .vector-toc-text:not([data-deva])' ).forEach( function ( span ) {
var orig = span.textContent;
if ( !orig.trim() ) return;
span.setAttribute( 'data-deva', orig );
translatableSpans.push( span );
} );
}
if ( currentScript !== 'deva' ) applyScript( currentScript );
watchTocActive();
}, 150 );
} );
}
if ( document.readyState === 'loading' ) {
document.addEventListener( 'DOMContentLoaded', init );
} else {
init();
}
}() );
// ── Main page: by-Grantha / by-Author toggle ──────────────────
( 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;
gBtn.addEventListener( 'click', function () { grHomeView( 'grantha' ); } );
aBtn.addEventListener( 'click', function () { grHomeView( 'author' ); } );
[ gBtn, aBtn ].forEach( function ( btn ) {
btn.addEventListener( 'keydown', function ( e ) {
if ( e.key === 'Enter' || e.key === ' ' ) btn.click();
} );
} );
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();
}
}() );