From 0bce1f3b3b8fb296016efd3d3a1cdd1f8ba8b03f Mon Sep 17 00:00:00 2001 From: vancouver29 Date: Tue, 14 Mar 2023 10:19:59 +0100 Subject: [PATCH] [MIG] attachment_preview: Migration to 15.0 --- attachment_preview/__manifest__.py | 6 +- attachment_preview/models/ir_attachment.py | 10 +- ...dget.js => attachmentPreviewWidget.esm.js} | 7 +- .../src/js/components/chatter/chatter.esm.js | 272 +++++++++++++++ .../src/js/components/chatter/chatter.js | 309 ------------------ .../attachment_card/attachment_card.esm.js | 276 ++++++++++++++++ .../models/attachment_card/attachment_card.js | 275 ---------------- 7 files changed, 560 insertions(+), 595 deletions(-) rename attachment_preview/static/src/js/{attachmentPreviewWidget.js => attachmentPreviewWidget.esm.js} (95%) create mode 100644 attachment_preview/static/src/js/components/chatter/chatter.esm.js delete mode 100644 attachment_preview/static/src/js/components/chatter/chatter.js create mode 100644 attachment_preview/static/src/js/models/attachment_card/attachment_card.esm.js delete mode 100644 attachment_preview/static/src/js/models/attachment_card/attachment_card.js diff --git a/attachment_preview/__manifest__.py b/attachment_preview/__manifest__.py index 52dbf876..5c56817c 100644 --- a/attachment_preview/__manifest__.py +++ b/attachment_preview/__manifest__.py @@ -15,9 +15,9 @@ "assets": { "web._assets_primary_variables": [], "web.assets_backend": [ - "attachment_preview/static/src/js/models/attachment_card/attachment_card.js", - "attachment_preview/static/src/js/attachmentPreviewWidget.js", - "attachment_preview/static/src/js/components/chatter/chatter.js", + "attachment_preview/static/src/js/models/attachment_card/attachment_card.esm.js", + "attachment_preview/static/src/js/attachmentPreviewWidget.esm.js", + "attachment_preview/static/src/js/components/chatter/chatter.esm.js", "attachment_preview/static/src/scss/attachment_preview.scss", ], "web.assets_frontend": [], diff --git a/attachment_preview/models/ir_attachment.py b/attachment_preview/models/ir_attachment.py index f109d881..46f5bb99 100644 --- a/attachment_preview/models/ir_attachment.py +++ b/attachment_preview/models/ir_attachment.py @@ -17,7 +17,7 @@ class IrAttachment(models.Model): @api.model def get_binary_extension(self, model, ids, binary_field, filename_field=None): result = {} - ids_to_browse = ids if isinstance(ids, collections.Iterable) else [ids] + ids_to_browse = ids if isinstance(ids, collections.abc.Iterable) else [ids] # First pass: load fields in bin_size mode to avoid loading big files # unnecessarily. @@ -51,21 +51,21 @@ class IrAttachment(models.Model): # _logger.debug( # "Magic determined mimetype %s from file %s", # mimetype, - # this.store_fname, + # this.store_fname # ) else: mimetype = magic.from_buffer(this[binary_field], mime=True) - # _logger.debug("Magic determined mimetype %s from buffer", mimetype) + _logger.debug("Magic determined mimetype %s from buffer", mimetype) except ImportError: (mimetype, encoding) = mimetypes.guess_type( "data:;base64," + this[binary_field].decode("utf-8"), strict=False ) - _logger.debug("Mimetypes guessed type %s from buffer", mimetype) + # _logger.debug("Mimetypes guessed type %s from buffer", mimetype) extension = mimetypes.guess_extension(mimetype.split(";")[0], strict=False) result[this.id] = extension for _id in result: result[_id] = (result[_id] or "").lstrip(".").lower() - return result if isinstance(ids, collections.Iterable) else result[ids] + return result if isinstance(ids, collections.abc.Iterable) else result[ids] @api.model def get_attachment_extension(self, ids): diff --git a/attachment_preview/static/src/js/attachmentPreviewWidget.js b/attachment_preview/static/src/js/attachmentPreviewWidget.esm.js similarity index 95% rename from attachment_preview/static/src/js/attachmentPreviewWidget.js rename to attachment_preview/static/src/js/attachmentPreviewWidget.esm.js index 1de03242..61d5fb63 100644 --- a/attachment_preview/static/src/js/attachmentPreviewWidget.js +++ b/attachment_preview/static/src/js/attachmentPreviewWidget.esm.js @@ -1,6 +1,5 @@ /** @odoo-module */ import Widget from "web.Widget"; -import {_t} from "web.core"; var active_attachment_index = 0; var is_first_click = true; @@ -17,7 +16,7 @@ var AttachmentPreviewWidget = Widget.extend({ }, start: function () { - // first_click = true; + // First_click = true; var res = this._super.apply(this, arguments); this.$overlay = $(".attachment_preview_overlay"); this.$iframe = $(".attachment_preview_iframe"); @@ -102,7 +101,9 @@ var AttachmentPreviewWidget = Widget.extend({ if (is_first_click) { for (let i = 0; i < this.attachments.length; i++) { - if (parseInt(this.attachments[i].id) === this.active_attachment_id) { + if ( + parseInt(this.attachments[i].id, 10) === this.active_attachment_id + ) { active_attachment_index = i; is_first_click = false; } diff --git a/attachment_preview/static/src/js/components/chatter/chatter.esm.js b/attachment_preview/static/src/js/components/chatter/chatter.esm.js new file mode 100644 index 00000000..5eee6df0 --- /dev/null +++ b/attachment_preview/static/src/js/components/chatter/chatter.esm.js @@ -0,0 +1,272 @@ +/** @odoo-module **/ +import AttachmentPreviewWidget from "../../attachmentPreviewWidget"; +import {Chatter} from "@mail/components/chatter/chatter"; +import {patch} from "web.utils"; + +odoo.define("attachment_preview.chatter", function (require) { + const components = {Chatter}; + var rpc = require("web.rpc"); + var basic_fields = require("web.basic_fields"); + var core = require("web.core"); + var _t = core._t; + + 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) + + "&zoom=automatic" + + "&title=" + + encodeURIComponent(attachment_title) + + "#" + + attachment_url.replace(window.location.origin, ""); + } + } else { + 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; + } + + function canPreview(extension) { + return ( + $.inArray(extension, [ + "odt", + "odp", + "ods", + "fodt", + "pdf", + "ott", + "fodp", + "otp", + "fods", + "ots", + ]) > -1 + ); + } + + patch( + components.Chatter.prototype, + "attachment_preview/static/src/js/components/chatter/chatter.js", + { + /** + * @override + */ + constructor(...args) { + this._super(...args); + + this._showPreview = this._showPreview.bind(this); + this.canPreview = this.canPreview.bind(this); + }, + + /** + * @override + */ + _update() { + // Var res = this._super.apply(this, arguments); + var self = this; + self._getPreviewableAttachments().then(function (atts) { + self.previewableAttachments = atts; + self._updatePreviewButtons(self.previewableAttachments); + if (!self.attachmentPreviewWidget) { + self.attachmentPreviewWidget = new AttachmentPreviewWidget( + self + ); + self.attachmentPreviewWidget.setAttachments(atts); + } + self.previewableAttachments = atts; + // ChatterpreviewableAttachments = atts; + self.attachmentPreviewWidget.setAttachments(atts); + }); + }, + + _getPreviewableAttachments: function () { + var self = this; + var deferred = $.Deferred(); + const chatter = self.messaging.models["mail.chatter"].get( + self.props.chatterLocalId + ); + const thread = chatter ? chatter.thread : undefined; + + var attachments = {}; + if (thread) { + attachments = thread.allAttachments; + } + + attachments = _.object( + attachments.map((attachment) => { + return attachment.id; + }), + attachments.map((attachment) => { + if (attachment.defaultSource) { + 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, + }; + }) + ); + + 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 + ), + }; + } + ); + + deferred.resolve(reviewableAttachments); + }, + + function () { + deferred.reject(); + } + ); + + return deferred.promise(); + }, + + _updatePreviewButtons: function (previewableAttachments) { + $(this) + .find(".o_attachment_preview") + .each(function () { + var $this = $(this); + var att = _.findWhere(previewableAttachments, { + id: $this.attr("data-id"), + }); + if (att) { + $this.attr("data-extension", att.extension); + } else { + $this.remove(); + } + }); + }, + } + ); + + basic_fields.FieldBinaryFile.include(components.Chatter); + basic_fields.FieldBinaryFile.include({ + showPreview( + attachment_id, + attachment_url, + attachment_extension, + attachment_title, + split_screen + ) { + var url = getUrl( + attachment_id, + attachment_url, + attachment_extension, + attachment_title + ); + if (split_screen) { + this.trigger("onAttachmentPreview", {url: url}); + } else { + window.open(url); + } + }, + + _renderReadonly: function () { + var self = this; + this._super.apply(this, arguments); + + if (this.recordData.id) { + this._getBinaryExtension().then(function (extension) { + if (canPreview(extension)) { + // Self._renderPreviewButton(extension, recordData); + self._renderPreviewButton(extension); + } + }); + } + }, + + _renderPreviewButton: function (extension) { + this.$previewBtn = $(""); + this.$previewBtn.addClass("fa fa-external-link mr-2"); + this.$previewBtn.attr("href"); + this.$previewBtn.attr( + "title", + _.str.sprintf(_t("Preview %s"), this.field.string) + ); + this.$previewBtn.attr("data-extension", extension); + this.$el.find(".fa-download").after(this.$previewBtn); + this.$previewBtn.on("click", this._onPreview.bind(this)); + }, + + _getBinaryExtension: function () { + return rpc.query({ + model: "ir.attachment", + method: "get_binary_extension", + args: [this.model, this.recordData.id, this.name, this.attrs.filename], + }); + }, + + _onPreview: function (event) { + this.showPreview( + null, + _.str.sprintf( + "/web/content?model=%s&field=%s&id=%d", + this.model, + this.name, + this.recordData.id + ), + $(event.currentTarget).attr("data-extension"), + _.str.sprintf(_t("Preview %s"), this.field.string), + false + ); + event.stopPropagation(); + }, + }); +}); diff --git a/attachment_preview/static/src/js/components/chatter/chatter.js b/attachment_preview/static/src/js/components/chatter/chatter.js deleted file mode 100644 index d3f5dd24..00000000 --- a/attachment_preview/static/src/js/components/chatter/chatter.js +++ /dev/null @@ -1,309 +0,0 @@ -/** @odoo-module **/ -import {Chatter} from "@mail/components/chatter/chatter"; -import {patch} from "web.utils"; -import AttachmentPreviewWidget from "../../attachmentPreviewWidget"; - -const components = {Chatter}; -var rpc = require("web.rpc"); - -var basic_fields = require("web.basic_fields"); - -var chatterpreviewableAttachments = []; -var core = require("web.core"); -var _t = core._t; - -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) + - "&zoom=automatic" + - "&title=" + - encodeURIComponent(attachment_title) + - "#" + - attachment_url.replace(window.location.origin, ""); - } - } else { - 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; -} - -function canPreview(extension) { - return ( - $.inArray(extension, [ - "odt", - "odp", - "ods", - "fodt", - "pdf", - "ott", - "fodp", - "otp", - "fods", - "ots", - ]) > -1 - ); -} - -patch( - components.Chatter.prototype, - "attachment_preview/static/src/js/components/chatter/chatter.js", - { - /** - * @override - */ - constructor(...args) { - this._super(...args); - - this._showPreview = this._showPreview.bind(this); - this.canPreview = this.canPreview.bind(this); - }, - - /** - * @private - */ - _showPreview( - attachment_id, - attachment_url, - attachment_extension, - attachment_title, - split_screen - ) { - let active_attURL = ""; - this.attachmentList.attachments.forEach((att) => { - if (parseInt(att.localId.slice(20).slice(0, -1)) === attachment_id) { - if (att.__values.url === undefined) { - att.__values.url = attachment_url.slice( - window.location.origin.length - ); - active_attURL = att.__values.url; - } - } - }); - var url = getUrl( - attachment_id, - attachment_url, - attachment_extension, - attachment_title - ); - if (split_screen) { - this.component.trigger("onAttachmentPreview", { - url: url, - active_attachment_id: active_attachment_id, - }); - } else { - window.open(url); - } - }, - - /** - * @override - */ - _update() { - var res = this._super.apply(this, arguments); - var self = this; - self._getPreviewableAttachments().then( - function (atts) { - self.previewableAttachments = atts; - self._updatePreviewButtons(self.previewableAttachments); - if (!self.attachmentPreviewWidget) { - self.attachmentPreviewWidget = new AttachmentPreviewWidget( - self - ); - self.attachmentPreviewWidget.setAttachments(atts); - } - self.previewableAttachments = atts; - chatterpreviewableAttachments = atts; - self.attachmentPreviewWidget.setAttachments(atts); - }.bind(self) - ); - }, - - _getPreviewableAttachments: function () { - var self = this; - var deferred = $.Deferred(); - var $items = $(".o_attachment_preview"); - - const chatter = this.messaging.models["mail.chatter"].get( - this.props.chatterLocalId - ); - const thread = chatter ? chatter.thread : undefined; - - if (thread) { - attachments = thread.allAttachments; - } - - var attachments = _.object( - attachments.map((attachment) => { - return attachment.id; - }), - attachments.map((attachment) => { - if (attachment.defaultSource) { - return { - url: attachment.defaultSource, - extension: attachment.extension, - title: attachment.name, - }; - } else { - 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); - }), - ], - }).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 - ), - }; - } - ); - - deferred.resolve(reviewableAttachments); - }, - - function () { - deferred.reject(); - } - ); - - return deferred.promise(); - }, - - _updatePreviewButtons: function (previewableAttachments) { - $(this) - .find(".o_attachment_preview") - .each(function () { - var $this = $(this); - var att = _.findWhere(previewableAttachments, { - id: $this.attr("data-id"), - }); - if (att) { - $this.attr("data-extension", att.extension); - } else { - $this.remove(); - } - }); - }, - } -); - -basic_fields.FieldBinaryFile.include(components.Chatter); -basic_fields.FieldBinaryFile.include({ - showPreview( - attachment_id, - attachment_url, - attachment_extension, - attachment_title, - split_screen - ) { - var url = getUrl( - attachment_id, - attachment_url, - attachment_extension, - attachment_title - ); - if (split_screen) { - this.trigger("onAttachmentPreview", {url: url}); - } else { - window.open(url); - } - }, - - _renderReadonly: function () { - var self = this; - this._super.apply(this, arguments); - - if (this.recordData.id) { - this._getBinaryExtension().then(function (extension) { - if (canPreview(extension)) { - // self._renderPreviewButton(extension, recordData); - self._renderPreviewButton(extension); - } - }); - } - }, - - _renderPreviewButton: function (extension) { - this.$previewBtn = $(""); - this.$previewBtn.addClass("fa fa-external-link mr-2"); - this.$previewBtn.attr("href"); - this.$previewBtn.attr( - "title", - _.str.sprintf(_t("Preview %s"), this.field.string) - ); - this.$previewBtn.attr("data-extension", extension); - this.$el.find(".fa-download").after(this.$previewBtn); - this.$previewBtn.on("click", this._onPreview.bind(this)); - }, - - _getBinaryExtension: function () { - return rpc.query({ - model: "ir.attachment", - method: "get_binary_extension", - args: [this.model, this.recordData.id, this.name, this.attrs.filename], - }); - }, - - _onPreview: function (event) { - this.showPreview( - null, - _.str.sprintf( - "/web/content?model=%s&field=%s&id=%d", - this.model, - this.name, - this.recordData.id - ), - $(event.currentTarget).attr("data-extension"), - _.str.sprintf(_t("Preview %s"), this.field.string), - false - ); - event.stopPropagation(); - }, -}); 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 new file mode 100644 index 00000000..5a381cd9 --- /dev/null +++ b/attachment_preview/static/src/js/models/attachment_card/attachment_card.esm.js @@ -0,0 +1,276 @@ +/** @odoo-module **/ +import AttachmentPreviewWidget from "../../attachmentPreviewWidget.esm"; +import FormRenderer from "web.FormRenderer"; +import {registerInstancePatchModel} from "@mail/model/model_core"; + +odoo.define("attachment_preview.attachment_card", function (require) { + var rpc = require("web.rpc"); + + var chatterpreviewableAttachments = []; + var active_attachment_id = 0; + var first_click = true; + + FormRenderer.include({ + custom_events: _.extend({}, FormRenderer.prototype.custom_events, { + onAttachmentPreview: "_onAttachmentPreview", + }), + attachmentPreviewWidget: null, + + init: function () { + var res = this._super(...arguments); + this.attachmentPreviewWidget = new AttachmentPreviewWidget(this); + this.attachmentPreviewWidget.on( + "hidden", + this, + 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 + ); + } + + 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; + } + + registerInstancePatchModel( + "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( + this.attachmentList.attachments.map((attachment) => { + console.log("attachment", attachment); + return attachment.id; + }), + this.attachmentList.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, + }; + }) + ); + + 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; + }); + }, + + /** + * @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_url, + attachment_extension, + attachment_title, + split_screen + ) { + // Let active_attURL = ""; + // this.attachmentList.attachments.forEach((att) => { + // if ( + // parseInt(att.localId.slice(20).slice(0, -1)) === attachment_id + // ) { + // if (att.__values.url === undefined) { + // att.__values.url = attachment_url.slice( + // window.location.origin.length + // ); + // active_attURL = att.__values.url; + // } + // } + // }); + var url = getUrl( + attachment_id, + attachment_url, + attachment_extension, + attachment_title + ); + + 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_extension = "pdf", + attachment_title = this.attachment.filename, + attachment_url = this.attachment.defaultSource; + active_attachment_id = attachment_id; + + if (attachment_extension) { + self._showPreview( + attachment_id, + attachment_url, + attachment_extension, + attachment_title, + split_screen + ); + } else { + rpc.query({ + model: "ir.attachment", + method: "get_attachment_extension", + args: [attachment_id], + }).then(function (extension) { + self.showPreview( + attachment_id, + attachment_url, + extension, + null, + split_screen + ); + }); + } + }, + } + ); +}); diff --git a/attachment_preview/static/src/js/models/attachment_card/attachment_card.js b/attachment_preview/static/src/js/models/attachment_card/attachment_card.js deleted file mode 100644 index f70e6567..00000000 --- a/attachment_preview/static/src/js/models/attachment_card/attachment_card.js +++ /dev/null @@ -1,275 +0,0 @@ -/** @odoo-module **/ -import { - registerClassPatchModel, - registerInstancePatchModel, - registerFieldPatchModel, -} from "@mail/model/model_core"; - -import {attr} from "@mail/model/model_field"; - -import FormRenderer from "web.FormRenderer"; - -import AttachmentPreviewWidget from "../../attachmentPreviewWidget"; - -var rpc = require("web.rpc"); -var basic_fields = require("web.basic_fields"); - -var chatterpreviewableAttachments = []; -var active_attachment_id = 0; -var active_attachment_index = 0; -var first_click = true; - -FormRenderer.include({ - custom_events: _.extend({}, FormRenderer.prototype.custom_events, { - onAttachmentPreview: "_onAttachmentPreview", - }), - attachmentPreviewWidget: null, - - init: function (parent, state, params) { - var res = this._super(...arguments); - this.attachmentPreviewWidget = new AttachmentPreviewWidget(this); - this.attachmentPreviewWidget.on( - "hidden", - this, - 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_click) { - this.$el.addClass("attachment_preview"); - - this.attachmentPreviewWidget.setAttachments( - chatterpreviewableAttachments, - active_attachment_id, - first_click - ); - 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 - ); -} - -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; - } else { - 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; - } -} - -registerInstancePatchModel( - "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( - this.attachmentList.attachments.map((attachment) => { - console.log("attachment", attachment); - return attachment.id; - }), - this.attachmentList.attachments.map((attachment) => { - if ( - attachment.defaultSource && - attachment.defaultSource.length > 38 - ) { - return { - url: attachment.defaultSource, - extension: attachment.extension, - title: attachment.name, - }; - } else { - 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; - }); - }, - - /** - * @private - */ - _showPreview( - attachment_id, - attachment_url, - attachment_extension, - attachment_title, - split_screen - ) { - let active_attURL = ""; - this.attachmentList.attachments.forEach((att) => { - if (parseInt(att.localId.slice(20).slice(0, -1)) === attachment_id) { - if (att.__values.url === undefined) { - att.__values.url = attachment_url.slice( - window.location.origin.length - ); - active_attURL = att.__values.url; - } - } - }); - var url = getUrl( - attachment_id, - attachment_url, - attachment_extension, - attachment_title - ); - - console.log("active_attachment_id in _showPreview", active_attachment_id); - console.log("url in _showPreview", url); - if (split_screen) { - this.component.trigger("onAttachmentPreview", { - url: url, - active_attachment_id: active_attachment_id, - }); - } else { - window.open(url); - } - }, - - /** - * @private - */ - _onPreviewAttachment(event) { - event.preventDefault(); - - var self = this, - $target = $(event.currentTarget), - split_screen = $target.attr("data-target") !== "new", - attachment_id = this.attachment.id, - attachment_extension = "pdf", - attachment_title = this.attachment.filename, - attachment_url = this.attachment.defaultSource; - active_attachment_id = attachment_id; - - if (attachment_extension) { - this._showPreview( - attachment_id, - attachment_url, - attachment_extension, - attachment_title, - split_screen - ); - } else { - rpc.query({ - model: "ir.attachment", - method: "get_attachment_extension", - args: [attachment_id], - }).then(function (extension) { - this.showPreview( - attachment_id, - attachment_url, - extension, - null, - split_screen - ); - }); - } - }, - } -);