[IMP]attachment_preview: Fix issue with attachment JS not loading properly.

This commit is contained in:
Bhavesh Heliconia 2025-03-13 10:46:06 +05:30
parent 3ca5b22649
commit b13d72e5e8

View File

@ -1,265 +1,259 @@
/** @odoo-module **/ /** @odoo-module **/
/* eslint-disable no-undef */
import AttachmentPreviewWidget from "../../attachmentPreviewWidget.esm"; import AttachmentPreviewWidget from "../../attachmentPreviewWidget.esm";
import FormRenderer from "web.FormRenderer"; import FormRenderer from "web.FormRenderer";
import {registerInstancePatchModel} from "@mail/model/model_core"; import {registerInstancePatchModel} from "@mail/model/model_core";
odoo.define("attachment_preview.attachment_card", function (require) { var rpc = require("web.rpc");
var rpc = require("web.rpc");
var chatterpreviewableAttachments = []; var chatterpreviewableAttachments = [];
var active_attachment_id = 0; var active_attachment_id = 0;
var first_click = true; var first_click = true;
FormRenderer.include({ FormRenderer.include({
custom_events: _.extend({}, FormRenderer.prototype.custom_events, { custom_events: _.extend({}, FormRenderer.prototype.custom_events, {
onAttachmentPreview: "_onAttachmentPreview", onAttachmentPreview: "_onAttachmentPreview",
}), }),
attachmentPreviewWidget: null, attachmentPreviewWidget: null,
init: function () { init: function () {
var res = this._super(...arguments); var res = this._super(...arguments);
this.attachmentPreviewWidget = new AttachmentPreviewWidget(this); this.attachmentPreviewWidget = new AttachmentPreviewWidget(this);
this.attachmentPreviewWidget.on( this.attachmentPreviewWidget.on(
"hidden", "hidden",
this, this,
this._attachmentPreviewWidgetHidden this._attachmentPreviewWidgetHidden
);
return res;
},
start: function () {
var self = this;
return this._super.apply(this, arguments).then(function () {
self.attachmentPreviewWidget.insertAfter(self.$el);
});
},
_attachmentPreviewWidgetHidden: function () {
this.$el.removeClass("attachment_preview");
},
showAttachmentPreviewWidget: function (first_c) {
this.$el.addClass("attachment_preview");
this.attachmentPreviewWidget.setAttachments(
chatterpreviewableAttachments,
active_attachment_id,
first_c
);
this.attachmentPreviewWidget.show();
},
on_detach_callback: function () {
this.attachmentPreviewWidget.hide();
return this._super.apply(this, arguments);
},
_onAttachmentPreview: function () {
first_click = true;
this.showAttachmentPreviewWidget(first_click);
},
});
function canPreview(extension) {
return (
$.inArray(extension, [
"odt",
"odp",
"ods",
"fodt",
"pdf",
"ott",
"fodp",
"otp",
"fods",
"ots",
]) > -1
); );
} return res;
},
function getUrl( start: function () {
attachment_id, var self = this;
attachment_url, return this._super.apply(this, arguments).then(function () {
attachment_extension, self.attachmentPreviewWidget.insertAfter(self.$el);
attachment_title });
) { },
var url = "";
if (attachment_url) { _attachmentPreviewWidgetHidden: function () {
if (attachment_url.slice(0, 21) === "/web/static/lib/pdfjs") { this.$el.removeClass("attachment_preview");
url = (window.location.origin || "") + attachment_url; },
} else {
url = showAttachmentPreviewWidget: function (first_c) {
(window.location.origin || "") + this.$el.addClass("attachment_preview");
"/attachment_preview/static/lib/ViewerJS/index.html" +
"?type=" + this.attachmentPreviewWidget.setAttachments(
encodeURIComponent(attachment_extension) + chatterpreviewableAttachments,
"&title=" + active_attachment_id,
encodeURIComponent(attachment_title) + first_c
"&zoom=automatic" + );
"#" + this.attachmentPreviewWidget.show();
attachment_url.replace(window.location.origin, ""); },
}
return url; on_detach_callback: function () {
this.attachmentPreviewWidget.hide();
return this._super.apply(this, arguments);
},
_onAttachmentPreview: function () {
first_click = true;
this.showAttachmentPreviewWidget(first_click);
},
});
function canPreview(extension) {
return (
$.inArray(extension, [
"odt",
"odp",
"ods",
"fodt",
"pdf",
"ott",
"fodp",
"otp",
"fods",
"ots",
]) > -1
);
}
function getUrl(attachment_id, attachment_url, attachment_extension, attachment_title) {
var url = "";
if (attachment_url) {
if (attachment_url.slice(0, 21) === "/web/static/lib/pdfjs") {
url = (window.location.origin || "") + attachment_url;
} else {
url =
(window.location.origin || "") +
"/attachment_preview/static/lib/ViewerJS/index.html" +
"?type=" +
encodeURIComponent(attachment_extension) +
"&title=" +
encodeURIComponent(attachment_title) +
"&zoom=automatic" +
"#" +
attachment_url.replace(window.location.origin, "");
} }
url =
(window.location.origin || "") +
"/attachment_preview/static/lib/ViewerJS/index.html" +
"?type=" +
encodeURIComponent(attachment_extension) +
"&title=" +
encodeURIComponent(attachment_title) +
"&zoom=automatic" +
"#" +
"/web/content/" +
attachment_id +
"?model%3Dir.attachment";
return url; return url;
} }
url =
(window.location.origin || "") +
"/attachment_preview/static/lib/ViewerJS/index.html" +
"?type=" +
encodeURIComponent(attachment_extension) +
"&title=" +
encodeURIComponent(attachment_title) +
"&zoom=automatic" +
"#" +
"/web/content/" +
attachment_id +
"?model%3Dir.attachment";
registerInstancePatchModel( return url;
"mail.attachment_card", }
"attachment_preview/static/src/js/models/attachment_card/attachment_card.js",
{
/**
* @override
*/
_created() {
this._super();
this._onPreviewAttachment = this._onPreviewAttachment.bind(this);
var attachments = _.object( registerInstancePatchModel(
this.attachmentList.attachments.map((attachment) => { "mail.attachment_card",
console.log("attachment", attachment); "attachment_preview/static/src/js/models/attachment_card/attachment_card.js",
return attachment.id; {
}), /**
this.attachmentList.attachments.map((attachment) => { * @override
if ( */
attachment.defaultSource && _created() {
attachment.defaultSource.length > 38 this._super();
) { this._onPreviewAttachment = this._onPreviewAttachment.bind(this);
return {
url: attachment.defaultSource, var attachments = _.object(
extension: attachment.extension, this.attachmentList.attachments.map((attachment) => {
title: attachment.name, console.log("attachment", attachment);
}; return attachment.id;
} }),
this.attachmentList.attachments.map((attachment) => {
if (
attachment.defaultSource &&
attachment.defaultSource.length > 38
) {
return { return {
url: "/web/content?id=" + attachment.id + "&download=true", url: attachment.defaultSource,
extension: attachment.extension, extension: attachment.extension,
title: attachment.name, title: attachment.name,
}; };
}) }
return {
url: "/web/content?id=" + attachment.id + "&download=true",
extension: attachment.extension,
title: attachment.name,
};
})
);
rpc.query({
model: "ir.attachment",
method: "get_attachment_extension",
args: [
_.map(_.keys(attachments), function (id) {
return parseInt(id, 10);
}),
],
}).then(function (extensions) {
var reviewableAttachments = _.map(
_.keys(
_.pick(extensions, function (extension) {
return canPreview(extension);
})
),
function (id) {
return {
id: id,
url: attachments[id].url,
extension: extensions[id],
title: attachments[id].title,
previewUrl: getUrl(
id,
attachments[id].url,
extensions[id],
attachments[id].title
),
};
}
); );
chatterpreviewableAttachments = reviewableAttachments;
});
},
rpc.query({ /**
model: "ir.attachment", * @private
method: "get_attachment_extension", * @param {attachment_id} attachment_id of the attachment
args: [ * @param {attachment_url} attachment_url of the attachment
_.map(_.keys(attachments), function (id) { * @param {attachment_extension} attachment_extension of the attachment
return parseInt(id, 10); * @param {attachment_title} attachment_title of the attachment
}), * @param {split_screen} split_screen of the attachment
], */
}).then(function (extensions) { _showPreview(
var reviewableAttachments = _.map( attachment_id,
_.keys( attachment_url,
_.pick(extensions, function (extension) { attachment_extension,
return canPreview(extension); attachment_title,
}) split_screen
), ) {
function (id) { // Let active_attURL = "";
return { // this.attachmentList.attachments.forEach((att) => {
id: id, // if (
url: attachments[id].url, // parseInt(att.localId.slice(20).slice(0, -1)) === attachment_id
extension: extensions[id], // ) {
title: attachments[id].title, // if (att.__values.url === undefined) {
previewUrl: getUrl( // att.__values.url = attachment_url.slice(
id, // window.location.origin.length
attachments[id].url, // );
extensions[id], // active_attURL = att.__values.url;
attachments[id].title // }
), // }
}; // });
} var url = getUrl(
);
chatterpreviewableAttachments = reviewableAttachments;
});
},
/**
* @private
* @param {attachment_id} attachment_id of the attachment
* @param {attachment_url} attachment_url of the attachment
* @param {attachment_extension} attachment_extension of the attachment
* @param {attachment_title} attachment_title of the attachment
* @param {split_screen} split_screen of the attachment
*/
_showPreview(
attachment_id, attachment_id,
attachment_url, attachment_url,
attachment_extension, attachment_extension,
attachment_title, attachment_title
split_screen );
) {
// Let active_attURL = ""; if (split_screen) {
// this.attachmentList.attachments.forEach((att) => { this.component.trigger("onAttachmentPreview", {
// if ( url: url,
// parseInt(att.localId.slice(20).slice(0, -1)) === attachment_id active_attachment_id: active_attachment_id,
// ) { });
// if (att.__values.url === undefined) { } else {
// att.__values.url = attachment_url.slice( window.open(url);
// window.location.origin.length }
// ); },
// active_attURL = att.__values.url;
// } /**
// } * @private
// }); * @param {event} event
var url = getUrl( */
_onPreviewAttachment(event) {
event.preventDefault();
var self = this,
$target = $(event.currentTarget),
split_screen = $target.attr("data-target") !== "new",
attachment_id = this.attachment.id,
attachment_title = this.attachment.filename,
attachment_url = this.attachment.defaultSource;
active_attachment_id = attachment_id;
rpc.query({
model: "ir.attachment",
method: "get_attachment_extension",
args: [attachment_id],
}).then(function (extension) {
self._showPreview(
attachment_id, attachment_id,
attachment_url, attachment_url,
attachment_extension, extension,
attachment_title attachment_title,
split_screen
); );
});
if (split_screen) { },
this.component.trigger("onAttachmentPreview", { }
url: url, );
active_attachment_id: active_attachment_id,
});
} else {
window.open(url);
}
},
/**
* @private
* @param {event} event
*/
_onPreviewAttachment(event) {
event.preventDefault();
var self = this,
$target = $(event.currentTarget),
split_screen = $target.attr("data-target") !== "new",
attachment_id = this.attachment.id,
attachment_title = this.attachment.filename,
attachment_url = this.attachment.defaultSource;
active_attachment_id = attachment_id;
rpc.query({
model: "ir.attachment",
method: "get_attachment_extension",
args: [attachment_id],
}).then(function (extension) {
self._showPreview(
attachment_id,
attachment_url,
extension,
attachment_title,
split_screen
);
});
},
}
);
});