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 () {
   $('select[name="Tune[Composer][is_list]"], select[name="Tune[Lyricist][is_list]"]').on('select2:select', function (e) {
   $('select').on('select2:select', function (e) {
     var selected = e.params.data.text;
     var selectedText = e.params.data.text;
     var match = selected.match(/\((TITAF-[^)]+)\)$/); // extracts TITAF-P-xxxxxxx
     var match = selectedText.match(/\((TITAF-[^)]+)\)$/); // Extract page ID
     if (match) {
     if (match) {
       var newVal = match[1]; // TITAF-P-xxxxxxx
       var pageID = match[1];
       var newOption = new Option(newVal, newVal, true, true);
       var $select = $(this);
      $(this).append(newOption).trigger('change');
       var existingOptions = $select.find('option');
       var oldOption = $(this).find('option').filter(function () {
 
         return $(this).text() === selected;
      // Remove the long label
      existingOptions.each(function () {
         if ($(this).text() === selectedText) {
          $(this).remove();
        }
       });
       });
       oldOption.remove();
 
       // Add the cleaned ID
      var newOption = new Option(pageID, pageID, true, true);
      $select.append(newOption).trigger('change');
     }
     }
   });
   });
});
});

Revision as of 21:07, 26 May 2025

$(document).ready(function () {
  $('select').on('select2:select', function (e) {
    var selectedText = e.params.data.text;
    var match = selectedText.match(/\((TITAF-[^)]+)\)$/); // Extract page ID
    if (match) {
      var pageID = match[1];
      var $select = $(this);
      var existingOptions = $select.find('option');

      // Remove the long label
      existingOptions.each(function () {
        if ($(this).text() === selectedText) {
          $(this).remove();
        }
      });

      // Add the cleaned ID
      var newOption = new Option(pageID, pageID, true, true);
      $select.append(newOption).trigger('change');
    }
  });
});