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

181 lines
6.6 KiB
TypeScript
Raw Normal View History

2020-12-31 12:21:24 +01:00
import { DS4ItemDataType } from "../item/item-data";
2020-12-23 18:23:26 +01:00
import { DS4Actor } from "./actor";
import { DS4ActorDataType } from "./actor-data";
2020-12-23 16:52:20 +01:00
/**
* Extend the basic ActorSheet with some very simple modifications
* @extends {ActorSheet}
*/
2020-12-29 00:33:43 +01:00
export class DS4ActorSheet extends ActorSheet<DS4ActorDataType, DS4Actor, DS4ItemDataType> {
/**
* 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
*/
getData(): ActorSheetData<DS4ActorDataType, DS4Actor> {
const data = {
...super.getData(),
// Add the localization config to the data:
config: CONFIG.DS4,
// Add the items explicitly sorted by type to the data:
itemsByType: this.actor.itemTypes,
};
console.log("Data:", data);
return data;
}
2020-12-23 16:52:20 +01:00
/** @override */
2020-12-23 18:23:26 +01:00
static get defaultOptions(): FormApplicationOptions {
2020-12-23 16:52:20 +01:00
return mergeObject(super.defaultOptions, {
classes: ["ds4", "sheet", "actor"],
template: "systems/ds4/templates/actor/actor-sheet.hbs",
2020-12-30 00:35:18 +01:00
width: 725,
2020-12-23 16:52:20 +01:00
height: 600,
tabs: [{ navSelector: ".sheet-tabs", contentSelector: ".sheet-body", initial: "description" }],
});
}
/* -------------------------------------------- */
/** @override */
2020-12-23 18:23:26 +01:00
activateListeners(html: JQuery): void {
2020-12-23 16:52:20 +01:00
super.activateListeners(html);
// Everything below here is only needed if the sheet is editable
if (!this.options.editable) return;
// Add Inventory Item
html.find(".item-create").on("click", this._onItemCreate.bind(this));
2020-12-23 16:52:20 +01:00
// Update Inventory Item
html.find(".item-edit").on("click", (ev) => {
2020-12-23 16:52:20 +01:00
const li = $(ev.currentTarget).parents(".item");
const item = this.actor.getOwnedItem(li.data("itemId"));
item.sheet.render(true);
});
// Delete Inventory Item
2020-12-28 17:38:52 +01:00
html.find(".item-delete").on("click", (ev) => {
2020-12-23 16:52:20 +01:00
const li = $(ev.currentTarget).parents(".item");
this.actor.deleteOwnedItem(li.data("itemId"));
li.slideUp(200, () => this.render(false));
});
html.find(".item-change").on("change", this._onItemChange.bind(this));
2020-12-23 16:52:20 +01:00
// Rollable abilities.
html.find(".rollable").click(this._onRoll.bind(this));
}
/* -------------------------------------------- */
/**
* Handle creating a new Owned Item for the actor using initial data defined in the HTML dataset
* @param {Event} event The originating click event
* @private
*/
private _onItemCreate(event: JQuery.ClickEvent): Promise<Item> {
2020-12-23 16:52:20 +01:00
event.preventDefault();
const header = event.currentTarget;
// Get the type of item to create.
const type = header.dataset.type;
// Grab any data associated with this control.
const data = duplicate(header.dataset);
// Initialize a default name.
const name = `New ${type.capitalize()}`;
// Prepare the item object.
const itemData = {
name: name,
type: type,
data: data,
};
// Remove the type from the dataset since it's in the itemData.type prop.
2020-12-29 00:33:43 +01:00
delete itemData.data.type;
2020-12-23 16:52:20 +01:00
// Finally, create the item!
return this.actor.createOwnedItem(itemData);
}
/**
* Handle changes to properties of an Owned Item from within character sheet.
* Can currently properly bind: checkboxes, text/number/range input.
* Assumes the item property is given as the value of the HTML element property 'data-property'.
* @param {Event} ev The originating change event
* @private
*/
private _onItemChange(ev: JQuery.ChangeEvent<HTMLFormElement>): void {
ev.preventDefault();
console.log("Current target:", $(ev.currentTarget).get(0)["name"]);
const el: HTMLFormElement = $(ev.currentTarget).get(0);
const id = $(ev.currentTarget).parents(".item").data("itemId");
const item = duplicate(this.actor.getOwnedItem(id)); // getOwnedItem is typed incorrectly, it actually returns a ItemData<DS4ItemDataType>, not an Item
const property: string = $(ev.currentTarget).data("property");
let newValue;
// Early return:
// Disabled => do nothing
if (el.disabled || el.getAttribute("disabled")) return;
// name not given => raise
if (property === undefined) {
throw TypeError("HTML element does not provide 'data-property' attribute");
}
// One needs to differentiate between e.g. checkboxes (value="on") and select boxes etc.
// Checkbox:
if (el.type === "checkbox") {
newValue = el.checked; //!getProperty(item, property);
}
// Text input:
else if (el.type === "text") {
newValue = el.value;
}
// Numbers and ranges:
else if (["number", "range"].includes(el.type)) {
newValue = el.value.trim();
}
// // Radio Checkboxes (untested, cf. FormDataExtended.process)
// else if (el.type === "radio") {
// const chosen: HTMLFormElement = el.find((r: HTMLFormElement) => r["checked"]);
// newValue = chosen ? chosen.value : null;
// }
// // Multi-Select (untested, cf. FormDataExtended.process)
// else if (el.type === "select-multiple") {
// newValue = [];
// el.options.array.forEach((opt: HTMLOptionElement) => {
// if (opt.selected) newValue.push(opt.value);
// });
// unsupported:
else {
throw TypeError("Binding of item property to this type of HTML element not supported; given: " + el);
}
setProperty(item, property, newValue);
this.actor.updateOwnedItem(item);
}
2020-12-23 16:52:20 +01:00
/**
* Handle clickable rolls.
* @param {Event} event The originating click event
* @private
*/
private _onRoll(event: JQuery.ClickEvent): void {
2020-12-23 16:52:20 +01:00
event.preventDefault();
const element = event.currentTarget;
const dataset = element.dataset;
if (dataset.roll) {
2020-12-28 15:51:00 +01:00
const roll = new Roll(dataset.roll, this.actor.data.data);
2020-12-23 18:23:26 +01:00
const label = dataset.label ? `Rolling ${dataset.label}` : "";
2020-12-23 16:52:20 +01:00
roll.roll().toMessage({
speaker: ChatMessage.getSpeaker({ actor: this.actor }),
flavor: label,
});
}
}
}