Add export (txt, csv, json) and import

This commit is contained in:
Luc Didry 2016-09-13 09:06:15 +02:00
parent 38b830f6eb
commit b9c99d284c
4 changed files with 311 additions and 16 deletions

View file

@ -608,3 +608,7 @@ img {
.ui-resizable-ne { cursor: ne-resize; width: 9px; height: 9px; right: -5px; top: -5px;}
.ui-icon-gripsmall-diagonal-se { background-position: -64px -224px; }
.export button, .import * {
margin-right: 15px;
}

View file

@ -147,6 +147,10 @@ function getMessage(m) {
resizeBoard(message.data);
break;
case 'export':
download(message.data.filename, message.data.text);
break;
default:
//unknown message
alert('action inconnue : ' + JSON.stringify(message));
@ -679,6 +683,26 @@ function adjustCard(offsets, doSync) {
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
function download(filename, text) {
var element = document.createElement('a');
var mime = 'text/plain';
if (filename.match(/.csv$/)) {
mime = 'text/csv';
}
element.setAttribute('href', 'data:'+mime+';charset=utf-8,' + encodeURIComponent(text));
element.setAttribute('download', filename);
element.style.display = 'none';
document.body.appendChild(element);
element.click();
document.body.removeChild(element);
}
//////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////
$(function() {
@ -840,5 +864,43 @@ $(function() {
containment: 'parent'
});
$('#export-txt').click(function() {
socket.json.send({
action: 'exportTxt',
data: ($('.col').length !== 0) ? $('.col').css('width').replace('px', '') : null
});
})
$('#export-csv').click(function() {
socket.json.send({
action: 'exportCsv',
data: ($('.col').length !== 0) ? $('.col').css('width').replace('px', '') : null
});
})
$('#export-json').click(function() {
socket.json.send({
action: 'exportJson',
data: {
width: $('.board-outline').css('width').replace('px', ''),
height: $('.board-outline').css('height').replace('px', '')
}
});
})
$('#import-file').click(function(evt) {
evt.stopPropagation();
evt.preventDefault();
var f = $('#import-input').get(0).files[0];
var fr = new FileReader();
fr.onloadend = function() {
var text = fr.result;
socket.json.send({
action: 'importJson',
data: JSON.parse(text)
});
};
fr.readAsBinaryString(f);
})
});