From 8a939e84a1303e4a43c2283a1677b9277ab61d50 Mon Sep 17 00:00:00 2001
From: Johannes Loher <johannes.loher@fg4f.de>
Date: Wed, 30 Jun 2021 13:42:20 +0200
Subject: [PATCH] Make AktorSheet#getData compile

---
 src/module/actor/sheets/actor-sheet.ts | 91 ++++++++++++++------------
 src/module/settings.ts                 |  2 +-
 2 files changed, 49 insertions(+), 44 deletions(-)

diff --git a/src/module/actor/sheets/actor-sheet.ts b/src/module/actor/sheets/actor-sheet.ts
index 2490b51..93f03b6 100644
--- a/src/module/actor/sheets/actor-sheet.ts
+++ b/src/module/actor/sheets/actor-sheet.ts
@@ -9,15 +9,14 @@ import { ModifiableDataBaseTotal } from "../../common/common-data";
 import { DS4 } from "../../config";
 import { getCanvas } from "../../helpers";
 import { DS4Item } from "../../item/item";
-import { getDS4Settings } from "../../settings";
+import { DS4Settings, getDS4Settings } from "../../settings";
 import notifications from "../../ui/notifications";
-import { DS4Actor } from "../actor";
 import { isCheck } from "../actor-data-properties";
 
 /**
  * The base Sheet class for all DS4 Actors
  */
