[IMP] qweb, blockdom, components: t-on with modifiers

supported modifiers: capture, prevent, stop, self.
This commit is contained in:
Lucas Perais (lpe)
2021-10-27 14:45:29 +02:00
committed by Aaron Bohy
parent 2ae0149adb
commit 4cceb239dd
11 changed files with 427 additions and 39 deletions
+20 -7
View File
@@ -1,13 +1,26 @@
import { filterOutModifiersFromData } from "../blockdom/config";
export function mainEventHandler(data: any, ev: Event) {
if (typeof data === "function") {
data(ev);
} else {
data = filterOutModifiersFromData(data).data;
const ctx = data[0];
export const mainEventHandler = (data: any, ev: Event, currentTarget?: EventTarget|null) => {
const { data: _data, modifiers } = filterOutModifiersFromData(data);
data = _data;
let stopped = false;
if (modifiers.length) {
let selfMode = false;
const isSelf = ev.target === currentTarget;
for (const mod of modifiers) {
switch (mod) {
case "self": selfMode = true; if (isSelf) { continue; } else { return stopped; };
case "prevent": if ((selfMode && isSelf) || (!selfMode)) ev.preventDefault(); continue;
case "stop": if ((selfMode && isSelf) || (!selfMode)) ev.stopPropagation() ; stopped = true; continue;
}
}
}
if (typeof data[0] === "function") {
data[0](ev);
} else if (data[0].__owl__) {
const method = data[1];
const args = data[2] || [];
ctx.__owl__.component[method](...args, ev);
data[0].__owl__.component[method](...args, ev);
}
return stopped;
}