[MIG] attachment_preview to version 10.0

This commit is contained in:
tarteo
2019-10-01 09:23:56 +02:00
parent 295a6dc320
commit a83f972c2a
32 changed files with 2377 additions and 1356 deletions

View File

@@ -1,26 +0,0 @@
.openerp .oe_sidebar .oe_dropdown_menu li .oe-sidebar-attachment-preview
{
position: absolute;
right: 18px;
top: 0px;
display: none;
}
.openerp .oe_sidebar .oe_dropdown_menu li:hover .oe-sidebar-attachment-preview
{
display: inherit;
}
.openerp .oe_sidebar .oe_dropdown_menu li .oe-sidebar-attachment-preview:hover
{
text-decoration: none;
color: white;
background: #8786b7;
text-shadow: 0 1px 1px rgba(0, 0, 0, 0.4);
-moz-border-radius: 2px;
-webkit-border-radius: 2px;
border-radius: 2px;
}
.openerp .oe-binary-preview
{
cursor: pointer;
vertical-align: middle;
}

View File

@@ -1,242 +1,337 @@
//-*- coding: utf-8 -*-
//############################################################################
//
// OpenERP, Open Source Management Solution
// This module copyright (C) 2014 Therp BV (<http://therp.nl>).
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
//############################################################################
///* Copyright 2014 Therp BV (<http://therp.nl>)
// * License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). */
openerp.attachment_preview = function(instance)
{
var _t = instance.web._t;
openerp.attachment_preview.show_preview = function(
attachment_id, attachment_url, attachment_extension,
attachment_title)
{
var url = (window.location.origin || '') +
'/attachment_preview/static/lib/ViewerJS/index.html#' +
attachment_url.replace(window.location.origin, '') +
'&title=' + encodeURIComponent(attachment_title) +
'&ext=.' + encodeURIComponent(attachment_extension);
window.open(url);
};
openerp.attachment_preview.can_preview = function(extension)
{
return jQuery.inArray(
extension,
[
'odt', 'odp', 'ods', 'fodt', 'pdf', 'ott', 'fodp', 'otp',
'fods', 'ots'
]) > -1;
};
instance.web.Sidebar.include(
{
on_attachments_loaded: function(attachments)
{
var result = this._super.apply(this, arguments);
this.$el.find('.oe-sidebar-attachment-preview')
.click(this.on_attachment_preview);
this.update_preview_buttons();
return result;
odoo.define('attachment_preview', function(require) {
'use strict';
var core = require('web.core');
var _t = core._t;
var qweb = core.qweb;
var Sidebar = require('web.Sidebar');
var FieldBinaryFile = core.form_widget_registry.get('binary');
var FormView = require('web.FormView');
var Widget = require('web.Widget');
var Model = require('web.Model');
var AttachmentPreviewMixin = {
canPreview: function(extension) {
return $.inArray(
extension,
[
'odt', 'odp', 'ods', 'fodt', 'pdf', 'ott', 'fodp', 'otp',
'fods', 'ots'
]) > -1;
},
on_attachment_preview: function(e)
{
e.preventDefault();
e.stopPropagation();
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_url, attachment_extension, attachment_title);
if (split_screen) {
this.trigger_up('onAttachmentPreview', {url: url});
} else {
window.open(url);
}
},
};
Sidebar.include(AttachmentPreviewMixin);
Sidebar.include({
events: _.extend({}, Sidebar.prototype.events, {
'click .o_sidebar_preview_attachment': '_onPreviewAttachment'
}),
previewableAttachments: null,
redraw: function () {
this._super.apply(this, arguments);
this.getPreviewableAttachments().done(function(atts) {
this.previewableAttachments = atts;
this.updatePreviewButtons(atts);
this.trigger_up('setPreviewableAttachments', {attachments: atts});
}.bind(this));
},
_onPreviewAttachment: function(event) {
event.preventDefault();
var self = this,
$target = jQuery(e.currentTarget),
attachment_id = parseInt($target.attr('data-id')),
$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)
{
openerp.attachment_preview.show_preview(
attachment_id, attachment_url, attachment_extension,
attachment_title);
}
else
{
(new instance.web.Model('ir.attachment')).call(
'get_attachment_extension', [attachment_id], {})
.then(function(extension)
{
openerp.attachment_preview.show_preview(
attachment_id, attachment_url, extension);
if(attachment_extension) {
this.showPreview(attachment_id, attachment_url, attachment_extension, attachment_title, split_screen);
} else {
new Model('ir.attachment').call('get_attachment_extension', [
attachment_id
], {}).then(function(extension) {
self.showPreview(attachment_id, attachment_url, extension, null, split_screen);
});
}
},
update_preview_buttons: function()
{
getPreviewableAttachments: function() {
var self = this;
return (new instance.web.Model('ir.attachment')).call(
'get_attachment_extension',
[
this.$el.find('.oe-sidebar-attachment-preview')
.map(function()
{
return parseInt(jQuery(this).attr('data-id'));
})
.get()
],
{})
.then(function(extensions)
{
_(extensions).each(function(extension, id)
{
var $element = jQuery(
'a.oe-sidebar-attachment-preview[data-id="'
+ id + '"]');
if(openerp.attachment_preview.can_preview(extension))
{
$element.attr('data-extension', extension);
}
else
{
$element.remove();
}
});
var deferred = $.Deferred();
var $items = this.$el.find('.o_sidebar_preview_attachment');
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'),
};
}));
new Model('ir.attachment').call('get_attachment_extension', [
_.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']
)
}
});
},
});
instance.web.ListView.include(
{
reload_content: function()
{
var deferred = this._super.apply(this, arguments),
self = this;
deferred.then(function()
{
var $elements = self.$el.find('.oe-binary-preview');
if(!$elements.length)
{
return;
}
$elements.click(function(e)
{
e.stopPropagation();
var $target = jQuery(e.currentTarget),
attachment_id = parseInt($target.attr('data-id')),
attachment_extension = $target.attr('data-extension');
openerp.attachment_preview.show_preview(
attachment_id,
$target.siblings('a').attr('href'),
attachment_extension,
$target.attr('alt'));
});
return (new instance.web.Model('ir.attachment')).call(
'get_binary_extension',
[
$elements.attr('data-model'),
$elements
.map(function()
{
return parseInt(jQuery(this).attr('data-id'));
})
.get(),
$elements.attr('data-field'),
$elements.attr('data-filename'),
],
{})
.then(function(extensions)
{
_(extensions).each(function(extension, id)
{
var $element = $elements.filter(
'[data-id="' + id + '"]');
if(openerp.attachment_preview.can_preview(extension))
{
$element.attr('data-extension', extension);
}
else
{
$element.remove();
}
});
});
deferred.resolve(reviewableAttachments);
}, function() {
deferred.reject();
});
return deferred.promise();
},
updatePreviewButtons: function(previewableAttachments) {
this.$el.find('.o_sidebar_preview_attachment').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();
}
});
return deferred;
}
});
instance.web.list.Binary.include(
{
_format: function (row_data, options)
{
var link = this._super.apply(this, arguments);
link += _.template(
'<img class="oe-binary-preview" title="<%-preview_text%>" alt="<%-preview_text%>" data-id="<%-preview_id%>" data-model="<%-preview_model%>" data-field="<%-preview_field%>" data-filename="<%-preview_filename%>" src="/web/static/src/img/icons/gtk-print-preview.png" />',
{
preview_id: options.id,
preview_text: _.str.sprintf(_t('Preview %s'), this.string),
preview_model: options.model,
preview_field: this.id,
preview_filename: this.filename || '',
});
return link;
}
});
instance.web.form.FieldBinaryFile.include(
{
render_value: function()
{
this._super.apply(this, arguments);
if(this.get("effective_readonly") && this.get('value'))
{
var self = this;
(new instance.web.Model('ir.attachment')).call(
'get_binary_extension', [
this.view.dataset.model,
this.view.datarecord.id ? [this.view.datarecord.id] : [],
this.name,
this.node.attrs.filename,
],
{})
.then(function(extensions)
{
_(extensions).each(function(extension)
{
var $element = self.$el.find('.oe-binary-preview');
if(openerp.attachment_preview.can_preview(extension))
{
$element.click(function()
{
openerp.attachment_preview.show_preview(
null,
_.str.sprintf(
'/web/binary/saveas?session_id=%s&model=%s&field=%s&id=%d',
instance.session.session_id,
self.view.dataset.model,
self.name,
self.view.datarecord.id),
extension,
_.str.sprintf(_t('Preview %s'), self.field.string));
});
$element.attr('title', _.str.sprintf(_t('Preview %s'), self.field.string));
}
else
{
$element.remove();
}
});
});
}
else
{
this.$el.find('.oe-binary-preview').remove();
};
},
});
}
FieldBinaryFile.include(AttachmentPreviewMixin);
FieldBinaryFile.include({
events: _.extend({}, FieldBinaryFile.prototype.events, {
'click .fa-search': '_onPreview'
}),
_renderReadonly: function () {
var self = this;
this._super.apply(this, arguments);
this._getBinaryExtension().done(function(extension) {
if(self.canPreview(extension)) {
self._renderPreviewButton(extension);
}
});
},
_renderPreviewButton: function(extension) {
this.$previewBtn = $("<span/>");
this.$previewBtn.addClass('fa fa-search');
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);
},
_getBinaryExtension: function () {
return new Model('ir.attachment').call('get_binary_extension', [
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
);
}
});
var AttachmentPreviewWidget = Widget.extend({
template: 'attachment_preview.AttachmentPreviewWidget',
activeIndex: 0,
attachments: null,
events: {
'click .attachment_preview_close': '_onCloseClick',
'click .attachment_preview_previous': '_onPreviousClick',
'click .attachment_preview_next': '_onNextClick',
'click .attachment_preview_popout': '_onPopoutClick',
},
start: function () {
var res = this._super.apply(this, arguments);
this.$overlay = this.$el.find('.attachment_preview_overlay');
this.$iframe = this.$el.find('.attachment_preview_iframe');
this.$current = this.$el.find('.attachment_preview_current');
return res;
},
_onCloseClick: function () {
this.hide();
},
_onPreviousClick: function () {
this.previous();
},
_onNextClick: function () {
this.next();
},
_onPopoutClick: function () {
if (!this.attachments[this.activeIndex]) {
return;
}
window.open(this.attachments[this.activeIndex].previewUrl);
},
next: function() {
var index = this.activeIndex + 1;
if (index >= this.attachments.length) {
index = 0;
}
this.activeIndex = index;
this.updatePaginator();
this.loadPreview();
},
previous: function() {
var index = this.activeIndex - 1;
if (index < 0) {
index = this.attachments.length - 1;
}
this.activeIndex = index;
this.updatePaginator();
this.loadPreview();
},
show: function () {
this.$el.removeClass('hidden');
this.trigger('shown');
},
hide: function () {
this.$el.addClass('hidden');
this.trigger('hidden');
},
updatePaginator: function() {
var value = _.str.sprintf('%s / %s', this.activeIndex + 1, this.attachments.length);
this.$current.html(value);
},
loadPreview: function() {
if (this.attachments.length == 0) {
this.$iframe.attr('src', 'about:blank');
return;
}
var att = this.attachments[this.activeIndex];
this.$iframe.attr('src', att.previewUrl);
},
setAttachments: function(attachments) {
this.attachments = attachments;
this.activeIndex = 0;
this.updatePaginator();
this.loadPreview();
}
});
FormView.include({
custom_events: _.extend({}, FormView.prototype.custom_events, {
onAttachmentPreview: '_onAttachmentPreview',
setPreviewableAttachments: '_setPreviewableAttachments'
}),
attachmentPreviewWidget: null,
init: function() {
var res = this._super.apply(this, arguments);
this.attachmentPreviewWidget = new AttachmentPreviewWidget(this);
this.attachmentPreviewWidget.on('hidden', this, this._attachmentPreviewWidgetHidden);
return res;
},
start: function () {
this._super.apply(this, arguments);
this.attachmentPreviewWidget.insertAfter(this.$el);
},
_attachmentPreviewWidgetHidden: function() {
this.$el.removeClass('attachment_preview');
},
showAttachmentPreviewWidget: function () {
this.$el.addClass('attachment_preview');
this.attachmentPreviewWidget.setAttachments(
this.sidebar.previewableAttachments
);
this.attachmentPreviewWidget.show();
},
on_detach_callback: function () {
this.attachmentPreviewWidget.hide();
return this._super.apply(this, arguments);
},
_onAttachmentPreview: function (event) {
this.showAttachmentPreviewWidget();
},
_setPreviewableAttachments: function (event) {
this.attachmentPreviewWidget.setAttachments(
event.data.attachments
);
}
});
return {
AttachmentPreviewMixin: AttachmentPreviewMixin,
AttachmentPreviewWidget: AttachmentPreviewWidget
};
});

