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