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


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


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


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


          // show selected
           const target = container.querySelector('#' + tabId);
           const target = document.getElementById(tabId);
           if (target) {
           if (target) target.style.display = 'block';
            target.classList.add('active');
 
            // 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 => {


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


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


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


      try {
         if (container.dataset.rendered) return;
         // 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";
        let data;
        try {
          data = JSON.parse(container.dataset.options);
        } catch (e) {
          console.error("Bad dropdown JSON:", e);
          return;
        }


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


      // Label
        // 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';


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


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


      // Output
        // 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');
            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";


      // 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
    /* ================= INITIAL LOAD ================= */
      container.innerHTML = "";
      wrapper.appendChild(label);
      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:44, 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();
    });

  });
});