Jump to content

MediaWiki:Common.js: Difference between revisions

From Once Human Guide
No edit summary
No edit summary
Line 24: Line 24:
     });
     });


     /* ================= DROPDOWNS ================= */
     /* ================= DROPDOWN SYSTEM ================= */


     let selectedLeft = null;
     let selectedLeft = null;
Line 30: Line 30:


     function createDropdown(container, label, options, side) {
     function createDropdown(container, label, options, side) {
       const wrapper = document.createElement('div');
       const wrapper = document.createElement('div');
      wrapper.className = 'custom-dropdown';


       const button = document.createElement('button');
       const button = document.createElement('button');
      button.className = 'custom-dropdown-btn';
       button.textContent = label;
       button.textContent = label;


       const list = document.createElement('div');
       let list = null; // 🔥 only create when clicked
      list.className = 'custom-dropdown-list';
 
      button.onclick = function () {
 
        // remove existing open dropdowns
        document.querySelectorAll('.dropdown-list').forEach(el => el.remove());


      // 🔥 CRITICAL FIX — remove from layout completely
        list = document.createElement('div');
      list.style.position = "absolute";
        list.className = 'dropdown-list';
      list.style.top = "100%";
      list.style.left = "0";
      list.style.display = "none";


      Object.keys(options).forEach(key => {
        Object.keys(options).forEach(key => {
        const item = document.createElement('div');
          const item = document.createElement('div');
        item.className = 'custom-dropdown-item';
          item.textContent = options[key];
        item.textContent = options[key];


        item.onclick = function () {
          item.onclick = function () {
          button.textContent = options[key];
            button.textContent = options[key];


          if (side === 'left') selectedLeft = key;
            if (side === 'left') selectedLeft = key;
          if (side === 'right') selectedRight = key;
            if (side === 'right') selectedRight = key;


          list.style.display = "none";
            list.remove();
          updateResult();
            updateResult();
        };
          };


        list.appendChild(item);
          list.appendChild(item);
      });
        });


      button.onclick = function () {
         wrapper.appendChild(list);
         list.style.display = (list.style.display === "none") ? "block" : "none";
       };
       };


       wrapper.appendChild(button);
       wrapper.appendChild(button);
      wrapper.appendChild(list);
       container.innerHTML = '';
       container.innerHTML = '';
       container.appendChild(wrapper);
       container.appendChild(wrapper);

Revision as of 18:49, 21 April 2026

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

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

      buttons.forEach(btn => {
        btn.onclick = function () {
          const tabId = this.dataset.tab;
          const container = group.closest('.mw-tab-content') || document;

          container.querySelectorAll('.mw-tab-content').forEach(c => c.style.display = 'none');
          container.querySelectorAll('.mw-tab-btn').forEach(b => b.classList.remove('active'));

          this.classList.add('active');

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

      if (buttons.length) buttons[0].click();
    });

    /* ================= DROPDOWN SYSTEM ================= */

    let selectedLeft = null;
    let selectedRight = null;

    function createDropdown(container, label, options, side) {
      const wrapper = document.createElement('div');

      const button = document.createElement('button');
      button.textContent = label;

      let list = null; // 🔥 only create when clicked

      button.onclick = function () {

        // remove existing open dropdowns
        document.querySelectorAll('.dropdown-list').forEach(el => el.remove());

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

        Object.keys(options).forEach(key => {
          const item = document.createElement('div');
          item.textContent = options[key];

          item.onclick = function () {
            button.textContent = options[key];

            if (side === 'left') selectedLeft = key;
            if (side === 'right') selectedRight = key;

            list.remove();
            updateResult();
          };

          list.appendChild(item);
        });

        wrapper.appendChild(list);
      };

      wrapper.appendChild(button);
      container.innerHTML = '';
      container.appendChild(wrapper);
    }

    function updateResult() {
      const result = document.querySelector('[data-combo-result]');
      if (!result) return;

      if (!selectedLeft || !selectedRight) {
        result.style.display = "none";
        return;
      }

      result.style.display = "block";

      const key = selectedLeft + "|" + selectedRight;
      const data = JSON.parse(result.dataset.combo || "{}");

      result.innerHTML = data[key] || "No data for this combination.";
    }

    document.querySelectorAll('[data-dropdown-group]').forEach(group => {
      const left = group.querySelector('[data-left]');
      const right = group.querySelector('[data-right]');

      if (left) createDropdown(left, left.dataset.label, JSON.parse(left.dataset.options), "left");
      if (right) createDropdown(right, right.dataset.label, JSON.parse(right.dataset.options), "right");
    });

  });
});