cryptpad/www/poll/export.js

82 lines
2.6 KiB
JavaScript
Raw Normal View History

2018-10-18 16:50:38 +00:00
// This file is used when a user tries to export the entire CryptDrive.
// Pads from the code app will be exported using this format instead of plain text.
define([
'/customize/messages.js',
], function (Messages) {
var module = {
ext: '.csv'
};
2018-10-18 16:50:38 +00:00
var copyObject = function (obj) {
return JSON.parse(JSON.stringify(obj));
};
module.getCSV = function (content) {
var data = copyObject(content);
var res = '';
2018-10-19 16:37:15 +00:00
var count = {};
data.rowsOrder.forEach(function (rId) {
var c = Object.keys(data.cells)
.filter(function (k) {
return k.indexOf(rId) !== -1 && data.cells[k] === 1;
}).length;
count[rId] = c;
});
2018-10-18 16:50:38 +00:00
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"); }
2018-10-19 16:37:15 +00:00
res += count[rowId] || '?';
2018-10-18 16:50:38 +00:00
res += '\n';
});
return res;
};
module.main = function (userDoc, cb) {
2018-10-25 08:35:39 +00:00
var content = userDoc.content || userDoc.table;
2018-10-18 16:50:38 +00:00
var csv;
try {
csv = module.getCSV(content);
} catch (e) {
console.error(e);
var blob2 = new Blob([JSON.stringify(content, 0, 2)], {
type: 'application/json',
});
2018-10-19 16:37:15 +00:00
return void cb(blob2, true);
2018-10-18 16:50:38 +00:00
}
var blob = new Blob([csv], {type: "application/csv;charset=utf-8"});
cb(blob);
};
return module;
});