[IMP] owl: update to v1.0.0-beta4

This commit is contained in:
Géry Debongnie
2019-12-17 15:32:47 +01:00
parent 94461320aa
commit 7ee0f5e128
+53 -31
View File
@@ -1052,7 +1052,8 @@
this.inPreTag = false; this.inPreTag = false;
this.allowMultipleRoots = false; this.allowMultipleRoots = false;
this.hasParentWidget = false; this.hasParentWidget = false;
this.currentKey = ""; this.hasKey0 = false;
this.keyStack = [];
this.rootContext = this; this.rootContext = this;
this.templateName = name || "noname"; this.templateName = name || "noname";
this.addLine("let h = this.h;"); this.addLine("let h = this.h;");
@@ -1070,21 +1071,15 @@
*/ */
generateTemplateKey(prefix = "") { generateTemplateKey(prefix = "") {
const id = this.generateID(); const id = this.generateID();
if (this.loopNumber === 0 && !this.currentKey) { if (this.loopNumber === 0 && !this.hasKey0) {
return `'${prefix}__${id}__'`; return `'${prefix}__${id}__'`;
} }
let locationExpr = `\`${prefix}__${id}__`; let key = `\`${prefix}__${id}__`;
for (let i = 0; i < this.loopNumber - 1; i++) { let start = this.hasKey0 ? 0 : 1;
locationExpr += `\${i${i + 1}}__`; for (let i = start; i < this.loopNumber + 1; i++) {
} key += `\${key${i}}__`;
if (this.currentKey) {
const k = this.currentKey;
this.addLine(`let k${id} = ${locationExpr}\` + ${k};`);
}
else {
locationExpr += this.loopNumber ? `\${i${this.loopNumber}}__\`` : "`";
this.addLine(`let k${id} = ${locationExpr};`);
} }
this.addLine(`let k${id} = ${key}\`;`);
return `k${id}`; return `k${id}`;
} }
generateCode() { generateCode() {
@@ -1133,10 +1128,10 @@
return newContext; return newContext;
} }
indent() { indent() {
this.indentLevel++; this.rootContext.indentLevel++;
} }
dedent() { dedent() {
this.indentLevel--; this.rootContext.indentLevel--;
} }
addLine(line) { addLine(line) {
const prefix = new Array(this.indentLevel + 2).join(" "); const prefix = new Array(this.indentLevel + 2).join(" ");
@@ -1570,7 +1565,7 @@
} }
}); });
} }
_compile(name, elem, parentContext) { _compile(name, elem, parentContext, defineKey) {
const isDebug = elem.attributes.hasOwnProperty("t-debug"); const isDebug = elem.attributes.hasOwnProperty("t-debug");
const ctx = new CompilationContext(name); const ctx = new CompilationContext(name);
if (elem.tagName !== "t") { if (elem.tagName !== "t") {
@@ -1583,6 +1578,10 @@
ctx.hasParentWidget = true; ctx.hasParentWidget = true;
ctx.shouldDefineResult = false; ctx.shouldDefineResult = false;
ctx.addLine(`let c${ctx.parentNode} = extra.parentNode;`); ctx.addLine(`let c${ctx.parentNode} = extra.parentNode;`);
if (defineKey) {
ctx.addLine(`let key0 = extra.key || "";`);
ctx.hasKey0 = true;
}
} }
this._compileNode(elem, ctx); this._compileNode(elem, ctx);
if (!parentContext) { if (!parentContext) {
@@ -1659,9 +1658,6 @@
} }
return; return;
} }
if (ctx !== ctx.rootContext) {
ctx = ctx.subContext("currentKey", ctx.currentKey);
}
const firstLetter = node.tagName[0]; const firstLetter = node.tagName[0];
if (firstLetter === firstLetter.toUpperCase()) { if (firstLetter === firstLetter.toUpperCase()) {
// this is a component, we modify in place the xml document to change // this is a component, we modify in place the xml document to change
@@ -1908,8 +1904,8 @@
} }
} }
let nodeID = ctx.generateID(); let nodeID = ctx.generateID();
let nodeKey = ctx.currentKey || nodeID; let key = ctx.loopNumber || ctx.hasKey0 ? `\`\${key${ctx.loopNumber}}_${nodeID}\`` : nodeID;
const parts = [`key:${nodeKey}`]; const parts = [`key:${key}`];
if (attrs.length + tattrs.length > 0) { if (attrs.length + tattrs.length > 0) {
parts.push(`attrs:{${attrs.join(",")}}`); parts.push(`attrs:{${attrs.join(",")}}`);
} }
@@ -1938,6 +1934,10 @@
if (ctx.parentNode) { if (ctx.parentNode) {
ctx.addLine(`c${ctx.parentNode}.push(vn${nodeID});`); ctx.addLine(`c${ctx.parentNode}.push(vn${nodeID});`);
} }
else if (ctx.loopNumber || ctx.hasKey0) {
ctx.rootContext.shouldDefineResult = true;
ctx.addLine(`result = vn${nodeID};`);
}
return nodeID; return nodeID;
} }
_compileChildren(node, ctx) { _compileChildren(node, ctx) {
@@ -2198,7 +2198,7 @@
// ------------------------------------------------ // ------------------------------------------------
if (!qweb.subTemplates[subTemplate]) { if (!qweb.subTemplates[subTemplate]) {
qweb.subTemplates[subTemplate] = true; qweb.subTemplates[subTemplate] = true;
const subTemplateFn = qweb._compile(subTemplate, nodeTemplate.elem, ctx); const subTemplateFn = qweb._compile(subTemplate, nodeTemplate.elem, ctx, true);
qweb.subTemplates[subTemplate] = subTemplateFn; qweb.subTemplates[subTemplate] = subTemplateFn;
} }
// Step 3: compile t-call body if necessary // Step 3: compile t-call body if necessary
@@ -2231,14 +2231,17 @@
// ------------------------------------------------ // ------------------------------------------------
const callingScope = hasBody ? "scope" : "Object.assign(Object.create(context), scope)"; const callingScope = hasBody ? "scope" : "Object.assign(Object.create(context), scope)";
const parentComponent = `utils.getComponent(context)`; const parentComponent = `utils.getComponent(context)`;
const keyCode = ctx.loopNumber || ctx.hasKey0 ? `, key: ${ctx.generateTemplateKey()}` : "";
const parentNode = ctx.parentNode ? `c${ctx.parentNode}` : "result";
const extra = `Object.assign({}, extra, {parentNode: ${parentNode}, parent: ${parentComponent}${keyCode}})`;
if (ctx.parentNode) { if (ctx.parentNode) {
ctx.addLine(`this.subTemplates['${subTemplate}'].call(this, ${callingScope}, Object.assign({}, extra, {parentNode: c${ctx.parentNode}, parent: ${parentComponent}}));`); ctx.addLine(`this.subTemplates['${subTemplate}'].call(this, ${callingScope}, ${extra});`);
} }
else { else {
// this is a t-call with no parentnode, we need to extract the result // this is a t-call with no parentnode, we need to extract the result
ctx.rootContext.shouldDefineResult = true; ctx.rootContext.shouldDefineResult = true;
ctx.addLine(`result = []`); ctx.addLine(`result = []`);
ctx.addLine(`this.subTemplates['${subTemplate}'].call(this, ${callingScope}, Object.assign({}, extra, {parentNode: result, parent: ${parentComponent}}));`); ctx.addLine(`this.subTemplates['${subTemplate}'].call(this, ${callingScope}, ${extra});`);
ctx.addLine(`result = result[0]`); ctx.addLine(`result = result[0]`);
} }
// Step 5: restore previous scope // Step 5: restore previous scope
@@ -2291,6 +2294,14 @@
if (shouldWarn) { if (shouldWarn) {
console.warn(`Directive t-foreach should always be used with a t-key! (in template: '${ctx.templateName}')`); console.warn(`Directive t-foreach should always be used with a t-key! (in template: '${ctx.templateName}')`);
} }
if (nodeCopy.hasAttribute("t-key")) {
const expr = ctx.formatExpression(nodeCopy.getAttribute("t-key"));
ctx.addLine(`let key${ctx.loopNumber} = ${expr};`);
nodeCopy.removeAttribute("t-key");
}
else {
ctx.addLine(`let key${ctx.loopNumber} = i${ctx.loopNumber};`);
}
nodeCopy.removeAttribute("t-foreach"); nodeCopy.removeAttribute("t-foreach");
qweb._compileNode(nodeCopy, ctx); qweb._compileNode(nodeCopy, ctx);
ctx.dedent(); ctx.dedent();
@@ -2589,10 +2600,21 @@
QWeb.addDirective({ QWeb.addDirective({
name: "key", name: "key",
priority: 45, priority: 45,
atNodeEncounter({ ctx, value }) { atNodeEncounter({ ctx, value, node }) {
let id = ctx.generateID(); if (ctx.loopNumber === 0) {
ctx.addLine(`const nodeKey${id} = ${ctx.formatExpression(value)};`); ctx.keyStack.push(ctx.rootContext.hasKey0);
ctx.currentKey = `nodeKey${id}`; ctx.rootContext.hasKey0 = true;
}
ctx.addLine("{");
ctx.indent();
ctx.addLine(`let key${ctx.loopNumber} = ${ctx.formatExpression(value)};`);
},
finalize({ ctx }) {
ctx.dedent();
ctx.addLine("}");
if (ctx.loopNumber === 0) {
ctx.rootContext.hasKey0 = ctx.keyStack.pop();
}
} }
}); });
@@ -4864,9 +4886,9 @@
exports.useState = useState$1; exports.useState = useState$1;
exports.utils = utils; exports.utils = utils;
exports.__info__.version = '1.0.0-beta3'; exports.__info__.version = '1.0.0-beta4';
exports.__info__.date = '2019-12-16T08:57:32.773Z'; exports.__info__.date = '2019-12-17T14:27:10.310Z';
exports.__info__.hash = 'd9bf428'; exports.__info__.hash = '1db0f5a';
exports.__info__.url = 'https://github.com/odoo/owl'; exports.__info__.url = 'https://github.com/odoo/owl';
}(this.owl = this.owl || {})); }(this.owl = this.owl || {}));