From 84ea567045c7c553bc3e80c3ca527a9fb82022ee Mon Sep 17 00:00:00 2001 From: Vincent Vandemeulebrouck Date: Tue, 13 Apr 2021 00:58:05 +0200 Subject: [PATCH] =?UTF-8?q?S=C3=A9parer=20les=20piles=20d'=C3=A9l=C3=A9men?= =?UTF-8?q?ts=20#169?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- module/actor-sheet.js | 34 ++++++++++++--- module/dialog-split-item.js | 51 +++++++++++++++++++++++ templates/actor-inventaire-conteneur.html | 6 ++- templates/dialog-item-split.html | 11 +++++ 4 files changed, 95 insertions(+), 7 deletions(-) create mode 100644 module/dialog-split-item.js create mode 100644 templates/dialog-item-split.html diff --git a/module/actor-sheet.js b/module/actor-sheet.js index 15f9adf5..46ccb270 100644 --- a/module/actor-sheet.js +++ b/module/actor-sheet.js @@ -12,6 +12,7 @@ import { Misc } from "./misc.js"; import { RdDCombatManager } from "./rdd-combat.js"; import { RdDCarac } from "./rdd-carac.js"; import { RdDItem } from "./item.js"; +import { DialogSplitItem } from "./dialog-split-item.js"; /* -------------------------------------------- */ export class RdDActorSheet extends ActorSheet { @@ -49,20 +50,20 @@ export class RdDActorSheet extends ActorSheet { // // Entity data // formData.actor = formData.entity; // formData.data = formData.entity.data; - + // // Owned items // formData.items = formData.actor.items; // formData.items.sort((a, b) => (a.sort || 0) - (b.sort || 0)); - + // -------------- version 0.8.0 - + // // Copy and sort Items // items.sort((a, b) => (a.sort || 0) - (b.sort || 0)); // data.items = items; - + // // Copy Active Effects // data.effects = effects; - + // // Return template data let formData = { title: this.title, @@ -203,7 +204,7 @@ export class RdDActorSheet extends ActorSheet { }); d.render(true); } - + /* -------------------------------------------- */ async selectTypeOeuvre() { let typeOeuvres = RdDItem.getTypesOeuvres(); @@ -240,6 +241,11 @@ export class RdDActorSheet extends ActorSheet { // Everything below here is only needed if the sheet is editable 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 => { const li = $(ev.currentTarget).parents(".item"); const item = this.actor.items.get(li.data("item-id")); @@ -632,4 +638,20 @@ export class RdDActorSheet extends ActorSheet { // Update the Actor 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]) + } + } + } diff --git a/module/dialog-split-item.js b/module/dialog-split-item.js new file mode 100644 index 00000000..a3521493 --- /dev/null +++ b/module/dialog-split-item.js @@ -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); + }); + } + +} \ No newline at end of file diff --git a/templates/actor-inventaire-conteneur.html b/templates/actor-inventaire-conteneur.html index da3789c0..cfbd9a5a 100644 --- a/templates/actor-inventaire-conteneur.html +++ b/templates/actor-inventaire-conteneur.html @@ -6,7 +6,11 @@ {{else}} {{item.name}} {{/if}} - {{item.data.quantite}} + {{item.data.quantite}} + {{#if (gt item.data.quantite 1)}} + + {{/if}} + {{numberFormat item.data.encTotal decimals=2}}
{{#unless item.estContenu}} diff --git a/templates/dialog-item-split.html b/templates/dialog-item-split.html new file mode 100644 index 00000000..acfb7552 --- /dev/null +++ b/templates/dialog-item-split.html @@ -0,0 +1,11 @@ +
+ {{item.name}} +

{{item.name}}

+ +
+ + +
+
+