|
1 $(document).ready(function() { |
|
2 $("#applications_progress_bar").progressBar({showText: false}); |
|
3 }); |
|
4 |
|
5 function bulkReviewInit(bulk_review_link) { |
|
6 // get the JSON object with details of every application for bulk acceptance |
|
7 $.getJSON(bulk_review_link+"?_="+(new Date().getTime()), |
|
8 function(data){ |
|
9 // If there are applications to review... |
|
10 if (data.nr_applications != 0) { |
|
11 //...then fade out the button, show the progress bar and call the function for review |
|
12 $("#button_bulk_review").fadeOut("slow", |
|
13 function() { |
|
14 $("#applications_progress_bar").progressBar(0); |
|
15 $("#description_done").html(""); |
|
16 $("#applications_progress_bar").fadeIn("slow", bulkReview(data)); |
|
17 } |
|
18 ); |
|
19 }else { |
|
20 $("#description_done").html("<strong>No organizations to accept</strong>"); |
|
21 } |
|
22 } |
|
23 ); |
|
24 } |
|
25 |
|
26 function bulkReview(data) { |
|
27 // some global constants |
|
28 var GLOBAL_LINK = data.link; |
|
29 var TOTAL_APPLICATIONS = data.nr_applications; |
|
30 |
|
31 // some global variables set needed for internal iteration |
|
32 var application_index = 0; |
|
33 // number of iteration is not taken from data.nr_applications |
|
34 // to ensure avoidance of array out of bounds errors |
|
35 var total_index = data.applications.length; |
|
36 |
|
37 |
|
38 // call immediately the function for review |
|
39 // real iteration is inside |
|
40 setTimeout(function(){ |
|
41 var error_happened = false; |
|
42 |
|
43 var application = data.applications[application_index]; |
|
44 var current_application = application_index + 1; |
|
45 // regular expression to find a valid scope path inside matching parenthesis |
|
46 var re = /\((\w*)\)/; |
|
47 var scope_path = GLOBAL_LINK.match(re)[1]; |
|
48 // the URL is obtained by using the scope path found in the matching parenthesis |
|
49 var url_to_call = GLOBAL_LINK.replace(re, eval("application." + scope_path)); |
|
50 // now we can call the URL found |
|
51 $.ajax({ |
|
52 async: false, |
|
53 cache: false, |
|
54 url: url_to_call, |
|
55 timeout: 10000, |
|
56 success: function(data) { |
|
57 if (data) { |
|
58 // update progress bar percentage and description |
|
59 var percentage = Math.floor(100 * (current_application) / (TOTAL_APPLICATIONS)); |
|
60 $("#description_progressbar").html(" Processed application " + application.name + " (" + (current_application) + "/" + TOTAL_APPLICATIONS + ")"); |
|
61 $("#applications_progress_bar").progressBar(percentage); |
|
62 } |
|
63 }, |
|
64 error: function(XMLHttpRequest, textStatus, errorThrown) { |
|
65 // if there is an error return the button and leave a try again message |
|
66 error_happened = true; |
|
67 $("#button_bulk_review").fadeIn("slow", function() { |
|
68 $("#description_done").html("<strong class='error'> Error encountered, try again</strong>"); |
|
69 }); |
|
70 } |
|
71 }); |
|
72 // if there were no errors, continue the iteration |
|
73 if (!error_happened) { |
|
74 // prepare for new iteration and then recall this function |
|
75 application_index++; |
|
76 if (application_index < total_index) { |
|
77 setTimeout(arguments.callee, 0); |
|
78 } |
|
79 else { |
|
80 // all ok, tell the user we are done |
|
81 $("#applications_progress_bar").fadeOut("slow", |
|
82 function() { |
|
83 $("#applications_progress_bar").progressBar(0); |
|
84 $("#button_bulk_review").fadeIn("slow"); |
|
85 } |
|
86 ); |
|
87 $("#description_progressbar").html(""); |
|
88 $("#description_done").html("<strong>Done!</strong>"); |
|
89 } |
|
90 } |
|
91 },0); |
|
92 } |