Jump to content

MediaWiki:Common.js: Difference between revisions

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


     /* ================= TAB SYSTEM ================= */
     console.log("COMMON JS SAFE MODE");


     document.querySelectorAll('.mw-tab-buttons').forEach(tabGroup => {
    /* ===== SELECT DROPDOWNS (OLD SYSTEM) ===== */
     document.querySelectorAll('.mw-dropdown-ui select').forEach(select => {
      const container = select.closest('.mw-dropdown-ui');
      const sections = container.querySelectorAll('.dropdown-section');


       const buttons = tabGroup.querySelectorAll('.mw-tab-btn');
       select.addEventListener('change', function () {
        sections.forEach(s => s.style.display = 'none');


      buttons.forEach(btn => {
        const target = container.querySelector('#' + this.value);
        btn.addEventListener('click', function () {
        if (target) target.style.display = 'block';
 
      });
          const tabId = this.getAttribute('data-tab');
    });
 
          // deactivate buttons in this group only
          buttons.forEach(b => b.classList.remove('active'));
          this.classList.add('active');


          // find container
          const container = tabGroup.parentElement;


          // hide direct tab contents
    /* ===== DATA-OPTIONS DROPDOWNS (NEW SYSTEM) ===== */
          container.querySelectorAll(':scope > .mw-tab-content').forEach(c => {
    document.querySelectorAll('.mw-dropdown-ui[data-options]').forEach(container => {
            c.classList.remove('active');
          });


          // show selected
      if (container.dataset.rendered) return;
          const target = container.querySelector('#' + tabId);
          if (target) target.classList.add('active');


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


    });
      const btn = document.createElement('div');
      btn.className = 'mod-dropdown-btn';
      btn.textContent = "Select One";


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


    /* ================= DROPDOWN SYSTEM ================= */
      const output = document.createElement('div');
      output.className = 'mw-dropdown-output';
      output.textContent = "Select something";


    document.querySelectorAll('.mw-dropdown-ui').forEach(container => {
      Object.values(data).forEach(item => {
      const select = container.querySelector('select');
        const option = document.createElement('div');
      const sections = container.querySelectorAll('.dropdown-section');
        option.className = 'mod-dropdown-item';
        option.textContent = item.label;


      if (!select) return;
        option.onclick = () => {
          btn.textContent = item.label;
          list.style.display = 'none';
          output.innerHTML = item.content;
        };


      select.addEventListener('change', function () {
         list.appendChild(option);
        sections.forEach(s => s.style.display = 'none');
 
         const target = container.querySelector('#' + this.value);
        if (target) target.style.display = 'block';
       });
       });
    });


      btn.onclick = () => {
        list.style.display = list.style.display === 'block' ? 'none' : 'block';
      };


    /* ================= INITIAL TAB LOAD ================= */
      container.appendChild(btn);
      container.appendChild(list);
      container.appendChild(output);


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


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

Revision as of 21:52, 21 April 2026

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

    console.log("COMMON JS SAFE MODE");

    /* ===== SELECT DROPDOWNS (OLD SYSTEM) ===== */
    document.querySelectorAll('.mw-dropdown-ui select').forEach(select => {
      const container = select.closest('.mw-dropdown-ui');
      const sections = container.querySelectorAll('.dropdown-section');

      select.addEventListener('change', function () {
        sections.forEach(s => s.style.display = 'none');

        const target = container.querySelector('#' + this.value);
        if (target) target.style.display = 'block';
      });
    });


    /* ===== DATA-OPTIONS DROPDOWNS (NEW SYSTEM) ===== */
    document.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 JSON:", e);
        return;
      }

      const btn = document.createElement('div');
      btn.className = 'mod-dropdown-btn';
      btn.textContent = "Select One";

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

      const output = document.createElement('div');
      output.className = 'mw-dropdown-output';
      output.textContent = "Select something";

      Object.values(data).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);
      });

      btn.onclick = () => {
        list.style.display = list.style.display === 'block' ? 'none' : 'block';
      };

      container.appendChild(btn);
      container.appendChild(list);
      container.appendChild(output);

      container.dataset.rendered = "true";
    });

  });
});