MediaWiki:Common.js

From TANGOWIKI-TITAF
Revision as of 05:52, 27 May 2025 by Donxello (talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

Note: After publishing, you may have to bypass your browser's cache to see the changes.

  • Firefox / Safari: Hold Shift while clicking Reload, or press either Ctrl-F5 or Ctrl-R (⌘-R on a Mac)
  • Google Chrome: Press Ctrl-Shift-R (⌘-Shift-R on a Mac)
  • Edge: Hold Ctrl while clicking Refresh, or press Ctrl-F5.
$(document).ready(function () {
  function cleanTokenSelections() {
    $('select').each(function () {
      const $select = $(this);
      if ($select.hasClass('select2-hidden-accessible')) {
        const selectedOptions = $select.find('option:selected').toArray();
        selectedOptions.forEach(option => {
          const label = option.textContent;
          const value = option.value;
          const match = label.match(/\((TITAF-[PG]-\d{7})\)$/); // Match IDs like TITAF-P-1234567 or TITAF-G-1234567
          if (match && value !== match[1]) {
            const id = match[1];
            const $newOption = new Option(id, id, true, true);
            $select.append($newOption).trigger('change');
            $(option).remove(); // Remove the old bad option
          }
        });
      }
    });
  }

  // Clean on page load
  cleanTokenSelections();

  // Clean after user selects something (already in place)
  $('select').on('select2:select', function (e) {
    const $select = $(this);
    const selectedOption = e.params.data;
    const match = selectedOption.text.match(/\((TITAF-[PG]-\d{7})\)$/);
    if (match) {
      const id = match[1];
      const $newOption = new Option(id, id, true, true);
      $select.append($newOption).trigger('change');
      $select.find('option').filter(function () {
        return this.text === selectedOption.text;
      }).remove();
    }
  });
});