bug 1816: improved AJAX file upload and background job progress meters
[koha.git] / koha-tmpl / intranet-tmpl / prog / en / includes / background-job.inc
1 <!-- Background job progress indicator -->
2 <script type="text/javascript">
3     //<![CDATA[
4     var backgroundJobProgressTimer = 0;
5     var jobID = '';
6     var savedForm;
7     var inBackgroundJobProgressTimer = false;
8     function updateJobProgress() {
9         if (inBackgroundJobProgressTimer) {
10             return;
11         }
12         inBackgroundJobProgressTimer = true;
13         $.getJSON("/cgi-bin/koha/tools/background-job-progress.pl?jobID=" + jobID, function(json) {
14             var percentage = Math.floor(100 * json.progress / json.job_size);
15             if (json.job_status == 'completed') {
16                 percentage = 100;
17             }
18             $("#jobprogress").text(percentage);
19             if (percentage == 100) {
20                 clearInterval(backgroundJobProgressTimer); // just in case form submission fails
21                 completeJob();
22             }
23             inBackgroundJobProgressTimer = false;
24         });
25     }
26
27     function completeJob() {
28         savedForm.completedJobID.value = jobID;
29         savedForm.submit();
30     }
31
32     // submit a background job with data
33     // supplied from form f and activate
34     // progress indicator
35     function submitBackgroundJob(f) {
36         // check for background field
37         if (f.runinbackground) {
38             // set value of this hidden field for 
39             // use by CGI script
40             savedForm = f;
41             f.mainformsubmit.disabled = true;
42             f.runinbackground.value = 'true';
43
44             // gather up form submission
45             var inputs = [];
46             $(':input', f).each(function() {
47                 if (this.type == 'radio') {
48                     if (this.checked) {
49                         inputs.push(this.name + '=' + escape(this.value));
50                     }
51                 } else if (this.type == 'button') {
52                     ; // do nothing
53                 } else {
54                     inputs.push(this.name + '=' + escape(this.value));
55                 }
56                 
57             });
58
59             // and submit the request
60             $("#jobstatus").show();
61             $.ajax({
62                 data: inputs.join('&'),
63                 url: f.action,
64                 dataType: 'json',
65                 success: function(json) {
66                     jobID = json.jobID;
67                     inBackgroundJobProgressTimer = false;
68                     backgroundJobProgressTimer = setInterval("updateJobProgress()", 500);
69                 },
70                 error: function(xml, textStatus) {
71                     alert('Failed to submit form: ' + testStatus);
72                 }
73
74             });
75
76         } else {
77             // background job support not enabled,
78             // so just do a normal form submission
79             f.submit();
80         }
81         
82         return false;
83
84     }
85     //]]>
86 </script>