2022-05-26 23:45:40 +02:00
|
|
|
import { filterOutModifiersFromData } from "./blockdom/config";
|
2022-01-17 14:13:32 +01:00
|
|
|
import { STATUS } from "./status";
|
2022-07-18 10:41:50 +02:00
|
|
|
import { OwlError } from "./error_handling";
|
2021-10-26 11:01:57 +02:00
|
|
|
|
2021-10-28 17:47:16 +02:00
|
|
|
export const mainEventHandler = (data: any, ev: Event, currentTarget?: EventTarget | null) => {
|
2021-10-27 14:45:29 +02:00
|
|
|
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) {
|
2021-10-28 17:47:16 +02:00
|
|
|
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;
|
2021-10-27 14:45:29 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2021-11-12 12:56:07 +01:00
|
|
|
// If handler is empty, the array slot 0 will also be empty, and data will not have the property 0
|
|
|
|
|
// We check this rather than data[0] being truthy (or typeof function) so that it crashes
|
|
|
|
|
// as expected when there is a handler expression that evaluates to a falsy value
|
|
|
|
|
if (Object.hasOwnProperty.call(data, 0)) {
|
2022-01-20 14:52:47 +01:00
|
|
|
const handler = data[0];
|
|
|
|
|
if (typeof handler !== "function") {
|
2022-07-18 10:41:50 +02:00
|
|
|
throw new OwlError(`Invalid handler (expected a function, received: '${handler}')`);
|
2022-01-20 14:52:47 +01:00
|
|
|
}
|
2022-01-17 14:13:32 +01:00
|
|
|
let node = data[1] ? data[1].__owl__ : null;
|
|
|
|
|
if (node ? node.status === STATUS.MOUNTED : true) {
|
2022-01-20 14:52:47 +01:00
|
|
|
handler.call(node ? node.component : null, ev);
|
2022-01-17 14:13:32 +01:00
|
|
|
}
|
2020-11-26 16:45:25 +01:00
|
|
|
}
|
2021-10-27 14:45:29 +02:00
|
|
|
return stopped;
|
2021-10-28 17:47:16 +02:00
|
|
|
};
|