Skip to content

Commit

Permalink
close #2251
Browse files Browse the repository at this point in the history
fix inventory not being sorted
  • Loading branch information
moo-man committed Oct 8, 2024
1 parent 7936516 commit 2db5a60
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 2 deletions.
35 changes: 35 additions & 0 deletions src/apps/sheets/actor/base.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ export default class BaseWFRP4eActorSheet extends WarhammerActorSheetV2
convertCurrency : this._onConvertCurrency,
consolidateCurrency : this._onConsolidateCurrency,
collapseSection : this._onCollapseSection,
containerSort : this._onContainerSort,
createItem : this._onCreateItem,
configureActor : this._onConfigureActor,
useAspect : this._onUseAspect
Expand Down Expand Up @@ -625,6 +626,40 @@ export default class BaseWFRP4eActorSheet extends WarhammerActorSheetV2

this.actor.setFlag("wfrp4e", `sheetCollapsed.${section}`, !collapsed);
}

static async _onContainerSort(ev)
{
let direction = this._getParent(ev.target, "a").dataset.direction

let container = await this._getDocumentAsync(ev);

// All Containers on the same level as the sorted container
let containers = this.actor.itemTags.container.sort((a, b) => a.sort - b.sort).filter(i => i.system.location.value == container.system.location.value);

// Index of the sorted container
let index = containers.findIndex(i => i.id == container.id);

if ((index == 0 && direction == "up") || (index == containers.length - 1 && direction == "down"))
{
return;
}

// Index of the target container
let targetIndex = direction == "up" ? index - 1 : index + 1;
let target = containers[targetIndex];

// Remove sorted container
containers = containers.filter(i => i.id != container.id);

let sorted = SortingHelpers.performIntegerSort(container, {target, siblings: containers});
this.actor.updateEmbeddedDocuments("Item", sorted.map(s =>
{
return foundry.utils.mergeObject({
_id : s.target.id,
}, s.update);
}));

}

//#endregion
}
4 changes: 2 additions & 2 deletions src/apps/sheets/actor/standard-sheet.js
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ export default class StandardWFRP4eActorSheet extends BaseWFRP4eActorSheet
// All items referencing (inside) that container
var itemsInside = inContainers.filter(i => i.system.location.value == cont.id);
cont.system.carrying = itemsInside.filter(i => i.type != "container").sort((a, b) => a.sort - b.sort); // cont.system.carrying -> items the container is carrying
cont.system.packsInside = itemsInside.filter(i => i.type == "container").sort((a, b) => a.sort - b.sort);; // cont.system.packsInside -> containers the container is carrying
cont.system.packsInside = itemsInside.filter(i => i.type == "container").sort((a, b) => a.sort - b.sort); // cont.system.packsInside -> containers the container is carrying
cont.system.carries.current = itemsInside.reduce(function (prev, cur) { // cont.system.holding -> total encumbrance the container is holding
return Number(prev) + Number(cur.system.encumbrance.total);
}, 0);
Expand All @@ -316,7 +316,7 @@ export default class StandardWFRP4eActorSheet extends BaseWFRP4eActorSheet

_filterItemCategory(category, itemsInContainers) {
itemsInContainers = itemsInContainers.concat(category.items.filter(i => !!i.system.location?.value))
category.items = category.items.filter(i => !i.system.location?.value)
category.items = category.items.filter(i => !i.system.location?.value).sort((a, b) => a.sort - b.sort);
category.show = category.items.length > 0
return itemsInContainers
}
Expand Down
6 changes: 6 additions & 0 deletions static/templates/sheets/actor/tabs/actor-inventory.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,7 @@
<div>{{localize "Count Enc."}}</div>
<div>{{localize "Worn"}}</div>
<div class="small">{{localize "SHEET.EncAbbrev"}}</div>
<div class="small">{{localize "Sort"}}</div>

<div class="list-controls">
<a class="list-control" data-action="createItem" data-type="container"><i class="fas fa-plus"></i></a>
Expand Down Expand Up @@ -189,6 +190,11 @@
<a class="inactive disabled"><i class="far fa-circle"></i></a>
{{/if}}
<div class="small">{{this.system.encumbrance.total}}</div>
<div class="small">
<a data-action="containerSort" data-direction="up"><i class="fa-solid fa-arrow-up"></i></a>
<a data-action="containerSort" data-direction="down"><i class="fa-solid fa-arrow-down"></i></a>
</div>

<div class="list-controls">
{{#if this.system.collapsed}}
<a class="list-control" data-action="collapseSection" data-section="{{this.id}}" data-tooltip="{{localize 'SHEET.Expand'}}"><i class="fa-solid fa-chevron-down"></i></a>
Expand Down
4 changes: 4 additions & 0 deletions static/templates/sheets/partials/container-contents.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@
{{/if}} --}}

<div class="small">{{this.system.encumbrance.total}}</div>
<div class="small">
<a data-action="containerSort" data-direction="up"><i class="fa-solid fa-arrow-up"></i></a>
<a data-action="containerSort" data-direction="down"><i class="fa-solid fa-arrow-down"></i></a>
</div>
<div class="list-controls">
<a data-action="removeFromContainer" data-tooltip="{{localize 'SHEET.RemoveItem'}}"><i class="fa-solid fa-arrow-up-from-bracket"></i></a>
{{#if this.system.collapsed}}
Expand Down
3 changes: 3 additions & 0 deletions style/sheets/actors/tabs/_combat.scss
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@

.armour
{
.flexrow {
align-items: flex-start;
}
.sheet-list {
margin: 5px;
&:first-of-type {
Expand Down

0 comments on commit 2db5a60

Please sign in to comment.