[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
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")

View File

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

View File

@ -1,221 +1,278 @@
/* Copyright 2014 Therp BV (<http://therp.nl>)
* 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,
};
});

View File

@ -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;

View File

@ -1,24 +1,31 @@
<?xml version="1.0" encoding="UTF-8" ?>
<templates>
<t t-extend="mail.Attachment">
<t t-jquery=".o_attachment_download" t-operation="after">
<span
t-if="!attachment.callback and attachment.url"
class="fa fa-search ml4 o_attachment_preview"
<templates xml:space="preserve">
<t
t-name="Attachment"
t-inherit="mail.Attachment"
t-inherit-mode="extension"
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-url="attachment.url"
t-attf-title="Preview #{attachment.name} in side panel"
/>
<span
t-if="!attachment.callback and attachment.url"
class="fa fa-external-link ml4 o_attachment_preview o_attachment_preview_new_tab"
t-attf-title="Preview {{attachment.name}} in side panel"
>
<i class="fa fa-search" />
</div>
<div
class="ml4 o_attachment_preview o_attachment_preview_new_tab"
data-target="new"
t-att-data-id="attachment.id"
t-att-data-url="attachment.url"
t-attf-title="Open preview #{attachment.name} in a new tab"
/>
</t>
t-attf-title="Open preview {{attachment.name}} in a new tab"
>
<i class="fa fa-external-link" />
</div>
</xpath>
</t>
<t t-name="attachment_preview.AttachmentPreviewWidget">
@ -30,7 +37,7 @@
><i class="fa fa-chevron-left" /></button>
<button
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
class="fa fa-chevron-right"
/></button>