Explorar el Código

[ADD] download directly

Gogs hace 6 años
padre
commit
122273f41d
Se han modificado 1 ficheros con 53 adiciones y 13 borrados
  1. 53 13
      static/src/js/main.js

+ 53 - 13
static/src/js/main.js

@@ -414,7 +414,58 @@ openerp.print_engine = function (instance, local) {
                     new local.PdfViewerDialog(self, this.session.url('/web/report', params), action.name);
                 },
                 trigger_download_pdf: function (action) {
-                    console.log(this);
+                    var regex = /^(?:qweb-(pdf|html)|controller)$/;
+                    var url = null;
+
+                    instance.web.blockUI();
+
+                    if (_.has(action, 'report_type') && regex.test(action.report_type)) {
+                        var format = action.report_type.match(regex)[1];
+                        url = (format || action.report_file) && ('/report/' + format + '/' + action.report_name);
+
+                        if (_.isEmpty(action.data) && _.has(action.context, 'active_ids')) {
+                            url += '/' + action.context.active_ids.join(',');
+                        } else {
+                            url += '?options=' + encodeURIComponent(JSON.stringify(action.data)) + '&context=' + encodeURIComponent(JSON.stringify(action.context));
+                        }
+                    }
+                    
+                    if (!url) {
+                        var params = {
+                            action: JSON.stringify(action),
+                            token: (new Date()).getTime()
+                        };
+
+                        url = this.session.url('/web/report', params);
+                    }
+
+                    instance.pdfJs.getDocument(url).then(function (pdf) {
+                        pdf.getData().then(function (data) {
+                            instance.web.unblockUI();
+
+                            var url = instance.pdfJs.createObjectURL(data, 'application/pdf');
+                            var a = document.createElement('a');
+        
+                            a.href = url;
+                            a.download = action.name;
+                            a.target = '_parent';
+                            a.click();
+                        });
+                    });
+                },
+                trigger_print_pdf: function (action) {
+                    var url = '/print_engine/get_pdf';
+                    var ctx = _.clone(action.context);
+
+                    ctx.report_name = action.report_name;
+
+                    instance.web.jsonRpc(url, 'call', {
+                        context: ctx
+                    }).done(function (result) {
+                        instance.print_engine.socket_manager.request_print(result.data);
+                    }).fail(function (e) {
+                        instance.webclient.crashmanager.show_message(e);
+                    });
                 },
                 ir_actions_report_xml: function (action, options) {
                     var self = this;
@@ -446,18 +497,7 @@ openerp.print_engine = function (instance, local) {
                         return;
                     }
 
-                    var url = '/print_engine/get_pdf';
-                    var ctx = _.clone(action.context);
-
-                    ctx.report_name = action.report_name;
-
-                    instance.web.jsonRpc(url, 'call', {
-                        context: ctx
-                    }).done(function (result) {
-                        instance.print_engine.socket_manager.request_print(result.data);
-                    }).fail(function (e) {
-                        instance.webclient.crashmanager.show_message(e);
-                    });
+                    self.trigger_print_pdf(action);
                 }
             });
         }