From f686c09db06459c137bde3d77a3089c56f293dda Mon Sep 17 00:00:00 2001 From: Ivan Kuchin Date: Thu, 1 May 2014 23:19:34 +0200 Subject: [PATCH 1/3] use one drop location for all files --- coloring/coloring.js | 6 +++- coloring/index.html | 7 ++-- facility/facility.js | 8 +++-- facility/index.html | 7 ++-- tsp/index.html | 7 ++-- tsp/tsp.js | 4 +++ viz.css | 2 -- viz.js | 84 +++++++++++++++++++------------------------- vrp/index.html | 7 ++-- vrp/vrp.js | 9 +++-- 10 files changed, 71 insertions(+), 70 deletions(-) diff --git a/coloring/coloring.js b/coloring/coloring.js index 08d3cc8..8f1b502 100644 --- a/coloring/coloring.js +++ b/coloring/coloring.js @@ -523,10 +523,14 @@ function findMinMaxCoords (nodes) { } +function isBenchmark(text){ + return text.replace(/^\s+|\s+$/g, '').split(REGEX_NEWLINE).length > 2 +} + function loadBenchmark(text){ parseInputText(text); } function loadSolution(text){ parseSolutionText(text); -} \ No newline at end of file +} diff --git a/coloring/index.html b/coloring/index.html index d5912e2..c18ca97 100644 --- a/coloring/index.html +++ b/coloring/index.html @@ -26,9 +26,8 @@
-
Step 1 - Drop benchmark file here
-
Step 2 - Drop solution file here
-

+
Drop benchmark and solution files here
+

