mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
[REF] app: move TemplateSet into its own file
This commit is contained in:
committed by
Samuel Degueldre
parent
a187a376a0
commit
9163bb1e08
+1
-1
@@ -2,7 +2,7 @@ import { Component } from "../component/component";
|
|||||||
import { ComponentNode } from "../component/component_node";
|
import { ComponentNode } from "../component/component_node";
|
||||||
import { MountOptions } from "../component/fibers";
|
import { MountOptions } from "../component/fibers";
|
||||||
import { Scheduler } from "../component/scheduler";
|
import { Scheduler } from "../component/scheduler";
|
||||||
import { TemplateSet } from "./template_helpers";
|
import { TemplateSet } from "./template_set";
|
||||||
|
|
||||||
// reimplement dev mode stuff see last change in 0f7a8289a6fb8387c3c1af41c6664b2a8448758f
|
// reimplement dev mode stuff see last change in 0f7a8289a6fb8387c3c1af41c6664b2a8448758f
|
||||||
|
|
||||||
|
|||||||
+15
-72
@@ -1,12 +1,6 @@
|
|||||||
import { BDom, createBlock, html, list, multi, text, toggler } from "../blockdom";
|
import { BDom, multi, text, toggler } from "../blockdom";
|
||||||
import { compile, Template } from "../compiler";
|
|
||||||
import { component } from "../component/component_node";
|
|
||||||
import { validateProps } from "../component/props_validation";
|
import { validateProps } from "../component/props_validation";
|
||||||
|
|
||||||
const bdom = { text, createBlock, list, multi, html, toggler, component };
|
|
||||||
|
|
||||||
export const globalTemplates: { [key: string]: string } = {};
|
|
||||||
|
|
||||||
function withDefault(value: any, defaultValue: any): any {
|
function withDefault(value: any, defaultValue: any): any {
|
||||||
return value === undefined || value === null || value === false ? defaultValue : value;
|
return value === undefined || value === null || value === false ? defaultValue : value;
|
||||||
}
|
}
|
||||||
@@ -82,71 +76,6 @@ function setContextValue(ctx: { [key: string]: any }, key: string, value: any):
|
|||||||
ctx[key] = value;
|
ctx[key] = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
export const UTILS = {
|
|
||||||
// elem,
|
|
||||||
// setText,
|
|
||||||
withDefault,
|
|
||||||
zero: Symbol("zero"),
|
|
||||||
isBoundary,
|
|
||||||
callSlot,
|
|
||||||
capture,
|
|
||||||
// toClassObj,
|
|
||||||
withKey,
|
|
||||||
prepareList,
|
|
||||||
setContextValue,
|
|
||||||
shallowEqual,
|
|
||||||
toNumber,
|
|
||||||
validateProps,
|
|
||||||
};
|
|
||||||
|
|
||||||
export class TemplateSet {
|
|
||||||
rawTemplates: { [name: string]: string } = Object.create(globalTemplates);
|
|
||||||
templates: { [name: string]: Template } = {};
|
|
||||||
translateFn?: (s: string) => string;
|
|
||||||
translatableAttributes?: string[];
|
|
||||||
utils: typeof UTILS;
|
|
||||||
dev?: boolean;
|
|
||||||
|
|
||||||
constructor() {
|
|
||||||
const call = (subTemplate: string, ctx: any, parent: any) => {
|
|
||||||
const template = this.getTemplate(subTemplate);
|
|
||||||
return toggler(subTemplate, template(ctx, parent));
|
|
||||||
};
|
|
||||||
|
|
||||||
const getTemplate = (name: string) => this.getTemplate(name);
|
|
||||||
this.utils = Object.assign({}, UTILS, { getTemplate, call });
|
|
||||||
}
|
|
||||||
|
|
||||||
addTemplate(name: string, template: string, options: { allowDuplicate?: boolean } = {}) {
|
|
||||||
if (name in this.rawTemplates && !options.allowDuplicate) {
|
|
||||||
throw new Error(`Template ${name} already defined`);
|
|
||||||
}
|
|
||||||
this.rawTemplates[name] = template;
|
|
||||||
}
|
|
||||||
|
|
||||||
getTemplate(name: string): Template {
|
|
||||||
if (!(name in this.templates)) {
|
|
||||||
const rawTemplate = this.rawTemplates[name];
|
|
||||||
if (rawTemplate === undefined) {
|
|
||||||
throw new Error(`Missing template: "${name}"`);
|
|
||||||
}
|
|
||||||
const templateFn = compile(rawTemplate, {
|
|
||||||
name,
|
|
||||||
dev: this.dev,
|
|
||||||
translateFn: this.translateFn,
|
|
||||||
translatableAttributes: this.translatableAttributes,
|
|
||||||
});
|
|
||||||
|
|
||||||
// first add a function to lazily get the template, in case there is a
|
|
||||||
// recursive call to the template name
|
|
||||||
this.templates[name] = (context, parent) => this.templates[name](context, parent);
|
|
||||||
const template = templateFn(bdom, this.utils);
|
|
||||||
this.templates[name] = template;
|
|
||||||
}
|
|
||||||
return this.templates[name];
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
function toNumber(val: string): number | string {
|
function toNumber(val: string): number | string {
|
||||||
const n = parseFloat(val);
|
const n = parseFloat(val);
|
||||||
return isNaN(n) ? val : n;
|
return isNaN(n) ? val : n;
|
||||||
@@ -160,3 +89,17 @@ function shallowEqual(l1: any[], l2: any[]): boolean {
|
|||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export const UTILS = {
|
||||||
|
withDefault,
|
||||||
|
zero: Symbol("zero"),
|
||||||
|
isBoundary,
|
||||||
|
callSlot,
|
||||||
|
capture,
|
||||||
|
withKey,
|
||||||
|
prepareList,
|
||||||
|
setContextValue,
|
||||||
|
shallowEqual,
|
||||||
|
toNumber,
|
||||||
|
validateProps,
|
||||||
|
};
|
||||||
|
|||||||
@@ -0,0 +1,56 @@
|
|||||||
|
import { createBlock, html, list, multi, text, toggler } from "../blockdom";
|
||||||
|
import { compile, Template } from "../compiler";
|
||||||
|
import { component } from "../component/component_node";
|
||||||
|
import { UTILS } from "./template_helpers";
|
||||||
|
|
||||||
|
const bdom = { text, createBlock, list, multi, html, toggler, component };
|
||||||
|
|
||||||
|
export const globalTemplates: { [key: string]: string } = {};
|
||||||
|
|
||||||
|
export class TemplateSet {
|
||||||
|
rawTemplates: { [name: string]: string } = Object.create(globalTemplates);
|
||||||
|
templates: { [name: string]: Template } = {};
|
||||||
|
translateFn?: (s: string) => string;
|
||||||
|
translatableAttributes?: string[];
|
||||||
|
utils: typeof UTILS;
|
||||||
|
dev?: boolean;
|
||||||
|
|
||||||
|
constructor() {
|
||||||
|
const call = (subTemplate: string, ctx: any, parent: any) => {
|
||||||
|
const template = this.getTemplate(subTemplate);
|
||||||
|
return toggler(subTemplate, template(ctx, parent));
|
||||||
|
};
|
||||||
|
|
||||||
|
const getTemplate = (name: string) => this.getTemplate(name);
|
||||||
|
this.utils = Object.assign({}, UTILS, { getTemplate, call });
|
||||||
|
}
|
||||||
|
|
||||||
|
addTemplate(name: string, template: string, options: { allowDuplicate?: boolean } = {}) {
|
||||||
|
if (name in this.rawTemplates && !options.allowDuplicate) {
|
||||||
|
throw new Error(`Template ${name} already defined`);
|
||||||
|
}
|
||||||
|
this.rawTemplates[name] = template;
|
||||||
|
}
|
||||||
|
|
||||||
|
getTemplate(name: string): Template {
|
||||||
|
if (!(name in this.templates)) {
|
||||||
|
const rawTemplate = this.rawTemplates[name];
|
||||||
|
if (rawTemplate === undefined) {
|
||||||
|
throw new Error(`Missing template: "${name}"`);
|
||||||
|
}
|
||||||
|
const templateFn = compile(rawTemplate, {
|
||||||
|
name,
|
||||||
|
dev: this.dev,
|
||||||
|
translateFn: this.translateFn,
|
||||||
|
translatableAttributes: this.translatableAttributes,
|
||||||
|
});
|
||||||
|
|
||||||
|
// first add a function to lazily get the template, in case there is a
|
||||||
|
// recursive call to the template name
|
||||||
|
this.templates[name] = (context, parent) => this.templates[name](context, parent);
|
||||||
|
const template = templateFn(bdom, this.utils);
|
||||||
|
this.templates[name] = template;
|
||||||
|
}
|
||||||
|
return this.templates[name];
|
||||||
|
}
|
||||||
|
}
|
||||||
+1
-1
@@ -1,5 +1,5 @@
|
|||||||
import { registerSheet } from "./component/style";
|
import { registerSheet } from "./component/style";
|
||||||
import { globalTemplates } from "./app/template_helpers";
|
import { globalTemplates } from "./app/template_set";
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
// Global templates
|
// Global templates
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { TemplateSet } from "../../src/app/template_helpers";
|
import { TemplateSet } from "../../src/app/template_set";
|
||||||
import { mount } from "../../src/blockdom";
|
import { mount } from "../../src/blockdom";
|
||||||
import { makeTestFixture, renderToBdom, renderToString, snapshotEverything } from "../helpers";
|
import { makeTestFixture, renderToBdom, renderToString, snapshotEverything } from "../helpers";
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { TemplateSet } from "../../src/app/template_helpers";
|
import { TemplateSet } from "../../src/app/template_set";
|
||||||
import { mount } from "../../src/blockdom";
|
import { mount } from "../../src/blockdom";
|
||||||
import { renderToBdom, snapshotEverything } from "../helpers";
|
import { renderToBdom, snapshotEverything } from "../helpers";
|
||||||
|
|
||||||
|
|||||||
@@ -1,4 +1,4 @@
|
|||||||
import { TemplateSet } from "../../src/app/template_helpers";
|
import { TemplateSet } from "../../src/app/template_set";
|
||||||
import { renderToString, TestContext, compile } from "../helpers";
|
import { renderToString, TestContext, compile } from "../helpers";
|
||||||
|
|
||||||
// -----------------------------------------------------------------------------
|
// -----------------------------------------------------------------------------
|
||||||
|
|||||||
+2
-1
@@ -16,7 +16,8 @@ import { BDom } from "../src/blockdom";
|
|||||||
import { blockDom } from "../src";
|
import { blockDom } from "../src";
|
||||||
import { compile as compileTemplate, Template } from "../src/compiler";
|
import { compile as compileTemplate, Template } from "../src/compiler";
|
||||||
import { CodeGenOptions } from "../src/compiler/code_generator";
|
import { CodeGenOptions } from "../src/compiler/code_generator";
|
||||||
import { globalTemplates, TemplateSet, UTILS } from "../src/app/template_helpers";
|
import { UTILS } from "../src/app/template_helpers";
|
||||||
|
import { globalTemplates, TemplateSet } from "../src/app/template_set";
|
||||||
import { xml } from "../src/tags";
|
import { xml } from "../src/tags";
|
||||||
|
|
||||||
const mount = blockDom.mount;
|
const mount = blockDom.mount;
|
||||||
|
|||||||
Reference in New Issue
Block a user