Add Handlebars helpers
This commit is contained in:
parent
06c5320786
commit
edd4e49f62
4 changed files with 87 additions and 0 deletions
11
module/handlebarHelpers/_index.mjs
Normal file
11
module/handlebarHelpers/_index.mjs
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
import { handlebarsLocalizer, localizer } from "../utils/Localizer.mjs";
|
||||
import { options } from "./options.mjs";
|
||||
|
||||
export default {
|
||||
// #region Complex
|
||||
"rc-i18n": handlebarsLocalizer,
|
||||
"rc-options": options,
|
||||
|
||||
// #region Simple
|
||||
"rc-empty-state": (v) => v ?? localizer(`RipCrypt.common.empty`),
|
||||
};
|
||||
36
module/handlebarHelpers/options.mjs
Normal file
36
module/handlebarHelpers/options.mjs
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
import { localizer } from "../utils/Localizer.mjs";
|
||||
|
||||
/**
|
||||
* @typedef {object} Option
|
||||
* @property {string} [label]
|
||||
* @property {string|number} value
|
||||
* @property {boolean} [disabled]
|
||||
*/
|
||||
|
||||
/**
|
||||
* @param {string | number} selected
|
||||
* @param {Array<Option | string>} opts
|
||||
* @param {any} meta
|
||||
*/
|
||||
export function options(selected, opts, meta) {
|
||||
const { localize = false } = meta;
|
||||
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 ? localizer(opt.label) : opt.label}
|
||||
</option>`,
|
||||
);
|
||||
};
|
||||
};
|
||||
Loading…
Add table
Add a link
Reference in a new issue