import { Misc } from "./misc.js"; import { TMRType } from "./tmr-utility.js"; /** * Item sheet pour signes draconiques * @extends {ItemSheet} */ export class RdDSigneDraconiqueItemSheet extends ItemSheet { /** @override */ static get defaultOptions() { return mergeObject(super.defaultOptions, { classes: ["foundryvtt-reve-de-dragon", "sheet", "item"], template: "systems/foundryvtt-reve-de-dragon/templates/item-signedraconique-sheet.html", width: 550, height: 550 }); } /* -------------------------------------------- */ _getHeaderButtons() { let buttons = super._getHeaderButtons(); buttons.unshift({ class: "post", icon: "fas fa-comment", onclick: ev => this.item.postItem() }); return buttons; } /* -------------------------------------------- */ /** @override */ setPosition(options = {}) { const position = super.setPosition(options); const sheetHeader = this.element.find(".sheet-header"); const sheetBody = this.element.find(".sheet-body"); const bodyHeight = position.height - sheetHeader[0].clientHeight; sheetBody.css("height", bodyHeight); return position; } /* -------------------------------------------- */ async getData() { const formData = duplicate(Misc.data(this.object)); mergeObject(formData, { title: formData.name, isGM: game.user.isGM, owner: this.document.isOwner, isOwned: this.actor ? true : false, actorId: this.actor?.id, editable: this.isEditable, cssClass: this.isEditable ? "editable" : "locked", }); formData.tmrs = RdDSigneDraconiqueItemSheet.listTMRTypes(formData.data.typesTMR ?? []); return formData; } static listTMRTypes(typesTMR) { return Object.values(TMRType).map(value => Misc.upperFirst(value.name)) .sort() .map(name => { return { name: name, selected: typesTMR.includes(name) } }); } /* -------------------------------------------- */ /** @override */ activateListeners(html) { super.activateListeners(html); if (!this.options.editable) return; html.find(".select-tmr").change((event) => this.onSelectTmr(event)); html.find(".valeur-xp-sort").change((event) => this.onValeurXpSort(event.currentTarget.attributes['data-typereussite']?.value, Number(event.currentTarget.value))); } async onSelectTmr(event) { event.preventDefault(); const selectedTMR = $(".select-tmr").val(); this.object.update({ 'data.typesTMR': selectedTMR }); } async onValeurXpSort(qualite, valeur) { let tplData = Misc.templateData(this.object); if (valeur != tplData.valeur[qualite]) { await this.object.update({ [`data.valeur.${qualite}`]: valeur }); tplData = Misc.templateData(this.object); switch (qualite) { case "norm": if (valeur > tplData.valeur.sign) await this.object.update({ 'data.valeur.sign': valeur }); if (valeur > tplData.valeur.part) await this.object.update({ 'data.valeur.part': valeur }); break; case "sign": if (valeur < tplData.valeur.norm) await this.object.update({ 'data.valeur.norm': valeur }); if (valeur > tplData.valeur.part) await this.object.update({ 'data.valeur.part': valeur }); break; case "part": if (valeur < tplData.valeur.norm) await this.object.update({ 'data.valeur.norm': valeur }); if (valeur < tplData.valeur.sign) await this.object.update({ 'data.valeur.sign': valeur }); break; } } } /* -------------------------------------------- */ get template() { return `systems/foundryvtt-reve-de-dragon/templates/item-signedraconique-sheet.html`; } get title() { return `Signe draconique: ${this.object.name}`; } }