mirror of
https://github.com/odoo/owl.git
synced 2025-10-06 19:59:41 +07:00
[REF] Code prettification
This commit is contained in:
committed by
Aaron Bohy
parent
4cceb239dd
commit
80cb6b7a91
@@ -1,4 +1,4 @@
|
|||||||
export function filterOutModifiersFromData(dataList: any[]): { modifiers: string[], data: any[]} {
|
export function filterOutModifiersFromData(dataList: any[]): { modifiers: string[]; data: any[] } {
|
||||||
dataList = dataList.slice();
|
dataList = dataList.slice();
|
||||||
const modifiers = [];
|
const modifiers = [];
|
||||||
let elm;
|
let elm;
|
||||||
|
|||||||
@@ -76,6 +76,8 @@ function setupSyntheticEvent(evName: string, eventKey: string, capture: boolean
|
|||||||
if (CONFIGURED_SYNTHETIC_EVENTS[eventKey]) {
|
if (CONFIGURED_SYNTHETIC_EVENTS[eventKey]) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
document.addEventListener(evName, (event) => nativeToSyntheticEvent(eventKey, event), { capture });
|
document.addEventListener(evName, (event) => nativeToSyntheticEvent(eventKey, event), {
|
||||||
|
capture,
|
||||||
|
});
|
||||||
CONFIGURED_SYNTHETIC_EVENTS[eventKey] = true;
|
CONFIGURED_SYNTHETIC_EVENTS[eventKey] = true;
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -1,6 +1,6 @@
|
|||||||
import { filterOutModifiersFromData } from "../blockdom/config";
|
import { filterOutModifiersFromData } from "../blockdom/config";
|
||||||
|
|
||||||
export const mainEventHandler = (data: any, ev: Event, currentTarget?: EventTarget|null) => {
|
export const mainEventHandler = (data: any, ev: Event, currentTarget?: EventTarget | null) => {
|
||||||
const { data: _data, modifiers } = filterOutModifiersFromData(data);
|
const { data: _data, modifiers } = filterOutModifiersFromData(data);
|
||||||
data = _data;
|
data = _data;
|
||||||
let stopped = false;
|
let stopped = false;
|
||||||
@@ -9,9 +9,20 @@ export const mainEventHandler = (data: any, ev: Event, currentTarget?: EventTarg
|
|||||||
const isSelf = ev.target === currentTarget;
|
const isSelf = ev.target === currentTarget;
|
||||||
for (const mod of modifiers) {
|
for (const mod of modifiers) {
|
||||||
switch (mod) {
|
switch (mod) {
|
||||||
case "self": selfMode = true; if (isSelf) { continue; } else { return stopped; };
|
case "self":
|
||||||
case "prevent": if ((selfMode && isSelf) || (!selfMode)) ev.preventDefault(); continue;
|
selfMode = true;
|
||||||
case "stop": if ((selfMode && isSelf) || (!selfMode)) ev.stopPropagation() ; stopped = true; continue;
|
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;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -23,4 +34,4 @@ export const mainEventHandler = (data: any, ev: Event, currentTarget?: EventTarg
|
|||||||
data[0].__owl__.component[method](...args, ev);
|
data[0].__owl__.component[method](...args, ev);
|
||||||
}
|
}
|
||||||
return stopped;
|
return stopped;
|
||||||
}
|
};
|
||||||
|
|||||||
@@ -474,7 +474,10 @@ export class QWebCompiler {
|
|||||||
args = _args.slice(1, -1);
|
args = _args.slice(1, -1);
|
||||||
return "";
|
return "";
|
||||||
});
|
});
|
||||||
const modifiers = rawEvent.split(".").slice(1).map(m => `"${m}"`);
|
const modifiers = rawEvent
|
||||||
|
.split(".")
|
||||||
|
.slice(1)
|
||||||
|
.map((m) => `"${m}"`);
|
||||||
let modifiersCode = "";
|
let modifiersCode = "";
|
||||||
if (modifiers.length) {
|
if (modifiers.length) {
|
||||||
modifiersCode = `${modifiers.join(",")}, `;
|
modifiersCode = `${modifiers.join(",")}, `;
|
||||||
|
|||||||
@@ -124,9 +124,11 @@ test("two same block nodes with different handlers (synthetic)", async () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test("synthetic and native handlers can cohabitate", async () => {
|
test("synthetic and native handlers can cohabitate", async () => {
|
||||||
const block = createBlock('<div block-handler-0="click.synthetic"><div block-handler-1="click"/></div>');
|
const block = createBlock(
|
||||||
|
'<div block-handler-0="click.synthetic"><div block-handler-1="click"/></div>'
|
||||||
|
);
|
||||||
let steps: string[] = [];
|
let steps: string[] = [];
|
||||||
let handler1 = () => steps.push("1");;
|
let handler1 = () => steps.push("1");
|
||||||
let handler2 = () => steps.push("2");
|
let handler2 = () => steps.push("2");
|
||||||
const tree = block([handler1, handler2]);
|
const tree = block([handler1, handler2]);
|
||||||
|
|
||||||
@@ -140,9 +142,11 @@ test("synthetic and native handlers can cohabitate", async () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test("synthetic and native handlers can cohabitate (2)", async () => {
|
test("synthetic and native handlers can cohabitate (2)", async () => {
|
||||||
const block = createBlock('<div block-handler-0="click"><div block-handler-1="click.synthetic"/></div>');
|
const block = createBlock(
|
||||||
|
'<div block-handler-0="click"><div block-handler-1="click.synthetic"/></div>'
|
||||||
|
);
|
||||||
let steps: string[] = [];
|
let steps: string[] = [];
|
||||||
let handler1 = () => steps.push("1");;
|
let handler1 = () => steps.push("1");
|
||||||
let handler2 = () => steps.push("2");
|
let handler2 = () => steps.push("2");
|
||||||
const tree = block([handler1, handler2]);
|
const tree = block([handler1, handler2]);
|
||||||
|
|
||||||
@@ -156,12 +160,12 @@ test("synthetic and native handlers can cohabitate (2)", async () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test("synthetic and native handlers can cohabitate (3)", async () => {
|
test("synthetic and native handlers can cohabitate (3)", async () => {
|
||||||
const parent = createBlock(`<div block-handler-0="click"><block-child-0/><block-child-1/></div>`)
|
const parent = createBlock(`<div block-handler-0="click"><block-child-0/><block-child-1/></div>`);
|
||||||
const block = createBlock('<div block-handler-0="click"/>');
|
const block = createBlock('<div block-handler-0="click"/>');
|
||||||
const blockSynth = createBlock('<div block-handler-0="click.synthetic"/>');
|
const blockSynth = createBlock('<div block-handler-0="click.synthetic"/>');
|
||||||
let steps: string[] = [];
|
let steps: string[] = [];
|
||||||
const handler0 = () => steps.push("0");;
|
const handler0 = () => steps.push("0");
|
||||||
let handler1 = () => steps.push("1");;
|
let handler1 = () => steps.push("1");
|
||||||
let handler2 = () => steps.push("2");
|
let handler2 = () => steps.push("2");
|
||||||
const tree = parent([handler0], [block([handler1]), blockSynth([handler2])]);
|
const tree = parent([handler0], [block([handler1]), blockSynth([handler2])]);
|
||||||
|
|
||||||
@@ -177,12 +181,15 @@ test("synthetic and native handlers can cohabitate (3)", async () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test("synthetic and native handlers can cohabitate (5)", async () => {
|
test("synthetic and native handlers can cohabitate (5)", async () => {
|
||||||
const parent = createBlock(`<div block-handler-0="click"><block-child-0/><block-child-1/></div>`)
|
const parent = createBlock(`<div block-handler-0="click"><block-child-0/><block-child-1/></div>`);
|
||||||
const block = createBlock('<div block-handler-0="click"/>');
|
const block = createBlock('<div block-handler-0="click"/>');
|
||||||
const blockSynth = createBlock('<div block-handler-0="click.synthetic"/>');
|
const blockSynth = createBlock('<div block-handler-0="click.synthetic"/>');
|
||||||
let steps: string[] = [];
|
let steps: string[] = [];
|
||||||
const handler0 = (ev: Event) => { steps.push("0"); ev.stopPropagation();};
|
const handler0 = (ev: Event) => {
|
||||||
let handler1 = () => steps.push("1");;
|
steps.push("0");
|
||||||
|
ev.stopPropagation();
|
||||||
|
};
|
||||||
|
let handler1 = () => steps.push("1");
|
||||||
let handler2 = () => steps.push("2");
|
let handler2 = () => steps.push("2");
|
||||||
const tree = parent([handler0], [block([handler1]), blockSynth([handler2])]);
|
const tree = parent([handler0], [block([handler1]), blockSynth([handler2])]);
|
||||||
|
|
||||||
|
|||||||
@@ -327,7 +327,7 @@ describe("t-on", () => {
|
|||||||
expect(steps).toEqual(["btnClicked", "divClicked"]);
|
expect(steps).toEqual(["btnClicked", "divClicked"]);
|
||||||
});
|
});
|
||||||
|
|
||||||
test("t-on with prevent and/or stop modifiers", async () => {
|
test("t-on with prevent and/or stop modifiers", async () => {
|
||||||
expect.assertions(7);
|
expect.assertions(7);
|
||||||
const template = `<div>
|
const template = `<div>
|
||||||
<button t-on-click.prevent="onClickPrevented">Button 1</button>
|
<button t-on-click.prevent="onClickPrevented">Button 1</button>
|
||||||
|
|||||||
+40
-25
@@ -465,12 +465,15 @@ describe("qweb parser", () => {
|
|||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
|
|
||||||
test("simple t-foreach expression, t-key mandatory", async () => {
|
test("simple t-foreach expression, t-key mandatory", async () => {
|
||||||
expect(() => parse(`<t t-foreach="list" t-as="item"><t t-esc="item"/></t>`)).toThrowErrorMatchingSnapshot();
|
expect(() =>
|
||||||
|
parse(`<t t-foreach="list" t-as="item"><t t-esc="item"/></t>`)
|
||||||
})
|
).toThrowErrorMatchingSnapshot();
|
||||||
|
});
|
||||||
|
|
||||||
test("simple t-foreach expression", async () => {
|
test("simple t-foreach expression", async () => {
|
||||||
expect(parse(`<t t-foreach="list" t-as="item" t-key="item_index"><t t-esc="item"/></t>`)).toEqual({
|
expect(
|
||||||
|
parse(`<t t-foreach="list" t-as="item" t-key="item_index"><t t-esc="item"/></t>`)
|
||||||
|
).toEqual({
|
||||||
type: ASTType.TForEach,
|
type: ASTType.TForEach,
|
||||||
collection: "list",
|
collection: "list",
|
||||||
elem: "item",
|
elem: "item",
|
||||||
@@ -545,7 +548,9 @@ describe("qweb parser", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test("t-foreach expression on a span", async () => {
|
test("t-foreach expression on a span", async () => {
|
||||||
expect(parse(`<span t-foreach="list" t-as="item" t-key="item_index"><t t-esc="item"/></span>`)).toEqual({
|
expect(
|
||||||
|
parse(`<span t-foreach="list" t-as="item" t-key="item_index"><t t-esc="item"/></span>`)
|
||||||
|
).toEqual({
|
||||||
type: ASTType.TForEach,
|
type: ASTType.TForEach,
|
||||||
collection: "list",
|
collection: "list",
|
||||||
elem: "item",
|
elem: "item",
|
||||||
@@ -570,7 +575,9 @@ describe("qweb parser", () => {
|
|||||||
|
|
||||||
test("t-foreach expression on a span", async () => {
|
test("t-foreach expression on a span", async () => {
|
||||||
expect(
|
expect(
|
||||||
parse(`<span t-foreach="list" t-if="condition" t-as="item" t-key="item_index"><t t-esc="item"/></span>`)
|
parse(
|
||||||
|
`<span t-foreach="list" t-if="condition" t-as="item" t-key="item_index"><t t-esc="item"/></span>`
|
||||||
|
)
|
||||||
).toEqual({
|
).toEqual({
|
||||||
type: ASTType.TForEach,
|
type: ASTType.TForEach,
|
||||||
collection: "list",
|
collection: "list",
|
||||||
@@ -632,7 +639,9 @@ describe("qweb parser", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test("t-foreach in a div", async () => {
|
test("t-foreach in a div", async () => {
|
||||||
expect(parse(`<div><t t-foreach="list" t-as="item" t-key="item_index"><t t-esc="item"/></t></div>`)).toEqual({
|
expect(
|
||||||
|
parse(`<div><t t-foreach="list" t-as="item" t-key="item_index"><t t-esc="item"/></t></div>`)
|
||||||
|
).toEqual({
|
||||||
type: ASTType.DomNode,
|
type: ASTType.DomNode,
|
||||||
attrs: {},
|
attrs: {},
|
||||||
on: {},
|
on: {},
|
||||||
@@ -716,7 +725,9 @@ describe("qweb parser", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test("t-foreach expression with a t-call inside", async () => {
|
test("t-foreach expression with a t-call inside", async () => {
|
||||||
expect(parse(`<t t-foreach="list" t-as="item" t-key="item_index"><t t-call="blap"/></t>`)).toEqual({
|
expect(
|
||||||
|
parse(`<t t-foreach="list" t-as="item" t-key="item_index"><t t-call="blap"/></t>`)
|
||||||
|
).toEqual({
|
||||||
type: ASTType.TForEach,
|
type: ASTType.TForEach,
|
||||||
collection: "list",
|
collection: "list",
|
||||||
elem: "item",
|
elem: "item",
|
||||||
@@ -737,22 +748,24 @@ describe("qweb parser", () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
test("t-foreach expression with t-memo", async () => {
|
test("t-foreach expression with t-memo", async () => {
|
||||||
expect(parse(`<t t-foreach="list" t-as="item" t-memo="[row.x]" t-key="item_index"><t t-esc="item"/></t>`)).toEqual(
|
expect(
|
||||||
{
|
parse(
|
||||||
type: ASTType.TForEach,
|
`<t t-foreach="list" t-as="item" t-memo="[row.x]" t-key="item_index"><t t-esc="item"/></t>`
|
||||||
collection: "list",
|
)
|
||||||
elem: "item",
|
).toEqual({
|
||||||
key: "item_index",
|
type: ASTType.TForEach,
|
||||||
hasNoComponent: true,
|
collection: "list",
|
||||||
isOnlyChild: false,
|
elem: "item",
|
||||||
body: { type: ASTType.TEsc, expr: "item", defaultValue: "" },
|
key: "item_index",
|
||||||
memo: "[row.x]",
|
hasNoComponent: true,
|
||||||
hasNoFirst: true,
|
isOnlyChild: false,
|
||||||
hasNoIndex: false,
|
body: { type: ASTType.TEsc, expr: "item", defaultValue: "" },
|
||||||
hasNoLast: true,
|
memo: "[row.x]",
|
||||||
hasNoValue: true,
|
hasNoFirst: true,
|
||||||
}
|
hasNoIndex: false,
|
||||||
);
|
hasNoLast: true,
|
||||||
|
hasNoValue: true,
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
// ---------------------------------------------------------------------------
|
// ---------------------------------------------------------------------------
|
||||||
@@ -1161,7 +1174,9 @@ describe("qweb parser", () => {
|
|||||||
},
|
},
|
||||||
});
|
});
|
||||||
|
|
||||||
expect(parse(`<div t-foreach="list" t-translation="off" t-as="item" t-key="item_index">word</div>`)).toEqual({
|
expect(
|
||||||
|
parse(`<div t-foreach="list" t-translation="off" t-as="item" t-key="item_index">word</div>`)
|
||||||
|
).toEqual({
|
||||||
body: {
|
body: {
|
||||||
content: {
|
content: {
|
||||||
attrs: {},
|
attrs: {},
|
||||||
|
|||||||
Reference in New Issue
Block a user