import { DialogSplitItem } from "./dialog-split-item.js"; import { Misc } from "./misc.js"; export class RdDSheetUtility { static getItem(event, actor) { return actor.items.get(RdDSheetUtility.getItemId(event)); } static getItemId(event) { return RdDSheetUtility.getEventItemData(event, "item-id"); } static getEventItemData(event, property) { const node = RdDSheetUtility.getEventElement(event); return node?.data(property); } static getEventElement(event) { return $(event.currentTarget)?.parents(".item"); } static extractItemDropParameters(event, dragData, actor, objetVersConteneur) { console.log("extractItemDropParameters", actor.id, dragData); const itemId = dragData.id || dragData.data._id; const destItemId = $(event.target).parents(".item").attr("data-item-id"); return { sourceActorId: dragData.actorId, targetActorId: actor.id, itemId: itemId, destId: destItemId, srcId: objetVersConteneur[itemId], onEnleverConteneur: () => { delete objetVersConteneur[itemId]; }, onAjouterDansConteneur: (conteneurId) => { objetVersConteneur[itemId] = conteneurId; } } } static async splitItem(item, actor, onSplit = ()=>{}) { const dialog = await DialogSplitItem.create(item, async (item, split) => { await RdDSheetUtility._onSplitItem(item, split, actor); onSplit(); }); dialog.render(true); } static async _onSplitItem(item, split, actor) { if (split >= 1 && split < Misc.data(item).data.quantite) { await item.diminuerQuantite(split); const itemData = duplicate(Misc.data(item)); // todo: ajouter dans le même conteneur? itemData.data.quantite = split; await actor.createEmbeddedDocuments('Item', [itemData]) } } }