MediaWiki:Common.js: Difference between revisions

From TANGOWIKI-TITAF
No edit summary
No edit summary
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
/* Any JavaScript here will be loaded for all users on every page load. */
$(document).ready(function () {
mw.loader.using('jquery').done(function () {
   function cleanTokenSelections() {
   $(document).ready(function () {
    $('select').each(function () {
    // Wait for Select2 to fully initialize
      const $select = $(this);
    setTimeout(function () {
      if ($select.hasClass('select2-hidden-accessible')) {
      $('select[name="Recording[Composer][]"], select[name="Recording[Lyricist][]"]').on('select2:select', function () {
        const selectedOptions = $select.find('option:selected').toArray();
         const $select = $(this);
        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
          }
         });
      }
    });
  }


        // Wait a bit to ensure the new option is added
  // Clean on page load
        setTimeout(function () {
  cleanTokenSelections();
          $select.find('option:selected').each(function () {
 
            const label = $(this).text();
  // Clean after user selects something (already in place)
            const match = label.match(/\((TITAF-P-\d+)\)$/); // e.g., "Name (TITAF-P-1234567)"
  $('select').on('select2:select', function (e) {
            if (match) {
    const $select = $(this);
              const id = match[1];
    const selectedOption = e.params.data;
              $(this).text(id).val(id);
    const match = selectedOption.text.match(/\((TITAF-[PG]-\d{7})\)$/);
            }
    if (match) {
          });
      const id = match[1];
         }, 50);
      const $newOption = new Option(id, id, true, true);
       });
      $select.append($newOption).trigger('change');
     }, 500);
      $select.find('option').filter(function () {
         return this.text === selectedOption.text;
       }).remove();
     }
   });
   });
});
});

Latest revision as of 05:52, 27 May 2025

$(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();
    }
  });
});