Jump to content

MediaWiki:Common.js: Difference between revisions

From Once Human Guide
No edit summary
Tag: Reverted
No edit summary
Tag: Manual revert
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;


        // deactivate all buttons
    /* ================= TAB SYSTEM (SCOPED + SAFE) ================= */
        document.querySelectorAll('.mw-tab-btn').forEach(b => b.classList.remove('active'));
 
        btn.classList.add('active');
    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');
 
            // render dropdowns inside visible tab
            initDropdowns(target);
          }


        // 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 (DATA-OPTIONS ONLY) ================= */


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


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


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


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


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


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


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


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


      const output = document.createElement('div');
        // SORT ALPHABETICALLY
      output.style.marginTop = "12px";
        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';
              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";


      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 = "";
    /* ================= INITIAL LOAD ================= */
      container.appendChild(label);
      container.appendChild(select);
      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:46, 21 April 2026

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

    console.log("FINAL SYSTEM LOADED");

    /* ================= 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');

            // render dropdowns inside visible tab
            initDropdowns(target);
          }

        });
      });

    });


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

    function initDropdowns(scope) {

      scope.querySelectorAll('.mw-dropdown-ui[data-options]').forEach(container => {

        if (container.dataset.rendered) return;

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

        // CLEAR ONLY ONCE (safe)
        container.innerHTML = '';

        // BUTTON
        const btn = document.createElement('div');
        btn.className = 'mod-dropdown-btn';
        btn.textContent = '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.textContent = 'Select something to see details';

        // SORT ALPHABETICALLY
        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';
              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";

      });

    }


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

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

  });
});