From 14944bb108ee9b7366df91ef86b48d2d958a6703 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20M=C3=BChlstrasser?= Date: Sat, 1 Nov 2014 22:51:11 +0100 Subject: [PATCH 1/6] Add file upload button to C1Pjs --- configs/c1p/serial/all.xml | 1 + configs/c1p/serial/samples.xml | 1 + my_modules/c1pjs-client/lib/serial.js | 13 +++++++++++++ my_modules/c1pjs-client/templates/components.xsl | 3 +++ 4 files changed, 18 insertions(+) diff --git a/configs/c1p/serial/all.xml b/configs/c1p/serial/all.xml index cd752a2d1..0f342cb87 100644 --- a/configs/c1p/serial/all.xml +++ b/configs/c1p/serial/all.xml @@ -21,4 +21,5 @@ SPACEWAR Load File + diff --git a/configs/c1p/serial/samples.xml b/configs/c1p/serial/samples.xml index db168eb20..dafaff9a3 100644 --- a/configs/c1p/serial/samples.xml +++ b/configs/c1p/serial/samples.xml @@ -10,4 +10,5 @@ PRESIDENTS Load File + diff --git a/my_modules/c1pjs-client/lib/serial.js b/my_modules/c1pjs-client/lib/serial.js index c21a91f0b..71c2f9cdc 100644 --- a/my_modules/c1pjs-client/lib/serial.js +++ b/my_modules/c1pjs-client/lib/serial.js @@ -124,6 +124,19 @@ C1PSerialPort.prototype.setBinding = function(c, t, s, e) }; }(this); return true; + case "uploadSerial": + this.bindings[s] = e; + var serial = this; + e.addEventListener('change', function () { + var file = e.files[0]; + var reader = new FileReader(); + reader.onload = function () { + // serial.println("uploading " + file.name + "..."); + serial.loadFile(file.name, reader.result, 0); + }; + reader.readAsText(file); + }); + return true; default: break; } diff --git a/my_modules/c1pjs-client/templates/components.xsl b/my_modules/c1pjs-client/templates/components.xsl index abdb87994..ec4a38463 100644 --- a/my_modules/c1pjs-client/templates/components.xsl +++ b/my_modules/c1pjs-client/templates/components.xsl @@ -285,6 +285,9 @@
+ + +
From ddfe2e5af3bbba2a03fd3f0965aed138eed31401 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20M=C3=BChlstrasser?= Date: Mon, 3 Nov 2014 20:50:09 +0100 Subject: [PATCH 2/6] Better label text for the button that triggers the file selection box. --- configs/c1p/serial/all.xml | 2 +- configs/c1p/serial/samples.xml | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/configs/c1p/serial/all.xml b/configs/c1p/serial/all.xml index 0f342cb87..b4372ece7 100644 --- a/configs/c1p/serial/all.xml +++ b/configs/c1p/serial/all.xml @@ -21,5 +21,5 @@ SPACEWAR
Load File - + diff --git a/configs/c1p/serial/samples.xml b/configs/c1p/serial/samples.xml index dafaff9a3..49900f086 100644 --- a/configs/c1p/serial/samples.xml +++ b/configs/c1p/serial/samples.xml @@ -10,5 +10,5 @@ PRESIDENTS Load File - + From 4c2624785f6bdfad002659ebce6e9625c5a75327 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20M=C3=BChlstrasser?= Date: Tue, 4 Nov 2014 21:56:09 +0100 Subject: [PATCH 3/6] Implement local file upload with form. This fixes the issue that after selecting a file it cannot be uploaded directly again without selecting another file in between. --- configs/c1p/serial/all.xml | 2 +- configs/c1p/serial/samples.xml | 2 +- my_modules/c1pjs-client/lib/serial.js | 10 +++++++--- my_modules/c1pjs-client/templates/components.xsl | 7 ++++++- 4 files changed, 15 insertions(+), 6 deletions(-) diff --git a/configs/c1p/serial/all.xml b/configs/c1p/serial/all.xml index b4372ece7..7833c5e49 100644 --- a/configs/c1p/serial/all.xml +++ b/configs/c1p/serial/all.xml @@ -21,5 +21,5 @@ SPACEWAR Load File - + diff --git a/configs/c1p/serial/samples.xml b/configs/c1p/serial/samples.xml index 49900f086..bb0b268be 100644 --- a/configs/c1p/serial/samples.xml +++ b/configs/c1p/serial/samples.xml @@ -10,5 +10,5 @@ PRESIDENTS Load File - + diff --git a/my_modules/c1pjs-client/lib/serial.js b/my_modules/c1pjs-client/lib/serial.js index 71c2f9cdc..bf51851b5 100644 --- a/my_modules/c1pjs-client/lib/serial.js +++ b/my_modules/c1pjs-client/lib/serial.js @@ -127,15 +127,19 @@ C1PSerialPort.prototype.setBinding = function(c, t, s, e) case "uploadSerial": this.bindings[s] = e; var serial = this; - e.addEventListener('change', function () { - var file = e.files[0]; + e.onsubmit = function (event) { + var file = event.currentTarget[1].files[0]; + var reader = new FileReader(); reader.onload = function () { // serial.println("uploading " + file.name + "..."); serial.loadFile(file.name, reader.result, 0); }; reader.readAsText(file); - }); + + // Prevent reloading of web page after form submission + return false; + }; return true; default: break; diff --git a/my_modules/c1pjs-client/templates/components.xsl b/my_modules/c1pjs-client/templates/components.xsl index ec4a38463..b333923b3 100644 --- a/my_modules/c1pjs-client/templates/components.xsl +++ b/my_modules/c1pjs-client/templates/components.xsl @@ -286,7 +286,12 @@
- +
+
+ + +
+

From 008c51f352ab70195d30d3f9ce9e2a59482f73d6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20M=C3=BChlstrasser?= Date: Tue, 4 Nov 2014 22:18:15 +0100 Subject: [PATCH 4/6] Disable local file upload if FileReader support is not available. --- my_modules/c1pjs-client/lib/serial.js | 33 ++++++++++++++++----------- 1 file changed, 20 insertions(+), 13 deletions(-) diff --git a/my_modules/c1pjs-client/lib/serial.js b/my_modules/c1pjs-client/lib/serial.js index bf51851b5..236d4929a 100644 --- a/my_modules/c1pjs-client/lib/serial.js +++ b/my_modules/c1pjs-client/lib/serial.js @@ -125,21 +125,28 @@ C1PSerialPort.prototype.setBinding = function(c, t, s, e) }(this); return true; case "uploadSerial": - this.bindings[s] = e; - var serial = this; - e.onsubmit = function (event) { - var file = event.currentTarget[1].files[0]; + // Check for availability of FileReader + if (window.FileReader && window.File && window.FileList && window.Blob ) { + var serial = this; + this.bindings[s] = e; + e.onsubmit = function (event) { + var file = event.currentTarget[1].files[0]; - var reader = new FileReader(); - reader.onload = function () { - // serial.println("uploading " + file.name + "..."); - serial.loadFile(file.name, reader.result, 0); + var reader = new FileReader(); + reader.onload = function () { + // serial.println("uploading " + file.name + "..."); + serial.loadFile(file.name, reader.result, 0); + }; + reader.readAsText(file); + + // Prevent reloading of web page after form submission + return false; }; - reader.readAsText(file); - - // Prevent reloading of web page after form submission - return false; - }; + } + else { + this.println("FileReader support not available, disabling local file upload"); + e.parentNode.removeChild(e); + } return true; default: break; From 3b3fcadee8c237ff5b7eff703cfc3f1b133c374d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20M=C3=BChlstrasser?= Date: Wed, 5 Nov 2014 23:09:33 +0100 Subject: [PATCH 5/6] Disable "Load Local File" button unless a file is selected. --- my_modules/c1pjs-client/lib/serial.js | 10 ++++++++++ my_modules/c1pjs-client/templates/components.xsl | 2 +- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/my_modules/c1pjs-client/lib/serial.js b/my_modules/c1pjs-client/lib/serial.js index 236d4929a..ef4055523 100644 --- a/my_modules/c1pjs-client/lib/serial.js +++ b/my_modules/c1pjs-client/lib/serial.js @@ -129,6 +129,16 @@ C1PSerialPort.prototype.setBinding = function(c, t, s, e) if (window.FileReader && window.File && window.FileList && window.Blob ) { var serial = this; this.bindings[s] = e; + + // Enable "Load Local File" button only if a file is actually selected + e.addEventListener('change', function () { + var fieldset = e.children[0]; + var files = fieldset.children[0].files; + var submit = fieldset.children[1]; + + submit.disabled = (files.length == 0); + }); + e.onsubmit = function (event) { var file = event.currentTarget[1].files[0]; diff --git a/my_modules/c1pjs-client/templates/components.xsl b/my_modules/c1pjs-client/templates/components.xsl index b333923b3..4a42d98ce 100644 --- a/my_modules/c1pjs-client/templates/components.xsl +++ b/my_modules/c1pjs-client/templates/components.xsl @@ -289,7 +289,7 @@
- +
From b1fa1460d2c57bf174477b3bff4e90f9e30e729b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Stephan=20M=C3=BChlstrasser?= Date: Thu, 6 Nov 2014 19:42:14 +0100 Subject: [PATCH 6/6] Fixed Closure compiler warning. WARNING - actual parameter 2 of C1PSerialPort.prototype.loadFile does not match formal parameter --- my_modules/c1pjs-client/lib/serial.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/my_modules/c1pjs-client/lib/serial.js b/my_modules/c1pjs-client/lib/serial.js index 73c470d60..d9b457c1a 100644 --- a/my_modules/c1pjs-client/lib/serial.js +++ b/my_modules/c1pjs-client/lib/serial.js @@ -145,7 +145,7 @@ C1PSerialPort.prototype.setBinding = function(c, t, s, e) var reader = new FileReader(); reader.onload = function () { // serial.println("uploading " + file.name + "..."); - serial.loadFile(file.name, reader.result, 0); + serial.loadFile(file.name, reader.result.toString(), 0); }; reader.readAsText(file);