Improve performance on fancy key selector init

This commit is contained in:
Wolfsblvt 2024-08-05 01:06:37 +02:00
parent 4966139fd1
commit 3523ec334d
2 changed files with 12 additions and 7 deletions

View File

@ -1728,20 +1728,24 @@ export function select2ModifyOptions(element, items, { select = false, changeEve
/** @type {Select2Option[]} */
const dataItems = items.map(x => typeof x === 'string' ? { id: getSelect2OptionId(x), text: x } : x);
const existingValues = [];
const optionsToSelect = [];
const newOptions = [];
dataItems.forEach(item => {
// Set the value, creating a new option if necessary
if (element.find('option[value=\'' + item.id + '\']').length) {
if (select) existingValues.push(item.id);
if (select) optionsToSelect.push(item.id);
} else {
// Create a DOM Option and optionally pre-select by default
var newOption = new Option(item.text, item.id, select, select);
// Append it to the select
element.append(newOption);
if (select) element.trigger('change', changeEventArgs);
newOptions.push(newOption);
if (select) optionsToSelect.push(item.id);
}
if (existingValues.length) element.val(existingValues).trigger('change', changeEventArgs);
});
element.append(newOptions);
if (optionsToSelect.length) element.val(optionsToSelect).trigger('change', changeEventArgs);
}
/**

View File

@ -2293,6 +2293,9 @@ function getWorldEntry(name, data, entry) {
}
if (isFancyInput) {
// First initialize existing values as options, before initializing select2, to speed up performance
select2ModifyOptions(input, entry[entryPropName], { select: true, changeEventArgs: { skipReset: true, noSave: true } });
input.select2({
ajax: dynamicSelect2DataViaAjax(() => worldEntryKeyOptionsCache),
tags: true,
@ -2334,8 +2337,6 @@ function getWorldEntry(name, data, entry) {
input.next('span.select2-container').find('textarea')
.val(key).trigger('input');
}, { openDrawer: true });
select2ModifyOptions(input, entry[entryPropName], { select: true, changeEventArgs: { skipReset: true, noSave: true } });
}
else {
// Compatibility with mobile devices. On mobile we need a text input field, not a select option control, so we need its own event handlers