]> git.plutz.net Git - lobster/commitdiff
auto submit therapy form data
authorPaul Hänsch <paul@plutz.net>
Tue, 14 Jan 2020 22:27:12 +0000 (23:27 +0100)
committerPaul Hänsch <paul@plutz.net>
Tue, 14 Jan 2020 22:27:12 +0000 (23:27 +0100)
therapies/autosave.js [new file with mode: 0644]
therapies/page.sh
therapies/therapy_draw.js
therapies/update_therapy.sh

diff --git a/therapies/autosave.js b/therapies/autosave.js
new file mode 100644 (file)
index 0000000..bf67274
--- /dev/null
@@ -0,0 +1,48 @@
+var button = document.querySelector('#savebutton');
+var formdata_old = '';
+var formdata = '';
+
+function postsubmit(){
+  if ( this.status == 200 ) {
+    console.log('successful auto submit of form data');
+    button.setAttribute('style', 'display: none;');
+  } else {
+    console.log('!!! Error response while auto submitting form data');
+    button.setAttribute('style', 'display: block;');
+  }
+}
+function failsubmit(){
+  console.log('!!! Timeout while auto submitting form data');
+  button.setAttribute('style', 'display: block;');
+}
+
+function formencode(fd){
+    var send;
+    send='autosubmit=false';
+    for (var tup of fd.entries()){
+      send += '&' + encodeURIComponent(tup[0]) + '=' + encodeURIComponent(tup[1]);
+    }
+    return send;
+}
+
+function formsend(){
+  var request = new XMLHttpRequest();
+  request.open('post', '/therapies/update_therapy.sh');
+  request.timeout = 5000;
+  request.onload = postsubmit;
+  request.ontimeout = failsubmit;
+  request.onerror = failsubmit;
+  request.onabort = failsubmit;
+
+  formdata = formencode(new FormData(document.querySelector('#report')));
+
+  if ( ! (formdata == formdata_old) ) {
+    console.log( 'send' );
+    request.send( formdata );
+    formdata_old = formdata;
+  }
+}
+
+formdata_old = formencode(new FormData(document.querySelector('#report')));
+button.setAttribute('style', 'display: none;');
+setInterval(formsend, 500);
index 2f0136ba25f9cfa8f8641fbe7a009965e5d2a049..c3d8078567331ca783385b8db8462e38e030c148 100755 (executable)
@@ -211,10 +211,11 @@ cat <<EOF
   [canvas #canvas .dotmark .ov width="${bg_dim%x*}" height="${bg_dim#*x}" ]
   [input type=hidden #image_serialize name=imagedata value=""]
 
-  [button type=submit $(l10n save)]
+  [button #savebutton type=submit $(l10n save)]
 ]
 
 [span #jsdebug style="display: none; position: fixed; right:0; bottom:0" Debug]
 
 [script type="text/javascript" src="/therapies/therapy_draw.js"]
+[script type="text/javascript" src="/therapies/autosave.js"]
 EOF
index 9710564ce46803335a11855a437e5d3c911b777d..604c000de3810e01f8d10d41dcdefdfb1b791eda 100644 (file)
@@ -1,4 +1,4 @@
-// Copyright 2016 Paul Hänsch
+// Copyright 2016, 2020 Paul Hänsch
 //
 // This file is part of Confetti.
 // 
index 406f4f983ca809d53bb94e13c2f559bc36440869..a8d78017f3abc4674e24332a1d3d3d4e8a011fc3 100755 (executable)
@@ -60,7 +60,7 @@ for key in $(POST_KEYS); do
       ;;
     *) printf %s:%s\\n "$key" "$(POST "$key" |sed ':X;N;$!bX; s;\n;\\n;g;')" ;;
   esac
-done >"$tempfile"
+done >"$tempfile" 2>&-
 
 if [ "$(POST delete_session)" ]; then
   n="$(POST delete_session)"
@@ -100,4 +100,8 @@ fi
 mv "$tempfile" "$tpyfile"
 rm -f "${_DATA}/cache/${tpy%%.*}.vcf.cache"
 
-REDIRECT "/therapies/${tpy%.*}/${tpy#*.}"
+if [ "$(POST autosubmit)" = "true" ]; then
+  printf 'HTTP/1.1 200 OK\r\nContent-Length: 0\r\n\r\n'
+else
+  REDIRECT "/therapies/${tpy%.*}/${tpy#*.}"
+fi