Skip to content

Commit 9351335

Browse files
authored
Merge pull request #22 from xeqi/delete-bonds
feat: deleting bonds removes row from sheet
2 parents 52f49b9 + 8ab6af4 commit 9351335

File tree

2 files changed

+39
-90
lines changed

2 files changed

+39
-90
lines changed

module/documents/actor.mjs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -758,6 +758,20 @@ export class FUActor extends Actor {
758758
if (hpChange !== 0 && !levelChanged) options.damageTaken = hpChange * -1;
759759
}
760760

761+
// Foundry's form update handlers send back bond information as an object {0: ..., 1: ....}
762+
// So correct an update in that form and create an updated bond array to properly represent the changes
763+
const bonds = changed.system?.resources?.bonds;
764+
if (bonds) {
765+
if (!Array.isArray(bonds)) {
766+
const currentBonds = [];
767+
const maxIndex = Object.keys(bonds).length
768+
for (let i = 0; i < maxIndex; i++) {
769+
currentBonds.push(bonds[i]);
770+
}
771+
changed.system.resources.bonds = currentBonds;
772+
}
773+
}
774+
761775
await super._preUpdate(changed, options, user);
762776
}
763777

module/sheets/actor-standard-sheet.mjs

Lines changed: 25 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -554,113 +554,48 @@ export class FUStandardActorSheet extends ActorSheet {
554554
});
555555

556556
// Check if bonds object exists, if not, initialize
557-
if (!this.actor.system.resources.bonds) {
558-
initializeBonds(this.actor);
559-
}
560-
561-
async function initializeBonds(actor) {
557+
const bonds = this.actor.system.resources.bonds
558+
if (!bonds) {
562559
const initialBonds = [];
563-
actor.system.resources.bonds = initialBonds;
564-
await actor.update({ 'system.resources.bonds': initialBonds });
565-
// console.log('Bonds initialized:', initialBonds);
560+
this.actor.system.resources.bonds = initialBonds;
561+
this.actor.update({ 'system.resources.bonds': initialBonds });
562+
}
563+
else if (!Array.isArray(bonds)) {
564+
//Convert bonds as object of indexes to bonds as array
565+
const currentBonds = [];
566+
for (const k in bonds) {
567+
currentBonds[k] = bonds[k];
568+
}
569+
this.actor.system.resources.bonds = currentBonds;
570+
this.actor.update({ 'system.resources.bonds': currentBonds });
566571
}
567572

568-
async function addBond(actor) {
569-
const bondsObject = actor.system.resources.bonds || {};
570-
571-
// Check if the maximum number of bonds (6) has been reached
572-
if (Object.keys(bondsObject).length >= 6) {
573+
// Event listener for adding a new bonds
574+
html.find('.bond-add').click(async (ev) => {
575+
ev.preventDefault();
576+
const bonds = this.actor.system.resources.bonds
577+
if (bonds.length >= 6) {
573578
ui.notifications.warn('Maximum number of bonds (6) reached.');
574579
return;
575580
}
576-
577-
// Find the next available index for the new bond
578-
let newIndex = 0;
579-
580-
// Find the next available index for the new bond
581-
while (bondsObject[newIndex]) {
582-
newIndex++;
583-
}
584-
585-
// Create a new bond object
586-
const newBond = {
581+
const newBonds = [...bonds];
582+
newBonds.push({
587583
name: '',
588584
admInf: '',
589585
loyMis: '',
590586
affHat: '',
591587
strength: 0,
592-
};
593-
594-
// Add the new bond to the bonds object using the next available index
595-
bondsObject[newIndex] = newBond;
596-
597-
// Update the actor's data with the modified bonds object
598-
await actor.update({ 'system.resources.bonds': bondsObject });
599-
600-
// Trigger a sheet re-render
601-
actor.sheet.render();
602-
603-
// console.log('Bonds after adding:', bondsObject);
604-
}
605-
606-
async function deleteBond(actor, index) {
607-
const bondsObject = actor.system.resources.bonds || {};
608-
609-
if (bondsObject[index]) {
610-
// Clear all the fields of the bond at the specified index
611-
bondsObject[index] = {
612-
name: '',
613-
admInf: '',
614-
loyMis: '',
615-
affHat: '',
616-
strength: 0,
617-
};
618-
619-
// Update the actor's data with the modified bonds object
620-
await actor.update({ 'system.resources.bonds': bondsObject });
621-
622-
// Trigger a sheet re-render
623-
actor.sheet.render();
624-
625-
// console.log('Bonds after clearing:', bondsObject);
626-
}
627-
}
628-
629-
// Event listener for adding a new bonds
630-
html.find('.bond-add').click(async (ev) => {
631-
ev.preventDefault();
632-
await addBond(this.actor);
588+
});
589+
await this.actor.update({ 'system.resources.bonds': newBonds });
633590
});
634591

635592
// Event listener for deleting a bond
636593
html.find('.bond-delete').click(async (ev) => {
637594
ev.preventDefault();
638-
const bondsObject = this.actor.system.resources.bonds || {};
639595
const bondIndex = $(ev.currentTarget).data('bond-index');
640-
await deleteBond(this.actor, bondIndex);
641-
await this.actor.update({ 'system.resources.bonds': bondsObject });
642-
});
643-
644-
// Event listener for keeping track of bond on change
645-
html.find('.select-bonds').change(async (ev) => {
646-
const fieldName = $(ev.currentTarget).attr('name');
647-
const newValue = $(ev.currentTarget).val();
648-
649-
// Split the fieldName to extract bondIndex
650-
const parts = fieldName.split('.');
651-
const bondIndex = parseInt(parts[3]);
652-
653-
// Get the current bonds array
654-
const bonds = this.actor.system.resources.bonds || [];
655-
656-
// Check if the bond object exists at the specified index
657-
if (bondIndex >= 0 && bondIndex < bonds.length) {
658-
// Update the bond property based on fieldName
659-
bonds[bondIndex][parts[4]] = newValue;
660-
661-
// Update the actor's data with the modified bonds array
662-
await this.actor.update({ 'system.resources.bonds': bonds });
663-
}
596+
const newBonds = [...this.actor.system.resources.bonds];
597+
newBonds.splice(bondIndex,1)
598+
await this.actor.update({ 'system.resources.bonds': newBonds });
664599
});
665600

666601
function _sortAlphaList(array, html) {

0 commit comments

Comments
 (0)