From f7decb885d1b44c557aaf1016a72fcb5fb270432 Mon Sep 17 00:00:00 2001 From: TheLastScrub <67403321+TheLastScrub@users.noreply.github.com> Date: Mon, 29 Jul 2024 10:43:52 -0700 Subject: [PATCH] allow toggling sort from manual to alphabetical and back for items --- module/actor/actor-sheet.js | 84 +++++++++++++++++++++++--- template.json | 14 ++++- templates/actor/equipment-partial.html | 26 ++++++-- 3 files changed, 108 insertions(+), 16 deletions(-) diff --git a/module/actor/actor-sheet.js b/module/actor/actor-sheet.js index 688cae3..e55a662 100644 --- a/module/actor/actor-sheet.js +++ b/module/actor/actor-sheet.js @@ -161,17 +161,59 @@ export default class DeltaGreenActorSheet extends ActorSheet { } } - armor.sort(function (a, b) { - return a.sort - b.sort; - }); + if (actorData.system.sorting.armorSortAlphabetical) { + armor.sort(function (a, b) { + let x = a.name.toLowerCase(); + let y = b.name.toLowerCase(); + if (x < y) { + return -1; + } + if (x > y) { + return 1; + } + return 0; + }); + } else { + armor.sort(function (a, b) { + return a.sort - b.sort; + }); + } - weapons.sort(function (a, b) { - return a.sort - b.sort; - }); + if (actorData.system.sorting.weaponSortAlphabetical) { + weapons.sort(function (a, b) { + let x = a.name.toLowerCase(); + let y = b.name.toLowerCase(); + if (x < y) { + return -1; + } + if (x > y) { + return 1; + } + return 0; + }); + } else { + weapons.sort(function (a, b) { + return a.sort - b.sort; + }); + } - gear.sort(function (a, b) { - return a.sort - b.sort; - }); + if (actorData.system.sorting.gearSortAlphabetical) { + gear.sort(function (a, b) { + let x = a.name.toLowerCase(); + let y = b.name.toLowerCase(); + if (x < y) { + return -1; + } + if (x > y) { + return 1; + } + return 0; + }); + } else { + gear.sort(function (a, b) { + return a.sort - b.sort; + }); + } // Assign and return actorData.armor = armor; @@ -502,6 +544,30 @@ export default class DeltaGreenActorSheet extends ActorSheet { html.find(".clear-all-bond-damage-checks").click((ev) => { this._updateBondsRemoveAllDamagedCheckmarks(); }); + + // item sorting toggles + html.find(".toggle-item-sorting-style").click((event) => { + event.preventDefault(); + + const itemType = event.currentTarget.getAttribute("data-gear-type"); + + if (itemType === "weapon") { + this.actor.update({ + "system.sorting.weaponSortAlphabetical": + !this.actor.system.sorting.weaponSortAlphabetical, + }); + } else if (itemType === "armor") { + this.actor.update({ + "system.sorting.armorSortAlphabetical": + !this.actor.system.sorting.armorSortAlphabetical, + }); + } else if (itemType === "gear") { + this.actor.update({ + "system.sorting.gearSortAlphabetical": + !this.actor.system.sorting.gearSortAlphabetical, + }); + } + }); } // trigger an update on all bonds that have had their damaged flag checked off diff --git a/template.json b/template.json index 611042d..feec250 100644 --- a/template.json +++ b/template.json @@ -40,6 +40,13 @@ } } }, + "item-sorting": { + "sorting": { + "weaponSortAlphabetical": false, + "armorSortAlphabetical": false, + "gearSortAlphabetical": false + } + }, "human_skills": { "skills": { "accounting": { @@ -429,7 +436,7 @@ } }, "unnatural": { - "templates": ["base", "unnatural_skills"], + "templates": ["base", "unnatural_skills", "item-sorting"], "sanity": { "notes": "", "failedLoss": "1D4", @@ -441,7 +448,7 @@ "showUntrainedSkills": true }, "npc": { - "templates": ["base", "human_skills"], + "templates": ["base", "human_skills", "item-sorting"], "schemaVersion": 1.0, "sanity": { "value": 100, @@ -452,6 +459,7 @@ "showUntrainedSkills": true }, "vehicle": { + "templates": ["item-sorting"], "name": "", "description": "", "shortDescription": "", @@ -465,7 +473,7 @@ "passengers": [] }, "agent": { - "templates": ["base", "human_skills"], + "templates": ["base", "human_skills", "item-sorting"], "schemaVersion": 1.0, "sanity": { "value": 100, diff --git a/templates/actor/equipment-partial.html b/templates/actor/equipment-partial.html index 67c07f0..cc80c0a 100644 --- a/templates/actor/equipment-partial.html +++ b/templates/actor/equipment-partial.html @@ -9,9 +9,15 @@
  1. -
    +
    + {{#if actor.system.sorting.weaponSortAlphabetical}} + + {{else}} + + {{/if}} +
    + - {{localize 'DG.Gear.Name'}} @@ -100,7 +106,13 @@
  2. -
    +
    + {{#if actor.system.sorting.armorSortAlphabetical}} + + {{else}} + + {{/if}} +
    {{localize 'DG.Gear.Name'}}
    {{localize 'DG.Gear.ArmorRating'}} {{localize 'DG.Gear.Equipped'}} @@ -152,7 +164,13 @@
  3. -
    +
    + {{#if actor.system.sorting.gearSortAlphabetical}} + + {{else}} + + {{/if}} +
    {{localize 'DG.Gear.Name'}}