Skip to content

Commit

Permalink
make sendContentAsDownload method reusable (#12952)
Browse files Browse the repository at this point in the history
  • Loading branch information
tsteur authored and diosmosis committed May 22, 2018
1 parent c806d33 commit 8ecd0f1
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 37 deletions.
38 changes: 38 additions & 0 deletions plugins/Morpheus/javascripts/piwikHelper.js
Expand Up @@ -27,6 +27,44 @@ var piwikHelper = {
return $('<div/>').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
*/
Expand Down
Expand Up @@ -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',
Expand All @@ -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));
});
};

Expand Down

0 comments on commit 8ecd0f1

Please sign in to comment.