2019-11-01 23:01:16 +08:00
|
|
|
import { get } from "@ember/object";
|
|
|
|
|
|
|
|
export const RUNTIME_OPTIONS = {
|
2020-09-23 02:18:47 +08:00
|
|
|
allowProtoPropertiesByDefault: true,
|
|
|
|
};
|
|
|
|
|
2023-10-19 22:52:51 +08:00
|
|
|
export function registerRawHelpers(hbs, handlebarsClass, owner) {
|
2020-01-08 04:37:37 +08:00
|
|
|
if (!hbs.helpers) {
|
|
|
|
hbs.helpers = Object.create(handlebarsClass.helpers);
|
|
|
|
}
|
2023-10-19 22:52:51 +08:00
|
|
|
|
|
|
|
lazyLoadHelpers(hbs, owner);
|
|
|
|
|
2021-03-22 08:35:51 +08:00
|
|
|
if (hbs.__helpers_registered) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
hbs.__helpers_registered = true;
|
2019-11-01 23:01:16 +08:00
|
|
|
|
|
|
|
hbs.helpers["get"] = function (context, options) {
|
2020-01-08 04:37:37 +08:00
|
|
|
if (!context || !options.contexts) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (typeof context !== "string") {
|
|
|
|
return context;
|
|
|
|
}
|
|
|
|
|
|
|
|
let firstContext = options.contexts[0];
|
|
|
|
let val = firstContext[context];
|
2019-11-01 23:01:16 +08:00
|
|
|
|
2022-07-18 02:16:39 +08:00
|
|
|
if (context.toString().startsWith("controller.")) {
|
2019-11-01 23:01:16 +08:00
|
|
|
context = context.slice(context.indexOf(".") + 1);
|
|
|
|
}
|
|
|
|
|
|
|
|
return val === undefined ? get(firstContext, context) : val;
|
|
|
|
};
|
|
|
|
|
|
|
|
// #each .. in support (as format is transformed to this)
|
2021-01-27 19:39:20 +08:00
|
|
|
hbs.registerHelper(
|
|
|
|
"each",
|
|
|
|
function (localName, inKeyword, contextName, options) {
|
|
|
|
if (typeof contextName === "undefined") {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
let list = get(this, contextName);
|
|
|
|
let output = [];
|
2024-09-17 18:07:07 +08:00
|
|
|
let innerContext = options.contexts[0];
|
2021-01-27 19:39:20 +08:00
|
|
|
for (let i = 0; i < list.length; i++) {
|
|
|
|
innerContext[localName] = list[i];
|
|
|
|
output.push(options.fn(innerContext));
|
|
|
|
}
|
2024-09-17 18:07:07 +08:00
|
|
|
delete innerContext[localName];
|
2021-01-27 19:39:20 +08:00
|
|
|
return output.join("");
|
2019-11-01 23:01:16 +08:00
|
|
|
}
|
2021-01-27 19:39:20 +08:00
|
|
|
);
|
2019-11-01 23:01:16 +08:00
|
|
|
|
|
|
|
function stringCompatHelper(fn) {
|
|
|
|
const old = hbs.helpers[fn];
|
|
|
|
hbs.helpers[fn] = function (context, options) {
|
|
|
|
return old.apply(this, [hbs.helpers.get(context, options), options]);
|
|
|
|
};
|
|
|
|
}
|
2020-01-31 00:41:39 +08:00
|
|
|
|
|
|
|
// HACK: Ensure that the variable is resolved only once.
|
|
|
|
// The "get" function will be called twice because both `if` and `unless`
|
|
|
|
// helpers are patched to resolve the variable and `unless` is implemented
|
|
|
|
// as not `if`. For example, for {{#unless var}} will generate a stack
|
|
|
|
// trace like:
|
|
|
|
//
|
|
|
|
// - patched-unless("var") "var" is resolved to its value, val
|
|
|
|
// - unless(val) unless is implemented as !if
|
|
|
|
// - !patched-if(val) val is already resolved, but it is resolved again
|
|
|
|
// - !if(???) at this point, ??? usually stands for undefined
|
|
|
|
//
|
|
|
|
// The following code ensures that patched-unless will call `if` directly,
|
|
|
|
// `patched-unless("var")` will return `!if(val)`.
|
|
|
|
const oldIf = hbs.helpers["if"];
|
|
|
|
hbs.helpers["unless"] = function (context, options) {
|
|
|
|
return oldIf.apply(this, [
|
|
|
|
hbs.helpers.get(context, options),
|
|
|
|
{
|
|
|
|
fn: options.inverse,
|
|
|
|
inverse: options.fn,
|
|
|
|
hash: options.hash,
|
|
|
|
},
|
|
|
|
]);
|
|
|
|
};
|
|
|
|
|
2019-11-01 23:01:16 +08:00
|
|
|
stringCompatHelper("if");
|
|
|
|
stringCompatHelper("with");
|
|
|
|
}
|
2023-10-19 22:52:51 +08:00
|
|
|
|
|
|
|
function lazyLoadHelpers(hbs, owner) {
|
|
|
|
// Reimplements `helperMissing` so that it triggers a lookup() for
|
|
|
|
// a helper of that name. Means we don't need to eagerly load all
|
|
|
|
// helpers/* files during boot.
|
|
|
|
hbs.registerHelper("helperMissing", function (...args) {
|
|
|
|
const opts = args[args.length - 1];
|
|
|
|
if (opts?.name) {
|
2024-07-27 05:13:12 +08:00
|
|
|
// Lookup and evaluate the relevant module. Raw helpers may be registered as a side effect
|
2023-10-19 22:52:51 +08:00
|
|
|
owner.lookup(`helper:${opts.name}`);
|
|
|
|
|
|
|
|
if (hbs.helpers[opts.name]) {
|
|
|
|
// Helper now exists, invoke it
|
|
|
|
return hbs.helpers[opts.name]?.call(this, ...arguments);
|
|
|
|
} else {
|
|
|
|
// Not a helper, treat as property
|
|
|
|
return hbs.helpers["get"].call(this, ...arguments);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|