summaryrefslogtreecommitdiff
path: root/jasper_reports/static/src/js/report/jasperactionmanager.js
blob: 57616bf3904dfb0be38db11da52aa366da40b611 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
odoo.define('report_xml.report', function (require) {
    'use strict';

    var ActionManager = require('web.ActionManager');
    var core = require('web.core');
    var framework = require('web.framework');
    var session = require('web.session');
    var _t = core._t;

    ActionManager.include({

        _executeReportAction: function (action, options) {
            var self = this;
            if (action.report_type === 'jasper') {
                return self._triggerDownload(action, options, 'jasper');
            }
            return this._super.apply(this, arguments);
        },

        _downloadReportJasper: function (url) {
            var self = this;
            framework.blockUI();
            return new Promise(function (resolve, reject) {
                var reporttype = url.split('/')[2];
                var type = 'jasper';
                var blocked = !session.get_file({
                    url: '/report/download',
                    data: {
                        data: JSON.stringify([url, type]),
                        context: JSON.stringify(session.user_context),
                    },
                    success: resolve,
                    error: (error) => {
                        self.call('crash_manager', 'rpc_error', error);
                        reject();
                    },
                    complete: framework.unblockUI,
                });
                if (blocked) {
                    // AAB: this check should be done in get_file service directly,
                    // should not be the concern of the caller (and that way, get_file
                    // could return a promise)
                    var message = _t('A popup window with your report was blocked. You ' +
                                     'may need to change your browser settings to allow ' +
                                     'popup windows for this page.');
                    self.do_warn(_t('Warning'), message, true);
                }
            });
        },

        _makeReportUrlsJasper: function (action) {
            var reportUrls = this._makeReportUrls(action);
            reportUrls.jasper = '/report/jasper/' + action.report_name;

            // We may have to build a query string with `action.data`. It's the place
            // were report's using a wizard to customize the output traditionally put
            // their options.
            if (_.isUndefined(action.data) || _.isNull(action.data) ||
                (_.isObject(action.data) && _.isEmpty(action.data))) {
                if (action.context.active_ids) {
                    var activeIDsPath = '/' + action.context.active_ids.join(',');
                    reportUrls = _.mapObject(reportUrls, function (value) {
                        return value += activeIDsPath;
                    });
                }
                reportUrls.html += '?context=' + encodeURIComponent(JSON.stringify(session.user_context));
            } else {
                var serializedOptionsPath = '?options=' + encodeURIComponent(JSON.stringify(action.data));
                serializedOptionsPath += '&context=' + encodeURIComponent(JSON.stringify(action.context));
                reportUrls = _.mapObject(reportUrls, function (value) {
                    return value += serializedOptionsPath;
                });
            }
            return reportUrls;
        },
    
            _triggerDownload: function (action, options, type) {
            if (type === "jasper") {
                var self = this;
                var reportUrls = this._makeReportUrlsJasper(action);
                return this._downloadReportJasper(
                    reportUrls[type]).then(function () {
                    if (action.close_on_report_download) {
                        var closeAction = { 
                            type: 'ir.actions.act_window_close' 
                        };
                        return self.doAction(
                            closeAction, _.pick(options, 'on_close'));
                    } else {
                        return options.on_close();
                    }
                }
                );
            }
            return this._super.apply(this, arguments);
    
        },

    });
});