Added bulk acceptance and progress bar in review org applications view.
authorLennard de Rijk <ljvderijk@gmail.com>
Sat, 14 Feb 2009 21:18:12 +0000
changeset 1328 cd175dddc15c
parent 1327 f6f70eb8d8bf
child 1329 c0af9fa2f83d
Added bulk acceptance and progress bar in review org applications view. In the list of organization applications for reviewing, if you click the button "click here" the whole first text line will fade out and the progress bar will fade in while starting to contact the server for the list of orgs to accept and then make synchronous calls for acceptance, while updating the progress bar, the name of the organization currently accepting and the number of orgs already accepted against the total. Inside the script, what's inside the parenthesis is converted due to regexp (in this case (link_id)) and then read the json_object.applications[index].link_id. By doing this with an eval(), you can use other names as well and the script will be reading for example json_object.applications[index].attribute_name if you insert "(attribute_name)" inside the link returned by {{ bulk_accept_link }}. Notes by Lennard: -Put Done outside the for-loop so that it also shows when there are 0 pre-accepted organizations. -Made some minor style fixes Patch by: Mario Ferraro Reviewed by: Lennard de Rijk
app/jquery/jquery-progressbar.js
app/soc/content/images/progressbar.gif
app/soc/content/images/progressbg_green.gif
app/soc/templates/soc/org_app/review_overview.html
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/app/jquery/jquery-progressbar.js	Sat Feb 14 21:18:12 2009 +0000
@@ -0,0 +1,130 @@
+/*
+ * jQuery Progress Bar plugin
+ * Version 1.1.0 (06/20/2008)
+ * @requires jQuery v1.2.1 or later
+ *
+ * Copyright (c) 2008 Gary Teo
+ * http://t.wits.sg
+
+USAGE:
+	$(".someclass").progressBar();
+	$("#progressbar").progressBar();
+	$("#progressbar").progressBar(45);							// percentage
+	$("#progressbar").progressBar({showText: false });			// percentage with config
+	$("#progressbar").progressBar(45, {showText: false });		// percentage with config
+*/
+(function($) {
+	$.extend({
+		progressBar: new function() {
+			this.defaults = {
+				increment	: 2,
+				speed		: 15,
+				showText	: true,											// show text with percentage in next to the progressbar? - default : true
+				width		: 120,											// Width of the progressbar - don't forget to adjust your image too!!!
+				boxImage	: '/soc/content/images/progressbar.gif',		// boxImage : image around the progress bar
+				barImage	: '/soc/content/images/progressbg_green.gif',	// Image to use in the progressbar. Can be an array of images too.
+				height		: 12											// Height of the progressbar - don't forget to adjust your image too!!!
+			};
+			
+			/* public methods */
+			this.construct = function(arg1, arg2) {
+				var argpercentage	= null;
+				var argconfig		= null;
+				
+				if (arg1 != null) {
+					if (!isNaN(arg1)) {
+						argpercentage 	= arg1;
+						if (arg2 != null) {
+							argconfig	= arg2; }
+					} else {
+						argconfig		= arg1; 
+					}
+				}
+				
+				return this.each(function(child) {
+					var pb		= this;
+					if (argpercentage != null && this.bar != null && this.config != null) {
+						this.config.tpercentage	= argpercentage;
+						if (argconfig != null)
+							pb.config			= $.extend(this.config, argconfig);
+					} else {
+						var $this				= $(this);
+						var config				= $.extend({}, $.progressBar.defaults, argconfig);
+						var percentage			= argpercentage;
+						if (argpercentage == null)
+							var percentage		= $this.html().replace("%","");	// parsed percentage
+						
+
+						$this.html("");
+						var bar					= document.createElement('img');
+						var text				= document.createElement('span');
+						bar.id 					= this.id + "_percentImage";
+						text.id 				= this.id + "_percentText";
+						bar.src					= config.boxImage;
+						bar.width				= config.width;
+						var $bar				= $(bar);
+						var $text				= $(text);
+						
+						this.bar				= $bar;
+						this.ntext				= $text;
+						this.config				= config;
+						this.config.cpercentage	= 0;
+						this.config.tpercentage	= percentage;
+						
+						$bar.css("width", config.width + "px");
+						$bar.css("height", config.height + "px");
+						$bar.css("background-image", "url(" + config.barImage + ")");
+						$bar.css("padding", "0");
+						$bar.css("margin", "0");
+
+						$this.append($bar);
+						$this.append($text);
+						
+						bar.alt				= this.tpercentage;
+						bar.title			= this.tpercentage;
+					}
+					
+					
+					
+					var t = setInterval(function() {
+						var config		= pb.config;
+						var cpercentage = parseInt(config.cpercentage);
+						var tpercentage = parseInt(config.tpercentage);
+						var increment	= parseInt(config.increment);
+						var bar			= pb.bar;
+						var text		= pb.ntext;
+						var pixels		= config.width / 100;			// Define how many pixels go into 1%
+						
+						bar.css("background-position", (((config.width * -1)) + (cpercentage * pixels)) + 'px 50%');
+						
+						if (config.showText)
+							text.html(" " + Math.round(cpercentage) + "%");
+						
+						if (cpercentage > tpercentage) {
+							if (cpercentage - increment  < tpercentage) {
+								pb.config.cpercentage = 0 + tpercentage
+							} else {
+								pb.config.cpercentage -= increment;
+							}
+						}
+						else if (pb.config.cpercentage < pb.config.tpercentage) {
+							if (cpercentage + increment  > tpercentage) {
+								pb.config.cpercentage = tpercentage
+							} else {
+								pb.config.cpercentage += increment;
+							}
+						} 
+						else {
+							clearInterval(t);
+						}
+					}, pb.config.speed); 
+				});
+			};
+		}
+	});
+		
+	$.fn.extend({
+        progressBar: $.progressBar.construct
+	});
+	
+})(jQuery);
Binary file app/soc/content/images/progressbar.gif has changed
Binary file app/soc/content/images/progressbg_green.gif has changed
--- a/app/soc/templates/soc/org_app/review_overview.html	Sat Feb 14 20:26:27 2009 +0000
+++ b/app/soc/templates/soc/org_app/review_overview.html	Sat Feb 14 21:18:12 2009 +0000
@@ -12,7 +12,56 @@
 See the License for the specific language governing permissions and
 limitations under the License.
 {% endcomment %}