@@ -52,4 +51,4 @@ - \ No newline at end of file + diff --git a/facility/facility.js b/facility/facility.js index 9fd5add..1cab752 100644 --- a/facility/facility.js +++ b/facility/facility.js @@ -403,7 +403,11 @@ function vizSolution() { } } - + +function isBenchmark(text){ + return text.replace(/^\s+|\s+$/g, '').split(REGEX_NEWLINE).length > 2 +} + function loadBenchmark(text){ if (parseInputText(text)); vizBenchmark(); @@ -454,4 +458,4 @@ function checkValidity() { reportError(errors.join(" ")); return true; } - \ No newline at end of file + diff --git a/facility/index.html b/facility/index.html index a42bc37..b17ee60 100644 --- a/facility/index.html +++ b/facility/index.html @@ -16,9 +16,8 @@
-
Step 1 - Drop benchmark file here
-
Step 2 - Drop solution file here
-

+
Drop benchmark and solution files here
+

@@ -38,4 +37,4 @@ - \ No newline at end of file + diff --git a/tsp/index.html b/tsp/index.html index a252a80..d2257f0 100644 --- a/tsp/index.html +++ b/tsp/index.html @@ -14,9 +14,8 @@
-
Step 1 - Drop benchmark file here
-
Step 2 - Drop solution file here
-

+
Drop benchmark and solution files here
+

@@ -36,4 +35,4 @@ - \ No newline at end of file + diff --git a/tsp/tsp.js b/tsp/tsp.js index c674712..433c5ed 100644 --- a/tsp/tsp.js +++ b/tsp/tsp.js @@ -300,6 +300,10 @@ function vizSolution(solution){ } +function isBenchmark(text){ + return text.replace(/^\s+|\s+$/g, '').split(REGEX_NEWLINE).length > 2 +} + function loadBenchmark(text){ console.log("loading data"); console.log(text); diff --git a/viz.css b/viz.css index 9a1d75f..c13f737 100644 --- a/viz.css +++ b/viz.css @@ -39,8 +39,6 @@ SOFTWARE. text-align:center; font:20pt bold,"Vollkorn"; color:#bbb; - float: left; - width: 45%; } #viz { diff --git a/viz.js b/viz.js index 9639bab..47e1884 100644 --- a/viz.js +++ b/viz.js @@ -65,61 +65,56 @@ function handleDragEnd(e) { } -function getFile(e) { +function getFiles(e) { var files = e.dataTransfer.files; if(files.length <= 0){ - reportError("please drop a file") - return null; + reportError("please drop a file(s)") + return []; } - if(files.length > 1){ - reportError("please drop only one file") - return null; + if(files.length > 2){ + reportError("please drop at most two files") + return []; } - return files[0] + return files } -function readFileAndLoad(file, loader) { +function readFilesAndLoad(files, loader) { if (!window.FileReader) { reportError('your browser does not have the necessary file reader API.'); return; } - var reader = new FileReader(); + var left = files.length + var texts = [] - reader.onload = function(e) { - //document.getElementById('debug').innerHTML = e.target.result; - loader(e.target.result) - } + for (var i = 0; i < files.length; i++) { + var reader = new FileReader(); - reader.onerror = function(e) { - console.log(e.target); - } + reader.onload = function(e) { + //document.getElementById('debug').innerHTML = e.target.result; + texts.push(e.target.result) + if (--left == 0) loader(texts) + } - reader.readAsText(file); -} + reader.onerror = function(e) { + console.log(e.target); + if (--left == 0) loader(texts) + } -function handleDropBenchmark(e) { - // this / e.target is current target element. - e.stopPropagation(); // stops the browser from redirecting. - e.preventDefault(); - - var cols = document.querySelectorAll('.drop_zone'); - [].forEach.call(cols, function (col) { - col.classList.remove('over'); - }); - - // See the section on the DataTransfer object. - file = getFile(e) - if(file == null){ - return false; + reader.readAsText(files[i]); } +} - //document.getElementById('debug').innerHTML = file; - readFileAndLoad(file, loadBenchmark); - return false; +function loadFiles(texts){ + for (var i = 0; i < texts.length; i++) { + if (isBenchmark(texts[i])) loadBenchmark(texts[i]) + } + for (var i = 0; i < texts.length; i++) { + if (!isBenchmark(texts[i])) loadSolution(texts[i]) + } } -function handleDropSolution(e) { +function handleDrop(e) { // this / e.target is current target element. e.stopPropagation(); // stops the browser from redirecting. e.preventDefault(); @@ -130,13 +125,13 @@ function handleDropSolution(e) { }); // See the section on the DataTransfer object. - file = getFile(e) - if(file == null){ + files = getFiles(e) + if(!files){ return false; } - readFileAndLoad(file, loadSolution); - + //document.getElementById('debug').innerHTML = file; + readFilesAndLoad(files, loadFiles); return false; } @@ -154,14 +149,9 @@ function setup() { col.addEventListener('dragend', handleDragEnd, false); }); - var cols = document.querySelectorAll('#benchmark_zone'); - [].forEach.call(cols, function(col) { - col.addEventListener('drop', handleDropBenchmark, false); - }); - - var cols = document.querySelectorAll('#solution_zone'); + var cols = document.querySelectorAll('#drop_zone'); [].forEach.call(cols, function(col) { - col.addEventListener('drop', handleDropSolution, false); + col.addEventListener('drop', handleDrop, false); }); } diff --git a/vrp/index.html b/vrp/index.html index 28ac77e..1a8070b 100644 --- a/vrp/index.html +++ b/vrp/index.html @@ -17,9 +17,8 @@
-
Step 1 - Drop benchmark file here
-
Step 2 - Drop solution file here
-

+
Drop benchmark and solution files here
+

@@ -39,4 +38,4 @@ - \ No newline at end of file + diff --git a/vrp/vrp.js b/vrp/vrp.js index 83d36c0..26f4b9a 100644 --- a/vrp/vrp.js +++ b/vrp/vrp.js @@ -464,7 +464,12 @@ function vizSolution() { .attr("stroke-width", line_width); }); } - + +function isBenchmark(text){ + var lines = text.replace(/^\s+|\s+$/g, '').split(REGEX_NEWLINE) + return lines.length == parseInt(lines[0]) + 1 +} + function loadBenchmark(text){ parseInputText(text); vizBenchmark(); @@ -474,4 +479,4 @@ function loadSolution(text){ parseSolutionText(text); vizSolution(); } - \ No newline at end of file + From 5ea5c6abf60541b89e5995c5d90de0244a30ccaa Mon Sep 17 00:00:00 2001 From: Ivan Kuchin Date: Sat, 3 May 2014 18:08:24 +0200 Subject: [PATCH 2/3] cleanup spaces in viz.js --- viz.js | 33 +++++++++++++++------------------ 1 file changed, 15 insertions(+), 18 deletions(-) diff --git a/viz.js b/viz.js index 47e1884..e9674a1 100644 --- a/viz.js +++ b/viz.js @@ -83,7 +83,7 @@ function readFilesAndLoad(files, loader) { reportError('your browser does not have the necessary file reader API.'); return; } - + var left = files.length var texts = [] @@ -123,14 +123,14 @@ function handleDrop(e) { [].forEach.call(cols, function (col) { col.classList.remove('over'); }); - + // See the section on the DataTransfer object. files = getFiles(e) if(!files){ return false; } - //document.getElementById('debug').innerHTML = file; + //document.getElementById('debug').innerHTML = file; readFilesAndLoad(files, loadFiles); return false; } @@ -140,23 +140,20 @@ function handleDrop(e) { ***/ function setup() { - var cols = document.querySelectorAll('.drop_zone'); - [].forEach.call(cols, function(col) { - col.addEventListener('dragstart', handleDragStart, false); - col.addEventListener('dragenter', handleDragEnter, false); - col.addEventListener('dragleave', handleDragLeave, false); - col.addEventListener('dragover', handleDragOver, false); - col.addEventListener('dragend', handleDragEnd, false); - }); + var cols = document.querySelectorAll('.drop_zone'); + [].forEach.call(cols, function(col) { + col.addEventListener('dragstart', handleDragStart, false); + col.addEventListener('dragenter', handleDragEnter, false); + col.addEventListener('dragleave', handleDragLeave, false); + col.addEventListener('dragover', handleDragOver, false); + col.addEventListener('dragend', handleDragEnd, false); + }); - var cols = document.querySelectorAll('#drop_zone'); - [].forEach.call(cols, function(col) { - col.addEventListener('drop', handleDrop, false); - }); + var cols = document.querySelectorAll('#drop_zone'); + [].forEach.call(cols, function(col) { + col.addEventListener('drop', handleDrop, false); + }); } setup(); - - - From 62bd802aa7b7f7496a772cc6d3ce30fdabc78526 Mon Sep 17 00:00:00 2001 From: Ivan Kuchin Date: Sat, 3 May 2014 18:24:10 +0200 Subject: [PATCH 3/3] handle text drops --- viz.js | 31 ++++++++++--------------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/viz.js b/viz.js index e9674a1..54fab3b 100644 --- a/viz.js +++ b/viz.js @@ -64,20 +64,6 @@ function handleDragEnd(e) { }); } - -function getFiles(e) { - var files = e.dataTransfer.files; - if(files.length <= 0){ - reportError("please drop a file(s)") - return []; - } - if(files.length > 2){ - reportError("please drop at most two files") - return []; - } - return files -} - function readFilesAndLoad(files, loader) { if (!window.FileReader) { reportError('your browser does not have the necessary file reader API.'); @@ -105,7 +91,7 @@ function readFilesAndLoad(files, loader) { } } -function loadFiles(texts){ +function loadTexts(texts){ for (var i = 0; i < texts.length; i++) { if (isBenchmark(texts[i])) loadBenchmark(texts[i]) } @@ -124,14 +110,17 @@ function handleDrop(e) { col.classList.remove('over'); }); - // See the section on the DataTransfer object. - files = getFiles(e) - if(!files){ - return false; + if (e.dataTransfer.files.length) { + readFilesAndLoad(e.dataTransfer.files, loadTexts) + } else { + var text = e.dataTransfer.getData('Text') + if (text) { + loadTexts([text]) + } else { + reportError("please drop a file(s) or text") + } } - //document.getElementById('debug').innerHTML = file; - readFilesAndLoad(files, loadFiles); return false; }