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.

145 lines
5.5 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_web_preview.Sidebar', function(require) {
  20. "use strict";
  21. var core = require('web.core');
  22. var session = require('web.session');
  23. var pyUtils = require('web.py_utils');
  24. var Context = require('web.Context');
  25. var Sidebar = require('web.Sidebar');
  26. var PreviewManager = require('muk_preview.PreviewManager');
  27. var PreviewDialog = require('muk_preview.PreviewDialog');
  28. var QWeb = core.qweb;
  29. var _t = core._t;
  30. Sidebar.include({
  31. events: _.extend({}, Sidebar.prototype.events, {
  32. 'click .mk_preview_report': '_onReportPreview',
  33. }),
  34. _onReportPreview: function(event) {
  35. this.$('[data-toggle="tooltip"]').tooltip({delay: 0});
  36. var index = $(event.currentTarget).data('index');
  37. var item = this.items['print'][index];
  38. if (item.action) {
  39. this.trigger_up('sidebar_data_asked', {
  40. callback: function (env) {
  41. var contextValues = {
  42. active_id: env.activeIds[0],
  43. active_ids: env.activeIds,
  44. active_model: env.model,
  45. active_domain: env.domain || [],
  46. };
  47. var context = pyUtils.eval('context',
  48. new Context(env.context, contextValues)
  49. );
  50. this._rpc({
  51. route: '/web/action/load',
  52. params: {
  53. action_id: item.action.id,
  54. context: context,
  55. },
  56. }).done(function (result) {
  57. result.context = new Context(
  58. result.context || {}, contextValues
  59. ).set_eval_context(context);
  60. result.flags.new_window = true;
  61. result.flags = result.flags || {};
  62. if (result.report_type === 'qweb-pdf') {
  63. this.call('report', 'checkWkhtmltopdf').then(function (state) {
  64. if (state === 'upgrade' || state === 'ok') {
  65. result.context = pyUtils.eval(
  66. 'context', result.context
  67. );
  68. this._callReportPreview(
  69. result, item.label,
  70. 'pdf', 'application/pdf'
  71. );
  72. } else {
  73. this._callReportAction(result);
  74. }
  75. }.bind(this));
  76. } else if (result.report_type === 'qweb-text') {
  77. result.context = pyUtils.eval(
  78. 'context', result.context
  79. );
  80. this._callReportPreview(
  81. result, item.label,
  82. 'text', 'text/plain'
  83. );
  84. } else {
  85. this._callReportAction(result);
  86. }
  87. }.bind(this));
  88. }.bind(this),
  89. });
  90. }
  91. event.stopPropagation();
  92. event.preventDefault();
  93. },
  94. _callReportAction: function(action) {
  95. this.do_action(action, {
  96. on_close: function () {
  97. this.trigger_up('reload');
  98. }.bind(this),
  99. });
  100. },
  101. _callReportPreview: function(action, label, type, mimetype) {
  102. var reportUrls = {
  103. pdf: '/report/pdf/' + action.report_name,
  104. text: '/report/text/' + action.report_name,
  105. };
  106. if (_.isUndefined(action.data) || _.isNull(action.data) ||
  107. (_.isObject(action.data) && _.isEmpty(action.data))) {
  108. if (action.context.active_ids) {
  109. var activeIDsPath = '/' + action.context.active_ids.join(',');
  110. reportUrls = _.mapObject(reportUrls, function (value) {
  111. return value += activeIDsPath;
  112. });
  113. }
  114. } else {
  115. var serializedOptionsPath = '?options=' + encodeURIComponent(JSON.stringify(action.data));
  116. serializedOptionsPath += '&context=' + encodeURIComponent(JSON.stringify(action.context));
  117. reportUrls = _.mapObject(reportUrls, function (value) {
  118. return value += serializedOptionsPath;
  119. });
  120. }
  121. var url = session.url('/report/download', {
  122. data: JSON.stringify([
  123. reportUrls[type],
  124. action.report_type
  125. ]),
  126. token: core.csrf_token,
  127. });
  128. var preview = new PreviewDialog(
  129. this, [{
  130. url: url,
  131. filename: label,
  132. mimetype: mimetype,
  133. }], 0
  134. );
  135. preview.appendTo($('body'));
  136. },
  137. });
  138. });