-export class DS4ActorSheet extends ActorSheet<ActorSheet.Options> {
+export class DS4ActorSheet extends ActorSheet<ActorSheet.Options, DS4ActorSheetData> {
     /** @override */
     static get defaultOptions(): ActorSheet.Options {
         // TODO: Improve
@@ -50,41 +49,41 @@ export class DS4ActorSheet extends ActorSheet<ActorSheet.Options> {
         return `${basePath}/${this.actor.data.type}-sheet.hbs`;
     }
 
-    // /**
-    //  * This method returns the data for the template of the actor sheet.
-    //  * It explicitly adds the items of the object sorted by type in the
-    //  * object itemsByType.
-    //  * @returns The data fed to the template of the actor sheet
-    //  */
-    // async getData(): Promise<ActorSheet.Data<DS4Actor>> {
-    //     const itemsByType = Object.fromEntries(
-    //         Object.entries(this.actor.itemTypes).map(([itemType, items]) => {
-    //             return [itemType, items.map((item) => item.data).sort((a, b) => (a.sort || 0) - (b.sort || 0))];
-    //         }),
-    //     );
-    //     const data = {
-    //         ...this._addTooltipsToData(await super.getData()),
-    //         // Add the localization config to the data:
-    //         config: DS4,
-    //         // Add the items explicitly sorted by type to the data:
-    //         itemsByType,
-    //         settings: getDS4Settings(),
-    //     };
-    //     return data;
-    // }
+    /**
+     * This method returns the data for the template of the actor sheet.
+     * It explicitly adds the items of the object sorted by type in the
+     * object itemsByType.
+     * @returns The data fed to the template of the actor sheet
+     */
+    async getData(): Promise<DS4ActorSheetData> {
+        const itemsByType = Object.fromEntries(
+            Object.entries(this.actor.itemTypes).map(([itemType, items]) => {
+                return [itemType, items.map((item) => item.data).sort((a, b) => (a.sort || 0) - (b.sort || 0))];
+            }),
+        );
+        const data = {
+            ...this.addTooltipsToData(await super.getData()),
+            // Add the localization config to the data:
+            config: DS4,
+            // Add the items explicitly sorted by type to the data:
+            itemsByType,
+            settings: getDS4Settings(),
+        };
+        return data;
+    }
 
-    // protected _addTooltipsToData(data: ActorSheet.Data<DS4Actor>): ActorSheet.Data<DS4Actor> {
-    //     const valueGroups = [data.data.attributes, data.data.traits, data.data.combatValues];
+    protected addTooltipsToData(data: ActorSheet.Data): ActorSheet.Data {
+        const valueGroups = [data.data.data.attributes, data.data.data.traits, data.data.data.combatValues];
 
-    //     valueGroups.forEach((valueGroup) => {
-    //         Object.values(valueGroup).forEach((attribute: ModifiableDataBaseTotal<number> & { tooltip?: string }) => {
-    //             attribute.tooltip = this._getTooltipForValue(attribute);
-    //         });
-    //     });
-    //     return data;
-    // }
+        valueGroups.forEach((valueGroup) => {
+            Object.values(valueGroup).forEach((attribute: ModifiableDataBaseTotal<number> & { tooltip?: string }) => {
+                attribute.tooltip = this.getTooltipForValue(attribute);
+            });
+        });
+        return data;
+    }
 
-    protected _getTooltipForValue(value: ModifiableDataBaseTotal<number>): string {
+    protected getTooltipForValue(value: ModifiableDataBaseTotal<number>): string {
         return `${value.base} (${game.i18n.localize("DS4.TooltipBaseValue")}) + ${value.mod} (${game.i18n.localize(
             "DS4.TooltipModifier",
         )}) ➞ ${game.i18n.localize("DS4.TooltipEffects")} ➞ ${value.total}`;
@@ -98,7 +97,7 @@ export class DS4ActorSheet extends ActorSheet<ActorSheet.Options> {
         if (!this.options.editable) return;
 
         // Add Inventory Item
-        html.find(".item-create").on("click", this._onItemCreate.bind(this));
+        html.find(".item-create").on("click", this.onItemCreate.bind(this));
 
         // Update Inventory Item
         html.find(".item-edit").on("click", (ev) => {
@@ -121,18 +120,18 @@ export class DS4ActorSheet extends ActorSheet<ActorSheet.Options> {
             li.slideUp(200, () => this.render(false));
         });
 
-        html.find(".item-change").on("change", this._onItemChange.bind(this));
+        html.find(".item-change").on("change", this.onItemChange.bind(this));
 
-        html.find(".rollable-item").on("click", this._onRollItem.bind(this));
+        html.find(".rollable-item").on("click", this.onRollItem.bind(this));
 
-        html.find(".rollable-check").on("click", this._onRollCheck.bind(this));
+        html.find(".rollable-check").on("click", this.onRollCheck.bind(this));
     }
 
     /**
      * Handle creating a new Owned Item for the actor using initial data defined in the HTML dataset
      * @param event - The originating click event
      */
-    protected _onItemCreate(event: JQuery.ClickEvent): void {
+    protected onItemCreate(event: JQuery.ClickEvent): void {
         event.preventDefault();
         const header = event.currentTarget;
 
@@ -155,7 +154,7 @@ export class DS4ActorSheet extends ActorSheet<ActorSheet.Options> {
      * Assumes the item property is given as the value of the HTML element property 'data-property'.
      * @param ev - The originating change event
      */
-    protected _onItemChange(ev: JQuery.ChangeEvent): void {
+    protected onItemChange(ev: JQuery.ChangeEvent): void {
         ev.preventDefault();
         const el: HTMLFormElement = $(ev.currentTarget).get(0);
         const id = $(ev.currentTarget).parents(".item").data("itemId");
@@ -236,7 +235,7 @@ export class DS4ActorSheet extends ActorSheet<ActorSheet.Options> {
      * Handle clickable item rolls.
      * @param event - The originating click event
      */
-    protected _onRollItem(event: JQuery.ClickEvent): void {
+    protected onRollItem(event: JQuery.ClickEvent): void {
         event.preventDefault();
         const id = $(event.currentTarget).parents(".item").data("itemId");
         const item = this.actor.getEmbeddedDocument("Item", id, { strict: true }) as DS4Item; // TODO: improve in upstream types
@@ -247,7 +246,7 @@ export class DS4ActorSheet extends ActorSheet<ActorSheet.Options> {
      * Handle clickable check rolls.
      * @param event - The originating click event
      */
-    protected _onRollCheck(event: JQuery.ClickEvent): void {
+    protected onRollCheck(event: JQuery.ClickEvent): void {
         event.preventDefault();
         const check = event.currentTarget.dataset["check"];
         this.actor.rollCheck(check).catch((e) => notifications.error(e, { log: true }));
@@ -291,3 +290,9 @@ export class DS4ActorSheet extends ActorSheet<ActorSheet.Options> {
         return super._onDropItem(event, data);
     }
 }
+
+interface DS4ActorSheetData extends ActorSheet.Data<ActorSheet.Options> {
+    config: typeof DS4;
+    itemsByType: Record<string, foundry.data.ItemData[]>;
+    settings: DS4Settings;
+}
diff --git a/src/module/settings.ts b/src/module/settings.ts
index e08a378..625e695 100644
--- a/src/module/settings.ts
+++ b/src/module/settings.ts
@@ -33,7 +33,7 @@ export function registerSystemSettings(): void {
     });
 }
 
-interface DS4Settings {
+export interface DS4Settings {
     systemMigrationVersion: number;
     useSlayingDiceForAutomatedChecks: boolean;
     showSlayerPoints: boolean;