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.

135 lines
4.8 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
  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 framework = require('web.framework');
  23. var Widget = require('web.Widget');
  24. var PreviewHandler = require('muk_preview.PreviewHandler');
  25. var PreviewGenerator = require('muk_preview.PreviewGenerator');
  26. var QWeb = core.qweb;
  27. var _t = core._t;
  28. var PreviewDialog = Widget.extend({
  29. init: function(parent, generator, url, mimetype, extension, title) {
  30. this._super(parent);
  31. this._opened = $.Deferred();
  32. this.title = title || _t('Preview');
  33. this.url = url;
  34. this.mimetype = mimetype;
  35. this.extension = extension;
  36. this.$modal = $(QWeb.render('PreviewDialog', {title: this.title, url: this.url}));
  37. this.$modal.on('hidden.bs.modal', _.bind(this.destroy, this));
  38. this.$modal.find('.preview-maximize').on('click', _.bind(this.maximize, this));
  39. this.$modal.find('.preview-minimize').on('click', _.bind(this.minimize, this));
  40. this.$modal.find('.preview-print').on('click', _.bind(this.print, this));
  41. this.generator = generator;
  42. this.generator.widget = this;
  43. },
  44. renderElement: function() {
  45. this._super();
  46. var self = this;
  47. this.generator.createPreview(this.url, this.mimetype, this.extension, this.title).then(function($content) {
  48. self.setElement($("<div/>").addClass("modal-body preview-body").append($content));
  49. self.$modal.find('.preview-print').toggle($content.hasClass('printable'));
  50. });
  51. },
  52. open: function() {
  53. var self = this;
  54. $('.tooltip').remove();
  55. this.$modal.draggable({
  56. handle: '.modal-header',
  57. helper: false
  58. });
  59. this.replace(this.$modal.find(".modal-body")).then(function() {
  60. self.$modal.modal('show');
  61. self._opened.resolve();
  62. });
  63. return self;
  64. },
  65. maximize: function(e) {
  66. this.$modal.find('.preview-maximize').toggle();
  67. this.$modal.find('.preview-minimize').toggle();
  68. this.$modal.addClass("modal-fullscreen");
  69. },
  70. minimize: function(e) {
  71. this.$modal.find('.preview-maximize').toggle();
  72. this.$modal.find('.preview-minimize').toggle();
  73. this.$modal .removeClass("modal-fullscreen");
  74. },
  75. print: function(e) {
  76. var $printable = this.$modal.find('.printable');
  77. framework.blockUI();
  78. setTimeout(function() {
  79. framework.unblockUI();
  80. }, ($printable.data('print-delay') || 333) * 0.95);
  81. if(this.$modal.find('.print-content').length) {
  82. this.$modal.find('.print-content').printThis({
  83. importCSS: true,
  84. importStyle: true,
  85. printDelay: $printable.data('print-delay') || 333,
  86. loadCSS: $printable.data('print-css') || "",
  87. });
  88. } else {
  89. this.$modal.find('.printable').printThis({
  90. importCSS: true,
  91. importStyle: true,
  92. printDelay: $printable.data('print-delay') || 333,
  93. loadCSS: $printable.data('print-css') || "",
  94. });
  95. }
  96. },
  97. close: function() {
  98. var draggable = this.$modal.draggable( "instance" );
  99. if (draggable) {
  100. this.$modal.draggable("destroy");
  101. }
  102. this.$modal.modal('hide');
  103. },
  104. destroy: function(reason) {
  105. $('.tooltip').remove();
  106. if(this.isDestroyed()) {
  107. return;
  108. }
  109. this.trigger("closed", reason);
  110. this._super();
  111. this.$modal.modal('hide');
  112. this.$modal.remove();
  113. setTimeout(function () {
  114. var modals = $('body > .modal').filter(':visible');
  115. if(modals.length) {
  116. modals.last().focus();
  117. $('body').addClass('modal-open');
  118. }
  119. }, 0);
  120. }
  121. });
  122. PreviewDialog.createPreviewDialog = function (parent, url, mimetype, extension, title) {
  123. return new PreviewDialog(parent, new PreviewGenerator(parent, {}), url, mimetype, extension, title).open();
  124. };
  125. return PreviewDialog;
  126. });