bug 1816: improved AJAX file upload and background job progress meters
authorGalen Charlton <galen.charlton@liblime.com>
Mon, 18 Feb 2008 21:06:40 +0000 (10:06 +1300)
committerJoshua Ferraro <jmf@liblime.com>
Tue, 19 Feb 2008 01:32:04 +0000 (19:32 -0600)
Signed-off-by: Chris Cormack <chris@bigballofwax.co.nz>
Signed-off-by: Joshua Ferraro <jmf@liblime.com>
koha-tmpl/intranet-tmpl/prog/en/includes/background-job.inc
koha-tmpl/intranet-tmpl/prog/en/includes/file-upload.inc

index 10104ce..cfa9e94 100644 (file)
@@ -1,20 +1,26 @@
 <!-- Background job progress indicator -->
 <script type="text/javascript">
     //<![CDATA[
+    var backgroundJobProgressTimer = 0;
     var jobID = '';
     var savedForm;
+    var inBackgroundJobProgressTimer = false;
     function updateJobProgress() {
+        if (inBackgroundJobProgressTimer) {
+            return;
+        }
+        inBackgroundJobProgressTimer = true;
         $.getJSON("/cgi-bin/koha/tools/background-job-progress.pl?jobID=" + jobID, function(json) {
             var percentage = Math.floor(100 * json.progress / json.job_size);
             if (json.job_status == 'completed') {
                 percentage = 100;
             }
-            $("#jobprogress").text(percentage + '%');
-            if (percentage < 100) {
-                setTimeout("updateJobProgress()",200);
-            } else {
+            $("#jobprogress").text(percentage);
+            if (percentage == 100) {
+                clearInterval(backgroundJobProgressTimer); // just in case form submission fails
                 completeJob();
             }
+            inBackgroundJobProgressTimer = false;
         });
     }
 
 
             // and submit the request
             $("#jobstatus").show();
-            setTimeout("updateJobProgress()", 2000);
             $.ajax({
                 data: inputs.join('&'),
                 url: f.action,
                 dataType: 'json',
                 success: function(json) {
                     jobID = json.jobID;
+                    inBackgroundJobProgressTimer = false;
+                    backgroundJobProgressTimer = setInterval("updateJobProgress()", 500);
                 },
                 error: function(xml, textStatus) {
                     alert('Failed to submit form: ' + testStatus);
@@ -72,9 +79,6 @@
             f.submit();
         }
         
-        //$("#jobstatus").show();
-        //setTimeout("updateJobProgress()",2000);
-        //updateJobProgress();
         return false;
 
     }
index b1dc970..cd18f0b 100644 (file)
@@ -2,19 +2,28 @@
 <script type="text/javascript" src="<!-- TMPL_VAR name="themelang" -->/lib/jquery/plugins/ajaxfileupload.js"></script>
 <script type="text/javascript">
     //<![CDATA[
+    var fileUploadProgressTimer = 0;
+    var inFileUploadProgressTimer = false;
+    var fileUploadProgressTimerCanceled = false;
     function updateProgress() {
+        if (inFileUploadProgressTimer) {
+            // since $.getJSON is asynchronous, wait
+            // until the last one is finished
+            return;
+        }
+        inFileUploadProgressTimer = true;
         $.getJSON("/cgi-bin/koha/tools/upload-file-progress.pl", function(json) {
-            $("#fileuploadprogress").text(json.progress + '%');
-            if (json.progress < 100) {
-                setTimeout("updateProgress()",200);
+            if (!fileUploadProgressTimerCanceled) {
+                $("#fileuploadprogress").text(json.progress);
             }
+            inFileUploadProgressTimer = false;
         });
     }
     function ajaxFileUpload()
     {
-
+        fileUploadProgressTimerCanceled = false;
         $("#fileuploadstatus").show();
-        setTimeout("updateProgress()",2000);
+        fileUploadProgressTimer = setInterval("updateProgress()",500);
         $.ajaxFileUpload (
             {
                 url:'/cgi-bin/koha/tools/upload-file.pl',
                         $("#fileuploadfailed").text("Upload failed -- database in maintenance state");
                     } else {
                          $("#uploadedfileid").val(data.fileid);
+                         $("#fileuploadprogress").text("100");
                     }
+                    fileUploadProgressTimerCanceled = true;
+                    clearInterval(fileUploadProgressTimer);
                 },
                 error: function (data, status, e) {
+                    fileUploadProgressTimerCanceled = true;
                     alert(e);
+                    clearInterval(fileUploadProgressTimer);
                 }
             }
         )
-        updateProgress();
         return false;
 
     }