Jump to content

MediaWiki:Common.js: Difference between revisions

From Once Human Guide
No edit summary
No edit summary
 
(74 intermediate revisions by the same user not shown)
Line 1: Line 1:
mw.loader.using('mediawiki.util').then(function () {
/**
    $(function () {
* MediaWiki:Common.js
* Once Human Guide
*/


        // Rename Discussion → Discord
mw.loader.using(['mediawiki.util']).then(function () {
        const talkTab = document.querySelector('#ca-talk a');
  $(function () {
        const talkItem = document.querySelector('#ca-talk');


        if (talkTab) {
    /* ============================================================
            talkTab.textContent = 'Discord';
    * TAB SYSTEM
            talkTab.href = 'https://discord.gg/FZtkXeGeUA';
    * Wires up .mw-tab-buttons groups so each .mw-tab-btn shows its
            talkTab.target = '_blank';
    * matching .mw-tab-content sibling.
             talkTab.rel = 'noopener noreferrer';
    * ============================================================ */
    function initTabs() {
      document.querySelectorAll('.mw-tab-buttons').forEach(group => {
        const buttons = group.querySelectorAll('.mw-tab-btn');
        buttons.forEach(btn => {
          btn.addEventListener('click', function () {
             const tabId = this.getAttribute('data-tab');


             talkTab.addEventListener('click', function (e) {
             // Deactivate buttons in this group only
                e.preventDefault();
            buttons.forEach(b => b.classList.remove('active'));
                window.open('https://discord.gg/FZtkXeGeUA', '_blank');
            this.classList.add('active');
 
            // Hide only the direct-child tab panels
            const container = group.parentElement;
            container.querySelectorAll(':scope > .mw-tab-content').forEach(c => {
              c.classList.remove('active');
             });
             });
        }


        // Add Road Map BEFORE Discord
            // Show the target panel and init any dropdowns inside it
        const tabList = document.querySelector('#p-associated-pages .vector-menu-content-list, #p-namespaces .vector-menu-content-list');
            const target = container.querySelector('#' + tabId);
            if (target) {
              target.classList.add('active');
              initDropdowns(target);
            }
          });
        });
      });


        if (tabList && !document.getElementById('ca-roadmap')) {
      // Auto-click the first tab in each group on load
            const li = document.createElement('li');
      document.querySelectorAll('.mw-tab-buttons').forEach(group => {
            li.id = 'ca-roadmap';
        const first = group.querySelector('.mw-tab-btn');
            li.className = talkItem ? talkItem.className.replace(/\bselected\b|\bactive\b/g, '').trim() : 'mw-list-item';
        if (first) first.click();
      });
    }


            const a = document.createElement('a');
    /* ============================================================
            a.href = 'https://ohwikiguide.com/index.php/Road_Map';
    * JSON-DRIVEN DROPDOWN UI
    * Renders .mw-dropdown-ui[data-options] containers as a
    * label button, sorted list, and detail output panel.
    * Supports both the new "lines" array format and the old
    * semicolon-separated "content" string format.
    * ============================================================ */
    function initDropdowns(scope) {
      scope.querySelectorAll('.mw-dropdown-ui[data-options]').forEach(container => {
        if (container.dataset.rendered === 'true') return;


            const span = document.createElement('span');
        let data;
            span.textContent = 'Road Map';
        try {
          data = JSON.parse(container.dataset.options);
        } catch (e) {
          console.error('Bad dropdown JSON:', e);
          return;
        }


            a.appendChild(span);
        container.innerHTML = '';
            li.appendChild(a);


            if (talkItem) {
        const btn = document.createElement('div');
                tabList.insertBefore(li, talkItem);
        btn.className = 'mod-dropdown-btn';
            } else {
         btn.textContent = container.dataset.label || 'Select One';
                tabList.appendChild(li);
            }
         }


    });
        const list = document.createElement('div');
});
        list.className = 'mod-dropdown-list';
mw.loader.using('mediawiki.util').then(function () {
  $(function () {


    document.querySelectorAll('.mw-dropdown-ui').forEach(container => {
        const output = document.createElement('div');
        output.className = 'mw-dropdown-output';
        output.innerHTML = '<div class="mw-ui-placeholder">Select something to see details</div>';


      let raw = container.getAttribute('data-options');
        Object.values(data)
      if (!raw) return;
          .sort((a, b) => a.label.localeCompare(b.label))
          .forEach(item => {
            const option = document.createElement('div');
            option.className = 'mod-dropdown-item';
            option.textContent = item.label;
            option.onclick = () => {
              btn.textContent = item.label;
              list.style.display = 'none';


      let data;
              let lines = [];
              if (item.lines && Array.isArray(item.lines)) {
                lines = item.lines;
              } else if (item.content) {
                lines = item.content.split(';').map(s => s.trim()).filter(Boolean);
              }


      try {
              output.innerHTML =
        data = JSON.parse(raw);
                `<div class="mw-ui-title">${item.label}</div>` +
      } catch (e) {
                lines.map(line => `<div class="mw-ui-line">${line}</div>`).join('');
        console.error('Dropdown JSON error:', e);
            };
        container.innerHTML = '<span style="color:red;">Invalid dropdown data</span>';
            list.appendChild(option);
        return;
          });
      }


      // Create dropdown
        // Toggle list open/closed
      const select = document.createElement('select');
        btn.onclick = (e) => {
      select.style.padding = '6px';
          e.stopPropagation();
          list.style.display = list.style.display === 'block' ? 'none' : 'block';
        };


      const defaultOption = document.createElement('option');
        // Close when clicking outside
      defaultOption.value = '';
        document.addEventListener('click', () => {
      defaultOption.textContent = 'Select one';
          list.style.display = 'none';
      select.appendChild(defaultOption);
        });


      Object.keys(data).forEach(key => {
        container.appendChild(btn);
         const opt = document.createElement('option');
         container.appendChild(list);
         opt.value = key;
         container.appendChild(output);
         opt.textContent = data[key].label;
         container.dataset.rendered = 'true';
        select.appendChild(opt);
       });
       });
    }


      // Create output panel
    /* ============================================================
      const output = document.createElement('div');
    * EXCLUSIVE COLLAPSIBLE DROPDOWNS (Loot Crate pages)
      output.style.marginLeft = '20px';
    * When one item's image is opened, all others in the group
      output.style.padding = '12px';
    * are forced to collapse so only one is visible at a time.
      output.style.border = '1px solid #444';
    * ============================================================ */
       output.style.background = '#111';
    function initExclusiveCollapsibles() {
       output.style.minWidth = '250px';
       const outerKey = 'lootcrate';
       output.innerHTML = 'Pick something';
       const outerCollapsibleId = 'mw-customcollapsible-' + outerKey;
       let suppress = false;


       // Layout wrapper
       // Listen on document and check the click target. Use NATIVE click()
    const wrapper = document.createElement('div');
      // to retrigger MediaWiki's handler — jQuery .trigger() doesn't always
wrapper.style.display = 'flex';
      // reach handlers that MW attached with native addEventListener.
wrapper.style.gap = '20px';
      document.addEventListener('click', function (e) {
wrapper.style.alignItems = 'flex-start';
        if (suppress) return;


// LEFT SIDE (Dropdown + Label)
        // Find an ancestor with a mw-customtoggle-* class
const left = document.createElement('div');
        let toggle = e.target;
        while (toggle && toggle !== document) {
          if (toggle.className && /mw-customtoggle-(\S+)/.test(toggle.className)) break;
          toggle = toggle.parentElement;
        }
        if (!toggle || toggle === document) return;


const leftLabel = document.createElement('div');
        // Must be inside the outer loot-crate dropdown
leftLabel.textContent = 'Hide';
        const outerCol = document.getElementById(outerCollapsibleId);
leftLabel.style.marginBottom = '6px';
        if (!outerCol || !outerCol.contains(toggle)) return;
leftLabel.style.fontWeight = 'bold';


left.appendChild(leftLabel);
        const match = toggle.className.match(/mw-customtoggle-(\S+)/);
left.appendChild(select);
        if (!match) return;
        const clickedKey = match[1];
        const pickedName = toggle.textContent.trim();


// RIGHT SIDE (Output + Label)
        setTimeout(function () {
const right = document.createElement('div');
          suppress = true;


const rightLabel = document.createElement('div');
          // Close any other items that are currently open
rightLabel.textContent = 'Perks';
          const inners = outerCol.querySelectorAll('[class*="mw-customtoggle-"]');
rightLabel.style.marginBottom = '6px';
          inners.forEach(function (t) {
rightLabel.style.fontWeight = 'bold';
            const m = t.className.match(/mw-customtoggle-(\S+)/);
            if (!m || m[1] === clickedKey) return;
            const img = document.getElementById('mw-customcollapsible-' + m[1]);
            if (img && !img.classList.contains('mw-collapsed')) {
              t.click();
            }
          });


right.appendChild(rightLabel);
          // Update outer dropdown label
right.appendChild(output);
          const outerToggle = document.querySelector('.mw-customtoggle-' + outerKey);
          if (outerToggle) {
            const labelSpan = outerToggle.querySelector('span');
            if (labelSpan) labelSpan.textContent = pickedName;
          }


// Add both sides
          // Close the outer dropdown
wrapper.appendChild(left);
          if (outerCol && !outerCol.classList.contains('mw-collapsed') && outerToggle) {
wrapper.appendChild(right); container.appendChild(wrapper);
            outerToggle.click();
          }


      // Change handler
          suppress = false;
       select.addEventListener('change', function () {
        }, 50);
        const val = this.value;
       }, true);
    }


        if (!val || !data[val]) {
    /* ============================================================
          output.innerHTML = 'Pick something';
    * DISCORD TAB OVERRIDE
          return;
    * Repurposes the page's "Discussion" tab as a Discord invite.
         }
    * ============================================================ */
 
    function initDiscordTab() {
         output.innerHTML =
      const discussionTab = document.querySelector('#ca-talk a');
          '<b>' + data[val].label + '</b><br><br>' +
      if (discussionTab) {
          data[val].content;
         discussionTab.href = 'https://discord.com/invite/FZtkXeGeUA';
       });
         discussionTab.target = '_blank';
 
        discussionTab.textContent = 'Discord';
     });
       }
     }


    /* ============================================================
    * BOOT
    * ============================================================ */
    initTabs();
    initExclusiveCollapsibles();
    initDiscordTab();
   });
   });
});
});
console.log("COMMON JS LOADED");

