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

8e3f3e9331cf13709850637d2a11b4a6b0ade8a9

Wolfsblvt <wolfsblvt@gmail.com>

2 files changed, +43 -1Ignore whitespace
public/index.html+2 -1
@@ -3630,7 +3630,8 @@
36303630 <div id="OpenAllWIEntries" class="menu_button fa-solid fa-expand" title="Open all Entries" data-i18n="[title]Open all Entries"></div>
36313631 <div id="CloseAllWIEntries" class="menu_button fa-solid fa-compress" title="Close all Entries" data-i18n="[title]Close all Entries"></div>
36323632 <div id="world_popup_new" class="menu_button fa-solid fa-plus" title="New Entry" data-i18n="[title]New Entry"></div>
36333633 <div id="world_backfill_memos" class="menu_button fa-solid fa-notes-medical" title="Fill empty Memo/Titles with Keywords" data-i18n="[title]Fill empty Memo/Titles with Keywords"></div><div id="world_apply_custom_sorting" class="menu_button fa-solid fa-solid fa-arrow-down-9-1"
3634+ title="Apply custom sorting as Order" data-i18n="[title]Apply custom sorting as Order"></div>
36343635 <div id="world_import_button" class="menu_button fa-solid fa-file-import" title="Import World Info" data-i18n="[title]Import World Info"></div>
36353636 <div id="world_popup_export" class="menu_button fa-solid fa-file-export" title="Export World Info" data-i18n="[title]Export World Info"></div>
36363637 <div id="world_duplicate" class="menu_button fa-solid fa-paste" title="Duplicate World Info" data-i18n="[title]Duplicate World Info"></div>
public/scripts/world-info.js+41 -0
@@ -1931,6 +1931,47 @@ function displayWorldEntries(name, data, navigation = navigation_option.none, fl
19311931 }
19321932 });
19331933
1934+ $('#world_apply_custom_sorting').off('click').on('click', async () => {
1935+ const entryCount = Object.keys(data.entries).length;
1936+ const moreThan100 = entryCount > 100;
1937+
1938+ let content = '<span>Apply your custom sorting to the "Order" field. The Order values will go down from the chosen number.</span>';
1939+ if (moreThan100) {
1940+ content += `<div class="m-t-1"><i class="fa-solid fa-triangle-exclamation" style="color: #FFD43B;"></i> More than 100 entries in this world. You have to choose a number higher than that to work.<br />(Usual default: 100)<br />Minimum: ${entryCount}</div>`;
1941+ }
1942+
1943+ const result = await Popup.show.input('Apply Custom Sorting', content, moreThan100 ? '' : '100', { okButton: 'Apply', cancelButton: 'Cancel' });
1944+ if (!result) return;
1945+
1946+ const start = Number(result);
1947+ if (isNaN(start) || start < 0) {
1948+ toastr.error('Invalid number: ' + result, 'Apply Custom Sorting');
1949+ return;
1950+ }
1951+ if (start < entryCount) {
1952+ toastr.warning('The number must be higher than the total entry count: ' + entryCount, 'Apply Custom Sorting');
1953+ return;
1954+ }
1955+
1956+ let counter = 0;
1957+ for (const entry of Object.values(data.entries)) {
1958+ const newOrder = start - (entry.displayIndex ?? 0);
1959+ if (entry.order === newOrder) continue;
1960+
1961+ entry.order = newOrder;
1962+ setOriginalDataValue(data, entry.order, 'order', entry.order);
1963+ counter++;
1964+ }
1965+
1966+ if (counter > 0) {
1967+ toastr.info(`Updated ${counter} Order values`, 'Apply Custom Sorting');
1968+ await saveWorldInfo(name, data, true);
1969+ updateEditor(navigation_option.previous);
1970+ } else {
1971+ toastr.info('All values up to date', 'Apply Custom Sorting');
1972+ }
1973+ });
1974+
19341975 $('#world_popup_export').off('click').on('click', () => {
19351976 if (name && data) {
19361977 const jsonValue = JSON.stringify(data);