app/soc/content/js/bulk-review-090304.js
changeset 1646 42b8e294792e
parent 1570 cda9f118bfef
child 2800 cd9eed2b787e
equal deleted inserted replaced
1645:c7e05dafaede 1646:42b8e294792e
       
     1 $(document).ready(function() {
       
     2 	$("#applications_progress_bar").progressBar({showText: false});
       
     3 });
       
     4 
       
     5 function bulkReviewInit(bulk_review_link,button) {
       
     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 				$("[id^=button_bulk_]").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 				var no_organization_text="No organizations to ";
       
    21 				if ($(button).attr("id").indexOf("reject")!=-1) {
       
    22 					no_organization_text+="reject";
       
    23 				}
       
    24 				else {
       
    25 					no_organization_text+="accept";
       
    26 				}
       
    27 				$("#description_done").html("<strong>"+no_organization_text+"</strong>");
       
    28 			}
       
    29 		}
       
    30 	);
       
    31 }
       
    32 
       
    33 function bulkReview(data) {
       
    34 	// some global constants
       
    35 	var GLOBAL_LINK = data.link;
       
    36 	var TOTAL_APPLICATIONS = data.nr_applications;
       
    37 
       
    38 	// some global variables set needed for internal iteration
       
    39 	var application_index = 0;
       
    40 	// number of iteration is not taken from data.nr_applications
       
    41 	// to ensure avoidance of array out of bounds errors
       
    42 	var total_index = data.applications.length;
       
    43 
       
    44 
       
    45 	// call immediately the function for review
       
    46 	// real iteration is inside
       
    47 	setTimeout(function(){
       
    48 		var error_happened = false;
       
    49 
       
    50 		var application = data.applications[application_index];
       
    51 		var current_application = application_index + 1;
       
    52 		// regular expression to find a valid scope path inside matching parenthesis
       
    53 		var re = /\((\w*)\)/;
       
    54 		var scope_path = GLOBAL_LINK.match(re)[1];
       
    55 		// the URL is obtained by using the scope path found in the matching parenthesis
       
    56 		var url_to_call = GLOBAL_LINK.replace(re, eval("application." + scope_path));
       
    57 		// now we can call the URL found
       
    58 		$.ajax({
       
    59 			async: false,
       
    60 			cache: false,
       
    61 			url: url_to_call,
       
    62 			timeout: 10000,
       
    63 			success: function(data) {
       
    64 				if (data) {
       
    65 					// update progress bar percentage and description
       
    66 					var percentage = Math.floor(100 * (current_application) / (TOTAL_APPLICATIONS));
       
    67 					$("#description_progressbar").html(" Processed application " + application.name + " (" + (current_application) + "/" + TOTAL_APPLICATIONS + ")");
       
    68 					$("#applications_progress_bar").progressBar(percentage);
       
    69 				}
       
    70 			},
       
    71 			error: function(XMLHttpRequest, textStatus, errorThrown) {
       
    72 				// if there is an error return the button and leave a try again message
       
    73 				error_happened = true;
       
    74 				$("[id^=button_bulk_]").fadeIn("slow", function() {
       
    75 					$("#description_done").html("<strong class='error'> Error encountered, try again</strong>");
       
    76 				});
       
    77 			}
       
    78 		});
       
    79 		// if there were no errors, continue the iteration
       
    80 		if (!error_happened) {
       
    81 			// prepare for new iteration and then recall this function
       
    82 			application_index++;
       
    83 			if (application_index < total_index) {
       
    84 				setTimeout(arguments.callee, 0);
       
    85 			}
       
    86 			else {
       
    87 				// all ok, tell the user we are done
       
    88 				$("#applications_progress_bar").fadeOut("slow",
       
    89 					function() {
       
    90 						$("#applications_progress_bar").progressBar(0);
       
    91 						$("[id^=button_bulk_]").fadeIn("slow");
       
    92 					}
       
    93 				);
       
    94 				$("#description_progressbar").html("");
       
    95 				$("#description_done").html("<strong>Done!</strong>");
       
    96 			}
       
    97 		}
       
    98 	},0);
       
    99 }