Jump to content

MediaWiki:Common.js: Difference between revisions

From Once Human Guide
No edit summary
No edit summary
Line 2: Line 2:
   $(function () {
   $(function () {


     console.log("FINAL SYSTEM LOADED (UPGRADED)");
     console.log("FINAL SYSTEM LOADED (FULL REWRITE)");


     /* ================= TAB SYSTEM (SCOPED + SAFE) ================= */
     /* ================= TAB SYSTEM (SCOPED + SAFE) ================= */
Line 30: Line 30:
             target.classList.add('active');
             target.classList.add('active');


             // render dropdowns inside visible tab
             // initialize dropdowns inside this tab
             initDropdowns(target);
             initDropdowns(target);
           }
           }
Line 40: Line 40:




     /* ================= DROPDOWN SYSTEM (UPGRADED) ================= */
     /* ================= DROPDOWN SYSTEM (FULL UPGRADE) ================= */


     function initDropdowns(scope) {
     function initDropdowns(scope) {
Line 46: Line 46:
       scope.querySelectorAll('.mw-dropdown-ui[data-options]').forEach(container => {
       scope.querySelectorAll('.mw-dropdown-ui[data-options]').forEach(container => {


         if (container.dataset.rendered) return;
         if (container.dataset.rendered === "true") return;


         let data;
         let data;
Line 58: Line 58:
         container.innerHTML = '';
         container.innerHTML = '';


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


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


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


         // SORT ALPHABETICALLY
         // ===== SORT + BUILD =====
         Object.values(data)
         Object.values(data)
           .sort((a, b) => a.label.localeCompare(b.label))
           .sort((a, b) => a.label.localeCompare(b.label))
Line 82: Line 82:


             option.onclick = () => {
             option.onclick = () => {
               btn.textContent = item.label;
               btn.textContent = item.label;
               list.style.display = 'none';
               list.style.display = 'none';


               // ===== NEW GAME-STYLE OUTPUT =====
              let lines = [];
 
               // ===== NEW FORMAT =====
               if (item.lines && Array.isArray(item.lines)) {
               if (item.lines && Array.isArray(item.lines)) {
                lines = item.lines;
              }


                output.innerHTML =
              // ===== OLD FORMAT AUTO-CONVERT =====
                  `<div class="mw-ui-title">${item.label}</div>` +
              else if (item.content) {
                   item.lines.map(line =>
                lines = item.content
                    `<div class="mw-ui-line">${line}</div>`
                   .split(';')
                   ).join('');
                  .map(s => s.trim())
                   .filter(Boolean);
              }


               } else {
               // ===== RENDER =====
                // fallback for old system
              output.innerHTML =
                output.innerHTML = item.content || '';
                `<div class="mw-ui-title">${item.label}</div>` +
              }
                lines.map(line =>
                  `<div class="mw-ui-line">${line}</div>`
                ).join('');
             };
             };


Line 103: Line 112:
           });
           });


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


         // BUILD
         // ===== CLOSE WHEN CLICK OUTSIDE =====
        document.addEventListener('click', () => {
          list.style.display = 'none';
        });
 
        // ===== BUILD =====
         container.appendChild(btn);
         container.appendChild(btn);
         container.appendChild(list);
         container.appendChild(list);

Revision as of 13:08, 24 April 2026

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

    console.log("FINAL SYSTEM LOADED (FULL REWRITE)");

    /* ================= TAB SYSTEM (SCOPED + SAFE) ================= */

    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 only this group
          buttons.forEach(b => b.classList.remove('active'));
          this.classList.add('active');

          const container = group.parentElement;

          // hide only direct children tabs
          container.querySelectorAll(':scope > .mw-tab-content').forEach(c => {
            c.classList.remove('active');
          });

          const target = container.querySelector('#' + tabId);
          if (target) {
            target.classList.add('active');

            // initialize dropdowns inside this tab
            initDropdowns(target);
          }

        });
      });

    });


    /* ================= DROPDOWN SYSTEM (FULL UPGRADE) ================= */

    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 = '';

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

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

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

        // ===== SORT + BUILD =====
        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 = [];

              // ===== NEW FORMAT =====
              if (item.lines && Array.isArray(item.lines)) {
                lines = item.lines;
              }

              // ===== OLD FORMAT AUTO-CONVERT =====
              else if (item.content) {
                lines = item.content
                  .split(';')
                  .map(s => s.trim())
                  .filter(Boolean);
              }

              // ===== RENDER =====
              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 DROPDOWN =====
        btn.onclick = (e) => {
          e.stopPropagation();
          list.style.display = list.style.display === 'block' ? 'none' : 'block';
        };

        // ===== CLOSE WHEN CLICK OUTSIDE =====
        document.addEventListener('click', () => {
          list.style.display = 'none';
        });

        // ===== BUILD =====
        container.appendChild(btn);
        container.appendChild(list);
        container.appendChild(output);

        container.dataset.rendered = "true";

      });

    }


    /* ================= INITIAL LOAD ================= */

    document.querySelectorAll('.mw-tab-buttons').forEach(group => {
      const first = group.querySelector('.mw-tab-btn');
      if (first) first.click();
    });

  });
});


/* ================= DISCORD TAB ================= */

$(function () {
  const discussionTab = document.querySelector('#ca-talk a');

  if (discussionTab) {
    discussionTab.href = "https://discord.com/invite/FZtkXeGeUA";
    discussionTab.target = "_blank";
    discussionTab.textContent = "Discord";
  }
});