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.

153 lines
5.1 KiB

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