Séparer les piles d'éléments #169

This commit is contained in:
Vincent Vandemeulebrouck 2021-04-13 00:58:05 +02:00
parent d21cd86ccd
commit 84ea567045
4 changed files with 95 additions and 7 deletions

View File

@ -12,6 +12,7 @@ import { Misc } from "./misc.js";
import { RdDCombatManager } from "./rdd-combat.js"; import { RdDCombatManager } from "./rdd-combat.js";
import { RdDCarac } from "./rdd-carac.js"; import { RdDCarac } from "./rdd-carac.js";
import { RdDItem } from "./item.js"; import { RdDItem } from "./item.js";
import { DialogSplitItem } from "./dialog-split-item.js";
/* -------------------------------------------- */ /* -------------------------------------------- */
export class RdDActorSheet extends ActorSheet { export class RdDActorSheet extends ActorSheet {
@ -240,6 +241,11 @@ export class RdDActorSheet extends ActorSheet {
// Everything below here is only needed if the sheet is editable // Everything below here is only needed if the sheet is editable
if (!this.options.editable) return; if (!this.options.editable) return;
html.find('.item-split').click(ev => {
const li = $(ev.currentTarget).parents(".item");
const item = this.actor.items.get(li.data("item-id"));
this.splitItem(item);
});
html.find('.item-edit').click(ev => { html.find('.item-edit').click(ev => {
const li = $(ev.currentTarget).parents(".item"); const li = $(ev.currentTarget).parents(".item");
const item = this.actor.items.get(li.data("item-id")); const item = this.actor.items.get(li.data("item-id"));
@ -632,4 +638,20 @@ export class RdDActorSheet extends ActorSheet {
// Update the Actor // Update the Actor
return this.object.update(formData); return this.object.update(formData);
} }
async splitItem(item) {
const dialog = await DialogSplitItem.create(item, (item, split) => this._onSplitItem(item, split));
dialog.render(true);
}
async _onSplitItem(item, split) {
const itemData = Misc.data(item);
if (split >= 1 && split < itemData.data.quantite) {
await item.diminuerQuantite(split);
itemData.data.quantite = split;
itemData.id = undefined;
await this.actor.createEmbeddedDocuments('Item', [itemData])
}
}
} }

View File

@ -0,0 +1,51 @@
import { Misc } from "./misc.js";
export class DialogSplitItem extends Dialog {
static async create(item, callback) {
const itemData = Misc.data(item);
const splitData = {
item: itemData,
choix: { quantite: 1, max: itemData.data.quantite - 1 }
};
const html = await renderTemplate(`systems/foundryvtt-reve-de-dragon/templates/dialog-item-split.html`, splitData);
return new DialogSplitItem(item, splitData, html, callback)
}
constructor(item, splitData, html, callback) {
let options = { classes: ["dialogsplit"], width: 300, height: 160, 'z-index': 99999 };
let conf = {
title: "Séparer en deux",
content: html,
default: "separer",
buttons: {
"separer": {
label: "Séparer", callback: it => {
this.onSplit();
}
}
}
};
super(conf, options);
this.callback = callback;
this.item = item;
this.splitData = splitData;
}
async onSplit(){
this.callback(this.item, this.splitData.choix.quantite);
}
/* -------------------------------------------- */
activateListeners(html) {
super.activateListeners(html);
html.find(".choix-quantite").change(event => {
this.splitData.choix.quantite = Number(event.currentTarget.value);
});
}
}

View File

@ -6,7 +6,11 @@
{{else}} {{else}}
<span class="item-name flex-grow">{{item.name}}</span> <span class="item-name flex-grow">{{item.name}}</span>
{{/if}} {{/if}}
<span class="item-quantite">{{item.data.quantite}}</span> <span class="item-quantite">{{item.data.quantite}}
{{#if (gt item.data.quantite 1)}}
<a class="item-control item-split" title="Séparer"><i class="fas fa-unlink"></i></a>
{{/if}}
</span>
<span class="item-quantite">{{numberFormat item.data.encTotal decimals=2}}</span> <span class="item-quantite">{{numberFormat item.data.encTotal decimals=2}}</span>
<div class="item-controls flex-grow"> <div class="item-controls flex-grow">
{{#unless item.estContenu}} {{#unless item.estContenu}}

View File

@ -0,0 +1,11 @@
<form class="rdddialog">
<img class="chat-icon" src="{{item.img}}" title="{{item.name}}" alt="{{item.name}}" />
<h4>{{item.name}}</h4>
<label>Quantité totale : {{item.data.quantite}}</label>
<div class="flexrow">
<label class="flex-grow">Quantité à séparer</label>
<input class="attribute-value choix-quantite flex-shrink" type="number" name="choix.quantite" value="{{choix.quantite}}"
min="1" max="{{choix.max}}" data-dtype="Number" />
</div>
</form>