-
-
Notifications
You must be signed in to change notification settings - Fork 387
Expand file tree
/
Copy pathbinary_field.esm.js
More file actions
65 lines (61 loc) · 2.18 KB
/
binary_field.esm.js
File metadata and controls
65 lines (61 loc) · 2.18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import {canPreview, showPreview} from "../../utils.esm";
import {BinaryField} from "@web/views/fields/binary/binary_field";
import {_t} from "@web/core/l10n/translation";
import {onMounted} from "@odoo/owl";
import {patch} from "@web/core/utils/patch";
import {sprintf} from "@web/core/utils/strings";
import {useService} from "@web/core/utils/hooks";
patch(BinaryField.prototype, {
setup() {
super.setup();
this.orm = useService("orm");
onMounted(this._preview_onMounted);
},
async _preview_onMounted() {
if (this.props.record.resId) {
var extension = await this.orm.call(
"ir.attachment",
"get_binary_extension",
[
this.props.record.resModel,
this.props.record.evalContext.id,
this.props.name,
this.props.fileNameField,
]
);
if (canPreview(extension)) {
this._renderPreviewButton(extension);
}
}
},
_renderPreviewButton(extension) {
// Add a button same as standard fa-download one.
var dl_button = $(this.__owl__.bdom.parentEl).find("button.fa-download");
if (dl_button.length !== 1) return;
var preview_button = $("<button/>");
preview_button.addClass("btn btn-secondary fa fa-external-link");
preview_button.attr("data-tooltip", "Preview");
preview_button.attr("aria-label", "Preview");
preview_button.attr("title");
preview_button.attr("data-extension", extension);
dl_button.after(preview_button);
preview_button.on("click", this._onPreview.bind(this));
},
_onPreview(event) {
showPreview(
null,
sprintf(
"/web/content?model=%s&field=%s&id=%s",
this.props.record.resModel,
this.props.name,
this.props.record.resId
),
$(event.currentTarget).attr("data-extension"),
sprintf(_t("Preview %s"), this.fileName),
false,
null,
this.fileName
);
event.stopPropagation();
},
});