Ability to export polls as csv

pull/1/head
yflory 7 years ago
parent 5cf86bf74a
commit 707d3b3e94

@ -341,10 +341,7 @@ div.cp-app-poll-realtime {
}
}
span {
user-select: none;
-moz-user-select: none;
-webkit-user-select: none;
-ms-user-select: none;
.tools_unselectable();
}
thead {
td {
@ -374,6 +371,12 @@ div.cp-app-poll-realtime {
width: 1em;
text-align: center;
}
.cp-app-poll-table-bookmark {
color: darken(@poll-th-fg, 30%);
&.cp-app-poll-table-bookmark-full {
color: @poll-th-fg;
}
}
}
input {
&[type="text"] {

@ -34,6 +34,7 @@ define([
Renderer)
{
var Messages = Cryptpad.Messages;
var saveAs = window.saveAs;
var Render = Renderer(Cryptpad);
var APP = window.APP = {
@ -59,6 +60,59 @@ define([
return JSON.parse(JSON.stringify(obj));
};
var getCSV = APP.getCSV = function () {
if (!APP.proxy) { return; }
var data = copyObject(APP.proxy.content);
var res = '';
var escapeStr = function (str) {
return '"' + str.replace(/"/g, '""') + '"';
};
[null].concat(data.rowsOrder).forEach(function (rowId, i) {
[null].concat(data.colsOrder).forEach(function (colId, j) {
// thead
if (i === 0) {
if (j === 0) { res += ','; return; }
if (!colId) { throw new Error("Invalid data"); }
res += escapeStr(data.cols[colId] || Messages.anonymous) + ',';
return;
}
// tbody
if (!rowId) { throw new Error("Invalid data"); }
if (j === 0) {
res += escapeStr(data.rows[rowId] || Messages.poll_optionPlaceholder) + ',';
return;
}
if (!colId) { throw new Error("Invalid data"); }
res += (data.cells[colId + '_' + rowId] || 3) + ',';
});
// last column: total
// thead
if (i === 0) {
res += escapeStr(Messages.poll_total) + '\n';
return;
}
// tbody
if (!rowId) { throw new Error("Invalid data"); }
res += APP.count[rowId] || '?';
res += '\n';
});
return res;
};
var exportFile = function () {
var csv = getCSV();
var suggestion = Title.suggestTitle(Title.defaultTitle);
Cryptpad.prompt(Messages.exportPrompt,
Cryptpad.fixFileName(suggestion) + '.csv', function (filename) {
if (!(typeof(filename) === 'string' && filename)) { return; }
var blob = new Blob([csv], {type: "application/csv;charset=utf-8"});
saveAs(blob, filename);
});
};
/*
Make sure that the realtime data structure has all the required fields
*/
@ -168,7 +222,7 @@ define([
$('[data-rt-id^="' + userid + '"]').closest('td')
.addClass("cp-app-poll-table-own");
$('.cp-app-poll-table-bookmark[data-rt-id="' + userid + '"]').css('visibility', '')
.removeClass('fa-bookmark-o').addClass('fa-bookmark')
.addClass('cp-app-poll-table-bookmark-full')
.attr('title', Messages.poll_bookmarked_col);
};
var styleUncommittedColumn = function () {
@ -273,6 +327,7 @@ define([
v: 0,
ids: []
};
APP.count = {};
APP.proxy.content.rowsOrder.forEach(function (rId) {
var count = Object.keys(APP.proxy.content.cells)
.filter(function (k) {
@ -284,6 +339,7 @@ define([
} else if (count && count === winner.v) {
winner.ids.push(rId);
}
APP.count[rId] = count;
APP.$table.find('[data-rt-count-id="' + rId + '"]')
.text(count)
.css({
@ -928,12 +984,17 @@ define([
$rightside.append($templateButton);
}
var $help = common.createButton().click(function () { showHelp(); }).appendTo($rightside);
/* add an export button */
var $export = common.createButton('export', true, {}, exportFile);
$rightside.append($export);
var $help = common.createButton('', true).click(function () { showHelp(); })
.appendTo($rightside);
APP.$helpButton = $help;
updateHelpButton();
if (APP.readOnly) { publish(true); return; }
var $publish = common.createButton()
var $publish = common.createButton('', true)
.removeClass('fa-question').addClass('fa-check')
.click(function () { publish(!APP.proxy.published); }).appendTo($rightside);
APP.$publishButton = $publish;

@ -289,7 +289,7 @@ var Renderer = function (Cryptpad) {
'data-rt-id': id,
'title': Cryptpad.Messages.poll_bookmark_col,
'style': 'visibility: hidden;',
class: 'cp-app-poll-table-bookmark fa fa-bookmark-o',
class: 'cp-app-poll-table-bookmark fa fa-thumb-tack',
}, []];
};

Loading…
Cancel
Save