From a48a4c00138205c3051e086e52301e0166d330e0 Mon Sep 17 00:00:00 2001 From: Gabriel Jenik Date: Mon, 27 Jun 2022 04:51:09 -0300 Subject: [PATCH] Fixed issue #18101: Spinning hexagon continues to spin although file is correctly downloaded (#2454) Dev Use FileSaver --- assets/scripts/admin/statistics.js | 30 +++++++++++++++++++++++++++++- 1 file changed, 29 insertions(+), 1 deletion(-) diff --git a/assets/scripts/admin/statistics.js b/assets/scripts/admin/statistics.js index 874bb3bccb1..fd45e6395d1 100644 --- a/assets/scripts/admin/statistics.js +++ b/assets/scripts/admin/statistics.js @@ -277,7 +277,6 @@ LS.Statistics2 = function () { }); $('#generate-statistics').submit(function () { - hideSection($('#generalfilters-chevron'), $('#statisticsgeneralfilters')); hideSection($('#responsefilters-chevron'), $('#filterchoices')) $('#statisticsoutput').show(); @@ -285,6 +284,12 @@ LS.Statistics2 = function () { $('#statistics-render-chevron').addClass('fa-chevron-down'); $('#view-stats-alert-info').hide(); $('#statsContainerLoading').show(); + if ($('input[name=outputtype]:checked').val() != 'html') { + var data = new FormData($(this).get(0)); + var url = $(this).attr('action'); + ajaxDownloadStats(url, data); + return false; + } //alert('ok'); }); @@ -590,6 +595,28 @@ LS.Statistics2 = function () { $(".stats-showpie").click(function () { changeGraphType('showpie', this.parentNode); }); + + var ajaxDownloadStats = function (url, data) { + const xhr = new XMLHttpRequest(); + xhr.open('POST', url, true); + xhr.responseType = 'blob'; + xhr.onload = () => { + const contentDisposition = xhr.getResponseHeader('Content-Disposition'); + const fileName = contentDisposition ? contentDisposition.match(/filename[^;=\n]*=['"](.*?|[^;\n]*)['"]/)[1] : ''; + if (fileName.length > 0) { + // saveAs is implemented by jszip/fileSaver.js + saveAs(xhr.response, fileName); + } else { + ajaxError(); + } + $('#statsContainerLoading').hide(); + }; + xhr.onerror = () => { + ajaxError(); + $('#statsContainerLoading').hide(); + }; + xhr.send(data); + }; }; var isWaiting = {}; @@ -615,6 +642,7 @@ function graphQuery(id, cmd, success) { } function ajaxError() { + // TODO: Use NotifyFader? alert("An error occured! Please reload the page!"); }