mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
[IMP] component: add support for t-on on compnents
This commit is contained in:
committed by
Samuel Degueldre
parent
67f86a4ab8
commit
50355e6a3d
@@ -1,4 +1,4 @@
|
||||
import { BDom, multi, text, toggler } from "../blockdom";
|
||||
import { BDom, multi, text, toggler, createCatcher } from "../blockdom";
|
||||
import { validateProps } from "../component/props_validation";
|
||||
import { Markup } from "../utils";
|
||||
import { html } from "../blockdom/index";
|
||||
@@ -199,4 +199,5 @@ export const helpers = {
|
||||
LazyValue,
|
||||
safeOutput,
|
||||
bind,
|
||||
createCatcher,
|
||||
};
|
||||
|
||||
@@ -0,0 +1,89 @@
|
||||
import { createEventHandler } from "./events";
|
||||
import type { VNode } from "./index";
|
||||
|
||||
type EventsSpec = { [name: string]: number };
|
||||
|
||||
type Catcher = (child: VNode, handlers: any[]) => VNode;
|
||||
|
||||
export function createCatcher(eventsSpec: EventsSpec): Catcher {
|
||||
let setupFns: any[] = [];
|
||||
let removeFns: any[] = [];
|
||||
for (let name in eventsSpec) {
|
||||
let index = eventsSpec[name];
|
||||
let { setup, remove } = createEventHandler(name);
|
||||
setupFns[index] = setup;
|
||||
removeFns[index] = remove;
|
||||
}
|
||||
let n = setupFns.length;
|
||||
|
||||
class VCatcher {
|
||||
child: VNode;
|
||||
handlers: any[];
|
||||
|
||||
parentEl?: HTMLElement | undefined;
|
||||
afterNode: Node | null = null;
|
||||
|
||||
constructor(child: VNode, handlers: any[]) {
|
||||
this.child = child;
|
||||
this.handlers = handlers;
|
||||
}
|
||||
|
||||
mount(parent: HTMLElement, afterNode: Node | null) {
|
||||
this.parentEl = parent;
|
||||
this.afterNode = afterNode;
|
||||
this.child.mount(parent, afterNode);
|
||||
for (let i = 0; i < n; i++) {
|
||||
let origFn = this.handlers[i][0];
|
||||
const self = this;
|
||||
this.handlers[i][0] = function (ev: any) {
|
||||
const target = ev.target;
|
||||
let currentNode: any = self.child.firstNode();
|
||||
const afterNode = self.afterNode;
|
||||
while (currentNode !== afterNode) {
|
||||
if (currentNode.contains(target)) {
|
||||
return origFn.call(this, ev);
|
||||
}
|
||||
currentNode = currentNode.nextSibling;
|
||||
}
|
||||
};
|
||||
setupFns[i].call(parent, this.handlers[i]);
|
||||
}
|
||||
}
|
||||
|
||||
moveBefore(other: VCatcher | null, afterNode: Node | null) {
|
||||
this.afterNode = null;
|
||||
this.child.moveBefore(other ? other.child : null, afterNode);
|
||||
}
|
||||
|
||||
patch(other: VCatcher, withBeforeRemove: boolean) {
|
||||
if (this === other) {
|
||||
return;
|
||||
}
|
||||
this.handlers = other.handlers;
|
||||
this.child.patch(other.child, withBeforeRemove);
|
||||
}
|
||||
|
||||
beforeRemove() {
|
||||
this.child.beforeRemove();
|
||||
}
|
||||
|
||||
remove() {
|
||||
for (let i = 0; i < n; i++) {
|
||||
removeFns[i].call(this.parentEl!);
|
||||
}
|
||||
this.child.remove();
|
||||
}
|
||||
|
||||
firstNode(): Node | undefined {
|
||||
return this.child.firstNode();
|
||||
}
|
||||
|
||||
toString(): string {
|
||||
return this.child.toString();
|
||||
}
|
||||
}
|
||||
|
||||
return function (child: VNode, handlers: any[]): VNode<VCatcher> {
|
||||
return new VCatcher(child, handlers);
|
||||
};
|
||||
}
|
||||
+12
-2
@@ -5,6 +5,7 @@ type EventHandlerSetter = (this: HTMLElement, data: any) => void;
|
||||
interface EventHandlerCreator {
|
||||
setup: EventHandlerSetter;
|
||||
update: EventHandlerSetter;
|
||||
remove: (this: HTMLElement) => void;
|
||||
}
|
||||
|
||||
export function createEventHandler(rawEvent: string): EventHandlerCreator {
|
||||
@@ -38,11 +39,15 @@ function createElementHandler(evName: string, capture: boolean = false): EventHa
|
||||
this.addEventListener(evName, listener, { capture });
|
||||
}
|
||||
|
||||
function remove(this: HTMLElement) {
|
||||
delete (this as any)[eventKey];
|
||||
this.removeEventListener(evName, listener, { capture });
|
||||
}
|
||||
function update(this: HTMLElement, data: any) {
|
||||
(this as any)[eventKey] = data;
|
||||
}
|
||||
|
||||
return { setup, update };
|
||||
return { setup, update, remove };
|
||||
}
|
||||
|
||||
// Synthetic handler: a form of event delegation that allows placing only one
|
||||
@@ -60,7 +65,12 @@ function createSyntheticHandler(evName: string, capture: boolean = false): Event
|
||||
_data[currentId] = data;
|
||||
(this as any)[eventKey] = _data;
|
||||
}
|
||||
return { setup, update: setup };
|
||||
|
||||
function remove(this: HTMLElement) {
|
||||
delete (this as any)[eventKey];
|
||||
}
|
||||
|
||||
return { setup, update: setup, remove };
|
||||
}
|
||||
|
||||
function nativeToSyntheticEvent(eventKey: string, event: Event) {
|
||||
|
||||
@@ -6,6 +6,7 @@ export { list } from "./list";
|
||||
export { multi } from "./multi";
|
||||
export { text, comment } from "./text";
|
||||
export { html } from "./html";
|
||||
export { createCatcher } from "./event_catcher";
|
||||
|
||||
export interface VNode<T = any> {
|
||||
mount(parent: HTMLElement, afterNode: Node | null): void;
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
+10
-4
@@ -119,6 +119,7 @@ export interface ASTComponent {
|
||||
name: string;
|
||||
isDynamic: boolean;
|
||||
dynamicProps: string | null;
|
||||
on: null | { [key: string]: string };
|
||||
props: { [name: string]: string };
|
||||
slots: { [name: string]: { content: AST; attrs?: { [key: string]: string }; scope?: string } };
|
||||
}
|
||||
@@ -650,7 +651,6 @@ function parseTSetNode(node: Element, ctx: ParsingContext): AST | null {
|
||||
|
||||
// Error messages when trying to use an unsupported directive on a component
|
||||
const directiveErrorMap = new Map([
|
||||
["t-on", "t-on is no longer supported on components. Consider passing a callback in props."],
|
||||
[
|
||||
"t-ref",
|
||||
"t-ref is no longer supported on components. Consider exposing only the public part of the component's API through a callback prop.",
|
||||
@@ -684,13 +684,19 @@ function parseComponent(node: Element, ctx: ParsingContext): AST | null {
|
||||
|
||||
const defaultSlotScope = node.getAttribute("t-slot-scope");
|
||||
node.removeAttribute("t-slot-scope");
|
||||
let on: ASTComponent["on"] = null;
|
||||
|
||||
const props: ASTComponent["props"] = {};
|
||||
for (let name of node.getAttributeNames()) {
|
||||
const value = node.getAttribute(name)!;
|
||||
if (name.startsWith("t-")) {
|
||||
const message = directiveErrorMap.get(name.split("-").slice(0, 2).join("-"));
|
||||
throw new Error(message || `unsupported directive on Component: ${name}`);
|
||||
if (name.startsWith("t-on-")) {
|
||||
on = on || {};
|
||||
on[name.slice(5)] = value;
|
||||
} else {
|
||||
const message = directiveErrorMap.get(name.split("-").slice(0, 2).join("-"));
|
||||
throw new Error(message || `unsupported directive on Component: ${name}`);
|
||||
}
|
||||
} else {
|
||||
props[name] = value;
|
||||
}
|
||||
@@ -755,7 +761,7 @@ function parseComponent(node: Element, ctx: ParsingContext): AST | null {
|
||||
}
|
||||
}
|
||||
}
|
||||
return { type: ASTType.TComponent, name, isDynamic, dynamicProps, props, slots };
|
||||
return { type: ASTType.TComponent, name, isDynamic, dynamicProps, props, slots, on };
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
Reference in New Issue
Block a user