diff --git a/attachment_preview/__manifest__.py b/attachment_preview/__manifest__.py index a0fa6a5768b..887d5b6162a 100644 --- a/attachment_preview/__manifest__.py +++ b/attachment_preview/__manifest__.py @@ -16,8 +16,11 @@ "web._assets_primary_variables": [], "web.assets_backend": [ "attachment_preview/static/src/js/attachmentPreviewWidget.esm.js", - "attachment_preview/static/src/js/models/attachment_card/attachment_card.esm.js", - "attachment_preview/static/src/js/components/chatter/chatter.esm.js", + "attachment_preview/static/src/js/utils.esm.js", + "attachment_preview/static/src/js/mail_models/attachment_card.esm.js", + "attachment_preview/static/src/js/mail_models/attachment_list.esm.js", + "attachment_preview/static/src/js/web_views/binary_field.esm.js", + "attachment_preview/static/src/js/web_views/form_renderer.esm.js", "attachment_preview/static/src/scss/attachment_preview.scss", "attachment_preview/static/src/xml/attachment_preview.xml", ], diff --git a/attachment_preview/static/src/js/mail_models/attachment_card.esm.js b/attachment_preview/static/src/js/mail_models/attachment_card.esm.js new file mode 100644 index 00000000000..e95aa73afe7 --- /dev/null +++ b/attachment_preview/static/src/js/mail_models/attachment_card.esm.js @@ -0,0 +1,37 @@ +/** @odoo-module **/ +import {query} from "web.rpc"; +import {registerPatch} from "@mail/model/model_core"; +import {showPreview} from "../utils.esm"; + +registerPatch({ + name: "AttachmentCard", + recordMethods: { + /** + * @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; + + query({ + model: "ir.attachment", + method: "get_attachment_extension", + args: [attachment_id], + }).then(function (extension) { + showPreview( + attachment_id, + self.attachment.defaultSource, + extension, + self.attachment.filename, + split_screen, + self.attachmentList.previewableAttachments + ); + }); + }, + }, +}); diff --git a/attachment_preview/static/src/js/mail_models/attachment_list.esm.js b/attachment_preview/static/src/js/mail_models/attachment_list.esm.js new file mode 100644 index 00000000000..1e0874ab263 --- /dev/null +++ b/attachment_preview/static/src/js/mail_models/attachment_list.esm.js @@ -0,0 +1,67 @@ +/** @odoo-module **/ +import {canPreview, getUrl} from "../utils.esm"; +import {query} from "web.rpc"; +import {registerPatch} from "@mail/model/model_core"; + +registerPatch({ + name: "AttachmentList", + lifecycleHooks: { + _created() { + var attachments = _.object( + this.attachments.map((attachment) => { + return attachment.id; + }), + this.attachments.map((attachment) => { + if ( + attachment.defaultSource && + attachment.defaultSource.length > 38 + ) { + return { + url: attachment.defaultSource, + extension: attachment.extension, + title: attachment.name, + }; + } + return { + url: "/web/content?id=" + attachment.id + "&download=true", + extension: attachment.extension, + title: attachment.name, + }; + }) + ); + + var self = this; + query({ + model: "ir.attachment", + method: "get_attachment_extension", + args: [ + _.map(_.keys(attachments), function (id) { + return parseInt(id, 10); + }), + ], + }).then(function (extensions) { + self.previewableAttachments = _.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 + ), + }; + } + ); + }); + }, + }, +}); diff --git a/attachment_preview/static/src/js/models/attachment_card/attachment_card.esm.js b/attachment_preview/static/src/js/models/attachment_card/attachment_card.esm.js deleted file mode 100644 index e7d1add9ece..00000000000 --- a/attachment_preview/static/src/js/models/attachment_card/attachment_card.esm.js +++ /dev/null @@ -1,217 +0,0 @@ -/** @odoo-module **/ -import {onMounted, onWillUnmount} from "@odoo/owl"; -import {AttachmentPreviewWidget} from "../../attachmentPreviewWidget.esm"; -import {FormRenderer} from "@web/views/form/form_renderer"; -import {bus} from "web.core"; -import {patch} from "@web/core/utils/patch"; -import {query} from "web.rpc"; -import {registerPatch} from "@mail/model/model_core"; - -patch(FormRenderer.prototype, "attachment_preview.FormRenderer", { - attachmentPreviewWidget: null, - - setup() { - var res = this._super(...arguments); - this.attachmentPreviewWidget = new AttachmentPreviewWidget(this); - this.attachmentPreviewWidget.on( - "hidden", - this, - this._attachmentPreviewWidgetHidden - ); - onMounted(() => { - this.attachmentPreviewWidget.insertAfter($(".o_form_view")); - bus.on("open_attachment_preview", this, this._onAttachmentPreview); - }); - onWillUnmount(() => { - bus.off("open_attachment_preview", this, this._onAttachmentPreview); - this.attachmentPreviewWidget.hide(); - }); - return res; - }, - - _attachmentPreviewWidgetHidden() { - $(".o_form_view").removeClass("attachment_preview"); - }, - - _onAttachmentPreview(attachment_id, attachment_info_list) { - $(".o_form_view").addClass("attachment_preview"); - this.attachmentPreviewWidget.setAttachments( - attachment_info_list, - attachment_id - ); - this.attachmentPreviewWidget.show(); - }, -}); -// FormRenderer patch - -export 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, ""); - } - 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"; - - return url; -} - -export function showPreview( - attachment_id, - attachment_url, - attachment_extension, - attachment_title, - split_screen, - attachment_info_list -) { - if (split_screen && attachment_info_list) { - bus.trigger("open_attachment_preview", attachment_id, attachment_info_list); - } else { - window.open( - getUrl( - attachment_id, - attachment_url, - attachment_extension, - attachment_title - ) - ); - } -} - -registerPatch({ - name: "AttachmentList", - lifecycleHooks: { - _created() { - var attachments = _.object( - this.attachments.map((attachment) => { - return attachment.id; - }), - this.attachments.map((attachment) => { - if ( - attachment.defaultSource && - attachment.defaultSource.length > 38 - ) { - return { - url: attachment.defaultSource, - extension: attachment.extension, - title: attachment.name, - }; - } - return { - url: "/web/content?id=" + attachment.id + "&download=true", - extension: attachment.extension, - title: attachment.name, - }; - }) - ); - - var self = this; - query({ - model: "ir.attachment", - method: "get_attachment_extension", - args: [ - _.map(_.keys(attachments), function (id) { - return parseInt(id, 10); - }), - ], - }).then(function (extensions) { - self.previewableAttachments = _.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 - ), - }; - } - ); - }); - }, - }, -}); -// AttachmentList patch - -registerPatch({ - name: "AttachmentCard", - recordMethods: { - /** - * @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; - - query({ - model: "ir.attachment", - method: "get_attachment_extension", - args: [attachment_id], - }).then(function (extension) { - showPreview( - attachment_id, - self.attachment.defaultSource, - extension, - self.attachment.filename, - split_screen, - self.attachmentList.previewableAttachments - ); - }); - }, - }, -}); -// AttachmentCard patch diff --git a/attachment_preview/static/src/js/utils.esm.js b/attachment_preview/static/src/js/utils.esm.js new file mode 100644 index 00000000000..a1bf20fa792 --- /dev/null +++ b/attachment_preview/static/src/js/utils.esm.js @@ -0,0 +1,81 @@ +/** @odoo-module **/ +import {bus} from "web.core"; + +export function canPreview(extension) { + return ( + $.inArray(extension, [ + "odt", + "odp", + "ods", + "fodt", + "pdf", + "ott", + "fodp", + "otp", + "fods", + "ots", + ]) > -1 + ); +} + +export 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, ""); + } + 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"; + + return url; +} + +export function showPreview( + attachment_id, + attachment_url, + attachment_extension, + attachment_title, + split_screen, + attachment_info_list +) { + if (split_screen && attachment_info_list) { + bus.trigger("open_attachment_preview", attachment_id, attachment_info_list); + } else { + window.open( + getUrl( + attachment_id, + attachment_url, + attachment_extension, + attachment_title + ) + ); + } +} diff --git a/attachment_preview/static/src/js/components/chatter/chatter.esm.js b/attachment_preview/static/src/js/web_views/binary_field.esm.js similarity index 95% rename from attachment_preview/static/src/js/components/chatter/chatter.esm.js rename to attachment_preview/static/src/js/web_views/binary_field.esm.js index 3de412fb034..c3885d77a46 100644 --- a/attachment_preview/static/src/js/components/chatter/chatter.esm.js +++ b/attachment_preview/static/src/js/web_views/binary_field.esm.js @@ -1,8 +1,5 @@ /** @odoo-module **/ -import { - canPreview, - showPreview, -} from "../../models/attachment_card/attachment_card.esm"; +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"; diff --git a/attachment_preview/static/src/js/web_views/form_renderer.esm.js b/attachment_preview/static/src/js/web_views/form_renderer.esm.js new file mode 100644 index 00000000000..a81e57acca1 --- /dev/null +++ b/attachment_preview/static/src/js/web_views/form_renderer.esm.js @@ -0,0 +1,42 @@ +/** @odoo-module **/ +import {onMounted, onWillUnmount} from "@odoo/owl"; +import {AttachmentPreviewWidget} from "../attachmentPreviewWidget.esm"; +import {FormRenderer} from "@web/views/form/form_renderer"; +import {bus} from "web.core"; +import {patch} from "@web/core/utils/patch"; + +patch(FormRenderer.prototype, "attachment_preview.FormRenderer", { + attachmentPreviewWidget: null, + + setup() { + var res = this._super(...arguments); + this.attachmentPreviewWidget = new AttachmentPreviewWidget(this); + this.attachmentPreviewWidget.on( + "hidden", + this, + this._attachmentPreviewWidgetHidden + ); + onMounted(() => { + this.attachmentPreviewWidget.insertAfter($(".o_form_view")); + bus.on("open_attachment_preview", this, this._onAttachmentPreview); + }); + onWillUnmount(() => { + bus.off("open_attachment_preview", this, this._onAttachmentPreview); + this.attachmentPreviewWidget.hide(); + }); + return res; + }, + + _attachmentPreviewWidgetHidden() { + $(".o_form_view").removeClass("attachment_preview"); + }, + + _onAttachmentPreview(attachment_id, attachment_info_list) { + $(".o_form_view").addClass("attachment_preview"); + this.attachmentPreviewWidget.setAttachments( + attachment_info_list, + attachment_id + ); + this.attachmentPreviewWidget.show(); + }, +});