[WIP] Migrating module to v14

This commit is contained in:
Foram Shah 2020-11-05 18:00:23 +05:30
parent 37abe5a7a5
commit c6c354e58e
5 changed files with 287 additions and 216 deletions

View File

@ -16,6 +16,8 @@ class IrAttachment(models.Model):
@api.model @api.model
def get_binary_extension(self, model, ids, binary_field, filename_field=None): def get_binary_extension(self, model, ids, binary_field, filename_field=None):
# import pdb
# pdb.set_trace()
result = {} result = {}
ids_to_browse = ids if isinstance(ids, collections.Iterable) else [ids] ids_to_browse = ids if isinstance(ids, collections.Iterable) else [ids]
@ -73,4 +75,4 @@ class IrAttachment(models.Model):
@api.model @api.model
def get_attachment_extension(self, ids): 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")

View File

@ -74,8 +74,6 @@ c;a?(e.title||(e.title=a.replace(/^.*[\\\/]/,"")),e.documentUrl=a,b(a,function(b
//]]> //]]>
</script> </script>
<!-- load some small tweaks for the Odoo integration /-->
<script src="../../src/js/viewerjs_tweaks.js" type="text/javascript" charset="utf-8"></script>
</head> </head>
<body> <body>

View File

@ -1,221 +1,278 @@
/* Copyright 2014 Therp BV (<http://therp.nl>) odoo.define("/attachment_preview/static/src/js/attachment_preview.js", function (
* License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). */ require
) {
odoo.define("attachment_preview", function (require) {
"use strict"; "use strict";
const useStore = require("mail/static/src/component_hooks/use_store/use_store.js");
var core = require("web.core"); const {Component} = owl;
var _t = core._t; const {useRef} = owl.hooks;
var qweb = core.qweb; const components = {
var Chatter = require("mail.Chatter"); 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 basic_fields = require("web.basic_fields");
var FormRenderer = require("web.FormRenderer"); var FormRenderer = require("web.FormRenderer");
var FormController = require("web.FormController");
var Widget = require("web.Widget"); var Widget = require("web.Widget");
var AttachmentPreviewMixin = { const attachment = patch(
canPreview: function (extension) { components.Attachment,
return ( "/attachment_preview/static/src/js/attachment_preview.js",
$.inArray(extension, [ {
"odt", previewableAttachments: null,
"odp", events: _.extend({}, components.Attachment.prototype.events, {
"ods", "click .o_attachment_preview": "_onPreviewAttachment",
"fodt", }),
"pdf", canPreview: function (extension) {
"ott", return (
"fodp", $.inArray(extension, [
"otp", "odt",
"fods", "odp",
"ots", "ods",
]) > -1 "fodt",
); "pdf",
}, "ott",
"fodp",
"otp",
"fods",
"ots",
]) > -1
);
},
getUrl: function ( 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(
attachment_id, attachment_id,
attachment_url, attachment_url,
attachment_extension, attachment_extension,
attachment_title attachment_title
); ) {
if (split_screen) { var url =
this.trigger_up("onAttachmentPreview", {url: url}); (window.location.origin || "") +
} else { "/attachment_preview/static/lib/ViewerJS/index.html" +
window.open(url); "?type=" +
} encodeURIComponent(attachment_extension) +
}, "&title=" +
}; encodeURIComponent(attachment_title) +
"#" +
attachment_url.replace(window.location.origin, "");
return url;
},
Chatter.include(AttachmentPreviewMixin); showPreview(
Chatter.include({ attachment_id,
events: _.extend({}, Chatter.prototype.events, { attachment_url,
"click .o_attachment_preview": "_onPreviewAttachment", attachment_extension,
}), attachment_title,
split_screen
previewableAttachments: null, ) {
var url = this.getUrl(
_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(
attachment_id, attachment_id,
attachment_url, attachment_url,
attachment_extension, attachment_extension,
attachment_title, attachment_title
split_screen
); );
} else { if (split_screen) {
this._rpc({ 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", model: "ir.attachment",
method: "get_attachment_extension", method: "get_attachment_extension",
args: [attachment_id], args: [
}).then(function (extension) { _.map(_.keys(attachments), function (id) {
self.showPreview( 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_id,
attachment_url, attachment_url,
extension, attachment_extension,
null, attachment_title,
split_screen 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 { } 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({ basic_fields.FieldBinaryFile.include({
events: _.extend({}, basic_fields.FieldBinaryFile.prototype.events, { events: _.extend({}, basic_fields.FieldBinaryFile.prototype.events, {
"click .fa-search": "_onPreview", "click .fa-search": "_onPreview",
@ -270,7 +327,6 @@ odoo.define("attachment_preview", function (require) {
event.stopPropagation(); event.stopPropagation();
}, },
}); });
var AttachmentPreviewWidget = Widget.extend({ var AttachmentPreviewWidget = Widget.extend({
template: "attachment_preview.AttachmentPreviewWidget", template: "attachment_preview.AttachmentPreviewWidget",
activeIndex: 0, activeIndex: 0,
@ -396,10 +452,12 @@ odoo.define("attachment_preview", function (require) {
}, },
showAttachmentPreviewWidget: function () { showAttachmentPreviewWidget: function () {
// Debugger;
this.$el.addClass("attachment_preview"); this.$el.addClass("attachment_preview");
this.attachmentPreviewWidget.setAttachments( this.attachmentPreviewWidget.setAttachments(
this.chatter.previewableAttachments this.attachments
// This._chatterContainerComponent.Component.components.Chatter.props.chatterLocalId.previewableAttachments
); );
this.attachmentPreviewWidget.show(); this.attachmentPreviewWidget.show();
}, },
@ -415,7 +473,7 @@ odoo.define("attachment_preview", function (require) {
}); });
return { return {
AttachmentPreviewMixin: AttachmentPreviewMixin, attachment,
AttachmentPreviewWidget: AttachmentPreviewWidget, AttachmentPreviewWidget: AttachmentPreviewWidget,
}; };
}); });

View File

@ -15,6 +15,12 @@
} }
} }
.o_Attachment_actions {
justify-content: space-between;
display: flex;
flex-direction: column;
}
.o_form_uri .fa-search { .o_form_uri .fa-search {
padding-left: 5px; padding-left: 5px;
padding-right: 5px; padding-right: 5px;

View File

@ -1,24 +1,31 @@
<?xml version="1.0" encoding="UTF-8" ?> <?xml version="1.0" encoding="UTF-8" ?>
<templates> <templates xml:space="preserve">
<t t-extend="mail.Attachment"> <t
<t t-jquery=".o_attachment_download" t-operation="after"> t-name="Attachment"
<span t-inherit="mail.Attachment"
t-if="!attachment.callback and attachment.url" t-inherit-mode="extension"
class="fa fa-search ml4 o_attachment_preview" owl="1"
>
<xpath expr="//div[hasclass('o_Attachment_actionDownload')]" position="before">
<div
class="ml4 o_attachment_preview"
t-on-click="_onPreviewAttachment"
t-att-data-id="attachment.id" t-att-data-id="attachment.id"
t-att-data-url="attachment.url" t-att-data-url="attachment.url"
t-attf-title="Preview #{attachment.name} in side panel" t-attf-title="Preview {{attachment.name}} in side panel"
/> >
<i class="fa fa-search" />
<span </div>
t-if="!attachment.callback and attachment.url" <div
class="fa fa-external-link ml4 o_attachment_preview o_attachment_preview_new_tab" class="ml4 o_attachment_preview o_attachment_preview_new_tab"
data-target="new" data-target="new"
t-att-data-id="attachment.id" t-att-data-id="attachment.id"
t-att-data-url="attachment.url" t-att-data-url="attachment.url"
t-attf-title="Open preview #{attachment.name} in a new tab" t-attf-title="Open preview {{attachment.name}} in a new tab"
/> >
</t> <i class="fa fa-external-link" />
</div>
</xpath>
</t> </t>
<t t-name="attachment_preview.AttachmentPreviewWidget"> <t t-name="attachment_preview.AttachmentPreviewWidget">
@ -30,7 +37,7 @@
><i class="fa fa-chevron-left" /></button> ><i class="fa fa-chevron-left" /></button>
<button <button
class="btn btn-sm btn-secondary disabled attachment_preview_current" class="btn btn-sm btn-secondary disabled attachment_preview_current"
>1 / 5</button> >1 / 2</button>
<button class="btn btn-sm btn-secondary attachment_preview_next"><i <button class="btn btn-sm btn-secondary attachment_preview_next"><i
class="fa fa-chevron-right" class="fa fa-chevron-right"
/></button> /></button>