ds4/src/item/item-sheet.ts

139 lines
4.6 KiB
TypeScript
Raw Normal View History

2021-06-26 22:02:00 +02:00
// SPDX-FileCopyrightText: 2021 Johannes Loher
// SPDX-FileCopyrightText: 2021 Oliver Rümpelein
// SPDX-FileCopyrightText: 2021 Gesina Schwalbe
//
// SPDX-License-Identifier: MIT
import { DS4ActiveEffect } from "../active-effect";
2021-02-05 02:35:47 +01:00
import { DS4 } from "../config";
2021-07-07 19:22:35 +02:00
import { getGame } from "../helpers";
2021-02-21 04:09:48 +01:00
import notifications from "../ui/notifications";
2022-01-21 03:22:17 +01:00
import { enforce } from "../utils";
import { isDS4ItemDataTypePhysical } from "./item-data-source-base";
2020-12-23 18:23:26 +01:00
2020-12-23 16:52:20 +01:00
/**
2021-02-07 11:51:36 +01:00
* The Sheet class for DS4 Items
2020-12-23 16:52:20 +01:00
*/
2021-06-30 13:26:15 +02:00
export class DS4ItemSheet extends ItemSheet<ItemSheet.Options, DS4ItemSheetData> {
static override get defaultOptions(): ItemSheet.Options {
2021-06-30 13:08:41 +02:00
return foundry.utils.mergeObject(super.defaultOptions, {
2022-01-21 03:22:17 +01:00
classes: ["sheet", "ds4-item-sheet"],
2020-12-23 16:52:20 +01:00
height: 400,
2022-01-21 03:22:17 +01:00
scrollY: [".ds4-sheet-body"],
tabs: [{ navSelector: ".ds4-sheet-tab-nav", contentSelector: ".ds4-sheet-body", initial: "description" }],
width: 540,
2020-12-23 16:52:20 +01:00
});
}
override get template(): string {
2021-04-13 21:40:52 +02:00
const basePath = "systems/ds4/templates/sheets/item";
return `${basePath}/${this.item.data.type}-sheet.hbs`;
2020-12-23 16:52:20 +01:00
}
2021-06-30 13:26:15 +02:00
override async getData(): Promise<DS4ItemSheetData> {
2021-06-30 14:24:23 +02:00
const data = {
2021-06-30 13:26:15 +02:00
...(await super.getData()),
config: DS4,
isOwned: this.item.isOwned,
actor: this.item.actor,
isPhysical: isDS4ItemDataTypePhysical(this.item.data.data),
};
2021-06-30 14:24:23 +02:00
return data;
2021-06-30 13:26:15 +02:00
}
override setPosition(
options: Partial<Application.Position> = {},
): (Application.Position & { height: number }) | void {
2020-12-23 16:52:20 +01:00
const position = super.setPosition(options);
2021-06-30 03:53:52 +02:00
if (position) {
const sheetBody = this.element.find(".sheet-body");
const bodyHeight = position.height - 192;
sheetBody.css("height", bodyHeight);
}
2020-12-23 16:52:20 +01:00
return position;
}
override activateListeners(html: JQuery): void {
2020-12-23 16:52:20 +01:00
super.activateListeners(html);
if (!this.options.editable) return;
2022-01-21 03:22:17 +01:00
html.find(".control-effect").on("click", this.onControlEffect.bind(this));
2020-12-23 16:52:20 +01:00
}
/**
2022-01-21 03:22:17 +01:00
* Handles a click on an element of this sheet to control an embedded effect of the item corresponding to this
* sheet.
*
* @param event - The originating click event
2020-12-23 16:52:20 +01:00
*/
2022-01-21 03:22:17 +01:00
protected onControlEffect(event: JQuery.ClickEvent): void {
2020-12-23 16:52:20 +01:00
event.preventDefault();
if (this.item.isOwned) {
2021-07-07 19:22:35 +02:00
return notifications.warn(getGame().i18n.localize("DS4.WarningManageActiveEffectOnOwnedItem"));
}
const a = event.currentTarget;
switch (a.dataset["action"]) {
case "create":
2022-01-21 03:22:17 +01:00
return this.onCreateEffect();
case "edit":
2022-01-21 03:22:17 +01:00
return this.onEditEffect(event);
case "delete":
return this.onDeleteEffect(event);
}
}
/**
2022-01-21 03:22:17 +01:00
* Creates a new embedded effect.
*
* @param event - The originating click event
*/
2022-01-21 03:22:17 +01:00
protected onCreateEffect(): void {
DS4ActiveEffect.createDefault(this.item);
2020-12-23 16:52:20 +01:00
}
2022-01-21 03:22:17 +01:00
/**
* Opens the sheet of the embedded effect corresponding to the clicked element.
*
* @param event - The originating click event
*/
protected onEditEffect(event: JQuery.ClickEvent): void {
const id = $(event.currentTarget)
.parents(embeddedDocumentListEntryProperties.ActiveEffect.selector)
.data(embeddedDocumentListEntryProperties.ActiveEffect.idDataAttribute);
const effect = this.item.effects.get(id);
enforce(effect, getGame().i18n.format("DS4.ErrorItemDoesNotHaveEffect", { id, item: this.item.name }));
effect.sheet?.render(true);
2022-01-21 03:22:17 +01:00
}
/**
* Deletes the embedded item corresponding to the clicked element.
*
* @param event - The originating click event
*/
protected onDeleteEffect(event: JQuery.ClickEvent): void {
const li = $(event.currentTarget).parents(embeddedDocumentListEntryProperties.ActiveEffect.selector);
const id = li.data(embeddedDocumentListEntryProperties.ActiveEffect.idDataAttribute);
this.item.deleteEmbeddedDocuments("ActiveEffect", [id]);
li.slideUp(200, () => this.render(false));
}
2020-12-23 16:52:20 +01:00
}
2021-06-30 13:26:15 +02:00
interface DS4ItemSheetData extends ItemSheet.Data<ItemSheet.Options> {
config: typeof DS4;
isOwned: boolean;
actor: DS4ItemSheet["item"]["actor"];
isPhysical: boolean;
}
2022-01-21 03:22:17 +01:00
/**
* This object contains information about specific properties embedded document list entries for each different type.
*/
const embeddedDocumentListEntryProperties = Object.freeze({
ActiveEffect: {
selector: ".effect",
idDataAttribute: "effectId",
},
});