[richfaces-svn-commits] JBoss Rich Faces SVN: r5572 - in trunk/sandbox/ui/progressBAR/src/main: java/org/richfaces/renderkit and 2 other directories.

richfaces-svn-commits at lists.jboss.org richfaces-svn-commits at lists.jboss.org
Wed Jan 23 14:37:34 EST 2008


Author: andrei_exadel
Date: 2008-01-23 14:37:34 -0500 (Wed, 23 Jan 2008)
New Revision: 5572

Modified:
   trunk/sandbox/ui/progressBAR/src/main/config/component/progressBar.xml
   trunk/sandbox/ui/progressBAR/src/main/java/org/richfaces/renderkit/AbstractProgressBarRenderer.java
   trunk/sandbox/ui/progressBAR/src/main/resources/org/richfaces/renderkit/html/js/progressBar.js
   trunk/sandbox/ui/progressBAR/src/main/templates/org/richfaces/progressBar.jspx
Log:


Modified: trunk/sandbox/ui/progressBAR/src/main/config/component/progressBar.xml
===================================================================
--- trunk/sandbox/ui/progressBAR/src/main/config/component/progressBar.xml	2008-01-23 17:07:24 UTC (rev 5571)
+++ trunk/sandbox/ui/progressBAR/src/main/config/component/progressBar.xml	2008-01-23 19:37:34 UTC (rev 5572)
@@ -35,6 +35,11 @@
 			<defaultvalue><![CDATA["ajax"]]></defaultvalue>
 		</property>
 		<property>
+			<name>parameters</name>
+			<classname>java.lang.Object</classname>
+			<description>Parameters for macrosubstitution in label</description>
+		</property>
+		<property>
 			<name>minValue</name>
 			<classname>java.lang.Object</classname>
 			<description>Min value when initial state should be rendered</description>
@@ -60,11 +65,13 @@
 			<name>completeClass</name>
 			<classname>java.lang.String</classname>
 			<description>CSS class that defines style for progress line rendering</description>
+			<defaultvalue>"rich-progress-bar-completed"</defaultvalue>
 		</property>
 		<property>
 			<name>remainClass</name>
 			<classname>java.lang.String</classname>
 			<description>CSS class that defines style for remained part of progress bar</description>
+			<defaultvalue>"rich-progress-bar-remained"</defaultvalue>
 		</property>
 		<property>
 			<name>initialClass</name>

Modified: trunk/sandbox/ui/progressBAR/src/main/java/org/richfaces/renderkit/AbstractProgressBarRenderer.java
===================================================================
--- trunk/sandbox/ui/progressBAR/src/main/java/org/richfaces/renderkit/AbstractProgressBarRenderer.java	2008-01-23 17:07:24 UTC (rev 5571)
+++ trunk/sandbox/ui/progressBAR/src/main/java/org/richfaces/renderkit/AbstractProgressBarRenderer.java	2008-01-23 19:37:34 UTC (rev 5572)
@@ -8,41 +8,157 @@
 
 import java.io.IOException;
 import java.math.BigDecimal;
+import java.util.HashMap;
 import java.util.Iterator;
 import java.util.Map;
 import java.util.Set;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
 
-import javax.el.ELContext;
-import javax.el.ValueExpression;
-import javax.el.VariableMapper;
+import javax.faces.FactoryFinder;
 import javax.faces.component.UIComponent;
 import javax.faces.context.FacesContext;
 import javax.faces.context.ResponseWriter;
+import javax.faces.render.RenderKit;
+import javax.faces.render.RenderKitFactory;
+import javax.servlet.http.HttpServletRequest;
 
 import org.ajax4jsf.context.AjaxContext;
+import org.ajax4jsf.context.AjaxContextImpl;
 import org.ajax4jsf.javascript.JSFunction;
 import org.ajax4jsf.javascript.JSFunctionDefinition;
