MediaWiki:Common.js: Difference between revisions

From UO Renaissance Wiki
Jump to navigation Jump to search
Add script for skills dropdown in Compendium sidebar
 
Add The World and Skills dropdowns in Compendium sidebar
Line 1: Line 1:
/* UO Renaissance Wiki Common.js */
/* UO Renaissance Wiki Common.js */
(function() {
(function() {
   function initSkillsDropdown() {
   function initCompendiumDropdowns() {
     var compendiumPortal = document.getElementById('p-Compendium');
     var compendiumPortal = document.getElementById('p-Compendium');
     if (!compendiumPortal) return;
     if (!compendiumPortal) return;


    // 1. The World Dropdown
    var worldLi = compendiumPortal.querySelector('li#n-The-World') ||
                  compendiumPortal.querySelector('a[href*="/wiki/index.php/The_World"]')?.closest('li');
    if (worldLi && !worldLi.querySelector('.uor-world-dropdown-wrap')) {
      var worldWrap = document.createElement('div');
      worldWrap.className = 'uor-world-dropdown-wrap';
      worldWrap.innerHTML = '<select class="uor-world-dropdown-select" aria-label="Select World Section">' +
        '<option value="/wiki/index.php/The_World">World Sections...</option>' +
        '<option value="/wiki/index.php/The_World#Towns">Towns</option>' +
        '<option value="/wiki/index.php/The_World#Dungeons">Dungeons</option>' +
        '<option value="/wiki/index.php/The_World#Points_of_Interest">Points of Interest</option>' +
      '</select>';
      var worldSelect = worldWrap.querySelector('select');
      var currentPath = window.location.pathname + window.location.hash;
      for (var i = 0; i < worldSelect.options.length; i++) {
        if (worldSelect.options[i].value && currentPath.indexOf(worldSelect.options[i].value) !== -1) {
          worldSelect.selectedIndex = i;
          break;
        }
      }
      worldSelect.addEventListener('change', function() {
        if (this.value) {
          window.location.href = this.value;
        }
      });
      worldLi.appendChild(worldWrap);
    }
    // 2. Skills Dropdown
     var skillsLi = compendiumPortal.querySelector('li#n-Skills') ||
     var skillsLi = compendiumPortal.querySelector('li#n-Skills') ||
                   compendiumPortal.querySelector('li#n-Skills-Overview') ||
                   compendiumPortal.querySelector('li#n-Skills-Overview') ||
                   compendiumPortal.querySelector('a[href*="/wiki/index.php/Skills"]')?.closest('li');
                   compendiumPortal.querySelector('a[href*="/wiki/index.php/Skills"]')?.closest('li');
     if (skillsLi && !skillsLi.querySelector('.uor-skills-dropdown-wrap')) {
     if (skillsLi && !skillsLi.querySelector('.uor-skills-dropdown-wrap')) {
       var wrap = document.createElement('div');
       var wrap = document.createElement('div');
Line 43: Line 74:


   if (document.readyState === 'loading') {
   if (document.readyState === 'loading') {
     document.addEventListener('DOMContentLoaded', initSkillsDropdown);
     document.addEventListener('DOMContentLoaded', initCompendiumDropdowns);
   } else {
   } else {
     initSkillsDropdown();
     initCompendiumDropdowns();
   }
   }
})();
})();

Revision as of 13:33, 8 September 2026

/* UO Renaissance Wiki Common.js */
(function() {
  function initCompendiumDropdowns() {
    var compendiumPortal = document.getElementById('p-Compendium');
    if (!compendiumPortal) return;

    // 1. The World Dropdown
    var worldLi = compendiumPortal.querySelector('li#n-The-World') ||
                  compendiumPortal.querySelector('a[href*="/wiki/index.php/The_World"]')?.closest('li');
    if (worldLi && !worldLi.querySelector('.uor-world-dropdown-wrap')) {
      var worldWrap = document.createElement('div');
      worldWrap.className = 'uor-world-dropdown-wrap';
      worldWrap.innerHTML = '<select class="uor-world-dropdown-select" aria-label="Select World Section">' +
        '<option value="/wiki/index.php/The_World">World Sections...</option>' +
        '<option value="/wiki/index.php/The_World#Towns">Towns</option>' +
        '<option value="/wiki/index.php/The_World#Dungeons">Dungeons</option>' +
        '<option value="/wiki/index.php/The_World#Points_of_Interest">Points of Interest</option>' +
      '</select>';

      var worldSelect = worldWrap.querySelector('select');
      var currentPath = window.location.pathname + window.location.hash;
      for (var i = 0; i < worldSelect.options.length; i++) {
        if (worldSelect.options[i].value && currentPath.indexOf(worldSelect.options[i].value) !== -1) {
          worldSelect.selectedIndex = i;
          break;
        }
      }

      worldSelect.addEventListener('change', function() {
        if (this.value) {
          window.location.href = this.value;
        }
      });

      worldLi.appendChild(worldWrap);
    }

    // 2. Skills Dropdown
    var skillsLi = compendiumPortal.querySelector('li#n-Skills') ||
                   compendiumPortal.querySelector('li#n-Skills-Overview') ||
                   compendiumPortal.querySelector('a[href*="/wiki/index.php/Skills"]')?.closest('li');
    if (skillsLi && !skillsLi.querySelector('.uor-skills-dropdown-wrap')) {
      var wrap = document.createElement('div');
      wrap.className = 'uor-skills-dropdown-wrap';
      wrap.innerHTML = '<select class="uor-skills-dropdown-select" aria-label="Select Skill Section">' +
        '<option value="/wiki/index.php/Skills">Skill Sections...</option>' +
        '<option value="/wiki/index.php/Skills#Combat">Combat</option>' +
        '<option value="/wiki/index.php/Skills#Crafting">Crafting</option>' +
        '<option value="/wiki/index.php/Skills#Creatures">Creatures</option>' +
        '<option value="/wiki/index.php/Skills#Lore_&_Knowledge">Lore & Knowledge</option>' +
        '<option value="/wiki/index.php/Skills#Miscellaneous">Miscellaneous</option>' +
        '<option value="/wiki/index.php/Skills#Rogue">Rogue</option>' +
        '<option value="/wiki/index.php/Spells">Spells & Magic</option>' +
      '</select>';

      var select = wrap.querySelector('select');
      var currentPath = window.location.pathname + window.location.hash;
      for (var i = 0; i < select.options.length; i++) {
        if (select.options[i].value && currentPath.indexOf(select.options[i].value) !== -1) {
          select.selectedIndex = i;
          break;
        }
      }

      select.addEventListener('change', function() {
        if (this.value) {
          window.location.href = this.value;
        }
      });

      skillsLi.appendChild(wrap);
    }
  }

  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', initCompendiumDropdowns);
  } else {
    initCompendiumDropdowns();
  }
})();