darkness-dependent-vision/src/darkness-dependent-vision-config.js

95 lines
3 KiB
JavaScript

// SPDX-FileCopyrightText: 2021 Johannes Loher
//
// SPDX-License-Identifier: MIT
/**
* An application that provides functionality to configure darkness dependent
* vision for a {@link TokenDocument}.
* @extends {FormApplication}
* @remarks Most of the code of this class is heavily inspired by the
* implementation of {@link TokenConfig}.
*/
export class DarknessDependentVisionConfig extends FormApplication {
constructor(object, options) {
super(object, options);
this.token = this.object;
if (this.isPrototype) this.token = new PrototypeTokenDocument(this.object.data.token, { actor: this.object });
}
static get defaultOptions() {
return foundry.utils.mergeObject(super.defaultOptions, {
classes: ['darkness-dependent-vision-config'],
template: `modules/darkness-dependent-vision/templates/darkness-dependent-vision-config.hbs`,
width: 480,
height: 'auto',
});
}
get id() {
return `darkness-dependent-vision-config-${this.object.id}`;
}
/**
* A convenience accessor to test whether we are configuring the prototype Token for an Actor.
* @type {boolean}
*/
get isPrototype() {
return this.object instanceof Actor;
}
/**
* Convenience access to the Actor document that this Token represents
* @type {Actor}
*/
get actor() {
return this.isPrototype ? this.object : this.token.actor;
}
get title() {
const name = this.isPrototype
? `[${game.i18n.localize('TOKEN.TitlePrototype')}] ${this.actor.name}`
: this.token.name;
return `${name}: ${game.i18n.localize('DarknessDependentVision.Title')}`;
}
get isDefault() {
return this.options.tokenConfig instanceof DefaultTokenConfig;
}
async getData() {
const data = this.isPrototype ? this.actor.data.token : this.token.data;
return {
object: data,
};
}
async render(force, options) {
const canConfigure = game.user.isGM || this.actor?.isOwner;
if (!game.user.can('TOKEN_CONFIGURE') || !canConfigure) {
ui.notifications?.warn(game.i18n.localize('DarknessDependentVision.WarningLackingPermissionToConfigure'));
return this;
}
return super.render(force, options);
}
async _updateObject(event, formData) {
// Configure the Default Token Configuration
if (this.isDefault) {
const current = game.settings.get('core', DefaultTokenConfig.SETTING);
const update = foundry.utils.mergeObject(current, formData, { inplace: false });
const result = await game.settings.set('core', DefaultTokenConfig.SETTING, update);
const tokenConfig = this.options.tokenConfig;
tokenConfig.data = new foundry.data.TokenData(result);
tokenConfig.object = new TokenDocument(tokenConfig.data, { actor: null });
tokenConfig.token = tokenConfig.object;
tokenConfig.render();
return result;
}
// Configure the Prototype Token data of an Actor
if (this.isPrototype) return this.actor.update({ token: formData });
// Update an embedded Token document
return this.token.update(formData);
}
}