Customizable sorting order for characters list

This commit is contained in:
SillyLossy
2023-04-10 17:48:17 +03:00
parent d1fb575243
commit defa7f357e
5 changed files with 79 additions and 15 deletions

View File

@ -1,9 +1,10 @@
import { saveSettingsDebounced } from "../script.js";
import { saveSettingsDebounced, characters } from "../script.js";
export {
loadPowerUserSettings,
collapseNewlines,
playMessageSound,
sortCharactersList,
power_user,
};
@ -38,6 +39,8 @@ let power_user = {
sheld_width: sheld_width.DEFAULT,
play_message_sound: false,
play_sound_unfocused: true,
sort_field: 'name',
sort_order: 'asc',
};
const storage_keys = {
@ -156,6 +159,25 @@ function loadPowerUserSettings(settings) {
$(`input[name="avatar_style"][value="${power_user.avatar_style}"]`).prop("checked", true);
$(`input[name="chat_display"][value="${power_user.chat_display}"]`).prop("checked", true);
$(`input[name="sheld_width"][value="${power_user.sheld_width}"]`).prop("checked", true);
$(`#character_sort_order option[data-order="${power_user.sort_order}"][data-field="${power_user.sort_field}"]`).prop("selected", true);
sortCharactersList();
}
function sortCharactersList() {
const sortFunc = (a, b) => power_user.sort_order == 'asc' ? compareFunc(a, b) : compareFunc(b, a);
const compareFunc = (first, second) => typeof first[power_user.sort_field] == "string"
? first[power_user.sort_field].localeCompare(second[power_user.sort_field])
: first[power_user.sort_field] - second[power_user.sort_field];
if (power_user.sort_field == undefined || characters.length === 0) {
return;
}
let orderedList = characters.slice().sort(sortFunc);
for (let i = 0; i < characters.length; i++) {
$(`.character_select[chid="${i}"]`).css({ 'order': orderedList.indexOf(characters[i]) });
}
}
$(document).ready(() => {
@ -241,6 +263,13 @@ $(document).ready(() => {
saveSettingsDebounced();
});
$("#character_sort_order").on('change', function () {
power_user.sort_field = $(this).find(":selected").data('field');
power_user.sort_order = $(this).find(":selected").data('order');
sortCharactersList();
saveSettingsDebounced();
});
$(window).on('focus', function() {
browser_has_focus = true;
});