Files
l5rx-chiaroscuro/system/scripts/settings/default-skills-dialog.js
2023-12-14 10:10:03 +01:00

149 lines
4.6 KiB
JavaScript

/**
* L5R Settings dialog for default skills list
* @extends {FormApplication}
*/
export class DefaultSkillsDialogL5r5e extends FormApplication {
/**
* Key for skills list in Settings
* @type {string}
*/
static skillsLisKey = "defaultSkillsList";
/**
* Assign the default options
* @override
*/
static get defaultOptions() {
return foundry.utils.mergeObject(super.defaultOptions, {
id: "l5r5e-settings-default-skills-dialog",
classes: ["l5r5e", "settings", "default-skills"],
template: CONFIG.l5r5e.paths.templates + "settings/default-skills-dialog.html",
title: game.i18n.localize("SETTINGS.DefaultSkillsList.Label"),
width: 500,
height: 680,
resizable: true,
closeOnSubmit: false,
submitOnClose: false,
submitOnChange: false,
dragDrop: [{ dragSelector: ".item-list .item", dropSelector: null }],
});
}
/**
* Prevent non GM to render this windows
* @override
*/
render(force = false, options = {}) {
if (!this.isEditable) {
console.log("L5R5E | You don't have the rights to display this application");
return false;
}
return super.render(force, options);
}
/**
* Is the Form Application currently editable?
* @type {boolean}
*/
get isEditable() {
return game.user.isGM;
}
/**
* Construct and return the data object used to render the HTML template for this form application.
* @param options
* @return {Object}
*/
async getData(options = null) {
// Transform skills uuids to items by categories
let skillList = await game.l5r5e.HelpersL5r5e.getSkillsItemsList();
skillList = game.l5r5e.HelpersL5r5e.splitSkillByCategory(skillList);
return {
...(await super.getData(options)),
skillList,
};
}
/**
* Handle dropped data on the Actor sheet
* @param {DragEvent} event
*/
async _onDrop(event) {
// *** Everything below here is only needed if the sheet is editable ***
if (!this.isEditable) {
console.log("L5R5E | This sheet is not editable");
return;
}
// Check item type and subtype
const item = await game.l5r5e.HelpersL5r5e.getDragnDropTargetObject(event);
if (!item || item.documentName !== "Item" || item?.type !== "skill") {
console.log(`L5R5E | Item dropped must be a Skill Item : ${item?.type}`, item);
return;
}
// EmbedItem actor item ?
if (item.uuid.startsWith('Actor.')) {
console.log("L5R5E | This element has been ignored because it's a EmbedItem actor item", item.uuid);
return;
}
const skillListUuids = game.settings.get(CONFIG.l5r5e.namespace, DefaultSkillsDialogL5r5e.skillsLisKey) || [];
// Dropped an item with same "id" as one owned
if (skillListUuids.some((embedItem) => embedItem.uuid === item.uuid)) {
console.log("L5R5E | This element has been ignored because it already exists", item.uuid);
return;
}
// Add the uuid and save
skillListUuids.push(item.uuid);
await game.settings.set(CONFIG.l5r5e.namespace, DefaultSkillsDialogL5r5e.skillsLisKey, skillListUuids);
// Refresh
this.render(false);
}
/**
* Subscribe to events from the sheet.
* @param {jQuery} html HTML content of the sheet.
*/
activateListeners(html) {
super.activateListeners(html);
// Commons
game.l5r5e.HelpersL5r5e.commonListeners(html);
// *** Everything below here is only needed if the sheet is editable ***
if (!this.isEditable) {
return;
}
// Delete an item
html.find(`.item-delete`).on("click", this._removeSkill.bind(this));
}
/**
* Remove an item from it's uuid
* @param {Event} event
* @private
*/
async _removeSkill(event) {
event.preventDefault();
event.stopPropagation();
const itemUuid = $(event.currentTarget).data("item-uuid");
if (!itemUuid) {
return;
}
// Remove and save
const skillListUuids = game.settings.get(CONFIG.l5r5e.namespace, DefaultSkillsDialogL5r5e.skillsLisKey) || [];
await game.settings.set(CONFIG.l5r5e.namespace, DefaultSkillsDialogL5r5e.skillsLisKey, skillListUuids.filter((uuid) => uuid !== itemUuid));
// Refresh
this.render(false);
}
}