From 8e3f3e9331cf13709850637d2a11b4a6b0ade8a9 Mon Sep 17 00:00:00 2001 From: Wolfsblvt Date: Mon, 22 Jul 2024 01:30:08 +0200 Subject: [PATCH 1/2] WI update "Order" from custom sorting button - Adds a button that automatically updates the "Order" values of entries based on the custom sorting order ("displayIndex") - Shows popup to choose starting value, because it's descending (default: 100) - shows warnings/errors if any issues appear with the value - warning inside popup if more than 100 entries exist, and a higher value has to be chosen - Implements #2533 --- public/index.html | 3 ++- public/scripts/world-info.js | 41 ++++++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+), 1 deletion(-) diff --git a/public/index.html b/public/index.html index 250fe0eae..949acd223 100644 --- a/public/index.html +++ b/public/index.html @@ -3630,7 +3630,8 @@ - + diff --git a/public/scripts/world-info.js b/public/scripts/world-info.js index 99ac56f2b..ae9f2819d 100644 --- a/public/scripts/world-info.js +++ b/public/scripts/world-info.js @@ -1931,6 +1931,47 @@ function displayWorldEntries(name, data, navigation = navigation_option.none, fl } }); + $('#world_apply_custom_sorting').off('click').on('click', async () => { + const entryCount = Object.keys(data.entries).length; + const moreThan100 = entryCount > 100; + + let content = 'Apply your custom sorting to the "Order" field. The Order values will go down from the chosen number.'; + if (moreThan100) { + content += `
More than 100 entries in this world. You have to choose a number higher than that to work.
(Usual default: 100)
Minimum: ${entryCount}
`; + } + + const result = await Popup.show.input('Apply Custom Sorting', content, moreThan100 ? '' : '100', { okButton: 'Apply', cancelButton: 'Cancel' }); + if (!result) return; + + const start = Number(result); + if (isNaN(start) || start < 0) { + toastr.error('Invalid number: ' + result, 'Apply Custom Sorting'); + return; + } + if (start < entryCount) { + toastr.warning('The number must be higher than the total entry count: ' + entryCount, 'Apply Custom Sorting'); + return; + } + + let counter = 0; + for (const entry of Object.values(data.entries)) { + const newOrder = start - (entry.displayIndex ?? 0); + if (entry.order === newOrder) continue; + + entry.order = newOrder; + setOriginalDataValue(data, entry.order, 'order', entry.order); + counter++; + } + + if (counter > 0) { + toastr.info(`Updated ${counter} Order values`, 'Apply Custom Sorting'); + await saveWorldInfo(name, data, true); + updateEditor(navigation_option.previous); + } else { + toastr.info('All values up to date', 'Apply Custom Sorting'); + } + }); + $('#world_popup_export').off('click').on('click', () => { if (name && data) { const jsonValue = JSON.stringify(data); From 5ef6315b25b244275cdb9662cb34253734cea39f Mon Sep 17 00:00:00 2001 From: Wolfsblvt Date: Mon, 22 Jul 2024 01:42:54 +0200 Subject: [PATCH 2/2] Allow values below entry count, just warn --- public/scripts/world-info.js | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/public/scripts/world-info.js b/public/scripts/world-info.js index ae9f2819d..df432f875 100644 --- a/public/scripts/world-info.js +++ b/public/scripts/world-info.js @@ -1937,10 +1937,10 @@ function displayWorldEntries(name, data, navigation = navigation_option.none, fl let content = 'Apply your custom sorting to the "Order" field. The Order values will go down from the chosen number.'; if (moreThan100) { - content += `
More than 100 entries in this world. You have to choose a number higher than that to work.
(Usual default: 100)
Minimum: ${entryCount}
`; + content += `
More than 100 entries in this world. If you don't choose a number higher than that, the lower entries will default to 0.
(Usual default: 100)
Minimum: ${entryCount}
`; } - const result = await Popup.show.input('Apply Custom Sorting', content, moreThan100 ? '' : '100', { okButton: 'Apply', cancelButton: 'Cancel' }); + const result = await Popup.show.input('Apply Custom Sorting', content, '100', { okButton: 'Apply', cancelButton: 'Cancel' }); if (!result) return; const start = Number(result); @@ -1949,13 +1949,12 @@ function displayWorldEntries(name, data, navigation = navigation_option.none, fl return; } if (start < entryCount) { - toastr.warning('The number must be higher than the total entry count: ' + entryCount, 'Apply Custom Sorting'); - return; + toastr.warning('A number lower than the entry count has been chosen. All entries below that will default to 0.', 'Apply Custom Sorting'); } let counter = 0; for (const entry of Object.values(data.entries)) { - const newOrder = start - (entry.displayIndex ?? 0); + const newOrder = Math.max(start - (entry.displayIndex ?? 0), 0); if (entry.order === newOrder) continue; entry.order = newOrder;