NSC Bogen & würfe
This commit is contained in:
1 parent
84daa12b7b
commit
fcb5d232b5
17 files changed
+2319
-655
No files matched your search
+47
-588
@@ -3,43 +3,30 @@ import { prepareActiveEffectCategories } from "../helpers/utils.mjs";
|
||||
import { addSpezialisierung, editSepzialisierung } from "./components/spezialisierung-actions.mjs";
|
||||
import { prepArmor, prepWeapons, prepWeight, prepSpells } from "./components/character-prep.mjs";
|
||||
import { rollSpell, rollWeapon, rollSkill } from "./components/roll-actions.mjs";
|
||||
import { M6ActorSheet } from "./actor-sheet.mjs";
|
||||
|
||||
const { api, sheets } = foundry.applications;
|
||||
|
||||
/**
|
||||
* Extend the basic ActorSheet with some very simple modifications.
|
||||
* Extend the M6ActorSheet.
|
||||
*/
|
||||
export class M6ActorSheet extends api.HandlebarsApplicationMixin(sheets.ActorSheetV2) {
|
||||
export class M6CharacterSheet extends M6ActorSheet {
|
||||
/** @inheritdoc */
|
||||
static DEFAULT_OPTIONS = {
|
||||
classes: ["M6", "actor"],
|
||||
window: {
|
||||
resizable: true
|
||||
},
|
||||
position: {
|
||||
width: 800,
|
||||
height: 800
|
||||
},
|
||||
actions: {
|
||||
viewDoc: this.#viewDoc,
|
||||
createDoc: this.#createDoc,
|
||||
deleteDoc: this.#deleteDoc,
|
||||
toggleEffect: this.#toggleEffect,
|
||||
rollSkill: rollSkill,
|
||||
rollWeapon: rollWeapon,
|
||||
rollSpell: rollSpell,
|
||||
cycleEquip: this.#cycleEquip,
|
||||
cycleLock: this.#cycleLock,
|
||||
addSpezialisierung: addSpezialisierung,
|
||||
editSepzialisierung: editSepzialisierung,
|
||||
increaseDoc: this.#increaseDoc,
|
||||
decreaseDoc: this.#decreaseDoc
|
||||
},
|
||||
form: {
|
||||
submitOnChange: true
|
||||
},
|
||||
dragDrop: [{ dragSelector: ".draggable", dropSelector: null }]
|
||||
};
|
||||
static get DEFAULT_OPTIONS() {
|
||||
return {
|
||||
...super.DEFAULT_OPTIONS, // Kopiert alle bestehenden Options aus M6ActorSheet
|
||||
actions: {
|
||||
...super.DEFAULT_OPTIONS.actions, // Kopiert alle bestehenden Actions aus M6ActorSheet
|
||||
cycleEquip: this.#cycleEquip,
|
||||
rollSkill: rollSkill,
|
||||
rollWeapon: rollWeapon,
|
||||
rollSpell: rollSpell,
|
||||
cycleEquip: this.#cycleEquip,
|
||||
addSpezialisierung: addSpezialisierung,
|
||||
editSepzialisierung: editSepzialisierung,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
/* -------------------------------------------------- */
|
||||
|
||||
@@ -48,33 +35,32 @@ export class M6ActorSheet extends api.HandlebarsApplicationMixin(sheets.ActorShe
|
||||
tabs: [
|
||||
{
|
||||
id: "eigenschaften",
|
||||
icon: "fa-shield-halved"
|
||||
icon: "fa-shield-halved",
|
||||
},
|
||||
|
||||
{
|
||||
id: "items",
|
||||
icon: "fa-suitcase"
|
||||
icon: "fa-suitcase",
|
||||
},
|
||||
{
|
||||
id: "zauber",
|
||||
icon: "fa-hand-sparkles"
|
||||
icon: "fa-hand-sparkles",
|
||||
},
|
||||
{
|
||||
id: "hintergrund",
|
||||
icon: "fa-list"
|
||||
icon: "fa-list",
|
||||
},
|
||||
{
|
||||
id: "biographie",
|
||||
icon: "fa-feather-pointed"
|
||||
}
|
||||
icon: "fa-feather-pointed",
|
||||
},
|
||||
// {
|
||||
// id: "effects",
|
||||
// icon: "fa-bolt"
|
||||
// }
|
||||
],
|
||||
initial: "eigenschaften",
|
||||
labelPrefix: "Midgard6.Sheets.Tabs"
|
||||
}
|
||||
labelPrefix: "Midgard6.Sheets.Tabs",
|
||||
},
|
||||
};
|
||||
|
||||
/* -------------------------------------------------- */
|
||||
@@ -82,75 +68,37 @@ export class M6ActorSheet extends api.HandlebarsApplicationMixin(sheets.ActorShe
|
||||
/** @inheritdoc */
|
||||
static PARTS = {
|
||||
tabs: {
|
||||
template: systemPath("templates/shared/tabs.hbs")
|
||||
template: systemPath("templates/shared/tabs.hbs"),
|
||||
},
|
||||
header: {
|
||||
template: systemPath("templates/actor/header.hbs")
|
||||
template: systemPath("templates/actor/header.hbs"),
|
||||
},
|
||||
eigenschaften: {
|
||||
template: systemPath("templates/actor/eigenschaften.hbs"),
|
||||
scrollable: [""]
|
||||
scrollable: [""],
|
||||
},
|
||||
zauber: {
|
||||
template: systemPath("templates/actor/zauber.hbs"),
|
||||
scrollable: [""]
|
||||
scrollable: [""],
|
||||
},
|
||||
items: {
|
||||
template: systemPath("templates/actor/items.hbs"),
|
||||
scrollable: [""]
|
||||
scrollable: [""],
|
||||
},
|
||||
hintergrund: {
|
||||
template: systemPath("templates/actor/hintergrund.hbs"),
|
||||
scrollable: [""]
|
||||
scrollable: [""],
|
||||
},
|
||||
biographie: {
|
||||
template: systemPath("templates/actor/biographie.hbs"),
|
||||
scrollable: [""]
|
||||
}
|
||||
scrollable: [""],
|
||||
},
|
||||
// effects: {
|
||||
// template: systemPath("templates/shared/effects.hbs"),
|
||||
// scrollable: [""]
|
||||
// }
|
||||
};
|
||||
|
||||
_attachPartListeners(partId, html, bubble) {
|
||||
super._attachPartListeners(partId, html, bubble);
|
||||
|
||||
switch (partId) {
|
||||
case "hintergrund":
|
||||
const collapsible = html.querySelectorAll("div[data-action=collapsible]");
|
||||
if (collapsible) {
|
||||
collapsible.forEach((node) => {
|
||||
node.addEventListener(
|
||||
"click",
|
||||
function (e) {
|
||||
e = window.event || e;
|
||||
if (this === e.target) {
|
||||
node.classList.toggle("active");
|
||||
var content = node.nextElementSibling;
|
||||
content.classList.toggle("active");
|
||||
node.getElementsByTagName("i")[0].classList.toggle("fa-compress");
|
||||
}
|
||||
},
|
||||
{ capture: true }
|
||||
);
|
||||
});
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
/* -------------------------------------------------- */
|
||||
|
||||
/** @inheritdoc */
|
||||
_initializeApplicationOptions(options) {
|
||||
const initialized = super._initializeApplicationOptions(options);
|
||||
|
||||
initialized.classes.push(initialized.document.type);
|
||||
|
||||
return initialized;
|
||||
}
|
||||
|
||||
/* -------------------------------------------------- */
|
||||
|
||||
/** @inheritdoc */
|
||||
@@ -164,7 +112,7 @@ export class M6ActorSheet extends api.HandlebarsApplicationMixin(sheets.ActorShe
|
||||
system: this.actor.system,
|
||||
skills: this.actor.system.skills,
|
||||
flags: this.actor.flags,
|
||||
config: CONFIG
|
||||
config: CONFIG,
|
||||
});
|
||||
return context;
|
||||
}
|
||||
@@ -187,7 +135,7 @@ export class M6ActorSheet extends api.HandlebarsApplicationMixin(sheets.ActorShe
|
||||
context.tab = context.tabs[partId];
|
||||
break;
|
||||
case "items":
|
||||
context.itemTypes = await this._getItems();
|
||||
context.itemTypes = await super._getItems();
|
||||
context.gewicht = await prepWeight(this.actor);
|
||||
context.tab = context.tabs[partId];
|
||||
break;
|
||||
@@ -196,7 +144,7 @@ export class M6ActorSheet extends api.HandlebarsApplicationMixin(sheets.ActorShe
|
||||
context.tab = context.tabs[partId];
|
||||
break;
|
||||
case "hintergrund":
|
||||
context.fields = await this._getFields();
|
||||
context.fields = await super._getFields();
|
||||
context.tab = context.tabs[partId];
|
||||
break;
|
||||
case "biographie":
|
||||
@@ -209,8 +157,8 @@ export class M6ActorSheet extends api.HandlebarsApplicationMixin(sheets.ActorShe
|
||||
// Data to fill in for inline rolls
|
||||
rollData: this.actor.getRollData(),
|
||||
// Relative UUID resolution
|
||||
relativeTo: this.actor
|
||||
}
|
||||
relativeTo: this.actor,
|
||||
},
|
||||
);
|
||||
context.tab = context.tabs[partId];
|
||||
break;
|
||||
@@ -220,29 +168,6 @@ export class M6ActorSheet extends api.HandlebarsApplicationMixin(sheets.ActorShe
|
||||
return context;
|
||||
}
|
||||
|
||||
/**
|
||||
* Handles the system fields for the form-fields generic.
|
||||
* @returns {object[]}
|
||||
*/
|
||||
async _getFields() {
|
||||
const doc = this.actor;
|
||||
const source = doc._source;
|
||||
const systemFields = CONFIG.Actor.dataModels[doc.type]?.schema.fields;
|
||||
const fieldSets = [];
|
||||
// TODO: Find a clever way to handle enrichment
|
||||
for (const field of Object.values(systemFields ?? {})) {
|
||||
const path = `system.${field.name}`;
|
||||
if (field instanceof foundry.data.fields.SchemaField) {
|
||||
const fieldset = { fieldset: true, legend: field.label, fields: [] };
|
||||
await this.#addSystemFields(fieldset, field.fields, source, path);
|
||||
fieldSets.push(fieldset);
|
||||
} else {
|
||||
fieldSets.push({ outer: { field, value: foundry.utils.getProperty(source, path) } });
|
||||
}
|
||||
}
|
||||
return fieldSets;
|
||||
}
|
||||
|
||||
/* -------------------------------------------------- */
|
||||
|
||||
async _getSkills() {
|
||||
@@ -257,7 +182,7 @@ export class M6ActorSheet extends api.HandlebarsApplicationMixin(sheets.ActorShe
|
||||
const fieldset = {
|
||||
fieldset: true,
|
||||
legend: `Midgard6.Skills.gruppen.${field.label}`,
|
||||
fields: []
|
||||
fields: [],
|
||||
};
|
||||
await this.#addSkillFields(fieldset, field.fields, source, path);
|
||||
fieldSets.push(fieldset);
|
||||
@@ -278,7 +203,7 @@ export class M6ActorSheet extends api.HandlebarsApplicationMixin(sheets.ActorShe
|
||||
field,
|
||||
value: foundry.utils.getProperty(source, path),
|
||||
localizationName: "Midgard6.Skills." + field.label,
|
||||
bonuses: foundry.utils.getProperty(source, _path).bonuses
|
||||
bonuses: foundry.utils.getProperty(source, _path).bonuses,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -293,14 +218,14 @@ export class M6ActorSheet extends api.HandlebarsApplicationMixin(sheets.ActorShe
|
||||
const fieldset = {
|
||||
fieldset: true,
|
||||
legend: `Midgard6.Attribute.${systemFields.label}`,
|
||||
fields: []
|
||||
fields: [],
|
||||
};
|
||||
for (const field of Object.values(systemFields.fields ?? {})) {
|
||||
const path = `system.resistenzen.${field.name}`;
|
||||
fieldset.fields.push({
|
||||
field,
|
||||
value: foundry.utils.getProperty(source, path),
|
||||
localizationName: "Midgard6.Resistenzen." + field.name
|
||||
localizationName: "Midgard6.Resistenzen." + field.name,
|
||||
});
|
||||
}
|
||||
return fieldset;
|
||||
@@ -318,7 +243,7 @@ export class M6ActorSheet extends api.HandlebarsApplicationMixin(sheets.ActorShe
|
||||
const fieldset = {
|
||||
fieldset: true,
|
||||
legend: `Midgard6.resources.${field.label}`,
|
||||
fields: []
|
||||
fields: [],
|
||||
};
|
||||
await this.#addSystemFields(fieldset, field.fields, source, path);
|
||||
fieldSets.push(fieldset);
|
||||
@@ -333,18 +258,11 @@ export class M6ActorSheet extends api.HandlebarsApplicationMixin(sheets.ActorShe
|
||||
const item = this.actor.items.get(target.dataset.itemId);
|
||||
item.update({
|
||||
system: {
|
||||
equipped: !item.system.equipped
|
||||
}
|
||||
equipped: !item.system.equipped,
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
static async #cycleLock(event) {
|
||||
const actor = this.actor;
|
||||
|
||||
// Update the locked state
|
||||
actor.update({ "system.locked": !actor.system.locked });
|
||||
}
|
||||
|
||||
/* -------------------------------------------------- */
|
||||
|
||||
/**
|
||||
@@ -359,7 +277,7 @@ export class M6ActorSheet extends api.HandlebarsApplicationMixin(sheets.ActorShe
|
||||
fieldset.fields.push({
|
||||
field,
|
||||
value: foundry.utils.getProperty(source, path),
|
||||
localizationName: "Midgard6.Skills." + field.label
|
||||
localizationName: "Midgard6.Skills." + field.label,
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -367,466 +285,7 @@ export class M6ActorSheet extends api.HandlebarsApplicationMixin(sheets.ActorShe
|
||||
|
||||
/* -------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Adapted from Actor#itemTypes.
|
||||
*/
|
||||
_getItems() {
|
||||
const types = Object.fromEntries(
|
||||
game.documentTypes.Item.map((t) => {
|
||||
return [t, { label: game.i18n.localize(CONFIG.Item.typeLabels[t]), items: [] }];
|
||||
})
|
||||
);
|
||||
for (const item of this.actor.items) {
|
||||
types[item.type].items.push(item);
|
||||
}
|
||||
|
||||
for (const item of this.actor.items) {
|
||||
types[item.type].items.sort((a, b) => a.sort - b.sort);
|
||||
}
|
||||
// Only show Base if it's actually being used
|
||||
if (types.base.items.length === 0) delete types.base;
|
||||
return types;
|
||||
}
|
||||
|
||||
_getZauber() {
|
||||
return this.actor.items.filter((item) => item.type === "zauber");
|
||||
}
|
||||
|
||||
/* -------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Actions performed after any render of the Application.
|
||||
* Post-render steps are not awaited by the render process.
|
||||
* @param {ApplicationRenderContext} context Prepared context data.
|
||||
* @param {RenderOptions} options Provided render options.
|
||||
* @protected
|
||||
* @inheritdoc
|
||||
*/
|
||||
async _onRender(context, options) {
|
||||
await super._onRender(context, options);
|
||||
this.#disableOverrides();
|
||||
}
|
||||
|
||||
/* -------------------------------------------------- */
|
||||
/* Event handlers */
|
||||
/* -------------------------------------------------- */
|
||||
|
||||
static async #increaseDoc(event, target) {
|
||||
const item = this.actor.items.get(target.dataset.itemId);
|
||||
if (event.shiftKey || event.ctrlKey) {
|
||||
return item.update({ ["system.anzahl"]: (item.system.anzahl += 10) });
|
||||
}
|
||||
return item.update({ ["system.anzahl"]: (item.system.anzahl += 1) });
|
||||
}
|
||||
|
||||
static async #decreaseDoc(event, target) {
|
||||
const item = this.actor.items.get(target.dataset.itemId);
|
||||
if (event.shiftKey || event.ctrlKey) {
|
||||
return item.update({ ["system.anzahl"]: (item.system.anzahl -= 10) });
|
||||
}
|
||||
return item.update({ ["system.anzahl"]: (item.system.anzahl -= 1) });
|
||||
}
|
||||
|
||||
/**
|
||||
* Renders an embedded document's sheet.
|
||||
*
|
||||
* @this M6ActorSheet
|
||||
* @param {PointerEvent} event The originating click event.
|
||||
* @param {HTMLElement} target The capturing HTML element which defined a [data-action].
|
||||
* @protected
|
||||
*/
|
||||
static async #viewDoc(event, target) {
|
||||
const doc = this._getEmbeddedDocument(target);
|
||||
doc.sheet.render(true);
|
||||
}
|
||||
|
||||
/* -------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Handles item deletion.
|
||||
*
|
||||
* @this M6ActorSheet
|
||||
* @param {PointerEvent} event The originating click event.
|
||||
* @param {HTMLElement} target The capturing HTML element which defined a [data-action].
|
||||
* @protected
|
||||
*/
|
||||
static async #deleteDoc(event, target) {
|
||||
const doc = this._getEmbeddedDocument(target);
|
||||
doc.delete();
|
||||
}
|
||||
|
||||
/* -------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Handle creating a new Owned Item or ActiveEffect for the actor using initial data defined in the HTML dataset.
|
||||
*
|
||||
* @this M6ActorSheet
|
||||
* @param {PointerEvent} event The originating click event.
|
||||
* @param {HTMLElement} target The capturing HTML element which defined a [data-action].
|
||||
* @private
|
||||
*/
|
||||
static async #createDoc(event, target) {
|
||||
const docCls = getDocumentClass(target.dataset.documentClass);
|
||||
const docData = {
|
||||
name: docCls.defaultName({
|
||||
type: target.dataset.type,
|
||||
parent: this.actor
|
||||
})
|
||||
};
|
||||
for (const [dataKey, value] of Object.entries(target.dataset)) {
|
||||
if (["action", "documentClass"].includes(dataKey)) continue;
|
||||
foundry.utils.setProperty(docData, dataKey, value);
|
||||
}
|
||||
const doc = await docCls.create(docData, { parent: this.actor });
|
||||
doc.sheet.render(true);
|
||||
}
|
||||
|
||||
/* -------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Determines effect parent to pass to helper.
|
||||
*
|
||||
* @this M6ActorSheet
|
||||
* @param {PointerEvent} event The originating click event.
|
||||
* @param {HTMLElement} target The capturing HTML element which defined a [data-action].
|
||||
* @private
|
||||
*/
|
||||
static async #toggleEffect(event, target) {
|
||||
const effect = this._getEmbeddedDocument(target);
|
||||
effect.update({ disabled: !effect.disabled });
|
||||
}
|
||||
|
||||
/* -------------------------------------------------- */
|
||||
/* Helper functions */
|
||||
/* -------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Fetches the embedded document representing the containing HTML element.
|
||||
*
|
||||
* @param {HTMLElement} target The element subject to search.
|
||||
* @returns {Item|ActiveEffect} The embedded Item or ActiveEffect.
|
||||
*/
|
||||
_getEmbeddedDocument(target) {
|
||||
const docRow = target.closest("[data-document-class]");
|
||||
if (docRow.dataset.documentClass === "Item") {
|
||||
return this.actor.items.get(docRow.dataset.itemId);
|
||||
} else if (docRow.dataset.documentClass === "ActiveEffect") {
|
||||
const parent =
|
||||
docRow.dataset.parentId === this.actor.id
|
||||
? this.actor
|
||||
: this.actor.items.get(docRow?.dataset.parentId);
|
||||
return parent.effects.get(docRow.dataset.effectId);
|
||||
} else {
|
||||
console.warn("Could not find document class");
|
||||
}
|
||||
}
|
||||
|
||||
/* -------------------------------------------------- */
|
||||
/* Actor override handling */
|
||||
/* -------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Submit a document update based on the processed form data.
|
||||
* @param {SubmitEvent} event The originating form submission event.
|
||||
* @param {HTMLFormElement} form The form element that was submitted.
|
||||
* @param {object} submitData Processed and validated form data to be used for a document update.
|
||||
* @returns {Promise<void>}
|
||||
* @protected
|
||||
* @inheritdoc
|
||||
*/
|
||||
async _processSubmitData(event, form, submitData) {
|
||||
const overrides = foundry.utils.flattenObject(this.actor.overrides);
|
||||
for (const k of Object.keys(overrides)) delete submitData[k];
|
||||
this.document.update(submitData);
|
||||
}
|
||||
|
||||
/* -------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Disables inputs subject to active effects.
|
||||
*/
|
||||
#disableOverrides() {
|
||||
const flatOverrides = foundry.utils.flattenObject(this.actor.overrides);
|
||||
for (const override of Object.keys(flatOverrides)) {
|
||||
const input = this.element.querySelector(`[name="${override}"]`);
|
||||
if (input) input.disabled = true;
|
||||
}
|
||||
}
|
||||
|
||||
/* -------------------------------------------------- */
|
||||
/* Drag and drop */
|
||||
/* -------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Define whether a user is able to begin a dragstart workflow for a given drag selector.
|
||||
* @param {string} selector The candidate HTML selector for dragging.
|
||||
* @returns {boolean} Can the current user drag this selector?
|
||||
* @protected
|
||||
*/
|
||||
_canDragStart(selector) {
|
||||
return this.isEditable;
|
||||
}
|
||||
|
||||
/* -------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Define whether a user is able to conclude a drag-and-drop workflow for a given drop selector.
|
||||
* @param {string} selector The candidate HTML selector for the drop target.
|
||||
* @returns {boolean} Can the current user drop on this selector?
|
||||
* @protected
|
||||
*/
|
||||
_canDragDrop(selector) {
|
||||
return this.isEditable;
|
||||
}
|
||||
|
||||
/* -------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Callback actions which occur at the beginning of a drag start workflow.
|
||||
* @param {DragEvent} event The originating DragEvent.
|
||||
* @protected
|
||||
*/
|
||||
_onDragStart(event) {
|
||||
const li = event.currentTarget;
|
||||
const dataset = event.target.dataset;
|
||||
|
||||
if ("link" in event.target.dataset) return;
|
||||
|
||||
let dragData = null;
|
||||
|
||||
if (li.dataset.effectId) {
|
||||
const effect = this.item.effects.get(li.dataset.effectId);
|
||||
dragData = effect.toDragData();
|
||||
}
|
||||
|
||||
dragData = {
|
||||
type: dataset.type,
|
||||
itemId: dataset.itemId
|
||||
};
|
||||
|
||||
if (!dragData) return;
|
||||
|
||||
event.dataTransfer.setData("text/plain", JSON.stringify(dragData));
|
||||
}
|
||||
|
||||
/* -------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Callback actions which occur when a dragged element is over a drop target.
|
||||
* @param {DragEvent} event The originating DragEvent.
|
||||
* @protected
|
||||
*/
|
||||
_onDragOver(event) {}
|
||||
|
||||
/* -------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Callback actions which occur when a dragged element is dropped on a target.
|
||||
* @param {DragEvent} event The originating DragEvent.
|
||||
* @protected
|
||||
*/
|
||||
async _onDrop(event) {
|
||||
const data = foundry.applications.ux.TextEditor.implementation.getDragEventData(event);
|
||||
const item = this.item;
|
||||
|
||||
const allowed = Hooks.call("dropItemSheetData", item, this, data);
|
||||
if (allowed === false) return;
|
||||
// Handle different data types
|
||||
switch (data.type) {
|
||||
case "ActiveEffect":
|
||||
return this._onDropActiveEffect(event, data);
|
||||
case "Actor":
|
||||
return this._onDropActor(event, data);
|
||||
case "Item":
|
||||
return this._onDropItem(event, data);
|
||||
case "Folder":
|
||||
return this._onDropFolder(event, data);
|
||||
}
|
||||
}
|
||||
|
||||
/* -------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Handle the dropping of ActiveEffect data onto an Actor Sheet.
|
||||
* @param {DragEvent} event The concluding DragEvent which contains drop data.
|
||||
* @param {object} data The data transfer extracted from the event.
|
||||
* @returns {Promise<ActiveEffect|boolean>} The created ActiveEffect object or false if it couldn't be created.
|
||||
* @protected
|
||||
*/
|
||||
async _onDropActiveEffect(event, data) {
|
||||
const aeCls = getDocumentClass("ActiveEffect");
|
||||
const effect = await aeCls.fromDropData(data);
|
||||
if (!this.item.isOwner || !effect) return false;
|
||||
|
||||
if (this.item.uuid === effect.parent?.uuid) return this._onEffectSort(event, effect);
|
||||
aeCls.create(effect, { parent: this.item });
|
||||
}
|
||||
|
||||
/* -------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Sorts an Active Effect based on its surrounding attributes.
|
||||
*
|
||||
* @param {DragEvent} event
|
||||
* @param {ActiveEffect} effect
|
||||
*/
|
||||
_onEffectSort(event, effect) {
|
||||
const effects = this.item.effects;
|
||||
const dropTarget = event.target.closest("[data-effect-id]");
|
||||
if (!dropTarget) return;
|
||||
const target = effects.get(dropTarget.dataset.effectId);
|
||||
|
||||
// Don't sort on yourself
|
||||
if (effect.id === target.id) return;
|
||||
|
||||
// Identify sibling items based on adjacent HTML elements
|
||||
const siblings = [];
|
||||
for (let el of dropTarget.parentElement.children) {
|
||||
const siblingId = el.dataset.effectId;
|
||||
if (siblingId && siblingId !== effect.id) siblings.push(effects.get(el.dataset.effectId));
|
||||
}
|
||||
|
||||
// Perform the sort
|
||||
const sortUpdates = foundry.utils.performIntegerSort(effect, {
|
||||
target,
|
||||
siblings
|
||||
});
|
||||
const updateData = sortUpdates.map((u) => {
|
||||
const update = u.update;
|
||||
update._id = u.target._id;
|
||||
return update;
|
||||
});
|
||||
|
||||
// Perform the update
|
||||
this.item.updateEmbeddedDocuments("ActiveEffect", updateData);
|
||||
}
|
||||
|
||||
/* -------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Handle dropping of an Actor data onto another Actor sheet.
|
||||
* @param {DragEvent} event The concluding DragEvent which contains drop data.
|
||||
* @param {object} data The data transfer extracted from the event.
|
||||
* @returns {Promise<object|boolean>} A data object which describes the result of the drop, or false if the drop was
|
||||
* not permitted.
|
||||
* @protected
|
||||
*/
|
||||
async _onDropActor(event, data) {
|
||||
if (!this.item.isOwner) return false;
|
||||
}
|
||||
|
||||
/* -------------------------------------------------- */
|
||||
|
||||
async _onDropItemCreate(actor, itemData) {
|
||||
itemData = itemData instanceof Array ? itemData : [itemData];
|
||||
return actor.createEmbeddedDocuments("Item", itemData);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle dropping of an item reference or item data onto an Actor Sheet.
|
||||
* @param {DragEvent} event The concluding DragEvent which contains drop data.
|
||||
* @param {object} data The data transfer extracted from the event.
|
||||
* @returns {Promise<Item[]|boolean>} The created or updated Item instances, or false if the drop was not permitted.
|
||||
* @protected
|
||||
*/
|
||||
async _onDropItem(event, data) {
|
||||
if (!this.actor.isOwner) return false;
|
||||
|
||||
if (this.actor.items.get(data.itemId)) return this._onSortItem(event, data);
|
||||
|
||||
const item = await Item.implementation.fromDropData(data);
|
||||
const itemData = item.toObject();
|
||||
|
||||
return this._onDropItemCreate(this.actor, itemData);
|
||||
}
|
||||
|
||||
/**
|
||||
* Handle a drop event for an existing embedded Item to sort that Item relative to its siblings
|
||||
* @param {Event} event
|
||||
* @param {Item} item
|
||||
* @private
|
||||
*/
|
||||
_onSortItem(event, item) {
|
||||
// Get the drag source and drop target
|
||||
const items = this.actor.items;
|
||||
const dropTarget = event.target.closest("[data-item-id]");
|
||||
const source = items.get(item.itemId);
|
||||
if (!dropTarget) return;
|
||||
const target = items.get(dropTarget.dataset.itemId);
|
||||
// Don't sort on yourself
|
||||
if (source.id === target.id) return;
|
||||
// Identify sibling items based on adjacent HTML elements
|
||||
const siblings = [];
|
||||
for (let el of dropTarget.parentElement.children) {
|
||||
const siblingId = el.dataset.itemId;
|
||||
if (siblingId && siblingId !== source.id) siblings.push(items.get(siblingId));
|
||||
}
|
||||
|
||||
// Perform the sort
|
||||
const sortUpdates = foundry.utils.performIntegerSort(source, {
|
||||
target,
|
||||
siblings
|
||||
});
|
||||
const updateData = sortUpdates.map((u) => {
|
||||
const update = u.update;
|
||||
update._id = u.target.id;
|
||||
return update;
|
||||
});
|
||||
|
||||
// Perform the update
|
||||
return this.actor.updateEmbeddedDocuments("Item", updateData);
|
||||
}
|
||||
|
||||
/* -------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Handle dropping of a Folder on an Actor Sheet.
|
||||
* The core sheet currently supports dropping a Folder of Items to create all items as owned items.
|
||||
* @param {DragEvent} event The concluding DragEvent which contains drop data.
|
||||
* @param {object} data The data transfer extracted from the event.
|
||||
* @returns {Promise<Item[]>}
|
||||
* @protected
|
||||
*/
|
||||
async _onDropFolder(event, data) {
|
||||
if (!this.item.isOwner) return [];
|
||||
}
|
||||
|
||||
/* -------------------------------------------------- */
|
||||
/* The following pieces set up drag */
|
||||
/* handling and are unlikely to need modification */
|
||||
/* -------------------------------------------------- */
|
||||
|
||||
// This is marked as private because there's no real need
|
||||
// for subclasses or external hooks to mess with it directly
|
||||
#dragDrop = this.#createDragDropHandlers();
|
||||
|
||||
/**
|
||||
* Returns an array of DragDrop instances.
|
||||
* @type {DragDrop[]}
|
||||
*/
|
||||
get dragDrop() {
|
||||
return this.#dragDrop;
|
||||
}
|
||||
|
||||
/* -------------------------------------------------- */
|
||||
|
||||
/**
|
||||
* Create drag-and-drop workflow handlers for this Application.
|
||||
* @returns {DragDrop[]} An array of DragDrop handlers.
|
||||
* @private
|
||||
*/
|
||||
#createDragDropHandlers() {
|
||||
return this.options.dragDrop.map((d) => {
|
||||
d.permissions = {
|
||||
dragstart: this._canDragStart.bind(this),
|
||||
drop: this._canDragDrop.bind(this)
|
||||
};
|
||||
d.callbacks = {
|
||||
dragstart: this._onDragStart.bind(this),
|
||||
dragover: this._onDragOver.bind(this),
|
||||
drop: this._onDrop.bind(this)
|
||||
};
|
||||
return new foundry.applications.ux.DragDrop(d);
|
||||
});
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user