| 1939 | } | 1939 | } |
| 1940 | }); | 1940 | }); |
| 1941 | | 1941 | |
| 1942 | $('#world_apply_custom_sorting').off('click').on('click', async () => { | 1942 | $('#world_apply_current_sorting').off('click').on('click', async () => { |
| 1943 | const entryCount = Object.keys(data.entries).length; | 1943 | const entryCount = Object.keys(data.entries).length; |
| 1944 | const moreThan100 = entryCount > 100; | 1944 | const moreThan100 = entryCount > 100; |
| 1945 | | 1945 | |
| 1946 | let content = '<span>Apply your custom sorting to the "Order" field. The Order values will go down from the chosen number.</span>'; | 1946 | let content = '<span>Apply your current sorting to the "Order" field. The Order values will go down from the chosen number.</span>'; |
| 1947 | if (moreThan100) { | 1947 | if (moreThan100) { |
| 1948 | content += `<div class="m-t-1"><i class="fa-solid fa-triangle-exclamation" style="color: #FFD43B;"></i> More than 100 entries in this world. If you don't choose a number higher than that, the lower entries will default to 0.<br />(Usual default: 100)<br />Minimum: ${entryCount}</div>`; | 1948 | content += `<div class="m-t-1"><i class="fa-solid fa-triangle-exclamation" style="color: #FFD43B;"></i> More than 100 entries in this world. If you don't choose a number higher than that, the lower entries will default to 0.<br />(Usual default: 100)<br />Minimum: ${entryCount}</div>`; |
| 1949 | } | 1949 | } |
| 1950 | | 1950 | |
| 1951 | const result = await Popup.show.input('Apply Custom Sorting', content, '100', { okButton: 'Apply', cancelButton: 'Cancel' }); | 1951 | const result = await Popup.show.input('Apply Current Sorting', content, '100', { okButton: 'Apply', cancelButton: 'Cancel' }); |
| 1952 | if (!result) return; | 1952 | if (!result) return; |
| 1953 | | 1953 | |
| 1954 | const start = Number(result); | 1954 | const start = Number(result); |
| 1955 | if (isNaN(start) || start < 0) { | 1955 | if (isNaN(start) || start < 0) { |
| 1956 | toastr.error('Invalid number: ' + result, 'Apply Custom Sorting'); | 1956 | toastr.error('Invalid number: ' + result, 'Apply Current Sorting'); |
| 1957 | return; | 1957 | return; |
| 1958 | } | 1958 | } |
| 1959 | if (start < entryCount) { | 1959 | if (start < entryCount) { |
| 1960 | toastr.warning('A number lower than the entry count has been chosen. All entries below that will default to 0.', 'Apply Custom Sorting'); | 1960 | toastr.warning('A number lower than the entry count has been chosen. All entries below that will default to 0.', 'Apply Current Sorting'); |
| 1961 | } | 1961 | } |
| 1962 | | 1962 | |
| 1963 | let counter = 0; | 1963 | // We need to sort the entries here, as the data source isn't sorted |
| 1964 | for (const entry of Object.values(data.entries)) { | 1964 | const entries = Object.values(data.entries); |
| 1965 | const newOrder = Math.max(start - (entry.displayIndex ?? 0), 0); | 1965 | sortEntries(entries); |
| | 1966 | |
| | 1967 | let updated = 0, current = start; |
| | 1968 | for (const entry of entries) { |
| | 1969 | const newOrder = Math.max(current--, 0); |
| 1966 | if (entry.order === newOrder) continue; | 1970 | if (entry.order === newOrder) continue; |
| 1967 | | 1971 | |
| 1968 | entry.order = newOrder; | 1972 | entry.order = newOrder; |
| 1969 | setOriginalDataValue(data, entry.order, 'order', entry.order); | 1973 | setOriginalDataValue(data, entry.order, 'order', entry.order); |
| 1970 | counter++; | 1974 | updated++; |
| 1971 | } | 1975 | } |
| 1972 | | 1976 | |
| 1973 | if (counter > 0) { | 1977 | if (updated > 0) { |
| 1974 | toastr.info(`Updated ${counter} Order values`, 'Apply Custom Sorting'); | 1978 | toastr.info(`Updated ${updated} Order values`, 'Apply Custom Sorting'); |
| 1975 | await saveWorldInfo(name, data, true); | 1979 | await saveWorldInfo(name, data, true); |
| 1976 | updateEditor(navigation_option.previous); | 1980 | updateEditor(navigation_option.previous); |
| 1977 | } else { | 1981 | } else { |