0
0
mirror of https://github.com/Doodle3D/doodle3d-client.git synced 2025-04-20 19:26:27 +02:00

update for API changes & cleanup

This commit is contained in:
Rick Companje 2015-06-02 14:44:10 +02:00
parent a3fc3995d8
commit ed8faf058f

View File

@ -6,135 +6,127 @@
* See file LICENSE.txt or visit http://www.gnu.org/licenses/gpl.html for full license details. * See file LICENSE.txt or visit http://www.gnu.org/licenses/gpl.html for full license details.
*/ */
// http://stackoverflow.com/questions/1403888/get-url-parameter-with-jquery var filemanager = function() {
function getURLParameter(name) { var className = 'filemanager';
return decodeURI((new RegExp('[&?]'+name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]); var sketchAPI = new SketchAPI();
}
var wifiboxURL = ""; if (location.host=='doodle3d') API.setURL('http://wifibox/d3dapi/'); //local
else API.setURL('/d3dapi/');
if (getURLParameter("r") != "null") wifiboxURL = 'http://192.168.5.1'; $("#logo").click(onLogoClick)
if (getURLParameter("wifiboxURL") != "null") wifiboxURL = getURLParameter("wifiboxURL"); $("#btnDelete").click(deleteSelectedSketches);
$("#btnSelectAll").click(selectAll);
$("#btnDeselectAll").click(deselectAll);
$("#uploads").change(upload);
$("#btnDownload").click(download);
$("#btnRefresh").click(refresh);
$("#btnCombine").click(combine);
var api = wifiboxURL+'/d3dapi/sketch/'; $("#btnUpload").click(function(e) {
e.preventDefault();
$("#uploads").trigger('click');
});
$("#logo").click(onLogoClick) var isBusy = true;
$("#btnDelete").click(deleteSelectedSketches);
$("#btnSelectAll").click(selectAll);
$("#btnDeselectAll").click(deselectAll);
$("#uploads").change(upload);
$("#btnDownload").click(download);
$("#btnRefresh").click(refresh);
$("#btnCombine").click(combine);
$("#btnUpload").click(function(e) { updateButtonStates();
e.preventDefault();
$("#uploads").trigger('click');
});
var isBusy = true;
updateButtonStates();
$.get(api+'list', function(data) { //?id=00003
if (data.status=='success') {
var list = data.data.list;
// list.reverse();
sketchAPI.list(function(data) {
isBusy = true; isBusy = true;
updateButtonStates(); updateButtonStates();
updateStatusMessage('loading '+list.length+' sketches...'); updateStatusMessage('loading '+data.list.length+' sketches...');
loadSketch(list, function() { loadSketch(data.list, function() {
console.log('done'); console.log('done');
isBusy = false; isBusy = false;
updateFreeSpace(); updateFreeSpace();
updateButtonStates(); updateButtonStates();
}); });
},function(response) {
alert('Error reading sketches');
console.log(response);
});
} else {
console.log('failure',data) function onLogoClick() {
location.href='/'+location.search;
} }
}).fail(function(status) { function loadSketch(list,cb) {
alert("Error ("+status.status+") connecting to "+api+'list'); var id = list.pop();
console.log(status);
});
if (!id) {
function onLogoClick() { if (cb) cb();
location.href='/'+location.search; return;
}
function loadSketch(list,cb) {
var id = list.pop();
$.get(api+'?id='+id, function(data) {
if (data.status=='success') {
addItem(id,data.data.data);
} }
updateStatusMessage('loading '+list.length+' sketches...'); sketchAPI.load(id,function(response) {
// console.log(className,'loadSketch',response);
addItem(id,response.data);
updateStatusMessage('loading '+list.length+' sketches...');
if (list.length>0) { if (list.length>0) {
loadSketch(list,function() { loadSketch(list,function() {
cb();
})
} else {
cb(); cb();
}) }
} else { })
cb();
}
});
}
function addItem(id,svgData,doPrepend) { }
var path;
if (!svgData) path = ""; function addItem(id,svgData,doPrepend) {
else if (typeof(svgData)!='string') path = ""; var path;
else if (svgData.indexOf("CDATA")==-1) path = "";
else path = svgData.split('d="')[1].split('"')[0];
var item = $('<li class="item ui-widget-content" data="'+id+'" title="'+id+'"></li>'); if (!svgData) path = "";
var svg = '<svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 640 540"><path fill="none" stroke="black" stroke-width="2" d="'+path+'"></path></svg>'; else if (typeof(svgData)!='string') path = "";
else if (svgData.indexOf("CDATA")==-1) path = "";
else path = svgData.split('d="')[1].split('"')[0];
var item = $('<li class="item ui-widget-content" data="'+id+'" title="'+id+'"></li>');
var svg = '<svg xmlns="http://www.w3.org/2000/svg" version="1.1" viewBox="0 0 640 540"><path fill="none" stroke="black" stroke-width="2" d="'+path+'"></path></svg>';
item.append(svg);
item.click(function() {
$(this).toggleClass('selected');
updateButtonStates();
})
if (doPrepend) $('#svgContainer').prepend(item);
else $('#svgContainer').append(item);
item.hide().fadeIn();
item.append(svg);
item.click(function() {
$(this).toggleClass('selected');
updateButtonStates(); updateButtonStates();
}) }
if (doPrepend) $('#svgContainer').prepend(item); function deleteSketches(list,cb) {
else $('#svgContainer').append(item); var id = list.pop();
item.hide().fadeIn();
updateButtonStates(); // $.post(api+'delete', {id:id}, function(data) {
}
function deleteSketches(list,cb) { sketchAPI.del(id,function(data) {
var id = list.pop();
$.post(api+'delete', {id:id}, function(data) { $('.item[data='+id+']').fadeOut('slow',function() {
$(this).remove(); //remove when effect is finished
});
updateStatusMessage("Deleting " + list.length + ' sketches...');
if (list.length>0) {
deleteSketches(list,cb);
} else {
cb();
}
$('.item[data='+id+']').fadeOut('slow',function() {
$(this).remove(); //remove when effect is finished
}); });
}
updateStatusMessage("Deleting " + list.length + ' sketches...'); function deleteSelectedSketches() {
if (!confirm('Do you want to delete '+$('.item.selected').length+' drawings?')) {
if (list.length>0) { return;
deleteSketches(list,cb);
} else {
cb();
} }
});
}
function deleteSelectedSketches() {
if (confirm('Do you want to delete '+$('.item.selected').length+' drawings?')) {
var ids = []; var ids = [];
$('.item.selected').map(function(){ $('.item.selected').map(function(){
ids.push($(this).attr('data')); ids.push($(this).attr('data'));
@ -153,131 +145,135 @@ function deleteSelectedSketches() {
deselectAll(); deselectAll();
updateButtonStates(); updateButtonStates();
} }
}
function selectAll() { function selectAll() {
$('.item').addClass('selected'); $('.item').addClass('selected');
updateButtonStates();
}
function deselectAll() {
$('.item').removeClass('selected');
updateButtonStates();
}
function updateButtonStates() {
var numItems = $('.item').length;
var numSelected = $('.item.selected').length;
var noSelection = numSelected==0;
$("#btnDelete").attr("disabled",isBusy || noSelection);
$("#btnDownload").attr("disabled",isBusy || noSelection);
$("#btnDeselectAll").attr("disabled",isBusy || noSelection);
$("#btnSelectAll").attr("disabled",isBusy || numItems==0);
$("#btnUpload").attr("disabled",isBusy || !noSelection);
$("#btnCombine").attr("disabled",isBusy || noSelection);
// $("#btnDelete").text("Delete" + (noSelection ? "" : " ("+numSelected+")"));
// $("#btnDownload").text("Download" + (noSelection ? "" : " ("+numSelected+")"));
// $("#btnPrint").text("Print" + (noSelection ? "..." : " ("+numSelected+")..."));
}
function uploadFile(files, index, next) {
var reader = new FileReader();
reader.readAsText(files[index], "UTF-8");
reader.onload = function (evt) {
console.log("onload",index); //,files[index],evt.target);
//process file
var svg = convertSvg(evt.target.result);
$.post(api, {data:svg}, function(data) {
if (data.status=='success') {
var id = data.data.id;
addItem(id,svg,true);
updateStatusMessage('uploading '+(files.length-index)+' sketches...');
if (index<files.length-1) {
uploadFile(files, index+1, next);
} else {
next(); //no more files, call back
}
}
});
}
reader.onerror = function (evt) {
console.log("onerror");
next(); //stop processing file(s), call back/
}
}
function upload() {
var files = $("#uploads")[0].files
var reader = new FileReader();
var cur = 0;
isBusy = true;
updateButtonStates();
updateStatusMessage("Uploading " + files.length + " files");
uploadFile(files, cur, function() {
console.log("done");
isBusy = false;
updateButtonStates(); updateButtonStates();
updateFreeSpace(); }
$("#frmUpload")[0].reset();
}) function deselectAll() {
} $('.item').removeClass('selected');
updateButtonStates();
}
function updateButtonStates() {
var numItems = $('.item').length;
var numSelected = $('.item.selected').length;
var noSelection = numSelected==0;
$("#btnDelete").attr("disabled",isBusy || noSelection);
$("#btnDownload").attr("disabled",isBusy || noSelection);
$("#btnDeselectAll").attr("disabled",isBusy || noSelection);
$("#btnSelectAll").attr("disabled",isBusy || numItems==0);
$("#btnUpload").attr("disabled",isBusy || !noSelection);
$("#btnCombine").attr("disabled",isBusy || noSelection);
// $("#btnDelete").text("Delete" + (noSelection ? "" : " ("+numSelected+")"));
// $("#btnDownload").text("Download" + (noSelection ? "" : " ("+numSelected+")"));
// $("#btnPrint").text("Print" + (noSelection ? "..." : " ("+numSelected+")..."));
}
function uploadFile(files, index, next) {
var reader = new FileReader();
reader.readAsText(files[index], "UTF-8");
reader.onload = function (evt) {
console.log("onload",index); //,files[index],evt.target);
//process file
var svg = convertSvg(evt.target.result);
$.post(api, {data:svg}, function(data) {
if (data.status=='success') {
var id = data.data.id;
addItem(id,svg,true);
updateStatusMessage('uploading '+(files.length-index)+' sketches...');
if (index<files.length-1) {
uploadFile(files, index+1, next);
} else {
next(); //no more files, call back
}
}
});
function updateFreeSpace() {
$.get(api+'status', function(data) { //?id=00003
if (data.status=='success') {
var numSketches = data.data.number_of_sketches;
var freeKb = Math.round(data.data.available/1024);
updateStatusMessage(numSketches+" sketches, "+freeKb+"k bytes free");
} }
});
}
function updateStatusMessage(msg) { reader.onerror = function (evt) {
$("#txtInfo").text(msg); console.log("onerror");
} next(); //stop processing file(s), call back/
}
}
function convertSvg(svg) { function upload() {
if (!svg) return ""; var files = $("#uploads")[0].files
if (typeof(svg)!='string') return ""; var reader = new FileReader();
if (svg.indexOf("CDATA")>-1) return svg; //assume this SVG is already ok var cur = 0;
isBusy = true;
updateButtonStates();
updateStatusMessage("Uploading " + files.length + " files");
//this fixes SVG's created by the kunstcentraal app uploadFile(files, cur, function() {
var re = /([a-zA-Z])\s?([0-9]{1,}) ([0-9]{1,})/g; console.log("done");
svg = svg.replace(re,"$1$2,$3"); isBusy = false;
re = /<\/svg>/g; updateButtonStates();
svg = svg.replace(re,"<!--<![CDATA[d3d-keys {\"height\":5,\"outlineShape\":\"none\",\"twist\":0}]]>-->\n</svg>"); updateFreeSpace();
$("#frmUpload")[0].reset();
})
}
svg = svg.replace("M0,0 ",""); //RC: hack function updateFreeSpace() {
sketchAPI.status(function(data) {
var numSketches = data.number_of_sketches;
var freeKb = Math.round(data.available/1024);
updateStatusMessage(numSketches+" sketches, "+freeKb+"k bytes free");
});
}
return svg; function updateStatusMessage(msg) {
} $("#txtInfo").text(msg);
}
function download() { function convertSvg(svg) {
$('.item.selected').each(function() { if (!svg) return "";
var id = $(this).attr('data'); if (typeof(svg)!='string') return "";
var svgData = $(this).html(); if (svg.indexOf("CDATA")>-1) return svg; //assume this SVG is already ok
console.log('downloading',id);
$('<a target="_blank" href="data:image/svg+xml,'+encodeURIComponent(svgData)+'" download="'+id+'.svg">')[0].click();
});
}
function refresh() { //this fixes SVG's created by the kunstcentraal app
location.reload(); var re = /([a-zA-Z])\s?([0-9]{1,}) ([0-9]{1,})/g;
} svg = svg.replace(re,"$1$2,$3");
re = /<\/svg>/g;
svg = svg.replace(re,"<!--<![CDATA[d3d-keys {\"height\":5,\"outlineShape\":\"none\",\"twist\":0}]]>-->\n</svg>");
function combine() { svg = svg.replace("M0,0 ",""); //RC: hack
var ids = [];
$('.item.selected').each(function() {
ids.push($(this).attr('data'));
});
location.href = '/printmanager/?ids=' + ids.join(); //+ location.search +
}
return svg;
}
function download() {
$('.item.selected').each(function() {
var id = $(this).attr('data');
var svgData = $(this).html();
console.log('downloading',id);
$('<a target="_blank" href="data:image/svg+xml,'+encodeURIComponent(svgData)+'" download="'+id+'.svg">')[0].click();
});
}
function refresh() {
location.reload();
}
function combine() {
var ids = [];
$('.item.selected').each(function() {
ids.push($(this).attr('data'));
});
location.href = '/printmanager/?ids=' + ids.join(); //+ location.search +
}
// http://stackoverflow.com/questions/1403888/get-url-parameter-with-jquery
function getURLParameter(name) {
return decodeURI((new RegExp('[&?]'+name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]);
}
}();