Skip to content

Commit adab3e6

Browse files
author
FedericoBraidi
committed
[FIX] web: don't open the record when clicking on a binary widget field
If a list view contains a field (column) with binary widget, on click it will download the content of the field. This is the intended behavior but at the same time it will, by default, open the record of which it is part, which is strange since the user only wants to download the content. With this PR we make use of .stop on the t-on-click to detach the execution of the function from the opening of the record. We also add unit tests for this. closes odoo#268044 Task: 6260266 X-original-commit: 0231d0a Signed-off-by: Aaron Bohy (aab) <aab@odoo.com>
1 parent 6b85e43 commit adab3e6

2 files changed

Lines changed: 45 additions & 3 deletions

File tree

addons/web/static/src/views/fields/binary/binary_field.xml

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,9 @@
1515
class="btn btn-link fa fa-download o_download_file_button"
1616
data-tooltip="Download"
1717
aria-label="Download"
18-
t-on-click="this.onFileDownload"
19-
/>
18+
t-on-click.stop="this.onFileDownload"
19+
>
20+
</button>
2021
</t>
2122
<t t-set-slot="toggler">
2223
<span t-if="!this.fileName" class="btn btn-primary o_select_file_button py-0">
@@ -53,7 +54,7 @@
5354
</t>
5455
</t>
5556
<t t-elif="this.props.record.resId and this.props.record.data[this.props.name]">
56-
<a class="o_form_uri" href="#" t-on-click.prevent="this.onFileDownload">
57+
<a class="o_form_uri" href="#" t-on-click.prevent.stop="this.onFileDownload">
5758
<span class="fa fa-download me-2" />
5859
<t t-if="this.fileName" t-out="this.fileName" />
5960
</a>

addons/web/static/tests/views/fields/binary_field.test.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,3 +528,44 @@ test("doesn't crash if value is not a string", async () => {
528528
});
529529
expect(".o_field_binary input").toHaveValue("");
530530
});
531+
532+
test("Binary field in list view doesn't open the record when clicked", async () => {
533+
Partner._records[0]["document"] = BINARY_FILE;
534+
onRpc("/web/content", async (request) => {
535+
expect.step("/web/content");
536+
const body = await request.formData();
537+
return new Blob([body.get("data")], { type: "text/plain" });
538+
});
539+
await mountView({
540+
resModel: "res.partner",
541+
type: "list",
542+
arch: `
543+
<list>
544+
<field name="document" filename="foo" widget="binary"/>
545+
<field name="foo"/>
546+
</list>
547+
`,
548+
selectRecord: () => {
549+
expect.step("selectRecord");
550+
},
551+
});
552+
553+
expect(`.o_data_row .o_data_cell`).toHaveText("coucou.txt");
554+
const deferred = new Deferred();
555+
const downloadOnClick = (ev) => {
556+
const target = ev.target;
557+
if (target.tagName === "A" && "download" in target.attributes) {
558+
ev.preventDefault();
559+
document.removeEventListener("click", downloadOnClick);
560+
deferred.resolve();
561+
}
562+
};
563+
document.addEventListener("click", downloadOnClick);
564+
after(() => document.removeEventListener("click", downloadOnClick));
565+
await contains(".o_field_widget[name='document'] .o_form_uri").click();
566+
await deferred;
567+
expect.verifySteps(["/web/content"]);
568+
569+
await contains(`.o_data_row .o_data_cell:eq(1)`).click();
570+
expect.verifySteps(["selectRecord"]);
571+
});

0 commit comments

Comments
 (0)