diff --git a/attachment_preview/models/ir_attachment.py b/attachment_preview/models/ir_attachment.py index a24cb4cc..425c357b 100644 --- a/attachment_preview/models/ir_attachment.py +++ b/attachment_preview/models/ir_attachment.py @@ -16,6 +16,8 @@ class IrAttachment(models.Model): @api.model def get_binary_extension(self, model, ids, binary_field, filename_field=None): + # import pdb + # pdb.set_trace() result = {} ids_to_browse = ids if isinstance(ids, collections.Iterable) else [ids] @@ -73,4 +75,4 @@ class IrAttachment(models.Model): @api.model def get_attachment_extension(self, ids): - return self.get_binary_extension(self._name, ids, "datas", "datas_fname") + return self.get_binary_extension(self._name, ids, "datas", "name") diff --git a/attachment_preview/static/lib/ViewerJS/index.html b/attachment_preview/static/lib/ViewerJS/index.html index c79df466..2eb1fd47 100644 --- a/attachment_preview/static/lib/ViewerJS/index.html +++ b/attachment_preview/static/lib/ViewerJS/index.html @@ -74,8 +74,6 @@ c;a?(e.title||(e.title=a.replace(/^.*[\\\/]/,"")),e.documentUrl=a,b(a,function(b //]]> - - diff --git a/attachment_preview/static/src/js/attachment_preview.js b/attachment_preview/static/src/js/attachment_preview.js index 48673718..64cb1b8b 100644 --- a/attachment_preview/static/src/js/attachment_preview.js +++ b/attachment_preview/static/src/js/attachment_preview.js @@ -1,221 +1,278 @@ -/* Copyright 2014 Therp BV () - * License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). */ - -odoo.define("attachment_preview", function (require) { +odoo.define("/attachment_preview/static/src/js/attachment_preview.js", function ( + require +) { "use strict"; - - var core = require("web.core"); - var _t = core._t; - var qweb = core.qweb; - var Chatter = require("mail.Chatter"); + const useStore = require("mail/static/src/component_hooks/use_store/use_store.js"); + const {Component} = owl; + const {useRef} = owl.hooks; + const components = { + Attachment: require("mail/static/src/components/attachment/attachment.js"), + Chatter: require("mail/static/src/components/chatter/chatter.js"), + AttachmentBox: require("mail/static/src/components/attachment_box/attachment_box.js"), + }; + const {patch} = require("web.utils"); + var rpc = require("web.rpc"); var basic_fields = require("web.basic_fields"); var FormRenderer = require("web.FormRenderer"); - var FormController = require("web.FormController"); var Widget = require("web.Widget"); - var AttachmentPreviewMixin = { - canPreview: function (extension) { - return ( - $.inArray(extension, [ - "odt", - "odp", - "ods", - "fodt", - "pdf", - "ott", - "fodp", - "otp", - "fods", - "ots", - ]) > -1 - ); - }, + const attachment = patch( + components.Attachment, + "/attachment_preview/static/src/js/attachment_preview.js", + { + previewableAttachments: null, + events: _.extend({}, components.Attachment.prototype.events, { + "click .o_attachment_preview": "_onPreviewAttachment", + }), + canPreview: function (extension) { + return ( + $.inArray(extension, [ + "odt", + "odp", + "ods", + "fodt", + "pdf", + "ott", + "fodp", + "otp", + "fods", + "ots", + ]) > -1 + ); + }, - getUrl: function ( - attachment_id, - attachment_url, - attachment_extension, - attachment_title - ) { - var url = - (window.location.origin || "") + - "/attachment_preview/static/lib/ViewerJS/index.html" + - "?type=" + - encodeURIComponent(attachment_extension) + - "&title=" + - encodeURIComponent(attachment_title) + - "#" + - attachment_url.replace(window.location.origin, ""); - return url; - }, - - showPreview: function ( - attachment_id, - attachment_url, - attachment_extension, - attachment_title, - split_screen - ) { - var url = this.getUrl( + getUrl: function ( attachment_id, attachment_url, attachment_extension, attachment_title - ); - if (split_screen) { - this.trigger_up("onAttachmentPreview", {url: url}); - } else { - window.open(url); - } - }, - }; + ) { + var url = + (window.location.origin || "") + + "/attachment_preview/static/lib/ViewerJS/index.html" + + "?type=" + + encodeURIComponent(attachment_extension) + + "&title=" + + encodeURIComponent(attachment_title) + + "#" + + attachment_url.replace(window.location.origin, ""); + return url; + }, - Chatter.include(AttachmentPreviewMixin); - Chatter.include({ - events: _.extend({}, Chatter.prototype.events, { - "click .o_attachment_preview": "_onPreviewAttachment", - }), - - previewableAttachments: null, - - _openAttachmentBox: function () { - var res = this._super.apply(this, arguments); - - this.getPreviewableAttachments().done( - function (atts) { - this.previewableAttachments = atts; - this.updatePreviewButtons(atts); - this.getParent().attachmentPreviewWidget.setAttachments(atts); - }.bind(this) - ); - - return res; - }, - - update: function () { - var res = this._super.apply(this, arguments); - var self = this; - if (this.getParent().$el.hasClass("attachment_preview")) { - this._fetchAttachments().done(function () { - self._openAttachmentBox(); - self.getPreviewableAttachments().done(function (atts) { - self.updatePreviewButtons(self.previewableAttachments); - self.previewableAttachments = atts; - self.getParent().attachmentPreviewWidget.setAttachments(atts); - }); - }); - } - return res; - }, - - _onPreviewAttachment: function (event) { - event.preventDefault(); - var self = this, - $target = $(event.currentTarget), - split_screen = $target.attr("data-target") !== "new", - attachment_id = parseInt($target.attr("data-id"), 10), - attachment_url = $target.attr("data-url"), - attachment_extension = $target.attr("data-extension"), - attachment_title = $target.attr("data-original-title"); - - if (attachment_extension) { - this.showPreview( + showPreview( + attachment_id, + attachment_url, + attachment_extension, + attachment_title, + split_screen + ) { + var url = this.getUrl( attachment_id, attachment_url, attachment_extension, - attachment_title, - split_screen + attachment_title ); - } else { - this._rpc({ + if (split_screen) { + this.trigger("onAttachmentPreview", {url: url}); + } else { + window.open(url); + } + }, + + _openAttachmentBox() { + this.getPreviewableAttachments().done( + function (atts) { + this.previewableAttachments = atts; + this.updatePreviewButtons(atts); + this.attachmentPreviewWidget.setAttachments(atts); + }.bind(this) + ); + }, + + getPreviewableAttachments: function () { + var self = this; + var deferred = $.Deferred(); + + var $items = $(this); + var attachments = _.object( + $items.map(function () { + return parseInt($(this).attr("data-id"), 10); + }), + $items.map(function () { + return { + url: $(this).attr("data-url"), + extension: $(this).attr("data-extension"), + title: $(this).attr("data-original-title"), + }; + }) + ); + + rpc.query({ model: "ir.attachment", method: "get_attachment_extension", - args: [attachment_id], - }).then(function (extension) { - self.showPreview( + args: [ + _.map(_.keys(attachments), function (id) { + return parseInt(id, 10); + }), + ], + }).then( + function (extensions) { + var reviewableAttachments = _.map( + _.keys( + _.pick(extensions, function (extension, id) { + return self.canPreview(extension); + }) + ), + function (id) { + return { + id: id, + url: attachments[id].url, + extension: extensions[id], + title: attachments[id].title, + previewUrl: self.getUrl( + id, + attachments[id].url, + extensions[id], + id + " - " + attachments[id].title + ), + }; + } + ); + deferred.resolve(reviewableAttachments); + }, + function () { + deferred.reject(); + } + ); + return deferred.promise(); + }, + + _update: function () { + var self = this; + self._openAttachmentBox(); + self.getPreviewableAttachments().done(function (atts) { + self.updatePreviewButtons(self.previewableAttachments); + self.previewableAttachments = atts; + self.attachmentPreviewWidget.setAttachments(atts); + }); + }, + + patched() { + this._update(); + }, + _onPreviewAttachment(event) { + event.preventDefault(); + var self = this, + $target = $(event.currentTarget), + split_screen = $target.attr("data-target") !== "new", + attachment_id = parseInt($target.attr("data-id"), 10), + attachment_url = this.attachmentUrl, + attachment_extension = $(event.currentTarget).attr( + "data-extension" + ), + attachment_title = $target.attr("data-original-title"); + + if (attachment_extension) { + this.showPreview( attachment_id, attachment_url, - extension, - null, + attachment_extension, + attachment_title, split_screen ); - }); - } - }, - - getPreviewableAttachments: function () { - var self = this; - var deferred = $.Deferred(); - - var $items = this.$el.find(".o_attachment_preview"); - var attachments = _.object( - $items.map(function () { - return parseInt($(this).attr("data-id"), 10); - }), - $items.map(function () { - return { - url: $(this).attr("data-url"), - extension: $(this).attr("data-extension"), - title: $(this).attr("data-original-title"), - }; - }) - ); - - this._rpc({ - 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, id) { - return self.canPreview(extension); - }) - ), - function (id) { - return { - id: id, - url: attachments[id].url, - extension: extensions[id], - title: attachments[id].title, - previewUrl: self.getUrl( - id, - attachments[id].url, - extensions[id], - id + " - " + attachments[id].title - ), - }; - } - ); - deferred.resolve(reviewableAttachments); - }, - function () { - deferred.reject(); - } - ); - return deferred.promise(); - }, - - updatePreviewButtons: function (previewableAttachments) { - this.$el.find(".o_attachment_preview").each(function () { - var $this = $(this); - var id = $this.attr("data-id"); - var att = _.findWhere(previewableAttachments, {id: id}); - if (att) { - $this.attr("data-extension", att.extension); } else { - $this.remove(); + 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 + ); + }); } - }); - }, - }); + }, - basic_fields.FieldBinaryFile.include(AttachmentPreviewMixin); + getPreviewableAttachments: function () { + var self = this; + var deferred = $.Deferred(); + + var $items = $(this).find(".o_attachment_preview"); + var attachments = _.object( + $items.map(function () { + return parseInt($(this).attr("data-id"), 10); + }), + $items.map(function () { + return { + url: $(this).attr("data-url"), + extension: $(this).attr("data-extension"), + title: $(this).attr("data-original-title"), + }; + }) + ); + + 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, id) { + return self.canPreview(extension); + }) + ), + function (id) { + return { + id: id, + url: attachments[id].url, + extension: extensions[id], + title: attachments[id].title, + previewUrl: self.getUrl( + id, + attachments[id].url, + extensions[id], + 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 id = $this.attr("data-id"); + var att = _.findWhere(previewableAttachments, {id: id}); + if (att) { + $this.attr("data-extension", att.extension); + } else { + $this.remove(); + } + }); + }, + } + ); + + basic_fields.FieldBinaryFile.include(attachment); basic_fields.FieldBinaryFile.include({ events: _.extend({}, basic_fields.FieldBinaryFile.prototype.events, { "click .fa-search": "_onPreview", @@ -270,7 +327,6 @@ odoo.define("attachment_preview", function (require) { event.stopPropagation(); }, }); - var AttachmentPreviewWidget = Widget.extend({ template: "attachment_preview.AttachmentPreviewWidget", activeIndex: 0, @@ -396,10 +452,12 @@ odoo.define("attachment_preview", function (require) { }, showAttachmentPreviewWidget: function () { + // Debugger; this.$el.addClass("attachment_preview"); this.attachmentPreviewWidget.setAttachments( - this.chatter.previewableAttachments + this.attachments + // This._chatterContainerComponent.Component.components.Chatter.props.chatterLocalId.previewableAttachments ); this.attachmentPreviewWidget.show(); }, @@ -415,7 +473,7 @@ odoo.define("attachment_preview", function (require) { }); return { - AttachmentPreviewMixin: AttachmentPreviewMixin, + attachment, AttachmentPreviewWidget: AttachmentPreviewWidget, }; }); diff --git a/attachment_preview/static/src/scss/attachment_preview.scss b/attachment_preview/static/src/scss/attachment_preview.scss index effd3f86..a6d14e42 100644 --- a/attachment_preview/static/src/scss/attachment_preview.scss +++ b/attachment_preview/static/src/scss/attachment_preview.scss @@ -15,6 +15,12 @@ } } +.o_Attachment_actions { + justify-content: space-between; + display: flex; + flex-direction: column; +} + .o_form_uri .fa-search { padding-left: 5px; padding-right: 5px; diff --git a/attachment_preview/static/src/xml/attachment_preview.xml b/attachment_preview/static/src/xml/attachment_preview.xml index a505f885..82404336 100644 --- a/attachment_preview/static/src/xml/attachment_preview.xml +++ b/attachment_preview/static/src/xml/attachment_preview.xml @@ -1,24 +1,31 @@ - - - - + + +
- - + +
+
- + t-attf-title="Open preview {{attachment.name}} in a new tab" + > + +
+
@@ -30,7 +37,7 @@ > + >1 / 2