diff --git a/attachment_preview/static/src/js/attachment_preview.js b/attachment_preview/static/src/js/attachment_preview.js index 3f1f6604..b2c6b339 100644 --- a/attachment_preview/static/src/js/attachment_preview.js +++ b/attachment_preview/static/src/js/attachment_preview.js @@ -1,7 +1,7 @@ -///* Copyright 2014 Therp BV () -// * License AGPL-3.0 or later (https://www.gnu.org/licenses/agpl). */ +/* 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', function (require) { 'use strict'; var core = require('web.core'); @@ -14,16 +14,15 @@ odoo.define('attachment_preview', function(require) { var Widget = require('web.Widget'); var AttachmentPreviewMixin = { - canPreview: function(extension) { + canPreview: function (extension) { return $.inArray( extension, - [ - 'odt', 'odp', 'ods', 'fodt', 'pdf', 'ott', 'fodp', 'otp', - 'fods', 'ots' + ['odt', 'odp', 'ods', 'fodt', 'pdf', 'ott', 'fodp', 'otp', + 'fods', 'ots', ]) > -1; }, - getUrl: function(attachment_id, attachment_url, attachment_extension, attachment_title) { + 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) + @@ -33,7 +32,7 @@ odoo.define('attachment_preview', function(require) { return url; }, - showPreview: function(attachment_id, attachment_url, attachment_extension, attachment_title, split_screen) { + 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}); @@ -46,51 +45,51 @@ odoo.define('attachment_preview', function(require) { Sidebar.include(AttachmentPreviewMixin); Sidebar.include({ events: _.extend({}, Sidebar.prototype.events, { - 'click .o_sidebar_preview_attachment': '_onPreviewAttachment' + 'click .o_sidebar_preview_attachment': '_onPreviewAttachment', }), previewableAttachments: null, _redraw: function () { this._super.apply(this, arguments); - this.getPreviewableAttachments().done(function(atts) { + this.getPreviewableAttachments().done(function (atts) { this.previewableAttachments = atts; this.updatePreviewButtons(atts); this.trigger_up('setPreviewableAttachments', {attachments: atts}); }.bind(this)); }, - _onPreviewAttachment: function(event) { + _onPreviewAttachment: function (event) { event.preventDefault(); var self = this, $target = $(event.currentTarget), - split_screen = $target.attr('data-target') != 'new', + 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) { + if (attachment_extension) { this.showPreview(attachment_id, attachment_url, attachment_extension, attachment_title, split_screen); } else { this._rpc({ model: 'ir.attachment', method: 'get_attachment_extension', - args: [attachment_id] - }).then(function(extension) { + args: [attachment_id], + }).then(function (extension) { self.showPreview(attachment_id, attachment_url, extension, null, split_screen); }); } }, - getPreviewableAttachments: function() { + getPreviewableAttachments: function () { var self = this; var deferred = $.Deferred(); var $items = this.$el.find('.o_sidebar_preview_attachment'); - var attachments = _.object($items.map(function() { + var attachments = _.object($items.map(function () { return parseInt($(this).attr('data-id'), 10); - }), $items.map(function() { + }), $items.map(function () { return { url: $(this).attr('data-url'), extension: $(this).attr('data-extension'), @@ -102,14 +101,14 @@ odoo.define('attachment_preview', function(require) { model: 'ir.attachment', method: 'get_attachment_extension', args: [ - _.map(_.keys(attachments), function(id) { + _.map(_.keys(attachments), function (id) { return parseInt(id, 10); - }) - ] - }).then(function(extensions) { - var reviewableAttachments = _.map(_.keys(_.pick(extensions, function(extension, id) { + }), + ], + }).then(function (extensions) { + var reviewableAttachments = _.map(_.keys(_.pick(extensions, function (extension, id) { return self.canPreview(extension); - })), function(id) { + })), function (id) { return { id: id, url: attachments[id]['url'], @@ -120,18 +119,18 @@ odoo.define('attachment_preview', function(require) { attachments[id]['url'], extensions[id], id + ' - ' + attachments[id]['title'] - ) - } + ), + }; }); deferred.resolve(reviewableAttachments); - }, function() { + }, function () { deferred.reject(); }); return deferred.promise(); }, - updatePreviewButtons: function(previewableAttachments) { - this.$el.find('.o_sidebar_preview_attachment').each(function() { + 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}); @@ -147,20 +146,20 @@ odoo.define('attachment_preview', function(require) { basic_fields.FieldBinaryFile.include(AttachmentPreviewMixin); basic_fields.FieldBinaryFile.include({ events: _.extend({}, basic_fields.FieldBinaryFile.prototype.events, { - 'click .fa-search': '_onPreview' + 'click .fa-search': '_onPreview', }), _renderReadonly: function () { var self = this; this._super.apply(this, arguments); - this._getBinaryExtension().done(function(extension) { - if(self.canPreview(extension)) { + this._getBinaryExtension().done(function (extension) { + if (self.canPreview(extension)) { self._renderPreviewButton(extension); } }); }, - _renderPreviewButton: function(extension) { + _renderPreviewButton: function (extension) { this.$previewBtn = $(""); this.$previewBtn.addClass('fa fa-search'); this.$previewBtn.attr('title', _.str.sprintf(_t('Preview %s'), this.field.string)); @@ -176,12 +175,12 @@ odoo.define('attachment_preview', function(require) { this.model, this.recordData.id, this.name, - this.attrs.filename - ] + this.attrs.filename, + ], }); }, - _onPreview: function(event) { + _onPreview: function (event) { this.showPreview( null, _.str.sprintf( @@ -194,7 +193,7 @@ odoo.define('attachment_preview', function(require) { _.str.sprintf(_t('Preview %s'), this.field.string), false ); - } + }, }); var AttachmentPreviewWidget = Widget.extend({ @@ -237,7 +236,7 @@ odoo.define('attachment_preview', function(require) { window.open(this.attachments[this.activeIndex].previewUrl); }, - next: function() { + next: function () { var index = this.activeIndex + 1; if (index >= this.attachments.length) { index = 0; @@ -247,7 +246,7 @@ odoo.define('attachment_preview', function(require) { this.loadPreview(); }, - previous: function() { + previous: function () { var index = this.activeIndex - 1; if (index < 0) { index = this.attachments.length - 1; @@ -267,13 +266,13 @@ odoo.define('attachment_preview', function(require) { this.trigger('hidden'); }, - updatePaginator: function() { + 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) { + loadPreview: function () { + if (this.attachments.length === 0) { this.$iframe.attr('src', 'about:blank'); return; } @@ -282,18 +281,18 @@ odoo.define('attachment_preview', function(require) { this.$iframe.attr('src', att.previewUrl); }, - setAttachments: function(attachments) { + setAttachments: function (attachments) { this.attachments = attachments; this.activeIndex = 0; this.updatePaginator(); this.loadPreview(); - } + }, }); FormRenderer.include({ attachmentPreviewWidget: null, - init: function() { + init: function () { var res = this._super.apply(this, arguments); this.attachmentPreviewWidget = new AttachmentPreviewWidget(this); this.attachmentPreviewWidget.on('hidden', this, this._attachmentPreviewWidgetHidden); @@ -305,7 +304,7 @@ odoo.define('attachment_preview', function(require) { this.attachmentPreviewWidget.insertAfter(this.$el); }, - _attachmentPreviewWidgetHidden: function() { + _attachmentPreviewWidgetHidden: function () { this.$el.removeClass('attachment_preview'); }, @@ -320,29 +319,29 @@ odoo.define('attachment_preview', function(require) { on_detach_callback: function () { this.attachmentPreviewWidget.hide(); return this._super.apply(this, arguments); - } + }, }); FormController.include({ custom_events: _.extend({}, FormController.prototype.custom_events, { onAttachmentPreview: '_onAttachmentPreview', - setPreviewableAttachments: '_setPreviewableAttachments' + setPreviewableAttachments: '_setPreviewableAttachments', }), _onAttachmentPreview: function (event) { this.renderer.showAttachmentPreviewWidget(); }, - _setPreviewableAttachments: function(event) { + _setPreviewableAttachments: function (event) { this.renderer.attachmentPreviewWidget.setAttachments( event.data.attachments ); - } + }, }); return { AttachmentPreviewMixin: AttachmentPreviewMixin, - AttachmentPreviewWidget: AttachmentPreviewWidget + AttachmentPreviewWidget: AttachmentPreviewWidget, }; }); diff --git a/attachment_preview/static/src/js/viewerjs_tweaks.js b/attachment_preview/static/src/js/viewerjs_tweaks.js index 42f9d836..cd8a0cdc 100644 --- a/attachment_preview/static/src/js/viewerjs_tweaks.js +++ b/attachment_preview/static/src/js/viewerjs_tweaks.js @@ -3,9 +3,9 @@ // This file contains tweaks for viewerjs itself and is not meant to be run in // OpenERP's context -(function(original_Viewer) { - window.Viewer = function(plugin, parameters) { - if(!plugin) { +(function (original_Viewer) { + window.Viewer = function (plugin, parameters) { + if (!plugin) { // eslint-disable-next-line no-alert alert('Unsupported file type'); }