From 8ecd0f1c9a116beaa4f646b692ade35913d8cd09 Mon Sep 17 00:00:00 2001 From: Thomas Steur Date: Wed, 23 May 2018 04:53:30 +1200 Subject: [PATCH] make sendContentAsDownload method reusable (#12952) --- plugins/Morpheus/javascripts/piwikHelper.js | 38 +++++++++++++++++++ .../manage-gdpr/managegdpr.controller.js | 38 +------------------ 2 files changed, 39 insertions(+), 37 deletions(-) diff --git a/plugins/Morpheus/javascripts/piwikHelper.js b/plugins/Morpheus/javascripts/piwikHelper.js index 36eed1c685b..8a76122ebe5 100644 --- a/plugins/Morpheus/javascripts/piwikHelper.js +++ b/plugins/Morpheus/javascripts/piwikHelper.js @@ -27,6 +27,44 @@ var piwikHelper = { return $('
').html(value).text(); }, + sendContentAsDownload: function (filename, content, mimeType) { + if (!mimeType) { + mimeType = 'text/plain'; + } + function downloadFile(content) + { + var node = document.createElement('a'); + node.style.display = 'none'; + if ('string' === typeof content) { + node.setAttribute('href', 'data:' + mimeType + ';charset=utf-8,' + encodeURIComponent(content)); + } else { + node.href = window.URL.createObjectURL(blob); + } + node.setAttribute('download', filename); + document.body.appendChild(node); + node.click(); + document.body.removeChild(node); + } + + var node; + if ('function' === typeof Blob) { + // browser supports blob + try { + var blob = new Blob([content], {type: mimeType}); + if (window.navigator.msSaveOrOpenBlob) { + window.navigator.msSaveBlob(blob, filename); + return; + } else { + downloadFile(blob); + return; + } + } catch (e) { + downloadFile(content); + } + } + downloadFile(content); + }, + /** * a nice cross-browser logging function */ diff --git a/plugins/PrivacyManager/angularjs/manage-gdpr/managegdpr.controller.js b/plugins/PrivacyManager/angularjs/manage-gdpr/managegdpr.controller.js index 2a717aa4d56..5f09ccd03ae 100644 --- a/plugins/PrivacyManager/angularjs/manage-gdpr/managegdpr.controller.js +++ b/plugins/PrivacyManager/angularjs/manage-gdpr/managegdpr.controller.js @@ -68,42 +68,6 @@ }; this.exportDataSubject = function () { - function sendContentAsDownload(filename, content) { - var mimeType = 'text/plain'; - function downloadFile(content) - { - var node = document.createElement('a'); - node.style.display = 'none'; - if ('string' === typeof content) { - node.setAttribute('href', 'data:' + mimeType + ';charset=utf-8,' + encodeURIComponent(content)); - } else { - node.href = window.URL.createObjectURL(blob); - } - node.setAttribute('download', filename); - document.body.appendChild(node); - node.click(); - document.body.removeChild(node); - } - - var node; - if ('function' === typeof Blob) { - // browser supports blob - try { - var blob = new Blob([content], {type: mimeType}); - if (window.navigator.msSaveOrOpenBlob) { - window.navigator.msSaveBlob(blob, filename); - return; - } else { - downloadFile(blob); - return; - } - } catch (e) { - downloadFile(content); - } - } - downloadFile(content); - } - var visitsToDelete = this.getActivatedDataSubjects(); piwikApi.post({ module: 'API', @@ -112,7 +76,7 @@ filter_limit: -1, }, {visits: visitsToDelete}).then(function (visits) { showSuccessNotification('Visits were successfully exported'); - sendContentAsDownload('exported_data_subjects.json', JSON.stringify(visits)); + piwik.helper.sendContentAsDownload('exported_data_subjects.json', JSON.stringify(visits)); }); };