diff ajax.js @ 916:42c3fbca0d86

Possibly address concurrency / switching tabs while uploading.
author Matti Hamalainen <ccr@tnsp.org>
date Thu, 27 Nov 2014 12:01:38 +0200
parents 43fa1c4c783f
children f931fd35136c
line wrap: on
line diff
--- a/ajax.js	Thu Nov 27 11:50:53 2014 +0200
+++ b/ajax.js	Thu Nov 27 12:01:38 2014 +0200
@@ -4,6 +4,7 @@
 // (C) Copyright 2012-2014 Tecnic Software productions (TNSP)
 //
 var jsMessageBoxCBCancel = null, jsMessageBoxCBData = null, jsMessageBoxCBOK = null;
+var jsUploadCBS = [];
 
 
 function jsHandleMessageBoxKeys(ev)
@@ -283,10 +284,11 @@
 
   if (formFile.size > fileMaxSize)
   {
-    jsErrorMessageBox("File size exceeds "+ jsFormatSize(maxSize) +".");
+    jsErrorMessageBox("File size exceeds "+ jsFormatSize(fileMaxSize) +".");
     return;
   }
 
+  var filename = formFile.name;
   var formElem = document.getElementById(formID);
   if (!formElem)
   {
@@ -296,9 +298,25 @@
 
   var formData = new FormData(formElem);
   var req = jsCreateXMLRequest();
-  req.upload.addEventListener("progress", jsUploadProgress, false);
-  req.addEventListener("error", jsUploadError, false);
-  req.addEventListener("abort", jsUploadAbort, false);
+  req.upload.addEventListener("progress", function(e)
+  {
+    if (e.lengthComputable)
+    {
+      var complete = Math.round(e.loaded * 100 / e.total);
+      if (complete < 100)
+        jsStatusMsg("Uploaded ["+filename+"] "+ complete.toString() +'%, '+ jsFormatSize(e.loaded));
+      else
+        jsStatusMsg("Upload ["+filename+"] finished ...");
+    }
+  }, false);
+  req.addEventListener("error", function(e)
+  {
+    jsErrorMessageBox("Error occured while uploading "+filename);
+  }, false);
+  req.addEventListener("abort", function(e)
+  {
+    jsStatusMsg("Upload of '"+filename+"' aborted.");
+  }, false);
 
   req.onreadystatechange = function()
   {
@@ -326,9 +344,15 @@
         
         case 200:
           if (fileCallback)
-            setTimeout(fileCallback, 1);
-
-          jsTitleMessageBox("File upload", req.responseText);
+          {
+            var tid = setTimeout(function()
+            {
+              jsRemoveUploadCB(tid);
+              setTimeout(fileCallback, 10);
+              jsTitleMessageBox("File upload", req.responseText);
+            }, 10);
+            jsUploadCBS.push(tid);
+          }
           break;
         
         default:
@@ -343,26 +367,19 @@
 }
 
 
-function jsUploadProgress(e)
+function jsCancelUploadCBS()
 {
-  if (e.lengthComputable)
+  if (jsUploadCBS.length > 0)
   {
-    var complete = Math.round(e.loaded * 100 / e.total);
-    if (complete < 100)
-      jsStatusMsg("Uploaded "+ complete.toString() +'%, '+ jsFormatSize(e.loaded));
-    else
-      jsStatusMsg("Upload finished ...");
+    for (var tid in jsUploadCBS)
+      clearTimeout(tid);
   }
 }
 
 
-function jsUploadError(e)
+function jsRemoveUploadCB(tid)
 {
-  jsErrorMessageBox("Error occured while uploading: ");
+  var index = jsUploadCBS.indexOf(tid);
+  if (index >= 0)
+    jsUploadCBS.splice(index, 1);
 }
-
-
-function jsUploadAbort(e)
-{
-  jsStatusMsg("File upload aborted.");
-}