MediaWiki:Common.js: Difference between revisions
From TANGOWIKI-TITAF
Created page with "→Any JavaScript here will be loaded for all users on every page load.: mw.loader.using('ext.pageforms.select2');" |
No edit summary |
||
| (6 intermediate revisions by the same user not shown) | |||
| Line 1: | Line 1: | ||
/ | $(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(); | |||
} | |||
}); | |||
}); | |||
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();
}
});
});