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.

148 lines
5.5 KiB

  1. /**********************************************************************************
  2. *
  3. * Copyright (c) 2017-2019 MuK IT GmbH.
  4. *
  5. * This file is part of MuK Preview
  6. * (see https://mukit.at).
  7. *
  8. * This program is free software: you can redistribute it and/or modify
  9. * it under the terms of the GNU Lesser General Public License as published by
  10. * the Free Software Foundation, either version 3 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU Lesser General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU Lesser General Public License
  19. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  20. *
  21. **********************************************************************************/
  22. odoo.define('muk_web_preview.Sidebar', function(require) {
  23. "use strict";
  24. var core = require('web.core');
  25. var session = require('web.session');
  26. var pyUtils = require('web.py_utils');
  27. var Context = require('web.Context');
  28. var Sidebar = require('web.Sidebar');
  29. var PreviewManager = require('muk_preview.PreviewManager');
  30. var PreviewDialog = require('muk_preview.PreviewDialog');
  31. var QWeb = core.qweb;
  32. var _t = core._t;
  33. Sidebar.include({
  34. events: _.extend({}, Sidebar.prototype.events, {
  35. 'click .mk_preview_report': '_onReportPreview',
  36. }),
  37. _onReportPreview: function(event) {
  38. this.$('[data-toggle="tooltip"]').tooltip({delay: 0});
  39. var index = $(event.currentTarget).data('index');
  40. var item = this.items['print'][index];
  41. if (item.action) {
  42. this.trigger_up('sidebar_data_asked', {
  43. callback: function (env) {
  44. var contextValues = {
  45. active_id: env.activeIds[0],
  46. active_ids: env.activeIds,
  47. active_model: env.model,
  48. active_domain: env.domain || [],
  49. };
  50. var context = pyUtils.eval('context',
  51. new Context(env.context, contextValues)
  52. );
  53. this._rpc({
  54. route: '/web/action/load',
  55. params: {
  56. action_id: item.action.id,
  57. context: context,
  58. },
  59. }).done(function (result) {
  60. result.context = new Context(
  61. result.context || {}, contextValues
  62. ).set_eval_context(context);
  63. result.flags.new_window = true;
  64. result.flags = result.flags || {};
  65. if (result.report_type === 'qweb-pdf') {
  66. this.call('report', 'checkWkhtmltopdf').then(function (state) {
  67. if (state === 'upgrade' || state === 'ok') {
  68. result.context = pyUtils.eval(
  69. 'context', result.context
  70. );
  71. this._callReportPreview(
  72. result, item.label,
  73. 'pdf', 'application/pdf'
  74. );
  75. } else {
  76. this._callReportAction(result);
  77. }
  78. }.bind(this));
  79. } else if (result.report_type === 'qweb-text') {
  80. result.context = pyUtils.eval(
  81. 'context', result.context
  82. );
  83. this._callReportPreview(
  84. result, item.label,
  85. 'text', 'text/plain'
  86. );
  87. } else {
  88. this._callReportAction(result);
  89. }
  90. }.bind(this));
  91. }.bind(this),
  92. });
  93. }
  94. event.stopPropagation();
  95. event.preventDefault();
  96. },
  97. _callReportAction: function(action) {
  98. this.do_action(action, {
  99. on_close: function () {
  100. this.trigger_up('reload');
  101. }.bind(this),
  102. });
  103. },
  104. _callReportPreview: function(action, label, type, mimetype) {
  105. var reportUrls = {
  106. pdf: '/report/pdf/' + action.report_name,
  107. text: '/report/text/' + action.report_name,
  108. };
  109. if (_.isUndefined(action.data) || _.isNull(action.data) ||
  110. (_.isObject(action.data) && _.isEmpty(action.data))) {
  111. if (action.context.active_ids) {
  112. var activeIDsPath = '/' + action.context.active_ids.join(',');
  113. reportUrls = _.mapObject(reportUrls, function (value) {
  114. return value += activeIDsPath;
  115. });
  116. }
  117. } else {
  118. var serializedOptionsPath = '?options=' + encodeURIComponent(JSON.stringify(action.data));
  119. serializedOptionsPath += '&context=' + encodeURIComponent(JSON.stringify(action.context));
  120. reportUrls = _.mapObject(reportUrls, function (value) {
  121. return value += serializedOptionsPath;
  122. });
  123. }
  124. var url = session.url('/report/download', {
  125. data: JSON.stringify([
  126. reportUrls[type],
  127. action.report_type
  128. ]),
  129. token: core.csrf_token,
  130. });
  131. var preview = new PreviewDialog(
  132. this, [{
  133. url: url,
  134. filename: label,
  135. mimetype: mimetype,
  136. }], 0
  137. );
  138. preview.appendTo($('body'));
  139. },
  140. });
  141. });