foundryvtt-reve-de-dragon/module/dialog-item-vente.js

94 lines
3.7 KiB
JavaScript
Raw Normal View History

2021-05-07 17:27:02 +02:00
import { HtmlUtility } from "./html-utility.js";
export class DialogItemVente extends Dialog {
static async display({ item, callback, service = undefined, quantiteMax = undefined }) {
const quantite = quantiteMax ?? item.getQuantite();
const isOwned = item.isOwned;
// const isOwned = item.isOwned || service?.actor;
2021-05-07 17:27:02 +02:00
const venteData = {
item: item,
alias: item.actor?.name ?? service?.name ?? game.user.name,
serviceId: service?.id,
vendeurId: item.actor?.id ?? service?.actor?.id,
prixOrigine: item.system.cout,
prixUnitaire: item.system.cout,
prixLot: item.system.cout,
2021-05-07 17:27:02 +02:00
tailleLot: 1,
quantiteNbLots: quantite,
quantiteMaxLots: quantite,
quantiteMax: quantite,
quantiteIllimite: service? service.system.illimite : !isOwned,
isOwned: isOwned,
2021-05-07 17:27:02 +02:00
};
const html = await renderTemplate(`systems/foundryvtt-reve-de-dragon/templates/dialog-item-vente.html`, venteData);
return new DialogItemVente(venteData, html, callback).render(true);
2021-05-07 17:27:02 +02:00
}
constructor(venteData, html, callback) {
let options = { classes: ["dialogvente"], width: 400, height: 'fit-content', 'z-index': 99999 };
2021-05-07 17:27:02 +02:00
let conf = {
title: "Proposer",
content: html,
default: "proposer",
buttons: { "proposer": { label: "Proposer", callback: it => { this.onProposer(it); } } }
2021-05-07 17:27:02 +02:00
};
super(conf, options);
this.callback = callback;
this.venteData = venteData;
}
activateListeners(html) {
super.activateListeners(html);
this.html = html;
HtmlUtility._showControlWhen(this.html.find(".quantiteNbLots"), !this.venteData.quantiteIllimite)
2021-05-07 17:27:02 +02:00
this.html.find(".tailleLot").change(event => this.setTailleLot(Number(event.currentTarget.value)));
this.html.find(".quantiteNbLots").change(event => this.setNbLots(Number(event.currentTarget.value)));
this.html.find(".quantiteIllimite").change(event => this.setQuantiteIllimite(event.currentTarget.checked));
this.html.find(".prixLot").change(event => this.setPrixLot(Number(event.currentTarget.value)));
2021-05-07 17:27:02 +02:00
}
async onProposer(it) {
await this.html.find(".tailleLot").change();
await this.html.find(".quantiteNbLots").change();
await this.html.find(".quantiteIllimite").change();
await this.html.find(".prixLot").change();
this.callback(this.venteData);
}
/* -------------------------------------------- */
2021-05-07 17:27:02 +02:00
setPrixLot(prixLot) {
this.venteData.prixLot = prixLot;
}
setTailleLot(tailleLot) {
// recalculer le prix du lot
if (tailleLot != this.venteData.tailleLot) {
this.venteData.prixLot = (tailleLot * this.venteData.prixOrigine).toFixed(2);
this.html.find(".prixLot").val(this.venteData.prixLot);
2021-05-07 17:27:02 +02:00
}
this.venteData.tailleLot = tailleLot;
// recalculer le nombre de lots max
this.venteData.quantiteMaxLots = Math.floor(this.venteData.quantiteMax / tailleLot);
this.venteData.quantiteNbLots = Math.min(this.venteData.quantiteMaxLots, this.venteData.quantiteNbLots);
this.html.find(".quantiteNbLots").val(this.venteData.quantiteNbLots);
this.html.find(".quantiteNbLots").attr("max", this.venteData.quantiteMaxLots)
2021-05-07 17:27:02 +02:00
}
setNbLots(nbLots) {
if (this.venteData.isOwned) {
nbLots = Math.max(0, Math.min(nbLots, this.venteData.quantiteMaxLots));
}
this.venteData.quantiteNbLots = nbLots;
this.html.find(".quantiteNbLots").val(this.venteData.quantiteNbLots);
2021-05-07 17:27:02 +02:00
}
setQuantiteIllimite(checked) {
this.venteData.quantiteIllimite = checked;
this.html.find(".label-quantiteIllimite").text(this.venteData.quantiteIllimite ? "Illimités" : "disponibles");
HtmlUtility._showControlWhen(this.html.find(".quantiteNbLots"), !this.venteData.quantiteIllimite)
2021-05-07 17:27:02 +02:00
}
}