Jump to content

MediaWiki:Common.js: Difference between revisions

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


     /* ================= DROPDOWN FIX (THIS WAS MISSING) ================= */
     /* ================= DROPDOWN SYSTEM ================= */


     document.querySelectorAll('.mw-dropdown-ui').forEach(container => {
     document.querySelectorAll('.mw-dropdown-ui').forEach(container => {
Line 42: Line 42:
         const options = JSON.parse(container.dataset.options || "{}");
         const options = JSON.parse(container.dataset.options || "{}");


         const select = document.createElement('select');
        // 🔥 SORT ALPHABETICALLY
         const sortedKeys = Object.keys(options).sort((a, b) =>
          options[a].label.localeCompare(options[b].label)
        );


         const first = document.createElement('option');
         const wrapper = document.createElement('div');
         first.textContent = label;
         wrapper.className = "mod-dropdown";
        first.value = "";
        select.appendChild(first);


         Object.keys(options).forEach(key => {
         const button = document.createElement('button');
          const opt = document.createElement('option');
        button.className = "mod-dropdown-btn";
          opt.value = key;
        button.textContent = label;
          opt.textContent = options[key].label;
 
          select.appendChild(opt);
        const list = document.createElement('div');
         });
         list.className = "mod-dropdown-list";


         const output = document.createElement('div');
         const output = document.createElement('div');
         output.className = 'mw-dropdown-output';
         output.className = 'mw-dropdown-output';


         select.onchange = function () {
         sortedKeys.forEach(key => {
          const selected = options[this.value];
          const item = document.createElement('div');
          output.innerHTML = selected ? selected.content : "";
          item.className = "mod-dropdown-item";
          item.textContent = options[key].label;
 
          item.onclick = function () {
            button.textContent = options[key].label;
            output.innerHTML = options[key].content;
            list.style.display = "none";
          };
 
          list.appendChild(item);
        });
 
        button.onclick = function () {
          list.style.display =
            list.style.display === "block" ? "none" : "block";
         };
         };
        wrapper.appendChild(button);
        wrapper.appendChild(list);


         container.innerHTML = '';
         container.innerHTML = '';
         container.appendChild(select);
         container.appendChild(wrapper);
         container.appendChild(output);
         container.appendChild(output);


Line 72: Line 90:
       }
       }


    });
  });
});
/* ================= SEARCH FILTER FOR DROPDOWNS ================= */
document.querySelectorAll('.trait-search').forEach(input => {
  input.addEventListener('input', function () {
    const value = this.value.toLowerCase();
    const dropdown = this.parentElement.querySelector('select');
    if (!dropdown) return;
    Array.from(dropdown.options).forEach(opt => {
      if (opt.value === "") return;
      opt.style.display =
        opt.text.toLowerCase().includes(value) ? "" : "none";
     });
     });


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

Revision as of 20:34, 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;

          let container =
            this.closest('.mw-tabs') ||
            this.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 ================= */

    document.querySelectorAll('.mw-dropdown-ui').forEach(container => {

      try {
        const label = container.dataset.label || "Select";
        const options = JSON.parse(container.dataset.options || "{}");

        // 🔥 SORT ALPHABETICALLY
        const sortedKeys = Object.keys(options).sort((a, b) =>
          options[a].label.localeCompare(options[b].label)
        );

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

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

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

        const output = document.createElement('div');
        output.className = 'mw-dropdown-output';

        sortedKeys.forEach(key => {
          const item = document.createElement('div');
          item.className = "mod-dropdown-item";
          item.textContent = options[key].label;

          item.onclick = function () {
            button.textContent = options[key].label;
            output.innerHTML = options[key].content;
            list.style.display = "none";
          };

          list.appendChild(item);
        });

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

        wrapper.appendChild(button);
        wrapper.appendChild(list);

        container.innerHTML = '';
        container.appendChild(wrapper);
        container.appendChild(output);

      } catch (e) {
        console.error("Dropdown error:", e);
      }

    });

  });
});