MediaWiki:Common.js: Difference between revisions

From TANGOWIKI-TITAF
No edit summary
No edit summary
 
Line 1: Line 1:
$(document).ready(function () {
$(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) {
   $('select').on('select2:select', function (e) {
     var selectedText = e.params.data.text;
     const $select = $(this);
     var match = selectedText.match(/\((TITAF-[^)]+)\)$/); // Extract page ID
    const selectedOption = e.params.data;
     const match = selectedOption.text.match(/\((TITAF-[PG]-\d{7})\)$/);
     if (match) {
     if (match) {
       var pageID = match[1];
       const id = match[1];
       var $select = $(this);
       const $newOption = new Option(id, id, true, true);
       var existingOptions = $select.find('option');
      $select.append($newOption).trigger('change');
 
       $select.find('option').filter(function () {
      // Remove the long label
         return this.text === selectedOption.text;
      existingOptions.each(function () {
       }).remove();
         if ($(this).text() === selectedText) {
          $(this).remove();
        }
       });
 
      // Add the cleaned ID
      var newOption = new Option(pageID, pageID, true, true);
      $select.append(newOption).trigger('change');
     }
     }
   });
   });
});
});

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