MediaWiki:Common.js: Difference between revisions

From TANGOWIKI-TITAF
No edit summary
No edit summary
 
(One intermediate revision by the same user not shown)
Line 1: Line 1:
$(document).ready(function () {
$(document).ready(function () {
   $('select[name="Tune[Composer][is_list]"], select[name="Tune[Lyricist][is_list]"]').on('select2:select', function (e) {
   function cleanTokenSelections() {
     var selected = e.params.data.text;
    $('select').each(function () {
     var match = selected.match(/\((TITAF-[^)]+)\)$/); // extracts TITAF-P-xxxxxxx
      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) {
     if (match) {
       var newVal = match[1]; // TITAF-P-xxxxxxx
       const id = match[1];
       var newOption = new Option(newVal, newVal, true, true);
       const $newOption = new Option(id, id, true, true);
       $(this).append(newOption).trigger('change');
       $select.append($newOption).trigger('change');
       var oldOption = $(this).find('option').filter(function () {
       $select.find('option').filter(function () {
         return $(this).text() === selected;
         return this.text === selectedOption.text;
       });
       }).remove();
      oldOption.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();
    }
  });
});