View File

@@ -1,33 +1,14 @@
//-*- coding: utf-8 -*-
//############################################################################
//
// OpenERP, Open Source Management Solution
// This module copyright (C) 2014 Therp BV (<http://therp.nl>).
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as
// published by the Free Software Foundation, either version 3 of the
// License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
//############################################################################
/* Copyright 2014 Therp BV (<http://therp.nl>)
* License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). */
// This file contains tweaks for viewerjs itself and is not meant to be run in
// OpenERP's context
var original_Viewer = Viewer;
Viewer = function(plugin, parameters)
{
var matches = (/&title=([^&]+)&/).exec(window.location.hash);
if(matches && matches.length > 1)
{
parameters.title = decodeURIComponent(matches[1]);
}
return original_Viewer(plugin, parameters);
}
(function(original_Viewer) {
window.Viewer = function(plugin, parameters) {
if(!plugin) {
// eslint-disable-next-line no-alert
alert('Unsupported file type');
}
return original_Viewer(plugin, parameters);
};
}(window.Viewer));

View File

@@ -0,0 +1,55 @@
.o_cp_sidebar {
li .o_sidebar_preview_attachment {
position: absolute;
top: 5px;
right: 26px;
&.o_sidebar_preview_attachment_new_tab {
right: 43px;
}
}
.open .dropdown-menu > li > a {
padding-right: 60px;
}
}
.o_form_uri .fa-search {
padding-left: 5px;
padding-right: 5px;
}
/* Preview widget */
.o_form_view.attachment_preview {
width: 60%;
float: left;
}
.o_form_view + .attachment_preview_widget {
width: 40%;
height: 100%;
float: right;
position: relative;
> .attachment_preview_iframe {
width: 100%;
height: calc(~"100% - 30px");
float: left;
}
.attachment_preview_buttons {
padding: 4px;
float: left;
width: 100%;
background-color: #FFF;
border-left: 1px solid @gray-light;
button {
min-width: 25px;
min-height: 25px;
padding-top: 2px;
padding-bottom: 2px;
}
}
}

