10: Refactor actor sheet code

- Refactored actor-sheet.js to move most of its logic into a separate
  helper.js module as static methods on a new SimpleHelper class. This
  doesn't add anything functional at the moment, but it's in
  preparation of re-using the same code for the item-sheet.js file.
This commit is contained in:
Matt Smith 2020-10-06 17:20:00 +00:00 committed by Andrew
parent 8a2ba78203
commit 8b958dd232
8 changed files with 748 additions and 528 deletions

View file

@ -1,4 +1,4 @@
import { ATTRIBUTE_TYPES } from "./constants.js";
import { EntitySheetHelper } from "./helper.js";
/**
* Extend the basic ItemSheet with some very simple modifications
@ -7,14 +7,15 @@ import { ATTRIBUTE_TYPES } from "./constants.js";
export class SimpleItemSheet extends ItemSheet {
/** @override */
static get defaultOptions() {
return mergeObject(super.defaultOptions, {
classes: ["worldbuilding", "sheet", "item"],
template: "systems/worldbuilding/templates/item-sheet.html",
width: 520,
height: 480,
tabs: [{navSelector: ".sheet-tabs", contentSelector: ".sheet-body", initial: "description"}]
});
static get defaultOptions() {
return mergeObject(super.defaultOptions, {
classes: ["worldbuilding", "sheet", "item"],
template: "systems/worldbuilding/templates/item-sheet.html",
width: 520,
height: 480,
tabs: [{navSelector: ".sheet-tabs", contentSelector: ".sheet-body", initial: "description"}],
scrollY: [".attributes"],
});
}
/* -------------------------------------------- */
@ -22,16 +23,34 @@ export class SimpleItemSheet extends ItemSheet {
/** @override */
getData() {
const data = super.getData();
data.dtypes = ATTRIBUTE_TYPES;
for ( let attr of Object.values(data.data.attributes) ) {
attr.isCheckbox = attr.dtype === "Boolean";
attr.isResource = attr.dtype === "Resource";
}
// Handle attribute groups.
EntitySheetHelper.getAttributeData(data);
return data;
}
/* -------------------------------------------- */
/** @override */
async _onSubmit(event, {updateData=null, preventClose=false, preventRender=false}={}) {
let attr = EntitySheetHelper.onSubmit(event);
// Submit the form if attr is true or an attr key.
if ( attr ) {
await super._onSubmit(event, {updateData: updateData, preventClose: preventClose, preventRender: preventRender});
// If attr is a key and not just true, set a very short timeout and retrigger focus after the original element is deleted and the new one is inserted.
if ( attr !== true) {
setTimeout(() => {
$(`input[name="${attr}"]`).parents('.attribute').find('.attribute-value').focus();
}, 10);
}
}
}
/* -------------------------------------------- */
/** @override */
setPosition(options={}) {
const position = super.setPosition(options);
@ -47,49 +66,26 @@ export class SimpleItemSheet extends ItemSheet {
activateListeners(html) {
super.activateListeners(html);
// Handle rollable attributes.
html.find(".attributes").on("click", "a.attribute-roll", EntitySheetHelper.onAttributeRoll.bind(this));
// Everything below here is only needed if the sheet is editable
if (!this.options.editable) return;
// Add draggable for macros.
html.find(".attributes a.attribute-roll").each((i, a) => {
a.setAttribute("draggable", true);
a.addEventListener("dragstart", ev => {
let dragData = ev.currentTarget.dataset;
ev.dataTransfer.setData('text/plain', JSON.stringify(dragData));
}, false);
});
// Add or Remove Attribute
html.find(".attributes").on("click", ".attribute-control", this._onClickAttributeControl.bind(this));
}
html.find(".attributes").on("click", ".attribute-control", EntitySheetHelper.onClickAttributeControl.bind(this));
/* -------------------------------------------- */
/**
* Listen for click events on an attribute control to modify the composition of attributes in the sheet
* @param {MouseEvent} event The originating left click event
* @private
*/
async _onClickAttributeControl(event) {
event.preventDefault();
const a = event.currentTarget;
const action = a.dataset.action;
const attrs = this.object.data.data.attributes;
const form = this.form;
// Add new attribute
if ( action === "create" ) {
const objKeys = Object.keys(attrs);
let nk = Object.keys(attrs).length + 1;
let newValue = `attr${nk}`;
let newKey = document.createElement("div");
while ( objKeys.includes(newValue) ) {
++nk;
newValue = `attr${nk}`;
}
newKey.innerHTML = `<input type="text" name="data.attributes.attr${nk}.key" value="${newValue}"/>`;
newKey = newKey.children[0];
form.appendChild(newKey);
await this._onSubmit(event);
}
// Remove existing attribute
else if ( action === "delete" ) {
const li = a.closest(".attribute");
li.parentElement.removeChild(li);
await this._onSubmit(event);
}
// Add attribute groups.
html.find(".groups").on("click", ".group-control", EntitySheetHelper.onClickAttributeGroupControl.bind(this));
}
/* -------------------------------------------- */
@ -97,28 +93,11 @@ export class SimpleItemSheet extends ItemSheet {
/** @override */
_updateObject(event, formData) {
// Handle the free-form attributes list
const formAttrs = expandObject(formData).data.attributes || {};
const attributes = Object.values(formAttrs).reduce((obj, v) => {
let k = v["key"].trim();
if ( /[\s\.]/.test(k) ) return ui.notifications.error("Attribute keys may not contain spaces or periods");
delete v["key"];
obj[k] = v;
return obj;
}, {});
// Handle attribute and group updates.
formData = EntitySheetHelper.updateAttributes(formData, this);
formData = EntitySheetHelper.updateGroups(formData, this);
// Remove attributes which are no longer used
for ( let k of Object.keys(this.object.data.data.attributes) ) {
if ( !attributes.hasOwnProperty(k) ) attributes[`-=${k}`] = null;
}
// Re-combine formData
formData = Object.entries(formData).filter(e => !e[0].startsWith("data.attributes")).reduce((obj, e) => {
obj[e[0]] = e[1];
return obj;
}, {_id: this.object._id, "data.attributes": attributes});
// Update the Item
// Update the Actor with the new form values.
return this.object.update(formData);
}
}