+import org.ajax4jsf.javascript.JSReference;
+import org.ajax4jsf.javascript.ScriptUtils;
 import org.ajax4jsf.renderkit.AjaxCommandRendererBase;
 import org.ajax4jsf.renderkit.AjaxRendererUtils;
 import org.ajax4jsf.renderkit.ComponentVariables;
 import org.ajax4jsf.renderkit.ComponentsVariableResolver;
 import org.ajax4jsf.renderkit.RendererUtils.HTML;
+import org.ajax4jsf.resource.CountingOutputWriter;
 import org.richfaces.component.UIProgressBar;
 
-import sun.management.counter.Variability;
-
 /**
  * Abstract progress bar renderer
  * 
  * @author "Andrey Markavtsov"
  * 
  */
-public class AbstractProgressBarRenderer extends AjaxCommandRendererBase {
+public class AbstractProgressBarRenderer extends TemplateEncoderRendererBase {
 
-    /** AJAX polling function name */
-    private static final String AJAX_POLL_FUNCTION = "A4J.AJAX.Poll";
+    /** Pattern to find macrosubstitution parameters defined in attrs */
+    private static final Pattern PARAMETERS = Pattern
+	    .compile("^\\s*([\\w\\d]+)\\s*[=:]\\s*['\"]*([\\w\\d]+)['\"]*\\s*$");
 
+    /** Expression for params string split */
+    private static final String SPLIT_EXPRS = "[;,]";
+
+
+    /*
+     * (non-Javadoc)
+     * 
+     * @see org.ajax4jsf.renderkit.AjaxCommandRendererBase#doDecode(javax.faces.context.FacesContext,
+     *      javax.faces.component.UIComponent)
+     */
+    @Override
+    protected void doDecode(FacesContext facesContext, UIComponent uiComponent) {
+	AjaxContext ajaxContext = AjaxContextImpl
+		.getCurrentInstance(facesContext);
+
+	if (!ajaxContext.isAjaxRequest()) {
+	    return;
+	}
+
+	HttpServletRequest request = (HttpServletRequest) facesContext
+		.getExternalContext().getRequest();
+
+	ComponentVariables variables = ComponentsVariableResolver.getVariables(
+		this, uiComponent);
+	Number percent = getNumber(uiComponent.getAttributes().get("value"));
+	Number maxValue = getNumber(uiComponent.getAttributes().get("maxValue"));
+	Map params = request.getParameterMap();
+	if (percent.doubleValue() < maxValue.doubleValue()
+		&& params.containsKey("percent")) {
+	    ajaxContext.removeRenderedArea(uiComponent
+		    .getClientId(facesContext));
+	    ajaxContext.setResponseData(getResponseData(uiComponent, percent));
+	    Object rerenderAfterComplete = uiComponent.getAttributes().get(
+		    "reRender");
+	    Set ajaxRegions = AjaxRendererUtils.asSet(rerenderAfterComplete);
+
+	    if (ajaxRegions != null) {
+		for (Iterator iter = ajaxRegions.iterator(); iter.hasNext();) {
+		    String id = iter.next().toString();
+		    ajaxContext.addComponentToAjaxRender(uiComponent, id);
+		}
+	    }
+	} else {
+	    ajaxContext.addComponentToAjaxRender(uiComponent);
+	}
+    }
+
     /**
+     * Returns ajax response data
+     * @param uiComponent
+     * @param percent
+     * @return
+     */
+    private String getResponseData(UIComponent uiComponent, Number percent) {
+	UIProgressBar progressBar = (UIProgressBar) uiComponent;
+	StringBuffer buffer = new StringBuffer();
+	buffer.append("percent:");
+	buffer.append(percent);
+	buffer.append(",");
+	
+	buffer.append("data:");
+	buffer.append(percent);
+	buffer.append(",");
+
+	boolean enabled = (Boolean) uiComponent.getAttributes().get("enabled");
+	buffer.append("enabled:");
+	buffer.append(Boolean.toString(enabled));
+	buffer.append(",");
+
+	addStyles2Responce(buffer, "completeClass", (String) uiComponent
+		.getAttributes().get("completeClass"));
+	addStyles2Responce(buffer, "remainClass", (String) uiComponent
+		.getAttributes().get("remainClass"));
+	addStyles2Responce(buffer, "styleClass", (String) uiComponent
+		.getAttributes().get("styleClass"));
+	addStyles2Responce(buffer, "initialClass", (String) uiComponent
+		.getAttributes().get("initialClass"));
+	addStyles2Responce(buffer, "finishClass", (String) uiComponent
+		.getAttributes().get("finishClass"));
+
+	return buffer.toString();
+
+    }
+
+    /**
+     * Add component classes to ajax response
+     * @param buffer
+     * @param attr
+     * @param newValue
+     */
+    private void addStyles2Responce(StringBuffer buffer, String attr,
+	    String newValue) {
+	if (newValue != null) {
+	    buffer.append(attr);
+	    buffer.append(":");
+	    buffer.append(newValue);
+	    buffer.append(",");
+	}
+    }
+    
+    /**
+     * Return poll function name 
+     * @param component
+     * @return
+     */
+    public String getVarName(UIComponent component) {
+	return "p" + Math.abs(component.hashCode());
+    }
+
+    /**
      * Methods encodes AJAX script for polling
      * 
      * @param context -
@@ -62,19 +178,27 @@
 	writer.writeAttribute(HTML.style_ATTRIBUTE, "display:none;", null);
 	writer.startElement(HTML.SCRIPT_ELEM, component);
 	writer.writeAttribute(HTML.TYPE_ATTR, "text/javascript", null);
+	String varName = getVarName(component);
+	StringBuffer pollScript = new StringBuffer("\n");
 	StringBuffer script = new StringBuffer("\n");
-	if (progressBar.isEnabled()) {
-	    JSFunction function = AjaxRendererUtils.buildAjaxFunction( // encode
-		    // script
-		    // if
-		    // polling
-		    // enable
-		    component, context, AJAX_POLL_FUNCTION);
+	if (isAjaxMode(component)) {
+
+	    pollScript.append(getStopPollScript(clientId));
+
+	    pollScript.append("var ");
+	    pollScript.append(varName);
+	    pollScript.append("= function () { \n");
+
+	    JSFunction function = AjaxRendererUtils.buildAjaxFunction(
+		    component, context, AjaxRendererUtils.AJAX_FUNCTION_NAME);
+	    function.addParameter(new JSReference("null"));
 	    Map options = AjaxRendererUtils.buildEventOptions(context,
 		    component);
 	    Integer interval = new Integer(progressBar.getInterval());
-	    options.put("pollinterval", interval);
-	    options.put("pollId", clientId);
+	    Map parameters = (Map) options.get("parameters");
+	    parameters.put("percent", "percent");
+	    options.put("oncomplete", getOnComplete(clientId, progressBar,
+		    context, writer, options));
 	    Object onsubmit = component.getAttributes().get("onsubmit");
 	    if (null != onsubmit) {
 		JSFunctionDefinition onsubmitFunction = new JSFunctionDefinition();
@@ -84,16 +208,72 @@
 
 	    function.addParameter(options);
 	    function.appendScript(script);
+
+	    pollScript.append(script);
+	    pollScript.append("\n A4J.AJAX._pollers['");
+	    pollScript.append(clientId);
+	    pollScript.append("'] = window.setTimeout(");
+	    pollScript.append(varName);
+	    pollScript.append(",");
+	    pollScript.append(progressBar.getInterval());
+	    pollScript.append(");\n");
+	    pollScript.append("}\n");
+	    if (progressBar.isEnabled()) {
+		pollScript.append(varName);
+		pollScript.append("();");
+	    }
+
 	} else {
-	    script.append("A4J.AJAX.StopPoll('").append(
-		    component.getClientId(context)).append("')");
+	    pollScript.append(getStopPollScript(clientId));
 	}
-	script.append(";\n");
-	writer.writeText(script.toString(), null);
+	pollScript.append(";\n");
+	writer.writeText(pollScript.toString(), null);
 	writer.endElement(HTML.SCRIPT_ELEM);
 	writer.endElement(HTML.SPAN_ELEM);
     }
+    
+    /**
+     * Check if component mode is AJAX
+     * @param component
+     * @return
+     */
+    private boolean isAjaxMode (UIComponent component) {
+	String mode = (String)component.getAttributes().get("mode");
+	return "ajax".equalsIgnoreCase(mode);
+    }
+    /**
+     * Creates oncomplete function
+     * @param clientId
+     * @param component
+     * @param context
+     * @param writer
+     * @param options
+     * @return
+     */
+    public JSFunctionDefinition getOnComplete(String clientId,
+	    UIProgressBar component, FacesContext context,
+	    ResponseWriter writer, Map options) {
+	JSFunctionDefinition functionDefinition = new JSFunctionDefinition();
+	functionDefinition.addParameter("request");
+	functionDefinition.addParameter("event");
+	functionDefinition.addParameter("data");
+	StringBuffer body = new StringBuffer();
+	ComponentVariables variables = ComponentsVariableResolver.getVariables(
+		this, component);
+	UIComponent nestingContainer = (UIComponent) AjaxRendererUtils
+		.findAjaxContainer(context, component);
+	UIComponent form = (UIComponent) AjaxRendererUtils
+		.getNestingForm(component);
+	String content = (String) variables.getVariable("content");
+	body.append("new ProgressBar('" + clientId + "').onComplete(data,'")
+		.append(content).append("',").append(
+			ScriptUtils.toScript(getParametersMap(component)))
+		.append(")");
+	functionDefinition.addToBody(body.toString());
 
+	return functionDefinition;
+    }
+
     /**
      * Methods encodes start facet of progress bar component
      * 
@@ -165,54 +345,54 @@
 	ComponentVariables variables = ComponentsVariableResolver.getVariables(
 		this, component);
 	Number value = (Number) variables.getVariable("percent");
+
 	String width = String.valueOf(value.intValue());
 	String style = (String) component.getAttributes().get("style");
 	Boolean permanent = (Boolean) component.getAttributes()
 		.get("permanent");
 	Boolean determined = (Boolean) component.getAttributes().get(
 		"determined");
-
+	String content = getLabel(context, component, writer);
 	if (!(permanent != null && permanent.booleanValue())) {
 
-	    String completeClass = (component.getAttributes().get(
-		    "completeClass") != null) ? (String) component
-		    .getAttributes().get("completeClass")
-		    : "rich-progress-bar-completed";
+	    String completeClass = (String) component.getAttributes().get(
+		    "completeClass");
 
-	    String remainClass = (component.getAttributes().get("remainClass") != null) ? (String) component
-		    .getAttributes().get("remainClass")
-		    : "rich-progress-bar-remained";
+	    String remainClass = (String) component.getAttributes().get(
+		    "remainClass");
 
 	    writer.startElement("div", component);
 	    getUtils().writeAttribute(writer, "class",
 		    remainClass + " rich-progress-bar-base");
 	    getUtils().writeAttribute(writer, "id", clientId + ":remain");
 	    getUtils().writeAttribute(writer, "style", style);
+	    
 
-	    if (determined.booleanValue()) {
-		renderChildren(context, component);
-	    }
+	    encodeChildren(context, component, clientId + ":remain");
 
+	    
 	    writer.endElement("div");
 	    writer.startElement("div", component);
 	    getUtils().writeAttribute(writer, "class",
 		    "rich-progress-bar-uploaded");
 	    getUtils().writeAttribute(writer, "id", clientId + ":upload");
-	    getUtils().writeAttribute(writer, "style",
-		    style + "; width: " + width + "%;");
+	    getUtils().writeAttribute(
+		    writer,
+		    "style",
+		    (style != null ? style + ";" : "") + " width: " + width
+			    + "%;");
 
 	    writer.startElement("div", component);
 	    getUtils().writeAttribute(writer, "class",
 		    completeClass + " rich-progress-bar-base");
 	    getUtils().writeAttribute(writer, "id", clientId + ":complete");
 	    getUtils().writeAttribute(writer, "style", style);
+	    
+	    encodeChildren(context, component, clientId + ":complete");
 
-	    if (determined.booleanValue()) {
-		renderChildren(context, component);
-	    }
+	    writer.endElement("div");
 
 	    writer.endElement("div");
-	    writer.endElement("div");
 	} else {
 	    writer.startElement("div", component);
 	    getUtils().writeAttribute(writer, "class",
@@ -222,9 +402,89 @@
 	}
     }
 
-    private void encodeChildrenComponents(FacesContext context,
+    /**
+     * Renderers children label components
+     * @param context
+     * @param component
+     * @param writer
+     * @return
+     */
+    public String getLabel(FacesContext context, UIComponent component,
+	    ResponseWriter writer) {
+	String childrenContent = null;
+	CountingOutputWriter customWriter = new CountingOutputWriter();
+	String defaultRenderKitId = context.getApplication()
+		.getDefaultRenderKitId();
+	if (null == defaultRenderKitId) {
+	    defaultRenderKitId = RenderKitFactory.HTML_BASIC_RENDER_KIT;
+	}
+	RenderKitFactory renderKitFactory = (RenderKitFactory) FactoryFinder
+		.getFactory(FactoryFinder.RENDER_KIT_FACTORY);
+	RenderKit renderKit = renderKitFactory.getRenderKit(context,
+		defaultRenderKitId);
+	try {
+
+	    ResponseWriter responseWriter = renderKit.createResponseWriter(
+		    customWriter, null, "UTF-8");
+	    context.setResponseWriter(responseWriter);
+	    renderChildrenComponents(context, component);
+	    childrenContent = customWriter.getContent().toString();
+	    context.setResponseWriter(writer);
+	} catch (Exception e) {
+	    // TODO: do nothing
+	} finally {
+	    context.setResponseWriter(writer);
+	}
+	return childrenContent;
+    }
+
+    /**
+     * Gets map with parameters defined in 'parameters' attr
+     * @param component
+     * @return
+     */
+    private Map<String, String> getParametersMap(UIComponent component) {
+	String parameters = (String) component.getAttributes()
+		.get("parameters");
+	if (parameters != null) {
+	    Map<String, String> map = new HashMap<String, String>();
+	    String[] strs = parameters.split(SPLIT_EXPRS);
+	    if (strs != null) {
+		for (String str : strs) {
+		    Matcher matcher = PARAMETERS.matcher(str);
+		    if (matcher.matches()) {
+			map.put(matcher.group(1), matcher.group(2));
+		    }
+		}
+	    }
+	    return map;
+	}
+
+	return null;
+    }
+
+    /**
+     * Return parameters splitted by ',' to be correctly parsed in JS
+     * @param context
+     * @param component
+     * @return
+     */
+    public String getParametersString(FacesContext context,
 	    UIComponent component) {
+	String parameters = (String) component.getAttributes()
+		.get("parameters");
+	StringBuffer buffer = new StringBuffer();
+	if (parameters != null) {
+	    String[] r = parameters.split(SPLIT_EXPRS);
 
+	    for (String s : r) {
+		buffer.append(s);
+		buffer.append(",");
+	    }
+	    buffer.deleteCharAt(buffer.length() - 1);
+	    return buffer.toString();
+	}
+	return "";
     }
 
     /**
@@ -270,9 +530,29 @@
 	if (completed != null) {
 	    renderChild(context, completed);
 	}
+	if (isAjax) {
+	    writer.startElement(HTML.SPAN_ELEM, component);
+	    writer.startElement(HTML.SCRIPT_ELEM, component);
+	    writer.write(getStopPollScript(clientId).toString());
+	    writer.endElement(HTML.SCRIPT_ELEM);
+	    writer.endElement(HTML.SPAN_ELEM);
+	}
 	writer.endElement(HTML.DIV_ELEM);
 
     }
+    
+    
+    public void encodeChildren(FacesContext context, UIComponent component, String id)
+	throws IOException {
+		ResponseWriter writer = context.getResponseWriter();
+		writer.startElement("script", component);
+		writer.write("var evaluator = ");
+		writeScriptBody(context, component, true);
+		//markup.invoke('getContent', context).join('');
+		writer.write(";\n $('"+id+"').innerHTML = evaluator.invoke('getContent', {'minValue': '0'}).join('');");
+		writer.endElement("script");
+    }
+	
 
     /**
      * Converts value attr to number value
@@ -303,6 +583,17 @@
 	return result;
     }
 
+    /**
+     * Returns JS script to stop polling
+     * @param clientId
+     * @return
+     */
+    private StringBuffer getStopPollScript(String clientId) {
+	StringBuffer buffer = new StringBuffer();
+	buffer.append("A4J.AJAX.StopPoll('").append(clientId).append("');\n");
+	return buffer;
+    }
+
     /*
      * (non-Javadoc)
      * 
@@ -313,39 +604,17 @@
 	return UIProgressBar.class;
     }
 
-    /*
-     * (non-Javadoc)
-     * 
-     * @see org.ajax4jsf.renderkit.AjaxCommandRendererBase#isSubmitted(javax.faces.context.FacesContext,
-     *      javax.faces.component.UIComponent)
-     */
-    protected boolean isSubmitted(FacesContext facesContext,
-	    UIComponent uiComponent) {
-	boolean submitted = super.isSubmitted(facesContext, uiComponent);
-	UIProgressBar poll = (UIProgressBar) uiComponent;
-	poll.setSubmitted(submitted);
-	return submitted;
-    }
 
-    /*
-     * (non-Javadoc)
+    /**
+     * Renders children components
      * 
-     * @see javax.faces.render.Renderer#getRendersChildren()
+     * @param facesContext
+     * @param component
+     * @throws IOException
      */
-    @Override
-    public boolean getRendersChildren() {
-	return true;
+    private void renderChildrenComponents(FacesContext facesContext,
+	    UIComponent component) throws IOException {
+	super.renderChildren(facesContext, component);
     }
 
-    /*
-     * (non-Javadoc)
-     * 
-     * @see org.ajax4jsf.renderkit.AjaxCommandRendererBase#encodeChildren(javax.faces.context.FacesContext,
-     *      javax.faces.component.UIComponent)
-     */
-    @Override
-    public void encodeChildren(FacesContext context, UIComponent component)
-	    throws IOException {
-	return; // We don't need to render children !
-    }
 }

Modified: trunk/sandbox/ui/progressBAR/src/main/resources/org/richfaces/renderkit/html/js/progressBar.js
===================================================================
--- trunk/sandbox/ui/progressBAR/src/main/resources/org/richfaces/renderkit/html/js/progressBar.js	2008-01-23 17:07:24 UTC (rev 5571)
+++ trunk/sandbox/ui/progressBAR/src/main/resources/org/richfaces/renderkit/html/js/progressBar.js	2008-01-23 19:37:34 UTC (rev 5572)
@@ -1,52 +1,167 @@
 ProgressBar = {};
-
 ProgressBar = Class.create();
 
 Object.extend(ProgressBar.prototype, {
     initialize: function(id) {
     	this.id = id;
+    	this.maxValue = $(id + ":max").value;
+    	this.minValue = $(id + ":min").value;
     	this.mode = "initial";
     },
-	getProgress: function () {
-		var d = $(this.id + ":remain");
+	getValue: function () {
+		var d = $(this.id + ":upload");
 		if (d != null) {
-			var v = d.innerHTML; 
-			if (v != null) {
-				return v.replace("%","");
+			var w = d.style.width; 
+			if (w) {
+				return w.substring(0,w.length - 1);		
 			}
 		}
-		else return null;
+	return null;
 	},
-	setProgress: function (val) {
+	onComplete: function (data, str, context) {
+		if (data) {
+			this.parseResponse(data);
+			context['value'] = this.value;
+			context['minValue'] = this.minValue;
+			context['maxValue'] = this.maxValue;
+
+			this.mode = "progress";
+			this.setValue(this.value);
+	
+			this.context = context;
+			//this.setLabel(str);
+			
+			//A4J.AJAX.Poll(containerId, formId, options);
+			/*if (this.value >= this.maxValue) {
+				this.disable();
+			}*/
+		}
+		
+	},
+	interpolate: function (placeholders) {
+		for(var k in this.context) {
+			var v = this.context[k];
+			var regexp = new RegExp("\\{" + k + "\\}", "g");
+			placeholders = placeholders.replace(regexp, v);
+		}
+		return placeholders;
+	},
+	parseResponse: function (data) {
+		var params = data.split(",");
+		for (var i = 0; i < params.length; i++) {
+				var param = params[i];
+				if (param) {
+					var t = param.split(":");
+					 	if (t)
+					 		if (t.length == 2) {
+					 			var k = t[0];
+					 			var v = t[1];
+		 						this.updateComponent(k,v);
+					 		}
+				} 										
+			}
+	},
+	updateComponent: function (k,v) {
+		switch (k) {
+			case "percent" : this.value = v; break;
+			case "enabled" : if (v == "false") { this.disable(); }; break;  
+			case "completeClass" : this.updateClassName($(this.id + ":complete"), v, "rich-progress-bar-base"); break;
+			case "remainClass" : this.updateClassName($(this.id + ":remain"), v, "rich-progress-bar-base");  break;
+			case "styleClass" : this.updateClassName($(this.id), v, "rich-progress-bar"); break;
+			case "initialClass" : break;
+			case "finishClass" : break;
+		}
+	},
+	updateClassName: function (o, newName, defaultClass) {
+		if (o.className) {
+			if (o.className.indexOf(newName) < 0){
+				o.className = newName + " "+ defaultClass;
+			}
+		}
+	},
+	setLabel: function (str) {
+		if (!this.context) { this.context = this.parseParams();}
+		str = this.interpolate(str);	
+		var d = $(this.id + ":remain");
+		if (d != null) d.innerHTML = str;
+		d = $(this.id + ":complete");
+		if (d != null) d.innerHTML = str;
+	},
+	parseParams: function () {
+		var params = $(this.id + ":parameters").value;
+		if (params != "") {
+			var map = new Array();
+			var p = params.split(",");
+			for (var i = 0; i < p.length; i++) {
+				var k = p[i].match("\\s*([\\w\\d]+)\\s*[:=]+");
+					if (k) {
+						k = k[1];
+					}
+				var v = p[i].match(".*[:=]+\\s*['\"]*([\\w\\d]+)['\"]*\\s*");
+					if (v) {
+						v = v[1];
+					}
+					if (k && v) {
+						map[k] = v;
+					}
+			}
+			map['value'] = this.getValue();
+			map['minValue'] = this.minValue;
+			map['maxValue'] = this.maxValue;
+			return map;
+		}
+	return null;
+	},
+	isAjaxMode: function () {
+		return ($(this.id + ":progressState") == null);
+	}, 
+	setValue: function (val) {
 		var p = val;
 		if (val != null) {
 			if (val.indexOf("%") < 0)
 				val = val + "%";
 		}
-		var max = $(this.id + ":max").value;
-		if ( parseFloat(p) >=  parseFloat(max))  { this.switchMode("completed"); return; }
+
+		if ( parseFloat(p) >=  parseFloat(this.maxValue))  {
+			if (!this.isAjaxMode()) { 
+				this.switchMode("completed"); return;
+			} 
+		}
 		
-		if ($(this.id + ":progressState") != null)
-		{
-			if (this.mode == "progress")
-				{
-				var d = $(this.id + ":remain");
-				if (d != null) d.innerHTML = val;
-				d = $(this.id + ":complete");
-				if (d != null) d.innerHTML = val;
-				d = $(this.id + ":upload");
-				if (d != null) d.style.width = val;
-				}
-			}	
+		if (this.mode == "progress" || this.isAjaxMode()) {
+			d = $(this.id + ":upload");
+			if (d != null) d.style.width = val;
+		}
+	
 	},
-	enable: function () {
-		this.switchMode("progress");
-		this.setProgress("0");
+	enable: function (ev) {
+		if (!this.isAjaxMode()) {
+			this.switchMode("progress");
+			this.setValue("0");
+		}else if (!A4J.AJAX._pollers[this.id]) {
+			var funcName = $(this.id + ":func").value;
+			var f = new Function(funcName + "();");
+			f();			
+		}
 	},
+	disable: function () {
+		A4J.AJAX.StopPoll(this.id);
+	},
 	finish: function () {
-		A4J.AJAX.StopPoll(this.id);
 		this.switchMode("completed");
 	},
+	getForm: function () {
+		var p = $(this.id).parentNode;
+		while (p) {
+		if (p) {
+			if (p.tagName.toUpperCase() == "FORM") {
+				return p;
+			}
+		  }
+		  p = p.parentNode;
+		}
+		return null;
+	}, 
 	switchMode : function (mode) {
 		switch (mode) {
 			case "initial" : 

Modified: trunk/sandbox/ui/progressBAR/src/main/templates/org/richfaces/progressBar.jspx
===================================================================
--- trunk/sandbox/ui/progressBAR/src/main/templates/org/richfaces/progressBar.jspx	2008-01-23 17:07:24 UTC (rev 5571)
+++ trunk/sandbox/ui/progressBAR/src/main/templates/org/richfaces/progressBar.jspx	2008-01-23 19:37:34 UTC (rev 5572)
@@ -10,7 +10,9 @@
 	<h:scripts>
 			new org.ajax4jsf.javascript.PrototypeScript(),
 			new org.ajax4jsf.javascript.AjaxScript(),
+			/org/richfaces/renderkit/html/scripts/utils.js,
 			/org/richfaces/renderkit/html/js/progressBar.js,
+		
 </h:scripts>
 
 	<f:clientId var="clientId" />
@@ -25,7 +27,9 @@
     Number maxValue = getNumber(component.getAttributes().get("maxValue"));
     boolean isAjax = ("ajax".equalsIgnoreCase(mode));
     Number value = getNumber(component.getAttributes().get("value"));
+    String content = getLabel(context, component, writer);
     variables.setVariable("percent",value);
+    variables.setVariable("content",content);
     variables.setVariable("style",(String)component.getAttributes().get("style"));
     variables.setVariable("styleClass",(String)component.getAttributes().get("styleClass"));
 
@@ -45,8 +49,12 @@
  ]]>
 </jsp:scriptlet>
 	<div class="rich-progress-bar #{styleClass}" style="#{style}" id="#{clientId}">
-		<f:call name="encodeProgressState"/> 
-		<f:call name="encodePollScript" />
+		<f:call name="encodeProgressState"/>
+	<input type="hidden" id="#{clientId}:min" disabled="true" value="#{component.attributes['minValue']}" /> 
+	<input type="hidden" id="#{clientId}:max" disabled="true" value="#{component.attributes['maxValue']}" />
+	<input type="hidden" id="#{clientId}:parameters" disabled="true" value="#{this:getParametersString(context, component)}" />
+	<input type="hidden" id="#{clientId}:func" disabled="true" value="#{this:getVarName(component)}" />
+	<f:call name="encodePollScript" />
 	</div>
 <jsp:scriptlet>
 <![CDATA[
@@ -64,11 +72,11 @@
 	<f:call name="encodeCompletedState">
 		<f:parameter value="false"/>
 	</f:call>
-	<input type="hidden" id="#{clientId}:min" value="#{component.attributes['minValue']}" /> 
-	<input type="hidden" id="#{clientId}:max" value="#{component.attributes['maxValue']}" />
+	<input type="hidden" id="#{clientId}:min" disabled="true" value="#{component.attributes['minValue']}" /> 
+	<input type="hidden" id="#{clientId}:max" disabled="true" value="#{component.attributes['maxValue']}" />
+	<input type="hidden" id="#{clientId}:parameters" disabled="true" value="#{this:getParametersString(context, component)}" />
 	<script>
-		var pr = new ProgressBar('#{clientId}');
-		pr.init();
+		new ProgressBar('#{clientId}').init();
 	</script>
 </div>
 <jsp:scriptlet>




More information about the richfaces-svn-commits mailing list