You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

101 lines
3.6 KiB

  1. /**********************************************************************************
  2. *
  3. * Copyright (C) 2017 MuK IT GmbH
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. *
  18. **********************************************************************************/
  19. odoo.define('muk_preview.PreviewDialog', function (require) {
  20. "use strict";
  21. var core = require('web.core');
  22. var Widget = require('web.Widget');
  23. var PreviewHandler = require('muk_preview.PreviewHandler');
  24. var PreviewGenerator = require('muk_preview.PreviewGenerator');
  25. var QWeb = core.qweb;
  26. var _t = core._t;
  27. var PreviewDialog = Widget.extend({
  28. init: function(parent, generator, url, mimetype, extension, title) {
  29. var self = this;
  30. this._super(parent);
  31. this.generator = generator;
  32. this._opened = $.Deferred();
  33. this.title = title || _t('Preview');
  34. this.url = url;
  35. this.mimetype = mimetype;
  36. this.extension = extension;
  37. this.$modal = $(QWeb.render('PreviewDialog', {title: this.title, url: this.url}));
  38. this.$modal.on('hidden.bs.modal', _.bind(this.destroy, this));
  39. this.$modal.find('.preview-maximize').on('click', _.bind(this.maximize, this));
  40. this.$modal.find('.preview-minimize').on('click', _.bind(this.minimize, this));
  41. },
  42. renderElement: function() {
  43. this._super();
  44. var self = this;
  45. this.generator.createPreview(this.url, this.mimetype, this.extension, this.title).then(function($content) {
  46. self.setElement($("<div/>").addClass("modal-body preview-body").append($content));
  47. });
  48. },
  49. open: function() {
  50. var self = this;
  51. $('.tooltip').remove();
  52. this.replace(this.$modal.find(".modal-body")).then(function() {
  53. self.$modal.modal('show');
  54. self._opened.resolve();
  55. });
  56. return self;
  57. },
  58. maximize: function(e) {
  59. this.$modal.find('.preview-maximize').toggle();
  60. this.$modal.find('.preview-minimize').toggle();
  61. this.$modal.addClass("modal-fullscreen");
  62. },
  63. minimize: function(e) {
  64. this.$modal.find('.preview-maximize').toggle();
  65. this.$modal.find('.preview-minimize').toggle();
  66. this.$modal .removeClass("modal-fullscreen");
  67. },
  68. close: function() {
  69. this.$modal.modal('hide');
  70. },
  71. destroy: function(reason) {
  72. $('.tooltip').remove();
  73. if(this.isDestroyed()) {
  74. return;
  75. }
  76. this.trigger("closed", reason);
  77. this._super();
  78. this.$modal.modal('hide');
  79. this.$modal.remove();
  80. setTimeout(function () {
  81. var modals = $('body > .modal').filter(':visible');
  82. if(modals.length) {
  83. modals.last().focus();
  84. $('body').addClass('modal-open');
  85. }
  86. }, 0);
  87. }
  88. });
  89. PreviewDialog.createPreviewDialog = function (owner, url, mimetype, extension, title) {
  90. return new PreviewDialog(owner, new PreviewGenerator(), url, mimetype, extension, title).open();
  91. };
  92. return PreviewDialog;
  93. });