[IMP] component: add support for t-on on compnents

This commit is contained in:
Géry Debongnie
2022-03-04 16:14:20 +01:00
committed by Samuel Degueldre
parent 67f86a4ab8
commit 50355e6a3d
12 changed files with 349 additions and 46 deletions
+23
View File
@@ -217,6 +217,9 @@ export class CodeGenerator {
translatableAttributes: string[] = TRANSLATABLE_ATTRS;
ast: AST;
staticCalls: { id: string; template: string }[] = [];
// todo: merge with staticCalls
// todo: add a setCodeValue function => 2 args, call addLine, use it instead of addlin
eventCatchers: { id: string; expr: string }[] = [];
helpers: Set<string> = new Set();
constructor(ast: AST, options: CodeGenOptions) {
@@ -265,6 +268,9 @@ export class CodeGenerator {
for (let { id, template } of this.staticCalls) {
mainCode.push(`const ${id} = getTemplate(${template});`);
}
for (let { id, expr } of this.eventCatchers) {
mainCode.push(`const ${id} = ${expr};`);
}
// define all blocks
if (this.blocks.length) {
@@ -1178,6 +1184,23 @@ export class CodeGenerator {
if (ast.isDynamic) {
blockExpr = `toggler(${expr}, ${blockExpr})`;
}
// event handling
if (ast.on) {
this.helpers.add("createCatcher");
let name = this.generateId("catcher");
let spec: any = {};
let handlers: any[] = [];
for (let ev in ast.on) {
let handlerId = this.generateId("hdlr");
let idx = handlers.push(handlerId) - 1;
spec[ev] = idx;
const handler = this.generateHandlerCode(ev, ast.on[ev]);
this.addLine(`let ${handlerId} = ${handler};`);
}
blockExpr = `${name}(${blockExpr}, [${handlers.join(",")}])`;
this.eventCatchers.push({ id: name, expr: `createCatcher(${JSON.stringify(spec)})` });
}
block = this.createBlock(block, "multi", ctx);
this.insertBlock(blockExpr, block, ctx);
}