Add my typical options helper for ease of use
This commit is contained in:
parent
61f41d610e
commit
40820988da
2 changed files with 36 additions and 0 deletions
|
|
@ -1,5 +1,7 @@
|
||||||
import { filePath } from "../consts.mjs";
|
import { filePath } from "../consts.mjs";
|
||||||
|
import { options } from "./options.mjs";
|
||||||
|
|
||||||
export default {
|
export default {
|
||||||
systemFilePath: filePath,
|
systemFilePath: filePath,
|
||||||
|
"taf-options": options,
|
||||||
};
|
};
|
||||||
|
|
|
||||||
34
module/handlebarsHelpers/options.mjs
Normal file
34
module/handlebarsHelpers/options.mjs
Normal file
|
|
@ -0,0 +1,34 @@
|
||||||
|
/**
|
||||||
|
* @typedef {object} Option
|
||||||
|
* @property {string} [label]
|
||||||
|
* @property {string|number} value
|
||||||
|
* @property {boolean} [disabled]
|
||||||
|
*/
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param {string | number} selected The selected value
|
||||||
|
* @param {Array<Option | string>} opts The options that are valid
|
||||||
|
* @param {any} meta The Handlebars meta processing
|
||||||
|
*/
|
||||||
|
export function options(selected, opts, meta) {
|
||||||
|
const { localize = false } = meta.hash;
|
||||||
|
selected = Handlebars.escapeExpression(selected);
|
||||||
|
const htmlOptions = [];
|
||||||
|
|
||||||
|
for (let opt of opts) {
|
||||||
|
if (typeof opt === `string`) {
|
||||||
|
opt = { label: opt, value: opt };
|
||||||
|
};
|
||||||
|
opt.value = Handlebars.escapeExpression(opt.value);
|
||||||
|
htmlOptions.push(
|
||||||
|
`<option
|
||||||
|
value="${opt.value}"
|
||||||
|
${selected === opt.value ? `selected` : ``}
|
||||||
|
${opt.disabled ? `disabled` : ``}
|
||||||
|
>
|
||||||
|
${localize ? game.i18n.format(opt.label) : opt.label}
|
||||||
|
</option>`,
|
||||||
|
);
|
||||||
|
};
|
||||||
|
return new Handlebars.SafeString(htmlOptions.join(`\n`));
|
||||||
|
};
|
||||||
Loading…
Add table
Add a link
Reference in a new issue