foundryvtt-reve-de-dragon/module/rdd-roll-resolution-table.js
Vincent Vandemeulebrouck 63770790b9 Fix multi-dialogs
Arrêter d'utiliser le jQuery $(selector) qui cause des effets de bord si
plusieurs élements de la page (ie: foundry) correspondent
au selector.

Stocker le html dans les Sheet/Dialogs lors de l'appel
activateListeners  afin de pouvoir s'y référer ensuite.

Utiliser this.html.find pour chercher dans le html de la fenêtre
courante.

Eliminer les référence par id html car l'id est unique (donc ne marche
pas en multi-fenêtres)
2022-12-09 02:07:59 +01:00

145 lines
4.9 KiB
JavaScript

import { Misc } from "./misc.js";
import { RdDResolutionTable } from "./rdd-resolution-table.js";
const titleTableDeResolution = 'Table de résolution';
/**
* Extend the base Dialog entity to select roll parameters
* @extends {Dialog}
*/
/* -------------------------------------------- */
export class RdDRollResolutionTable extends Dialog {
static resolutionTable = undefined;
/* -------------------------------------------- */
static async open() {
if (RdDRollResolutionTable.resolutionTable == undefined) {
const rollData = {}
RdDRollResolutionTable._setDefaultOptions(rollData);
let html = await renderTemplate('systems/foundryvtt-reve-de-dragon/templates/dialog-roll-resolution.html', rollData);
RdDRollResolutionTable.resolutionTable = new RdDRollResolutionTable(rollData, html);
RdDRollResolutionTable.resolutionTable.render(true);
}
else{
RdDRollResolutionTable.resolutionTable.bringToTop();
}
}
/* -------------------------------------------- */
static _setDefaultOptions(rollData) {
let defRollData = {
show: { title: titleTableDeResolution },
ajustementsConditions: CONFIG.RDD.ajustementsConditions,
difficultesLibres: CONFIG.RDD.ajustementsConditions,
etat: 0,
moral: 0,
carac: {},
finalLevel: 0,
diffConditions: 0,
diffLibre: 0,
use: { conditions:true, libre:true }
}
mergeObject(rollData, defRollData, { overwrite: false });
for (let i = 1; i < 21; i++) {
const key = `${i}`;
rollData.carac[key] = { type: "number", value: i, label: key }
}
let selected = (rollData.selectedCarac && rollData.selectedCarac.label)
? rollData.selectedCarac.label
: (Number.isInteger(rollData.selectedCarac))
? rollData.selectedCarac
: 10;
rollData.selectedCarac = rollData.carac[selected];
}
/* -------------------------------------------- */
constructor(rollData, html) {
let conf = {
title: titleTableDeResolution,
content: html,
buttons: {
'lancer-fermer': { label: 'Lancer les dés et fermer', callback: html => this.onLancerFermer() }
}
};
super(conf, { classes: ["rdd-roll-dialog"], top: 50, width: 'fit-content', height: 'fit-content', 'z-index': 99999 });
this.rollData = rollData;
}
activateListeners(html) {
super.activateListeners(html);
this.html = html;
this.bringToTop();
this.html.find("[name='diffLibre']").val(Misc.toInt(this.rollData.diffLibre));
this.html.find("[name='diffConditions']").val(Misc.toInt(this.rollData.diffConditions));
this.updateRollResult();
this.html.find('.lancer-table-resolution').click((event) => {
this.onLancer();
});
// Update !
this.html.find("[name='diffLibre']").change((event) => {
this.rollData.diffLibre = Misc.toInt(event.currentTarget.value);
this.updateRollResult();
});
this.html.find("[name='diffConditions']").change((event) => {
this.rollData.diffConditions = Misc.toInt(event.currentTarget.value);
this.updateRollResult();
});
this.html.find("[name='carac']").change((event) => {
let caracKey = event.currentTarget.value;
this.rollData.selectedCarac = this.rollData.carac[caracKey];
this.updateRollResult();
});
}
/* -------------------------------------------- */
async onLancer() {
await RdDResolutionTable.rollData(this.rollData);
console.log("RdDRollResolutionTable -=>", this.rollData, this.rollData.rolled);
await RdDResolutionTable.displayRollData(this.rollData);
}
/* -------------------------------------------- */
async onLancerFermer() {
await RdDResolutionTable.rollData(this.rollData);
console.log("RdDRollResolutionTable -=>", this.rollData, this.rollData.rolled);
await RdDResolutionTable.displayRollData(this.rollData);
}
/* -------------------------------------------- */
async updateRollResult() {
let rollData = this.rollData;
rollData.caracValue = parseInt(rollData.selectedCarac.value)
rollData.finalLevel = this._computeFinalLevel(rollData);
const htmlTable = await RdDResolutionTable.buildHTMLTable({
carac:rollData.caracValue,
level: rollData.finalLevel
});
// Mise à jour valeurs
this.html.find("[name='carac']").val(rollData.caracValue);
this.html.find(".roll-param-resolution").text(rollData.selectedCarac.value + " / " + Misc.toSignedString(rollData.finalLevel));
this.html.find(".table-resolution").remove();
this.html.find(".table-proba-reussite").remove();
this.html.find("div.placeholder-resolution").append(htmlTable)
}
/* -------------------------------------------- */
_computeFinalLevel(rollData) {
const diffConditions = Misc.toInt(rollData.diffConditions);
const diffLibre = Misc.toInt(rollData.diffLibre);
return diffLibre + diffConditions;
}
async close() {
await super.close();
RdDRollResolutionTable.resolutionTable = undefined;
}
}