Get the base favourite mechanism working so the items are visible on the skills card

This commit is contained in:
Oliver-Akins 2025-03-22 21:20:02 -06:00
parent 7d39c487dc
commit c495f45015
14 changed files with 165 additions and 27 deletions

View file

@ -139,7 +139,23 @@ export class HeroSkillsCardV1 extends GenericAppMixin(HandlebarsApplicationMixin
};
static async prepareAmmo(ctx) {
ctx.ammo = 0;
let total = 0;
ctx.favouriteAmmo = [];
for (const ammo of ctx.actor.itemTypes.ammo) {
total += ammo.system.quantity;
if (ctx.favouriteAmmo.length < 3 && ammo.getFlag(game.system.id, `favourited`)) {
ctx.favouriteAmmo.push({
uuid: ammo.uuid,
name: ammo.name,
quantity: ammo.system.quantity,
});
};
};
ctx.favouriteAmmo.length = 3; // assert array length
ctx.ammo = total;
return ctx;
};

View file

@ -52,6 +52,11 @@ export function GenericAppMixin(HandlebarsApp) {
};
};
/**
* @override
* This override makes it so that if the application has any framable popovers
* within it that are currently open, they will rerender as well.
*/
async _onRender() {
await super._onRender();
for (const manager of this._popoverManagers.values()) {

View file

@ -1,5 +1,7 @@
import { filePath } from "../../consts.mjs";
import { GenericPopoverMixin } from "./GenericPopoverMixin.mjs";
import { localizer } from "../../utils/Localizer.mjs";
import { Logger } from "../../utils/Logger.mjs";
const { HandlebarsApplicationMixin, ApplicationV2 } = foundry.applications.api;
@ -15,7 +17,10 @@ export class AmmoTracker extends GenericPopoverMixin(HandlebarsApplicationMixin(
`ripcrypt--AmmoTracker`,
],
},
actions: {},
actions: {
favourite: this.#favourite,
unfavourite: this.#unfavourite,
},
};
static PARTS = {
@ -26,17 +31,63 @@ export class AmmoTracker extends GenericPopoverMixin(HandlebarsApplicationMixin(
// #endregion
// #region Instance Data
_favouriteCount = 0;
// #endregion
// #region Lifecycle
async _preparePartContext(partId, data) {
const ctx = { partId };
ctx.canPin = false;
ctx.ammos = data.ammos;
let favouriteCount = 0;
ctx.ammos = data.ammos.map(ammo => {
const favourite = ammo.getFlag(game.system.id, `favourited`) ?? false;
if (favourite) { favouriteCount++ };
return {
ammo,
favourite,
};
});
this._favouriteCount = favouriteCount;
ctx.atFavouriteLimit = favouriteCount >= 3;
return ctx;
};
// #endregion
// #region Actions
static async #favourite(_, el) {
const targetEl = el.closest(`[data-item-id]`);
if (!targetEl) {
Logger.warn(`Cannot find a parent element with data-item-id`);
return;
};
// get count of favourites
if (this._favouriteCount > 3) {
ui.notifications.error(localizer(`RipCrypt.notifs.error.at-favourite-limit`));
return;
};
const data = targetEl.dataset;
const item = await fromUuid(data.itemId);
if (!item) { return };
item.setFlag(game.system.id, `favourited`, true);
};
static async #unfavourite(_, el) {
const targetEl = el.closest(`[data-item-id]`);
if (!targetEl) {
Logger.warn(`Cannot find a parent element with data-item-id`);
return;
};
const data = targetEl.dataset;
const item = await fromUuid(data.itemId);
if (!item) { return };
item.unsetFlag(game.system.id, `favourited`);
};
// #endregion
};

View file

@ -81,7 +81,7 @@ export function GenericPopoverMixin(HandlebarsApp) {
* want it to when being created.
*
* Most of this implementation is identical to the ApplicationV2
* implementation, the biggest difference is how targetLeft and targetRight
* implementation, the biggest difference is how targetLeft and targetTop
* are calculated.
*/
_updatePosition(position) {