bug 5309: Adjust progres bar
[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                                 var bgproperty = (parseInt(percentage/2)*3-300)+"px 0px";
19                 $("#jobprogress").css("background-position",bgproperty);
20                                 $("#jobprogresspercent").text(percentage);
21
22             if (percentage == 100) {
23                 clearInterval(backgroundJobProgressTimer); // just in case form submission fails
24                 completeJob();
25             }
26             inBackgroundJobProgressTimer = false;
27         });
28     }
29
30     function completeJob() {
31         savedForm.completedJobID.value = jobID;
32         savedForm.submit();
33     }
34
35     // submit a background job with data
36     // supplied from form f and activate
37     // progress indicator
38     function submitBackgroundJob(f) {
39         // check for background field
40         if (f.runinbackground) {
41             // set value of this hidden field for 
42             // use by CGI script
43             savedForm = f;
44             f.mainformsubmit.disabled = true;
45             f.runinbackground.value = 'true';
46
47             // gather up form submission
48             var inputs = [];
49             $(':input', f).each(function() {
50                 if (this.type == 'radio' || this.type == 'checkbox') {
51                     if (this.checked) {
52                         inputs.push(this.name + '=' + escape(this.value));
53                     }
54                 } else if (this.type == 'button') {
55                     ; // do nothing
56                 } else {
57                     inputs.push(this.name + '=' + escape(this.value));
58                 }
59                 
60             });
61
62             // and submit the request
63                         $("#jobpanel").show();
64             $("#jobstatus").show();
65             $.ajax({
66                 data: inputs.join('&'),
67                 url: f.action,
68                 dataType: 'json',
69                         type: 'post',
70                 success: function(json) {
71                     jobID = json.jobID;
72                     inBackgroundJobProgressTimer = false;
73                     backgroundJobProgressTimer = setInterval("updateJobProgress()", 500);
74                 },
75                 error: function(xml, textStatus) {
76                     alert('Failed to submit form: ' + textStatus);
77                 }
78
79             });
80
81         } else {
82             // background job support not enabled,
83             // so just do a normal form submission
84             f.submit();
85         }
86         
87         return false;
88
89     }
90     //]]>
91 </script>