View File

@@ -1,15 +1,35 @@
<?xml version="1.0" encoding="UTF-8"?>
<templates>
<t t-extend="Sidebar">
<t t-jquery="a.oe_sidebar_delete_item" t-operation="before">
<a t-if="section.name == 'files' and !item.callback and item.url" href="#" class="oe-sidebar-attachment-preview" t-att-data-id="item.id" t-att-data-url="item.url" t-attf-title="Preview #{item.name}">
<img t-att-src='_s + "/web/static/src/img/icons/gtk-print-preview.png"' width="12" height="12" border="0"/>
</a>
<t t-jquery="span.o_sidebar_delete_attachment" t-operation="before">
<span t-if="section.name == 'files' and widget.options.editable and !item.callback and item.url"
class="fa fa-search o_sidebar_preview_attachment"
t-att-data-id="item.id"
t-att-data-url="item.url"
t-attf-title="Preview #{item.name}"/>
<span t-if="section.name == 'files' and widget.options.editable and !item.callback and item.url"
class="fa fa-external-link o_sidebar_preview_attachment o_sidebar_preview_attachment_new_tab"
data-target="new"
t-att-data-id="item.id"
t-att-data-url="item.url"
t-attf-title="Open preview #{item.name} in a new tab"/>
</t>
</t>
<t t-extend="FieldBinaryFile">
<t t-jquery="a.oe_form_uri" t-operation="after">
<img class="oe-binary-preview" t-att-src='_s + "/web/static/src/img/icons/gtk-print-preview.png"'/>
</t>
<t t-name="attachment_preview.AttachmentPreviewWidget">
<div class="attachment_preview_widget hidden">
<div class="attachment_preview_buttons">
<div class="button-group pull-left">
<button class="btn btn-sm btn-default attachment_preview_previous"><i class="fa fa-chevron-left"/></button>
<button class="btn btn-sm btn-default disabled attachment_preview_current">1 / 5</button>
<button class="btn btn-sm btn-default attachment_preview_next"><i class="fa fa-chevron-right"/></button>
</div>
<button class="btn btn-sm btn-default pull-left ml8 attachment_preview_popout"><i class="fa fa-external-link"/></button>
<button class="btn btn-sm btn-default pull-right attachment_preview_close"><i class="fa fa-times"/></button>
</div>
<iframe class="attachment_preview_iframe"></iframe>
</div>
</t>
</templates>