+{% block scripts %}
+<script type="text/javascript" src="/jquery/jquery-progressbar.js"></script>
+<script type="text/javascript">
+	$(document).ready(function() {
+		$("#applications_progress_bar").progressBar({showText: false});
+	});
+
+	function acceptOrgInit() {
+		$("#acceptance_text").fadeOut("slow",
+			function() {
+				$("#applications_progress_bar").fadeIn("slow");
+			}
+		);
+		$.getJSON("{{ bulk_accept_link|safe }}",
+			function(data){
+				setTimeout(function(){acceptOrg(data)}, 0);
+			}
+		);
+	}
+
+	function acceptOrg(accepted_applications) {
+		var application_index = 0, max_applications=accepted_applications.applications.length;
+		for (application_index; application_index<max_applications; application_index++) {
+			// regular expression to find a valid scope path inside matching parenthesis
+			var re = /\((\w*)\)/;
+			var scope_path = accepted_applications.link.match(re)[1];
+			// the URL is obtained by using the scope path found in the matching parenthesis
+			var url_to_call = accepted_applications.link.replace(re,eval("accepted_applications.applications[application_index]."+scope_path));
+			// now we can call the URL found
+			$.ajax({
+				async: false,
+				url: url_to_call,
+				timeout: 10000,
+				complete: function(data) {
+					if (data) {
+						// update progress bar percentage and description
+						var percentage = Math.floor(100 * (application_index+1) / (accepted_applications.nr_applications));
+						$("#description_progressbar").html(" Processed application "+accepted_applications.applications[application_index].name+" ("+(application_index+1)+"/"+accepted_applications.nr_applications+")");
+						$("#applications_progress_bar").progressBar(percentage);
+					}
+				}
+			});
+		}
+		// tell the user we are done
+		$("#description_done").html(" <strong>Done!</strong>");
+
+	}
+</script>
+{% endblock %}
 {% block body %}
-If you want to accept all pre-accepted organizations please click < here >.
+<span id="acceptance_text">If you want to accept all pre-accepted organizations please <button id="button_accept" onclick="javascript:acceptOrgInit();">click here</button></span><span class="progressBar" style="display:none;" id="applications_progress_bar"></span><span id="description_progressbar"></span><span id="description_done"></span>
 {{ block.super}}
-{% endblock %}
\ No newline at end of file
+{% endblock %}