Skip to content

Commit

Permalink
[#1401] Code style cleanups
Browse files Browse the repository at this point in the history
  • Loading branch information
arbron committed Jan 7, 2023
1 parent 4b96ef7 commit 8a9b4f9
Show file tree
Hide file tree
Showing 8 changed files with 46 additions and 47 deletions.
18 changes: 9 additions & 9 deletions less/advancement.less
Original file line number Diff line number Diff line change
Expand Up @@ -54,15 +54,15 @@
grid-area: right;

&.level-list {
label {
flex: 0.5;
padding-right: 0.5rem;
text-align: end;
}
:is(input[type="text"], input[type="number"])::placeholder {
opacity: 0.5;
}
}
label {
flex: 0.5;
padding-right: 0.5rem;
text-align: end;
}
:is(input[type="text"], input[type="number"])::placeholder {
opacity: 0.5;
}
}
}
button[type="submit"] {
grid-column-end: span 2;
Expand Down
15 changes: 7 additions & 8 deletions module/applications/advancement/item-choice-config.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,13 @@ export default class ItemChoiceConfig extends AdvancementConfig {
if ( configuration.choices ) configuration.choices = this.constructor._cleanedObject(configuration.choices);

// Ensure items are still valid if type restriction or spell restriction are changed
configuration.pool ??= this.advancement.configuration.pool;
configuration.pool = await configuration.pool.reduce(async (pool, uuid) => {
const item = await fromUuid(uuid);
if ( this.advancement._validateItemType(item, {
type: configuration.type, restriction: configuration.restriction ?? {}, error: false
}) ) return [...await pool, uuid];
return pool;
}, []);
const pool = [];
for ( const uuid of (configuration.pool ?? this.advancement.configuration.pool) ) {
if ( this.advancement._validateItemType(await fromUuid(uuid), {
type: configuration.type, restriction: configuration.restriction ?? {}, strict: false
}) ) pool.push(uuid);
}
configuration.pool = pool;

return configuration;
}
Expand Down
18 changes: 9 additions & 9 deletions module/applications/advancement/item-choice-flow.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,13 @@ export default class ItemChoiceFlow extends ItemGrantFlow {

/**
* Cached items from the advancement's pool.
* @type {Array<Item5e>}
* @type {Item5e[]}
*/
pool;

/**
* List of dropped items.
* @type {Array<Item5e>}
* @type {Item5e[]}
*/
dropped;

Expand All @@ -42,16 +42,16 @@ export default class ItemChoiceFlow extends ItemGrantFlow {
?? Object.values(this.advancement.value[this.level] ?? {})
);
this.pool ??= await Promise.all(this.advancement.configuration.pool.map(fromUuid));
this.dropped ??= await (this.retainedData?.items ?? []).reduce(async (arrP, data) => {
const arr = await arrP;
const uuid = foundry.utils.getProperty(data, "flags.dnd5e.sourceId");
if ( !this.pool.find(i => uuid === i.uuid) ) {
if ( !this.dropped ) {
this.dropped = [];
for ( const data of this.retainedData?.items ?? [] ) {
const uuid = foundry.utils.getProperty(data, "flags.dnd5e.sourceId");
if ( this.pool.find(i => uuid === i.uuid) ) continue;
const item = await fromUuid(uuid);
item.dropped = true;
arr.push(item);
this.dropped.push(item);
}
return arr;
}, []);
}

const max = this.advancement.configuration.choices[this.level];
const choices = { max, current: this.selected.size, full: this.selected.size >= max };
Expand Down
2 changes: 1 addition & 1 deletion module/data/advancement/item-choice.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ export default class ItemChoiceConfigurationData extends foundry.abstract.DataMo
type: new foundry.data.fields.StringField({label: "DND5E.Type"}),
subtype: new foundry.data.fields.StringField({label: "DND5E.Subtype"}),
level: new foundry.data.fields.StringField({label: "DND5E.SpellLevel"})
}, {label: ""})
})
};
}
}
24 changes: 12 additions & 12 deletions module/documents/advancement/item-choice.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -74,23 +74,23 @@ export default class ItemChoiceAdvancement extends ItemGrantAdvancement {

/**
* Verify that the provided item can be used with this advancement based on the configuration.
* @param {Item5e} item Item that needs to be tested.
* @param {Item5e} item Item that needs to be tested.
* @param {object} config
* @param {string} config.type Type restriction on this advancement.
* @param {object} config.restriction Additional restrictions to be applied.
* @param {boolean} [config.error=true] Should an error be thrown when an invalid type is encountered?
* @returns {boolean} Is this type valid?
* @throws An error if the item is invalid and warn is `true`.
* @param {string} config.type Type restriction on this advancement.
* @param {object} config.restriction Additional restrictions to be applied.
* @param {boolean} [config.strict=true] Should an error be thrown when an invalid type is encountered?
* @returns {boolean} Is this type valid?
* @throws An error if the item is invalid and strict is `true`.
*/
_validateItemType(item, { type, restriction, error=true }={}) {
super._validateItemType(item, { error });
_validateItemType(item, { type, restriction, strict=true }={}) {
super._validateItemType(item, { strict });
type ??= this.configuration.type;
restriction ??= this.configuration.restriction;

// Type restriction is set and the item type does not match the selected type
if ( type && (type !== item.type) ) {
const typeLabel = game.i18n.localize(`ITEM.Type${restriction.capitalize()}`);
if ( error ) throw new Error(game.i18n.format("DND5E.AdvancementItemChoiceTypeWarning", { type: typeLabel }));
if ( strict ) throw new Error(game.i18n.format("DND5E.AdvancementItemChoiceTypeWarning", {type: typeLabel}));
return false;
}

Expand All @@ -102,16 +102,16 @@ export default class ItemChoiceAdvancement extends ItemGrantAdvancement {
if ( restriction.type !== item.system.type.value ) errorLabel = typeConfig.label;
else if ( subtype && (restriction.subtype !== item.system.type.subtype) ) errorLabel = subtype;
if ( errorLabel ) {
if ( error ) throw new Error(game.i18n.format("DND5E.AdvancementItemChoiceTypeWarning", { type: errorLabel }));
if ( strict ) throw new Error(game.i18n.format("DND5E.AdvancementItemChoiceTypeWarning", {type: errorLabel}));
return false;
}
}

// If spell level is restricted, ensure the spell is of the appropriate level
const l = parseInt(restriction.level);
if ( (type === "spell") && Number.isNumeric(l) && (item.system.level !== l) ) {
if ( (type === "spell") && !Number.isNaN(l) && (item.system.level !== l) ) {
const level = CONFIG.DND5E.spellLevels[l];
if ( error ) throw new Error(game.i18n.format("DND5E.AdvancementItemChoiceSpellLevelSpecificWarning", { level }));
if ( strict ) throw new Error(game.i18n.format("DND5E.AdvancementItemChoiceSpellLevelSpecificWarning", {level}));
return false;
}

Expand Down
12 changes: 6 additions & 6 deletions module/documents/advancement/item-grant.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -140,16 +140,16 @@ export default class ItemGrantAdvancement extends Advancement {

/**
* Verify that the provided item can be used with this advancement based on the configuration.
* @param {Item5e} item Item that needs to be tested.
* @param {Item5e} item Item that needs to be tested.
* @param {object} config
* @param {boolean} [config.error=true] Should an error be thrown when an invalid type is encountered?
* @returns {boolean} Is this type valid?
* @throws An error if the item is invalid and warn is `true`.
* @param {boolean} [config.strict=true] Should an error be thrown when an invalid type is encountered?
* @returns {boolean} Is this type valid?
* @throws An error if the item is invalid and strict is `true`.
*/
_validateItemType(item, { error=true }={}) {
_validateItemType(item, { strict=true }={}) {
if ( this.constructor.VALID_TYPES.has(item.type) ) return true;
const type = game.i18n.localize(`ITEM.Type${item.type.capitalize()}`);
if ( error ) throw new Error(game.i18n.format("DND5E.AdvancementItemTypeInvalidWarning", { type }));
if ( strict ) throw new Error(game.i18n.format("DND5E.AdvancementItemTypeInvalidWarning", {type}));
return false;
}
}
2 changes: 1 addition & 1 deletion templates/advancement/item-choice-flow.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
<h4 class="form-header">{{localize "DND5E.AdvancementLevelHeader" level=@key}}</h4>
{{#each this}}
<div class="item-name flexrow">
<div class="item-image" style="background-image: url({{this.img}});"></div>
<div class="item-image" style="background-image: url('{{this.img}}');"></div>
<label class="flexrow"><h4><a data-uuid="{{this.uuid}}">{{this.name}}</a></h4></label>
</div>
{{/each}}
Expand Down
2 changes: 1 addition & 1 deletion templates/advancement/item-grant-flow.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

{{#each items}}
<div class="item-name flexrow">
<div class="item-image" style="background-image: url({{this.img}});"></div>
<div class="item-image" style="background-image: url('{{this.img}}');"></div>
<label class="flexrow">
<h4>
<a data-uuid="{{uuid}}">{{this.name}}</a>
Expand Down

0 comments on commit 8a9b4f9

Please sign in to comment.