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 () {


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


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


       select.addEventListener('change', function () {
       select.addEventListener('change', function () {
Line 15: Line 17:
         if (target) target.style.display = 'block';
         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";
     });
     });


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

Revision as of 21:52, 21 April 2026

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

    console.log("BASELINE JS OK");

    // ORIGINAL WORKING SELECT DROPDOWNS
    document.querySelectorAll('.mw-dropdown-ui').forEach(container => {
      const select = container.querySelector('select');
      const sections = container.querySelectorAll('.dropdown-section');

      if (!select) return;

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

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

  });
});