changeset 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 a6b4b7401508
children eda104823649
files admin.js ajax.js
diffstat 2 files changed, 47 insertions(+), 22 deletions(-) [+]
line wrap: on
line diff
--- a/admin.js	Thu Nov 27 11:50:53 2014 +0200
+++ b/admin.js	Thu Nov 27 12:01:38 2014 +0200
@@ -66,6 +66,7 @@
         // Set active tab and refresh contents
         activeTabs[tabset] = id;
         setTimeout("refreshDispatch"+ tabset +"('"+ id +"');", 10);
+        jsCancelUploadCBS();
       }
       else
       {
@@ -435,6 +436,8 @@
 
   if (activeEntry != id || force)
   {
+    jsCancelUploadCBS();
+
     prevEntry = activeEntry;
     activeEntry = id;
 
@@ -451,6 +454,8 @@
 {
   var args = jsMakePostArgs({"name":1, "author":1, "filename":1, "info":1, "notes":1, "evalue":2}, "ne", id, true);
 
+  jsCancelUploadCBS();
+
   var msuccess = function(txt)
   {
     setTimeout("refreshDispatchCM("+ id +");", 50);
@@ -469,6 +474,8 @@
   var has_id = "compo_id" in lastPostArgs;
   var compo_id = lastPostArgs["compo_id"];
 
+  jsCancelUploadCBS();
+
   if (!("compo_id" in lastPostArgs))
   {
     args += "&compo_id=" + cid;
@@ -512,6 +519,7 @@
 
 function deleteEntry(cid, id)
 {
+  jsCancelUploadCBS();
   jsDeleteItem(id, "entry", "entries", "refreshDispatchCM("+ cid +");", "entry");
 }
 
--- 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.");
-}