ds4/src/module/item/item-sheet.ts

123 lines
4.3 KiB
TypeScript

import { DS4 } from "../config";
import notifications from "../ui/notifications";
import { DS4Item } from "./item";
import { isDS4ItemDataTypePhysical } from "./item-data";
/**
* The Sheet class for DS4 Items
*/
export class DS4ItemSheet extends ItemSheet<ItemSheet.Data<DS4Item>> {
/** @override */
static get defaultOptions(): BaseEntitySheet.Options {
const superDefaultOptions = super.defaultOptions;
return mergeObject(superDefaultOptions, {
width: 530,
height: 400,
classes: ["ds4", "sheet", "item"],
tabs: [{ navSelector: ".sheet-tabs", contentSelector: ".sheet-body", initial: "description" }],
scrollY: [".sheet-body"],
template: superDefaultOptions.template,
viewPermission: superDefaultOptions.viewPermission,
closeOnSubmit: superDefaultOptions.closeOnSubmit,
submitOnChange: superDefaultOptions.submitOnChange,
submitOnClose: superDefaultOptions.submitOnClose,
editable: superDefaultOptions.editable,
baseApplication: superDefaultOptions.baseApplication,
top: superDefaultOptions.top,
left: superDefaultOptions.left,
popOut: superDefaultOptions.popOut,
minimizable: superDefaultOptions.minimizable,
resizable: superDefaultOptions.resizable,
id: superDefaultOptions.id,
dragDrop: superDefaultOptions.dragDrop,
filters: superDefaultOptions.filters,
title: superDefaultOptions.title,
});
}
/** @override */
get template(): string {
const path = "systems/ds4/templates/item";
return `${path}/${this.item.data.type}-sheet.hbs`;
}
/** @override */
async getData(): Promise<ItemSheet.Data<DS4Item>> {
const data = {
...(await super.getData()),
config: DS4,
isOwned: this.item.isOwned,
actor: this.item.actor,
isPhysical: isDS4ItemDataTypePhysical(this.item.data.data),
};
return data;
}
/** @override */
setPosition(options: Partial<Application.Position> = {}): Application.Position & { height: number } {
const position = super.setPosition(options);
if ("find" in this.element) {
const sheetBody = this.element.find(".sheet-body");
const bodyHeight = position.height - 192;
sheetBody.css("height", bodyHeight);
} else {
console.log("Failure setting position.");
}
return position;
}
/** @override */
activateListeners(html: JQuery): void {
super.activateListeners(html);
if (!this.options.editable) return;
html.find(".effect-control").on("click", this._onManageActiveEffect.bind(this));
}
/**
* Handle management of ActiveEffects.
* @param event - he originating click event
*/
protected async _onManageActiveEffect(event: JQuery.ClickEvent): Promise<unknown> {
event.preventDefault();
if (this.item.isOwned) {
return notifications.warn(game.i18n.localize("DS4.WarningManageActiveEffectOnOwnedItem"));
}
const a = event.currentTarget;
const li = $(a).parents(".effect");
switch (a.dataset["action"]) {
case "create":
return this._createActiveEffect();
case "edit":
const id = li.data("effectId");
const effect = this.item.effects.get(id);
if (!effect) {
throw new Error(game.i18n.format("DS4.ErrorItemDoesNotHaveEffect", { id, item: this.item.name }));
}
return effect.sheet.render(true);
case "delete": {
return this.item.deleteEmbeddedEntity("ActiveEffect", li.data("effectId"));
}
}
}
/**
* Create a new ActiveEffect for the item using default data.
*/
protected async _createActiveEffect(): Promise<ActiveEffect.Data> {
const label = `New Effect`;
const createData = {
label: label,
changes: [],
duration: {},
transfer: true,
};
const effect = ActiveEffect.create(createData, this.item);
return effect.create({});
}
}