-
Notifications
You must be signed in to change notification settings - Fork 345
Expand file tree
/
Copy patheffect-change-config.mjs
More file actions
161 lines (137 loc) · 5.16 KB
/
Copy patheffect-change-config.mjs
File metadata and controls
161 lines (137 loc) · 5.16 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import { formatNumber } from "../../utils.mjs";
import DocumentSheet5e from "../api/document-sheet.mjs";
/**
* Application for editing a single active effect change.
*/
export default class EffectChangeConfig extends DocumentSheet5e {
/** @inheritDoc */
static DEFAULT_OPTIONS = {
canImport: false,
changeId: null,
classes: ["standard-form", "titlebar"],
form: {
closeOnSubmit: true,
handler: EffectChangeConfig.#handleFormSubmission
},
ownershipConfig: false,
position: {
width: 500
},
sheetConfig: false,
tag: "form"
};
/* -------------------------------------------- */
/** @override */
static PARTS = {
config: {
template: "systems/dnd5e/templates/effects/change-config.hbs"
},
footer: {
template: "templates/generic/form-footer.hbs"
}
};
/* -------------------------------------------- */
/* Properties */
/* -------------------------------------------- */
/**
* The change's source data.
* @type {object}
*/
get change() {
return this.effect.system._source.changes.find(c => c._id === this.options.changeId);
}
/* -------------------------------------------- */
/**
* Active effect to which this change belongs.
* @type {ActiveEffect5e}
*/
get effect() {
return this.options.document;
}
/* -------------------------------------------- */
/** @override */
get title() {
const number = formatNumber(this.effect.system.changes.findIndex(c => c._id === this.options.changeId) + 1);
return _loc("DND5E.EFFECT.Change.Title", { effect: this.effect.name, number });
}
/* -------------------------------------------- */
/* Rendering */
/* -------------------------------------------- */
/** @override */
_canRender(options) {
if ( !this.rendered ) return;
if ( !this.change ) this.close();
return false;
}
/* -------------------------------------------- */
/** @inheritDoc */
_initializeApplicationOptions(options) {
options = super._initializeApplicationOptions(options);
options.uniqueId = `${options.uniqueId}-Change-${options.changeId}`;
return options;
}
/* -------------------------------------------- */
/** @inheritDoc */
async _preparePartContext(partId, context, options) {
context = await super._preparePartContext(partId, context, options);
switch ( partId ) {
case "config": return this._prepareConfigContext(context, options);
case "footer": return this._prepareFooterContext(context, options);
}
return context;
}
/* -------------------------------------------- */
/**
* Prepare rendering context for the config section.
* @param {ApplicationRenderContext} context Context being prepared.
* @param {HandlebarsRenderOptions} options Options which configure application rendering behavior.
* @returns {ApplicationRenderContext}
* @protected
*/
async _prepareConfigContext(context, options) {
context.source = this.change;
context.defaultPriority = ActiveEffect.CHANGE_TYPES[context.source?.type]?.defaultPriority;
context.fields = this.effect.system.schema.fields.changes.element.fields;
context.hintText = _loc("DND5E.EFFECT.AttributeKeyTooltip", {
url: this.effect.type === "enchantment"
? "https://github.com/foundryvtt/dnd5e/wiki/Enchantment"
: "https://github.com/foundryvtt/dnd5e/wiki/Active-Effect-Guide"
});
context.typeOptions = Object.entries(ActiveEffect.CHANGE_TYPES)
.map(([value, { group, label }]) => ({ value, label: _loc(label), group: _loc(
CONFIG.ActiveEffect.changeTypes[value]?.group
?? `DND5E.EFFECT.Change.Group.${value in CONST.ACTIVE_EFFECT_CHANGE_TYPES ? "Standard" : "Custom"}`
) }))
.sort((a, b) => a.label.localeCompare(b.label, game.i18n.lang));
return context;
}
/* -------------------------------------------- */
/**
* Prepare rendering context for the footer buttons.
* @param {ApplicationRenderContext} context Context being prepared.
* @param {HandlebarsRenderOptions} options Options which configure application rendering behavior.
* @returns {ApplicationRenderContext}
* @protected
*/
async _prepareFooterContext(context, options) {
context.buttons = [{ type: "submit", icon: "fa-solid fa-floppy-disk", label: "EFFECT.Submit" }];
return context;
}
/* -------------------------------------------- */
/* Form Handling */
/* -------------------------------------------- */
/**
* Handle submission of the application.
* @this {EffectChangeConfig}
* @param {Event|SubmitEvent} event The form submission event.
* @param {HTMLFormElement} form The submitted form.
* @param {FormDataExtended} formData Data from the dialog.
*/
static #handleFormSubmission(event, form, formData) {
const changes = this.effect.system.toObject().changes;
const change = changes.find(c => c._id === this.options.changeId);
if ( !change ) return;
foundry.utils.mergeObject(change, foundry.utils.expandObject(formData.object));
this.effect.update({ "system.changes": changes });
}
}