Jump to content

MediaWiki:Common.js: Difference between revisions

From Once Human Guide
No edit summary
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");
     /* =========================
 
      TAB SYSTEM (FIXED)
     /* ================= TAB SYSTEM (SCOPED + SAFE) ================= */
    ========================= */
 
     document.querySelectorAll('.mw-tab-buttons').forEach(container => {
     document.querySelectorAll('.mw-tab-buttons').forEach(group => {
       const buttons = container.querySelectorAll('.mw-tab-btn');
 
       const buttons = group.querySelectorAll('.mw-tab-btn');


       buttons.forEach(btn => {
       buttons.forEach(btn => {
         btn.addEventListener('click', function () {
         btn.addEventListener('click', () => {
          const tabId = btn.dataset.tab;


          const tabId = this.getAttribute('data-tab');
           // deactivate siblings
 
           // deactivate only this group
           buttons.forEach(b => b.classList.remove('active'));
           buttons.forEach(b => b.classList.remove('active'));
           this.classList.add('active');
           btn.classList.add('active');
 
          const container = group.parentElement;


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


           const target = container.querySelector('#' + tabId);
          // show selected
           if (target) {
           const target = document.getElementById(tabId);
            target.classList.add('active');
           if (target) target.style.display = 'block';
 
            // render dropdowns inside visible tab
            initDropdowns(target);
          }
 
         });
         });
       });
       });


      // auto activate first tab
      if (buttons.length) buttons[0].click();
     });
     });


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


    /* ================= DROPDOWN SYSTEM (DATA-OPTIONS ONLY) ================= */
      let raw = container.getAttribute('data-options');


    function initDropdowns(scope) {
      if (!raw) return;


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


         if (container.dataset.rendered) return;
      try {
         // SAFE PARSE (handles broken JSON better)
        options = JSON.parse(raw);
      } catch (e) {
        console.error("Dropdown JSON failed:", raw);
        return;
      }


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


        // CLEAR ONLY ONCE (safe)
      // Wrapper
        container.innerHTML = '';
      const wrapper = document.createElement('div');
      wrapper.className = 'mw-dropdown-wrapper';


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


        // LIST
      // Select
        const list = document.createElement('div');
      const select = document.createElement('select');
        list.className = 'mod-dropdown-list';
      select.style.width = "100%";
      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
      // Output
        Object.values(data)
      const output = document.createElement('div');
          .sort((a, b) => a.label.localeCompare(b.label))
      output.style.marginTop = "12px";
          .forEach(item => {


            const option = document.createElement('div');
      // Build options
            option.className = 'mod-dropdown-item';
      Object.entries(options).forEach(([key, val]) => {
            option.textContent = item.label;
        const opt = document.createElement('option');
        opt.value = key;
        opt.textContent = val.label || key;
        select.appendChild(opt);
      });


            option.onclick = () => {
      // Handle change
              btn.textContent = item.label;
      select.addEventListener('change', () => {
              list.style.display = 'none';
        const selected = options[select.value];
              output.innerHTML = item.content;
         if (!selected) {
            };
           output.innerHTML = "";
 
          return;
            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";


        // IMPORTANT: allows FULL HTML + multiline
        output.innerHTML = selected.content || "";
       });
       });


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


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


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

Revision as of 22:43, 21 April 2026

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

    /* =========================
       TAB SYSTEM (FIXED)
    ========================= */
    document.querySelectorAll('.mw-tab-buttons').forEach(container => {
      const buttons = container.querySelectorAll('.mw-tab-btn');

      buttons.forEach(btn => {
        btn.addEventListener('click', () => {
          const tabId = btn.dataset.tab;

          // deactivate siblings
          buttons.forEach(b => b.classList.remove('active'));
          btn.classList.add('active');

          // hide all contents globally
          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 activate first tab
      if (buttons.length) buttons[0].click();
    });

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

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

      if (!raw) return;

      let options;

      try {
        // SAFE PARSE (handles broken JSON better)
        options = JSON.parse(raw);
      } catch (e) {
        console.error("Dropdown JSON failed:", raw);
        return;
      }

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

      // Wrapper
      const wrapper = document.createElement('div');
      wrapper.className = 'mw-dropdown-wrapper';

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

      // Select
      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);

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

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

      // Handle change
      select.addEventListener('change', () => {
        const selected = options[select.value];
        if (!selected) {
          output.innerHTML = "";
          return;
        }

        // IMPORTANT: allows FULL HTML + multiline
        output.innerHTML = selected.content || "";
      });

      // Clear container and rebuild
      container.innerHTML = "";
      wrapper.appendChild(label);
      wrapper.appendChild(select);
      wrapper.appendChild(output);
      container.appendChild(wrapper);

    });

  });
});