/* * This file is part of the Doodle3D project (http://doodle3d.com). * * Copyright (c) 2014, Doodle3D * This software is licensed under the terms of the GNU GPL v2 or later. * See file LICENSE.txt or visit http://www.gnu.org/licenses/gpl.html for full license details. */ var filemanager = function() { var className = 'filemanager'; var sketchAPI = new SketchAPI(); if (location.host=='doodle3d') API.setURL('http://wifibox/d3dapi/'); //local else API.setURL('/d3dapi/'); $("#logo").click(onLogoClick) $("#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) { e.preventDefault(); $("#uploads").trigger('click'); }); var isBusy = true; updateButtonStates(); sketchAPI.list(function(data) { isBusy = true; updateButtonStates(); updateStatusMessage('loading '+data.list.length+' sketches...'); loadSketch(data.list, function() { console.log('done'); isBusy = false; updateFreeSpace(); updateButtonStates(); }); },function(response) { alert('Error reading sketches'); console.log(response); }); function onLogoClick() { location.href='/'+location.search; } function loadSketch(list,cb) { var id = list.pop(); if (!id) { if (cb) cb(); return; } sketchAPI.load(id,function(response) { // console.log(className,'loadSketch',response); addItem(id,response.data); updateStatusMessage('loading '+list.length+' sketches...'); if (list.length>0) { loadSketch(list,function() { cb(); }) } else { cb(); } }) } function addItem(id,svgData,doPrepend) { var path; if (!svgData) path = ""; else if (typeof(svgData)!='string') path = ""; else if (svgData.indexOf("CDATA")==-1) path = ""; else path = svgData.split('d="')[1].split('"')[0]; var item = $('
  • '); var svg = ''; item.append(svg); item.click(function() { $(this).toggleClass('selected'); updateButtonStates(); }) if (doPrepend) $('#svgContainer').prepend(item); else $('#svgContainer').append(item); item.hide().fadeIn(); updateButtonStates(); } function deleteSketches(list,cb) { var id = list.pop(); // $.post(api+'delete', {id:id}, function(data) { sketchAPI.del(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(); } }); } function deleteSelectedSketches() { if (!confirm('Do you want to delete '+$('.item.selected').length+' drawings?')) { return; } var ids = []; $('.item.selected').map(function(){ ids.push($(this).attr('data')); }); isBusy = true; updateButtonStates(); deleteSketches(ids,function() { console.log('done deleting sketches'); isBusy = false; updateButtonStates(); updateFreeSpace(); }); deselectAll(); updateButtonStates(); } function selectAll() { $('.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-1) return svg; //assume this SVG is already ok //this fixes SVG's created by the kunstcentraal app 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,"\n"); svg = svg.replace("M0,0 ",""); //RC: hack return svg; } function download() { $('.item.selected').each(function() { var id = $(this).attr('data'); var svgData = $(this).html(); console.log('downloading',id); $('')[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]); } }();