fvtt-rolemaster-frp/module/sheets/skills/rmfrp_skill_sheet.js

110 lines
4.1 KiB
JavaScript
Raw Normal View History

import { RMFRPUtility } from "../../rmfrp-utility.js";
2024-08-03 16:41:04 +02:00
2024-07-26 09:20:48 +02:00
// Our Item Sheet extends the default
export default class RMFRPSkillSheet extends ItemSheet {
2024-07-26 09:20:48 +02:00
// Set the height and width
static get defaultOptions() {
2024-07-26 12:51:32 +02:00
return foundry.utils.mergeObject(super.defaultOptions, {
2024-07-26 09:20:48 +02:00
width: 530,
height: 440,
template: "systems/fvtt-rolemaster-frp/templates/sheets/skills/rmfrp-skill-sheet.html",
classes: ["rmfrp", "sheet", "item"]
2024-07-26 09:20:48 +02:00
});
}
// If our sheet is called here it is.
get template() {
return "systems/fvtt-rolemaster-frp/templates/sheets/skills/rmfrp-skill-sheet.html";
2024-07-26 09:20:48 +02:00
}
// Make the data available to the sheet template
async getData() {
const baseData = await super.getData();
2024-07-26 18:31:41 +02:00
let enrichedDescription = await TextEditor.enrichHTML(this.item.system.description, { async: true });
2024-07-26 09:20:48 +02:00
// Get a list of the parent item's skill categories for the dropdown
let ownedSkillCategories = this.prepareSkillCategoryValues();
// Figure out if a valid Skill Category is already selected
let selectedSkillCategory = this.prepareSelectedSkillCategory(ownedSkillCategories, this.object.system.category);
let sheetData = {
owner: this.item.isOwner,
editable: this.isEditable,
item: baseData.item,
system: baseData.item.system,
config: CONFIG.rmfrp,
2024-07-26 09:20:48 +02:00
owned_skillcats: ownedSkillCategories,
enrichedDescription: enrichedDescription,
selected_skillcat: selectedSkillCategory,
designations: CONFIG.rmfrp.skill_designations
2024-07-26 09:20:48 +02:00
};
return sheetData;
}
activateListeners(html) {
super.activateListeners(html);
// Catch the event when the user clicks one of the New Ranks Checkboxes in a Skill.
// It will increment by one or wrap back to zero on a value of three
html.find(".skillsheet-newrank").click(ev => {
switch (ev.currentTarget.getAttribute("value")) {
case "0":
2024-07-26 18:31:41 +02:00
this.object.update({ system: { new_ranks: { value: 1 } } });
2024-07-26 09:20:48 +02:00
break;
case "1":
2024-07-26 18:31:41 +02:00
this.object.update({ system: { new_ranks: { value: 2 } } });
2024-07-26 09:20:48 +02:00
break;
case "2":
2024-07-26 18:31:41 +02:00
this.object.update({ system: { new_ranks: { value: 3 } } });
2024-07-26 09:20:48 +02:00
break;
case "3":
2024-07-26 18:31:41 +02:00
this.object.update({ system: { new_ranks: { value: 0 } } });
2024-07-26 09:20:48 +02:00
break;
}
});
}
// Skills are related to Skill Categories so we need something to allow the user to choose that relationship
// If this Skill is owned then we will return a list of Skill Categories and allow them to choose
// Otherwise we'll just return 'Skill has no owner'
prepareSkillCategoryValues() {
let skillCategories = RMFRPUtility.getSkillCategories();
2024-08-03 16:41:04 +02:00
if (this.item.isEmbedded) {
skillCategories = this.item.parent.items.filter(it => it.type == "skill_category");
2024-07-26 09:20:48 +02:00
}
2024-08-07 17:27:08 +02:00
//console.log("CATEG", skillCategories);
2024-08-03 16:41:04 +02:00
return (skillCategories);
2024-07-26 09:20:48 +02:00
}
// Determine which Skill Category is selected and test that it is in the current list of categories.
// If it isn't set it to None.
prepareSelectedSkillCategory(ownedSkillCategories, selectedSkillCategory) {
let defaultSelectedCategory = "None";
2024-08-03 16:41:04 +02:00
let skillC = ownedSkillCategories.find(it => it.name.toLowerCase() == selectedSkillCategory.toLowerCase());
if (skillC) {
2024-07-26 09:20:48 +02:00
return (selectedSkillCategory);
} else {
return (defaultSelectedCategory);
}
}
// Populate the Skill Category Bonus field on the Skill Sheet.
// Iterate through the owned skill categories and if one of them matches the item id of currently
// selected skill category then set the Skill Category Bonus field to the Total Bonus field of the Skill Category
prepareSelectedSkillCategoryBonus(selected_skillcat) {
let skillC = this.parent?.items || RMFRPUtility.getSkillCategories();
2024-08-03 16:41:04 +02:00
if (skillC) {
let item = skillC.find(it => it.type == "skill_category" && it.name.toLowerCase() == itemData.system.category.toLowerCase());
if (item) {
this.system.category_bonus = item.system.total_bonus;
return
2024-07-26 09:20:48 +02:00
}
}
2024-08-03 16:41:04 +02:00
ui.notifications.warn("No Skill Categories found for " + this.name + ". Please create and link a Skill Category.");
2024-07-26 09:20:48 +02:00
}
}