mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
small qweb cleanup
This commit is contained in:
+6
-12
@@ -18,7 +18,7 @@ export class Context {
|
|||||||
code: string[] = [];
|
code: string[] = [];
|
||||||
variables: { [key: string]: any } = {};
|
variables: { [key: string]: any } = {};
|
||||||
escaping: boolean = false;
|
escaping: boolean = false;
|
||||||
parentNode: string | undefined;
|
parentNode: string;
|
||||||
indentLevel: number = 0;
|
indentLevel: number = 0;
|
||||||
rootContext: Context;
|
rootContext: Context;
|
||||||
caller: Element | undefined;
|
caller: Element | undefined;
|
||||||
@@ -27,6 +27,7 @@ export class Context {
|
|||||||
constructor() {
|
constructor() {
|
||||||
this.rootContext = this;
|
this.rootContext = this;
|
||||||
this.fragmentID = this.generateID();
|
this.fragmentID = this.generateID();
|
||||||
|
this.parentNode = this.fragmentID;
|
||||||
}
|
}
|
||||||
|
|
||||||
generateID(): string {
|
generateID(): string {
|
||||||
@@ -68,11 +69,7 @@ export class Context {
|
|||||||
}
|
}
|
||||||
|
|
||||||
addNode(nodeID: string) {
|
addNode(nodeID: string) {
|
||||||
if (this.parentNode) {
|
this.addLine(`${this.parentNode}.appendChild(${nodeID})`);
|
||||||
this.addLine(`${this.parentNode}.appendChild(${nodeID})`);
|
|
||||||
} else {
|
|
||||||
this.addLine(`${this.fragmentID}.appendChild(${nodeID})`);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
addLine(line: string) {
|
addLine(line: string) {
|
||||||
const prefix = new Array(this.indentLevel).join("\t");
|
const prefix = new Array(this.indentLevel).join("\t");
|
||||||
@@ -80,6 +77,9 @@ export class Context {
|
|||||||
const suffix = lastChar !== "}" && lastChar !== "{" ? ";" : "";
|
const suffix = lastChar !== "}" && lastChar !== "{" ? ";" : "";
|
||||||
this.code.push(prefix + line + suffix);
|
this.code.push(prefix + line + suffix);
|
||||||
}
|
}
|
||||||
|
getValue(val: any): any {
|
||||||
|
return val in this.variables ? this.getValue(this.variables[val]) : val;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@@ -298,12 +298,6 @@ export default class QWeb {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_getValue(val: any, ctx: Context): any {
|
|
||||||
if (val in ctx.variables) {
|
|
||||||
return this._getValue(ctx.variables[val], ctx);
|
|
||||||
}
|
|
||||||
return val;
|
|
||||||
}
|
|
||||||
_compileChildren(node: ChildNode, ctx: Context) {
|
_compileChildren(node: ChildNode, ctx: Context) {
|
||||||
if (node.childNodes.length > 0) {
|
if (node.childNodes.length > 0) {
|
||||||
for (let child of Array.from(node.childNodes)) {
|
for (let child of Array.from(node.childNodes)) {
|
||||||
|
|||||||
@@ -61,7 +61,7 @@ const ifDirective: Directive = {
|
|||||||
name: "if",
|
name: "if",
|
||||||
priority: 20,
|
priority: 20,
|
||||||
atNodeEncounter({ node, qweb, ctx }): boolean {
|
atNodeEncounter({ node, qweb, ctx }): boolean {
|
||||||
let cond = qweb._getValue(node.getAttribute("t-if")!, ctx);
|
let cond = ctx.getValue(node.getAttribute("t-if")!);
|
||||||
ctx.addLine(`if (${qweb._formatExpression(cond)}) {`);
|
ctx.addLine(`if (${qweb._formatExpression(cond)}) {`);
|
||||||
ctx.indent();
|
ctx.indent();
|
||||||
return false;
|
return false;
|
||||||
@@ -76,7 +76,7 @@ const elifDirective: Directive = {
|
|||||||
name: "elif",
|
name: "elif",
|
||||||
priority: 30,
|
priority: 30,
|
||||||
atNodeEncounter({ node, qweb, ctx }): boolean {
|
atNodeEncounter({ node, qweb, ctx }): boolean {
|
||||||
let cond = qweb._getValue(node.getAttribute("t-elif")!, ctx);
|
let cond = ctx.getValue(node.getAttribute("t-elif")!);
|
||||||
ctx.addLine(`else if (${qweb._formatExpression(cond)}) {`);
|
ctx.addLine(`else if (${qweb._formatExpression(cond)}) {`);
|
||||||
ctx.indent();
|
ctx.indent();
|
||||||
return false;
|
return false;
|
||||||
@@ -180,7 +180,7 @@ const escDirective: Directive = {
|
|||||||
let nodeID = qweb._compileGenericNode(node, ctx);
|
let nodeID = qweb._compileGenericNode(node, ctx);
|
||||||
ctx = ctx.withParent(nodeID);
|
ctx = ctx.withParent(nodeID);
|
||||||
}
|
}
|
||||||
let value = qweb._getValue(node.getAttribute("t-esc")!, ctx);
|
let value = ctx.getValue(node.getAttribute("t-esc")!);
|
||||||
compileValueNode(value, node, qweb, ctx.withEscaping());
|
compileValueNode(value, node, qweb, ctx.withEscaping());
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
@@ -194,7 +194,7 @@ const rawDirective: Directive = {
|
|||||||
let nodeID = qweb._compileGenericNode(node, ctx);
|
let nodeID = qweb._compileGenericNode(node, ctx);
|
||||||
ctx = ctx.withParent(nodeID);
|
ctx = ctx.withParent(nodeID);
|
||||||
}
|
}
|
||||||
let value = qweb._getValue(node.getAttribute("t-raw")!, ctx);
|
let value = ctx.getValue(node.getAttribute("t-raw")!);
|
||||||
compileValueNode(value, node, qweb, ctx);
|
compileValueNode(value, node, qweb, ctx);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user