Latest revision as of 13:38, 27 May 2026

/**
 * MediaWiki:Common.js
 * Once Human Guide
 */

mw.loader.using(['mediawiki.util']).then(function () {
  $(function () {

    /* ============================================================
     * TAB SYSTEM
     * Wires up .mw-tab-buttons groups so each .mw-tab-btn shows its
     * matching .mw-tab-content sibling.
     * ============================================================ */
    function initTabs() {
      document.querySelectorAll('.mw-tab-buttons').forEach(group => {
        const buttons = group.querySelectorAll('.mw-tab-btn');
        buttons.forEach(btn => {
          btn.addEventListener('click', function () {
            const tabId = this.getAttribute('data-tab');

            // Deactivate buttons in this group only
            buttons.forEach(b => b.classList.remove('active'));
            this.classList.add('active');

            // Hide only the direct-child tab panels
            const container = group.parentElement;
            container.querySelectorAll(':scope > .mw-tab-content').forEach(c => {
              c.classList.remove('active');
            });

            // Show the target panel and init any dropdowns inside it
            const target = container.querySelector('#' + tabId);
            if (target) {
              target.classList.add('active');
              initDropdowns(target);
            }
          });
        });
      });

      // Auto-click the first tab in each group on load
      document.querySelectorAll('.mw-tab-buttons').forEach(group => {
        const first = group.querySelector('.mw-tab-btn');
        if (first) first.click();
      });
    }

    /* ============================================================
     * JSON-DRIVEN DROPDOWN UI
     * Renders .mw-dropdown-ui[data-options] containers as a
     * label button, sorted list, and detail output panel.
     * Supports both the new "lines" array format and the old
     * semicolon-separated "content" string format.
     * ============================================================ */
    function initDropdowns(scope) {
      scope.querySelectorAll('.mw-dropdown-ui[data-options]').forEach(container => {
        if (container.dataset.rendered === 'true') return;

        let data;
        try {
          data = JSON.parse(container.dataset.options);
        } catch (e) {
          console.error('Bad dropdown JSON:', e);
          return;
        }

        container.innerHTML = '';

        const btn = document.createElement('div');
        btn.className = 'mod-dropdown-btn';
        btn.textContent = container.dataset.label || 'Select One';

        const list = document.createElement('div');
        list.className = 'mod-dropdown-list';

        const output = document.createElement('div');
        output.className = 'mw-dropdown-output';
        output.innerHTML = '<div class="mw-ui-placeholder">Select something to see details</div>';

        Object.values(data)
          .sort((a, b) => a.label.localeCompare(b.label))
          .forEach(item => {
            const option = document.createElement('div');
            option.className = 'mod-dropdown-item';
            option.textContent = item.label;
            option.onclick = () => {
              btn.textContent = item.label;
              list.style.display = 'none';

              let lines = [];
              if (item.lines && Array.isArray(item.lines)) {
                lines = item.lines;
              } else if (item.content) {
                lines = item.content.split(';').map(s => s.trim()).filter(Boolean);
              }

              output.innerHTML =
                `<div class="mw-ui-title">${item.label}</div>` +
                lines.map(line => `<div class="mw-ui-line">${line}</div>`).join('');
            };
            list.appendChild(option);
          });

        // Toggle list open/closed
        btn.onclick = (e) => {
          e.stopPropagation();
          list.style.display = list.style.display === 'block' ? 'none' : 'block';
        };

        // Close when clicking outside
        document.addEventListener('click', () => {
          list.style.display = 'none';
        });

        container.appendChild(btn);
        container.appendChild(list);
        container.appendChild(output);
        container.dataset.rendered = 'true';
      });
    }

    /* ============================================================
     * EXCLUSIVE COLLAPSIBLE DROPDOWNS (Loot Crate pages)
     * When one item's image is opened, all others in the group
     * are forced to collapse so only one is visible at a time.
     * ============================================================ */
    function initExclusiveCollapsibles() {
      const outerKey = 'lootcrate';
      const outerCollapsibleId = 'mw-customcollapsible-' + outerKey;
      let suppress = false;

      // Listen on document and check the click target. Use NATIVE click()
      // to retrigger MediaWiki's handler — jQuery .trigger() doesn't always
      // reach handlers that MW attached with native addEventListener.
      document.addEventListener('click', function (e) {
        if (suppress) return;

        // Find an ancestor with a mw-customtoggle-* class
        let toggle = e.target;
        while (toggle && toggle !== document) {
          if (toggle.className && /mw-customtoggle-(\S+)/.test(toggle.className)) break;
          toggle = toggle.parentElement;
        }
        if (!toggle || toggle === document) return;

        // Must be inside the outer loot-crate dropdown
        const outerCol = document.getElementById(outerCollapsibleId);
        if (!outerCol || !outerCol.contains(toggle)) return;

        const match = toggle.className.match(/mw-customtoggle-(\S+)/);
        if (!match) return;
        const clickedKey = match[1];
        const pickedName = toggle.textContent.trim();

        setTimeout(function () {
          suppress = true;

          // Close any other items that are currently open
          const inners = outerCol.querySelectorAll('[class*="mw-customtoggle-"]');
          inners.forEach(function (t) {
            const m = t.className.match(/mw-customtoggle-(\S+)/);
            if (!m || m[1] === clickedKey) return;
            const img = document.getElementById('mw-customcollapsible-' + m[1]);
            if (img && !img.classList.contains('mw-collapsed')) {
              t.click();
            }
          });

          // Update outer dropdown label
          const outerToggle = document.querySelector('.mw-customtoggle-' + outerKey);
          if (outerToggle) {
            const labelSpan = outerToggle.querySelector('span');
            if (labelSpan) labelSpan.textContent = pickedName;
          }

          // Close the outer dropdown
          if (outerCol && !outerCol.classList.contains('mw-collapsed') && outerToggle) {
            outerToggle.click();
          }

          suppress = false;
        }, 50);
      }, true);
    }

    /* ============================================================
     * DISCORD TAB OVERRIDE
     * Repurposes the page's "Discussion" tab as a Discord invite.
     * ============================================================ */
    function initDiscordTab() {
      const discussionTab = document.querySelector('#ca-talk a');
      if (discussionTab) {
        discussionTab.href = 'https://discord.com/invite/FZtkXeGeUA';
        discussionTab.target = '_blank';
        discussionTab.textContent = 'Discord';
      }
    }

    /* ============================================================
     * BOOT
     * ============================================================ */
    initTabs();
    initExclusiveCollapsibles();
    initDiscordTab();
  });
});