[IMP] app: add global values at compile time

This commit, add a new configuration on the App: globalValues.
It's a global object of elements available at compilations.

For instance:
```js
    const app = new App(SomeComponent, {
      globalValues: {
        plop: (string: any) => {
          steps.push(string);
        },
      },
    });
```

The plop function will be available at the compilation, so it can be
used on the templates :
```xml
<div t-on-click="() => __globals__.plop('click')" class="my-div"/>
```
This commit is contained in:
Jorge Pinna Puissant
2024-11-12 16:37:03 +01:00
committed by Géry Debongnie
parent 7e687234bf
commit 6b2486473f
8 changed files with 50 additions and 3 deletions
+4
View File
@@ -43,6 +43,7 @@ export interface Config {
export interface CodeGenOptions extends Config {
hasSafeContext?: boolean;
name?: string;
hasGlobalValues: boolean;
}
// using a non-html document so that <inner/outer>HTML serializes as XML instead
@@ -286,6 +287,9 @@ export class CodeGenerator {
this.dev = options.dev || false;
this.ast = ast;
this.templateName = options.name;
if (options.hasGlobalValues) {
this.helpers.add("__globals__");
}
}
generateCode(): string {
+4 -1
View File
@@ -12,10 +12,13 @@ export type TemplateFunction = (app: TemplateSet, bdom: any, helpers: any) => Te
interface CompileOptions extends Config {
name?: string;
customDirectives?: customDirectives;
hasGlobalValues: boolean;
}
export function compile(
template: string | Element,
options: CompileOptions = {}
options: CompileOptions = {
hasGlobalValues: false,
}
): TemplateFunction {
// parsing
const ast = parse(template, options.customDirectives);
+1 -1
View File
@@ -28,7 +28,7 @@ import { OwlError } from "../common/owl_error";
//------------------------------------------------------------------------------
const RESERVED_WORDS =
"true,false,NaN,null,undefined,debugger,console,window,in,instanceof,new,function,return,eval,void,Math,RegExp,Array,Object,Date".split(
"true,false,NaN,null,undefined,debugger,console,window,in,instanceof,new,function,return,eval,void,Math,RegExp,Array,Object,Date,__globals__".split(
","
);
+1
View File
@@ -13,5 +13,6 @@ TemplateSet.prototype._compileTemplate = function _compileTemplate(
translateFn: this.translateFn,
translatableAttributes: this.translatableAttributes,
customDirectives: this.customDirectives,
hasGlobalValues: this.hasGlobalValues,
});
};
+6 -1
View File
@@ -16,6 +16,7 @@ export interface TemplateSetConfig {
templates?: string | Document | Record<string, string>;
getTemplate?: (s: string) => Element | Function | string | void;
customDirectives?: customDirectives;
globalValues?: object;
}
export class TemplateSet {
@@ -30,6 +31,8 @@ export class TemplateSet {
translatableAttributes?: string[];
Portal = Portal;
customDirectives: customDirectives;
runtimeUtils: object;
hasGlobalValues: boolean;
constructor(config: TemplateSetConfig = {}) {
this.dev = config.dev || false;
@@ -46,6 +49,8 @@ export class TemplateSet {
}
this.getRawTemplate = config.getTemplate;
this.customDirectives = config.customDirectives || {};
this.runtimeUtils = { ...helpers, __globals__: config.globalValues || {} };
this.hasGlobalValues = Boolean(config.globalValues && Object.keys(config.globalValues).length);
}
addTemplate(name: string, template: string | Element) {
@@ -101,7 +106,7 @@ export class TemplateSet {
this.templates[name] = function (context, parent) {
return templates[name].call(this, context, parent);
};
const template = templateFn(this, bdom, helpers);
const template = templateFn(this, bdom, this.runtimeUtils);
this.templates[name] = template;
}
return this.templates[name];