Jump to content

MediaWiki:Common.js: Difference between revisions

From Once Human Guide
No edit summary
Tag: Manual revert
No edit summary
Tag: Reverted
Line 1: Line 1:
mw.loader.using(['mediawiki.util']).then(function () {
mw.loader.using('mediawiki.util').then(function () {
   $(function () {
   $(function () {


     console.log("FINAL SYSTEM LOADED");
     /* =========================
      ORIGINAL TAB SYSTEM (RESTORED)
    ========================= */
    document.querySelectorAll('.mw-tab-btn').forEach(btn => {
      btn.addEventListener('click', () => {
        const tabId = btn.dataset.tab;


    /* ================= TAB SYSTEM (SCOPED + SAFE) ================= */
        // deactivate all buttons
        document.querySelectorAll('.mw-tab-btn').forEach(b => b.classList.remove('active'));
        btn.classList.add('active');


    document.querySelectorAll('.mw-tab-buttons').forEach(group => {
        // hide all contents
 
        document.querySelectorAll('.mw-tab-content').forEach(c => {
      const buttons = group.querySelectorAll('.mw-tab-btn');
           c.style.display = 'none';
 
        });
      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
        // show selected
          container.querySelectorAll(':scope > .mw-tab-content').forEach(c => {
        const target = document.getElementById(tabId);
            c.classList.remove('active');
        if (target) target.style.display = 'block';
          });
 
          const target = container.querySelector('#' + tabId);
          if (target) {
            target.classList.add('active');
 
            // render dropdowns inside visible tab
            initDropdowns(target);
          }
 
        });
       });
       });
     });
     });


    // auto-click first tab (like your original behavior)
    const firstTab = document.querySelector('.mw-tab-btn');
    if (firstTab) firstTab.click();


    /* ================= DROPDOWN SYSTEM (DATA-OPTIONS ONLY) ================= */


     function initDropdowns(scope) {
     /* =========================
      DROPDOWN SYSTEM (FIX ONLY)
    ========================= */
    document.querySelectorAll('.mw-dropdown-ui').forEach(container => {


       scope.querySelectorAll('.mw-dropdown-ui[data-options]').forEach(container => {
       let raw = container.getAttribute('data-options');
      if (!raw) return;


        if (container.dataset.rendered) return;
      let options;


         let data;
      try {
         try {
         // ONLY improvement: safer parsing
          data = JSON.parse(container.dataset.options);
         options = JSON.parse(raw);
        } catch (e) {
      } catch (e) {
          console.error("Bad dropdown JSON:", e);
        console.error("Dropdown JSON failed:", raw);
          return;
        return;
        }
      }


        // CLEAR ONLY ONCE (safe)
      const labelText = container.dataset.label || "Select One";
        container.innerHTML = '';


        // BUTTON
      const label = document.createElement('div');
        const btn = document.createElement('div');
      label.textContent = labelText;
        btn.className = 'mod-dropdown-btn';
      label.style.marginBottom = "6px";
        btn.textContent = 'Select One';


        // LIST
      const select = document.createElement('select');
        const list = document.createElement('div');
      select.style.width = "100%";
        list.className = 'mod-dropdown-list';
      select.style.padding = "8px";


        // OUTPUT
      const defaultOpt = document.createElement('option');
        const output = document.createElement('div');
      defaultOpt.value = "";
        output.className = 'mw-dropdown-output';
      defaultOpt.textContent = "Select One";
        output.textContent = 'Select something to see details';
      select.appendChild(defaultOpt);


        // SORT ALPHABETICALLY
      const output = document.createElement('div');
        Object.values(data)
      output.style.marginTop = "12px";
          .sort((a, b) => a.label.localeCompare(b.label))
          .forEach(item => {


            const option = document.createElement('div');
      Object.entries(options).forEach(([key, val]) => {
            option.className = 'mod-dropdown-item';
        const opt = document.createElement('option');
            option.textContent = item.label;
        opt.value = key;
 
         opt.textContent = val.label || key;
            option.onclick = () => {
         select.appendChild(opt);
              btn.textContent = item.label;
      });
              list.style.display = 'none';
              output.innerHTML = item.content;
            };
 
            list.appendChild(option);
          });
 
        // TOGGLE
         btn.onclick = () => {
          list.style.display = list.style.display === 'block' ? 'none' : 'block';
         };
 
        // BUILD
        container.appendChild(btn);
        container.appendChild(list);
        container.appendChild(output);
 
        container.dataset.rendered = "true";


      select.addEventListener('change', () => {
        const selected = options[select.value];
        output.innerHTML = selected ? selected.content : "";
       });
       });


    }
      container.innerHTML = "";
 
      container.appendChild(label);
 
      container.appendChild(select);
    /* ================= INITIAL LOAD ================= */
      container.appendChild(output);


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


   });
   });
});
});

Revision as of 22:45, 21 April 2026

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

    /* =========================
       ORIGINAL TAB SYSTEM (RESTORED)
    ========================= */
    document.querySelectorAll('.mw-tab-btn').forEach(btn => {
      btn.addEventListener('click', () => {
        const tabId = btn.dataset.tab;

        // deactivate all buttons
        document.querySelectorAll('.mw-tab-btn').forEach(b => b.classList.remove('active'));
        btn.classList.add('active');

        // hide all contents
        document.querySelectorAll('.mw-tab-content').forEach(c => {
          c.style.display = 'none';
        });

        // show selected
        const target = document.getElementById(tabId);
        if (target) target.style.display = 'block';
      });
    });

    // auto-click first tab (like your original behavior)
    const firstTab = document.querySelector('.mw-tab-btn');
    if (firstTab) firstTab.click();


    /* =========================
       DROPDOWN SYSTEM (FIX ONLY)
    ========================= */
    document.querySelectorAll('.mw-dropdown-ui').forEach(container => {

      let raw = container.getAttribute('data-options');
      if (!raw) return;

      let options;

      try {
        // ONLY improvement: safer parsing
        options = JSON.parse(raw);
      } catch (e) {
        console.error("Dropdown JSON failed:", raw);
        return;
      }

      const labelText = container.dataset.label || "Select One";

      const label = document.createElement('div');
      label.textContent = labelText;
      label.style.marginBottom = "6px";

      const select = document.createElement('select');
      select.style.width = "100%";
      select.style.padding = "8px";

      const defaultOpt = document.createElement('option');
      defaultOpt.value = "";
      defaultOpt.textContent = "Select One";
      select.appendChild(defaultOpt);

      const output = document.createElement('div');
      output.style.marginTop = "12px";

      Object.entries(options).forEach(([key, val]) => {
        const opt = document.createElement('option');
        opt.value = key;
        opt.textContent = val.label || key;
        select.appendChild(opt);
      });

      select.addEventListener('change', () => {
        const selected = options[select.value];
        output.innerHTML = selected ? selected.content : "";
      });

      container.innerHTML = "";
      container.appendChild(label);
      container.appendChild(select);
      container.appendChild(output);

    });

  });
});