MediaWiki:Common.js: Difference between revisions

No edit summary
No edit summary
Line 2: Line 2:


   var currentScript = 'deva';
   var currentScript = 'deva';
  var initialized = false;


   // ── IAST transliteration (inline, no external library) ─────────
   // ── IAST transliteration (inline, no external library) ─────────
Line 113: Line 112:
   var translatableSpans = [];
   var translatableSpans = [];


function watchTocActive() {
  function tagTextNodes() {
  var toc = document.querySelector('.vector-toc');
    var content = document.querySelector('.mw-parser-output');
  if (!toc || toc._grObserved) return;
    if (!content) return;
  toc._grObserved = true;


  var observer = new MutationObserver(function (mutations) {
    var walker = document.createTreeWalker(content, NodeFilter.SHOW_TEXT);
    mutations.forEach(function (m) {
    var nodes = [];
      if (m.attributeName !== 'class') return;
    while (walker.nextNode()) nodes.push(walker.currentNode);
      var li = m.target;
      var link = li.querySelector(':scope > .vector-toc-link');
      if (!link) return;


      if (li.classList.contains('vector-toc-list-item-active')) {
    nodes.forEach(function (node) {
        link.style.color = '#f57c00';
      var p = node.parentNode;
        link.style.fontWeight = '700';
      if (!p) return;
 
      // Skip already tagged
        // Auto-expand: find the toggle button and trigger it if collapsed
      if (p.hasAttribute && p.hasAttribute('data-deva')) return;
        var toggle = li.querySelector(':scope > .vector-toc-toggle');
      // Skip UI elements
        if (toggle && toggle.getAttribute('aria-expanded') === 'false') {
      if (p.closest) {
          toggle.click();
         if (p.closest('.gr-script-bar') ||
        }
            p.closest('.vector-toc') ||
 
            p.closest('#toc') ||
        // Also expand parent if this is a level-2 item
             p.closest('.mw-editsection')) return;
         var parentLi = li.closest('.vector-toc-level-1');
        if (parentLi) {
          var parentToggle = parentLi.querySelector(':scope > .vector-toc-toggle');
          if (parentToggle && parentToggle.getAttribute('aria-expanded') === 'false') {
             parentToggle.click();
          }
        }
      } else {
        link.style.color = '';
        link.style.fontWeight = '';
       }
       }
      var orig = node.textContent;
      if (!orig.trim()) return;
      // Wrap in span with original stored
      var span = document.createElement('span');
      span.setAttribute('data-deva', orig);
      span.textContent = orig;
      p.replaceChild(span, node);
      translatableSpans.push(span);
     });
     });
   });
   }
 
  toc.querySelectorAll('.vector-toc-list-item').forEach(function (li) {
    observer.observe(li, { attributes: true, attributeFilter: ['class'] });
  });
}


   function applyScript(script) {
   function applyScript(script) {
Line 229: Line 217:
   // ── Init ───────────────────────────────────────────────────────
   // ── Init ───────────────────────────────────────────────────────
   function init() {
   function init() {
    if (initialized) return;
    initialized = true;
     tagTextNodes();
     tagTextNodes();
     buildBar();
     buildBar();
Line 236: Line 222:
   }
   }


   // Wait for MW to finish rendering before tagging nodes
   // MW hook fires when page content is ready
   if (window.mw) {
   if (window.mw) {
     mw.hook('wikipage.content').add(function () {
     mw.hook('wikipage.content').add(function () {
      // Reset on each page load
       translatableSpans = [];
       translatableSpans = [];
       initialized = false;
       var oldBar = document.querySelector('.gr-script-bar');
      if (oldBar) oldBar.remove();
       setTimeout(init, 100);
       setTimeout(init, 100);
     });
     });
  }
  // Fallback for non-MW environments
  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', init);
   } else {
   } else {
     if (document.readyState === 'loading') {
     init();
      document.addEventListener('DOMContentLoaded', init);
    } else {
      init();
    }
   }
   }


})();
})();