Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Altering CSV output #137

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 21 additions & 3 deletions extension/devtools/views/SitemapExportDataCSV.html
Original file line number Diff line number Diff line change
@@ -1,4 +1,22 @@
<p>
Export {{_id}} data as CSV. <br /> Waiting for the download button to appear. >
<span class="download-button" href="#"><a>Download now!</a></span>
<h2>Export {{_id}} data as CSV</h2>
<br/>

<label for="delimiter">Field delimiter:</label>
<input id="delimiter" type="text" required value=",">
<br/>

<label for="newline">Add new lines at end?</label>
<input id="newline" type="checkbox" required checked>
<br/>

<label for="utf-bom">Add UTF-8 BOM?</label>
<input id="utf-bom" type="checkbox" required checked>
<br/>

<button id="generate-csv">Generate CSV</button>
<br/>
<br/>

<p class="result">
Waiting for process to finish. > <span class="download-button" href="#"><a>Download now!</a></span>
</p>
25 changes: 19 additions & 6 deletions extension/scripts/Controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -1070,13 +1070,26 @@ SitemapController.prototype = {
var exportPanel = ich.SitemapExportDataCSV(sitemap);
$("#viewport").html(exportPanel);

$(".result").hide();

// generate data
$(".download-button").hide();
this.store.getSitemapData(sitemap, function (data) {
var blob = sitemap.getDataExportCsvBlob(data);
$(".download-button a").attr("href", window.URL.createObjectURL(blob));
$(".download-button a").attr("download", sitemap._id + ".csv");
$(".download-button").show();
$("#generate-csv").click(function () {
$(".result").show();
$(".download-button").hide();

var options = {
"delimiter": $("#delimiter").val(),
"newline": $("#newline").prop('checked'),
"containBom": $("#utf-bom").prop('checked')
};

this.store.getSitemapData(sitemap, function (data) {
var blob = sitemap.getDataExportCsvBlob(data, options);
$(".download-button a").attr("href", window.URL.createObjectURL(blob));
$(".download-button a").attr("download", sitemap._id + ".csv");
$(".download-button").show();
}.bind(this));

}.bind(this));

return true;
Expand Down
19 changes: 13 additions & 6 deletions extension/scripts/Sitemap.js
Original file line number Diff line number Diff line change
Expand Up @@ -174,12 +174,19 @@ Sitemap.prototype = {

return columns;
},
getDataExportCsvBlob: function (data) {

var columns = this.getDataColumns(),
delimiter = ',',
newline = "\n",
csvData = ['\ufeff']; // utf-8 bom char
getDataExportCsvBlob: function (data, option) {

var delimiterKey = "delimiter";
var newlineKey = "newline";
var containBomKey = "containBom";

var columns = this.getDataColumns(),
// default delimiter is comma
delimiter = option.hasOwnProperty(delimiterKey) ? option[delimiterKey] : ',',
// per default, new line is included at end of lines
newline = option.hasOwnProperty(newlineKey) ? (option[newlineKey] == true ? "\n" : "") : "\n",
// per default, utf8 BOM is included at the beginning.
csvData = option.hasOwnProperty(containBomKey) ? (option[containBomKey] == true ? ['\ufeff'] : []) : ['\ufeff']; // utf-8 bom char

// header
csvData.push(columns.join(delimiter) + newline)
Expand Down