Ability to export polls as csv
This commit is contained in:
@@ -341,10 +341,7 @@ div.cp-app-poll-realtime {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
span {
|
span {
|
||||||
user-select: none;
|
.tools_unselectable();
|
||||||
-moz-user-select: none;
|
|
||||||
-webkit-user-select: none;
|
|
||||||
-ms-user-select: none;
|
|
||||||
}
|
}
|
||||||
thead {
|
thead {
|
||||||
td {
|
td {
|
||||||
@@ -374,6 +371,12 @@ div.cp-app-poll-realtime {
|
|||||||
width: 1em;
|
width: 1em;
|
||||||
text-align: center;
|
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 {
|
input {
|
||||||
&[type="text"] {
|
&[type="text"] {
|
||||||
|
|||||||
@@ -34,6 +34,7 @@ define([
|
|||||||
Renderer)
|
Renderer)
|
||||||
{
|
{
|
||||||
var Messages = Cryptpad.Messages;
|
var Messages = Cryptpad.Messages;
|
||||||
|
var saveAs = window.saveAs;
|
||||||
|
|
||||||
var Render = Renderer(Cryptpad);
|
var Render = Renderer(Cryptpad);
|
||||||
var APP = window.APP = {
|
var APP = window.APP = {
|
||||||
@@ -59,6 +60,59 @@ define([
|
|||||||
return JSON.parse(JSON.stringify(obj));
|
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
|
Make sure that the realtime data structure has all the required fields
|
||||||
*/
|
*/
|
||||||
@@ -168,7 +222,7 @@ define([
|
|||||||
$('[data-rt-id^="' + userid + '"]').closest('td')
|
$('[data-rt-id^="' + userid + '"]').closest('td')
|
||||||
.addClass("cp-app-poll-table-own");
|
.addClass("cp-app-poll-table-own");
|
||||||
$('.cp-app-poll-table-bookmark[data-rt-id="' + userid + '"]').css('visibility', '')
|
$('.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);
|
.attr('title', Messages.poll_bookmarked_col);
|
||||||
};
|
};
|
||||||
var styleUncommittedColumn = function () {
|
var styleUncommittedColumn = function () {
|
||||||
@@ -273,6 +327,7 @@ define([
|
|||||||
v: 0,
|
v: 0,
|
||||||
ids: []
|
ids: []
|
||||||
};
|
};
|
||||||
|
APP.count = {};
|
||||||
APP.proxy.content.rowsOrder.forEach(function (rId) {
|
APP.proxy.content.rowsOrder.forEach(function (rId) {
|
||||||
var count = Object.keys(APP.proxy.content.cells)
|
var count = Object.keys(APP.proxy.content.cells)
|
||||||
.filter(function (k) {
|
.filter(function (k) {
|
||||||
@@ -284,6 +339,7 @@ define([
|
|||||||
} else if (count && count === winner.v) {
|
} else if (count && count === winner.v) {
|
||||||
winner.ids.push(rId);
|
winner.ids.push(rId);
|
||||||
}
|
}
|
||||||
|
APP.count[rId] = count;
|
||||||
APP.$table.find('[data-rt-count-id="' + rId + '"]')
|
APP.$table.find('[data-rt-count-id="' + rId + '"]')
|
||||||
.text(count)
|
.text(count)
|
||||||
.css({
|
.css({
|
||||||
@@ -928,12 +984,17 @@ define([
|
|||||||
$rightside.append($templateButton);
|
$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;
|
APP.$helpButton = $help;
|
||||||
updateHelpButton();
|
updateHelpButton();
|
||||||
|
|
||||||
if (APP.readOnly) { publish(true); return; }
|
if (APP.readOnly) { publish(true); return; }
|
||||||
var $publish = common.createButton()
|
var $publish = common.createButton('', true)
|
||||||
.removeClass('fa-question').addClass('fa-check')
|
.removeClass('fa-question').addClass('fa-check')
|
||||||
.click(function () { publish(!APP.proxy.published); }).appendTo($rightside);
|
.click(function () { publish(!APP.proxy.published); }).appendTo($rightside);
|
||||||
APP.$publishButton = $publish;
|
APP.$publishButton = $publish;
|
||||||
|
|||||||
@@ -289,7 +289,7 @@ var Renderer = function (Cryptpad) {
|
|||||||
'data-rt-id': id,
|
'data-rt-id': id,
|
||||||
'title': Cryptpad.Messages.poll_bookmark_col,
|
'title': Cryptpad.Messages.poll_bookmark_col,
|
||||||
'style': 'visibility: hidden;',
|
'style': 'visibility: hidden;',
|
||||||
class: 'cp-app-poll-table-bookmark fa fa-bookmark-o',
|
class: 'cp-app-poll-table-bookmark fa fa-thumb-tack',
|
||||||
}, []];
|
}, []];
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user