Added ability to export raw events. Fixes #102

pull/106/head
Jordan Wright 2016-01-29 13:18:06 -06:00
parent a8f722b80f
commit d714635e69
2 changed files with 22 additions and 5 deletions

View File

@ -73,20 +73,30 @@ function deleteCampaign() {
} }
// Exports campaign results as a CSV file // Exports campaign results as a CSV file
function exportAsCSV() { function exportAsCSV(scope) {
exportHTML = $("#exportButton").html() exportHTML = $("#exportButton").html()
var csvScope = null
switch (scope) {
case "results":
csvScope = campaign.results
break;
case "events":
csvScope = campaign.timeline
break;
}
if (!csvScope){return}
$("#exportButton").html('<i class="fa fa-spinner fa-spin"></i>') $("#exportButton").html('<i class="fa fa-spinner fa-spin"></i>')
var csvString = Papa.unparse(campaign.results, {}) var csvString = Papa.unparse(csvScope, {})
var csvData = new Blob([csvString], { var csvData = new Blob([csvString], {
type: 'text/csv;charset=utf-8;' type: 'text/csv;charset=utf-8;'
}); });
if (navigator.msSaveBlob) { if (navigator.msSaveBlob) {
navigator.msSaveBlob(csvData, 'results.csv'); navigator.msSaveBlob(csvData, scope + '.csv');
} else { } else {
var csvURL = window.URL.createObjectURL(csvData); var csvURL = window.URL.createObjectURL(csvData);
var dlLink = document.createElement('a'); var dlLink = document.createElement('a');
dlLink.href = csvURL; dlLink.href = csvURL;
dlLink.setAttribute('download', 'results.csv'); dlLink.setAttribute('download', scope + '.csv');
dlLink.click(); dlLink.click();
} }
$("#exportButton").html(exportHTML) $("#exportButton").html(exportHTML)

View File

@ -33,9 +33,16 @@
<a href="/campaigns" class="btn btn-default"> <a href="/campaigns" class="btn btn-default">
<i class="fa fa-arrow-circle-o-left fa-lg"></i> Back <i class="fa fa-arrow-circle-o-left fa-lg"></i> Back
</a> </a>
<button type="button" id="exportButton" class="btn btn-primary" onclick="exportAsCSV()"> <div class="btn-group">
<button type="button" id="exportButton" class="btn btn-primary dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="true">
<i class="fa fa-file-excel-o"></i> Export CSV <i class="fa fa-file-excel-o"></i> Export CSV
<i class="fa fa-caret-down"></i>
</button> </button>
<ul class="dropdown-menu" aria-labelledby="exportButton">
<li><a href="#" onclick="exportAsCSV('results')">Results</a></li>
<li><a href="#" onclick="exportAsCSV('events')">Raw Events</a></li>
</ul>
</div>
<button type="button" class="btn btn-danger" data-toggle="tooltip" onclick="deleteCampaign()"> <button type="button" class="btn btn-danger" data-toggle="tooltip" onclick="deleteCampaign()">
<i class="fa fa-trash-o fa-lg"></i> Delete <i class="fa fa-trash-o fa-lg"></i> Delete
</button> </button>