[richfaces-svn-commits] JBoss Rich Faces SVN: r5590 - in trunk/sandbox/ui/progressBAR/src/main: resources/org/richfaces/renderkit/html/js and 1 other directories.

richfaces-svn-commits at lists.jboss.org richfaces-svn-commits at lists.jboss.org
Thu Jan 24 10:04:28 EST 2008


Author: andrei_exadel
Date: 2008-01-24 10:04:27 -0500 (Thu, 24 Jan 2008)
New Revision: 5590

Modified:
   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:
Component refactoring

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-24 14:57:55 UTC (rev 5589)
+++ trunk/sandbox/ui/progressBAR/src/main/java/org/richfaces/renderkit/AbstractProgressBarRenderer.java	2008-01-24 15:04:27 UTC (rev 5590)
@@ -15,26 +15,20 @@
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
-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;
 
 /**
@@ -52,7 +46,10 @@
     /** Expression for params string split */
     private static final String SPLIT_EXPRS = "[;,]";
 
+    private static final String FORCE_STATE_PARAM = "forceState";
 
+    private static final String AJAX_POLL_FUNCTION = "A4J.AJAX.Poll";
+
     /*
      * (non-Javadoc)
      * 
@@ -71,12 +68,17 @@
 	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()
+	if (params.containsKey(FORCE_STATE_PARAM)) {
+	    ajaxContext.addComponentToAjaxRender(uiComponent);
+	    String [] str = (String [])params.get(FORCE_STATE_PARAM);
+	    for (String s : str) {
+		uiComponent.getAttributes().put(FORCE_STATE_PARAM,s);
+	    }
+
+	} else if (percent.doubleValue() < maxValue.doubleValue()
 		&& params.containsKey("percent")) {
 	    ajaxContext.removeRenderedArea(uiComponent
 		    .getClientId(facesContext));
@@ -97,7 +99,39 @@
     }
 
     /**
+     * TODO Description goes here.
+     * @param state
+     * @param context
+     * @param component
+     * @throws IOException
+     */
+    public void renderForcedState(String state, FacesContext context,
+	    UIComponent component) throws IOException {
+	if ("initial".equals(state)) {
+	    encodeInitialState(context, component);
+	} else if ("progress".equals(state)) {
+	    encodeProgressState(context, component);
+	} else if ("complete".equals(state)) {
+	    encodeCompletedState(context, component);
+	}
+	component.getAttributes().remove(FORCE_STATE_PARAM);
+    }
+
+    /**
+     * TODO Description goes here.
+     * @param component
+     * @return
+     */
+    public String getForcedState(UIComponent component) {
+	if (component.getAttributes().containsKey(FORCE_STATE_PARAM)) {
+	    return (String) component.getAttributes().get(FORCE_STATE_PARAM);
+	}
+	return null;
+    }
+
+    /**
      * Returns ajax response data
+     * 
      * @param uiComponent
      * @param percent
      * @return
@@ -108,7 +142,7 @@
 	buffer.append("percent:");
 	buffer.append(percent);
 	buffer.append(",");
-	
+
 	buffer.append("data:");
 	buffer.append(percent);
 	buffer.append(",");
@@ -135,6 +169,7 @@
 
     /**
      * Add component classes to ajax response
+     * 
      * @param buffer
      * @param attr
      * @param newValue
@@ -148,14 +183,15 @@
 	    buffer.append(",");
 	}
     }
-    
+
     /**
-     * Return poll function name 
+     * Return poll function name
+     * 
      * @param component
      * @return
      */
     public String getVarName(UIComponent component) {
-	return "p" + Math.abs(component.hashCode());
+	return String.valueOf(Math.abs(component.hashCode()));
     }
 
     /**
@@ -178,27 +214,24 @@
 	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 (isAjaxMode(component)) {
+	if (isAjaxMode(component) && progressBar.isEnabled()) {
+	    JSFunction function = AjaxRendererUtils.buildAjaxFunction(
+		    component, context, AJAX_POLL_FUNCTION);
 
-	    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("pollId", clientId);
+	    options.put("pollinterval", interval);
 	    Map parameters = (Map) options.get("parameters");
 	    parameters.put("percent", "percent");
 	    options.put("oncomplete", getOnComplete(clientId, progressBar,
 		    context, writer, options));
+	    pollScript.append("ProgressBar.Pollers['" + clientId
+		    + "']['options'] = " + ScriptUtils.toScript(options)
+		    + ";\n");
 	    Object onsubmit = component.getAttributes().get("onsubmit");
 	    if (null != onsubmit) {
 		JSFunctionDefinition onsubmitFunction = new JSFunctionDefinition();
@@ -210,19 +243,6 @@
 	    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 {
 	    pollScript.append(getStopPollScript(clientId));
 	}
@@ -231,18 +251,87 @@
 	writer.endElement(HTML.SCRIPT_ELEM);
 	writer.endElement(HTML.SPAN_ELEM);
     }
-    
+
     /**
+     * TODO Description goes here.
+     * @param context
+     * @param component
+     * @throws IOException
+     */
+    public void encodeLabelScript(FacesContext context, UIComponent component)
+	    throws IOException {
+	ResponseWriter writer = context.getResponseWriter();
+	StringBuffer script = new StringBuffer();
+	writer.startElement(HTML.SPAN_ELEM, component);
+	writer.startElement(HTML.SCRIPT_ELEM, component);
+	script.append("new ProgressBar('" + component.getClientId(context)
+		+ "').renderLabel();\n");
+	writer.append(script.toString());
+	writer.endElement(HTML.SCRIPT_ELEM);
+	writer.endElement(HTML.SPAN_ELEM);
+
+    }
+
+    /**
+     * TODO Description goes here.
+     * @param context
+     * @param component
+     * @throws IOException
+     */
+    public void encodeInitialScript(FacesContext context, UIComponent component)
+	    throws IOException {
+	ResponseWriter writer = context.getResponseWriter();
+	
+	writer.startElement(HTML.SPAN_ELEM, component);
+	writer.startElement(HTML.SCRIPT_ELEM, component);
+
+	StringBuffer script = new StringBuffer();
+	String clientId = component.getClientId(context);
+	String containerId = ((UIComponent)AjaxRendererUtils.findAjaxContainer(context, component)).getClientId(context);
+	String formId = ((UIComponent)AjaxRendererUtils.getNestingForm(component)).getClientId(context);
+	String mode = (String)component.getAttributes().get("mode");
+	Integer minValue = (Integer)component.getAttributes().get("minValue");
+	Integer maxValue = (Integer)component.getAttributes().get("maxValue");
+	
+	script.append("new ProgressBar().init('")
+	.append(clientId)
+	.append("','")
+	.append(containerId)
+	.append("','")
+	.append(formId)
+	.append("','")
+	.append(mode)	
+	.append("',")
+	.append(minValue)
+	.append(",")
+	.append(maxValue)
+	.append(",");
+	writer.write(script.toString());
+	writeScriptBody(context, component, true);
+	script = new StringBuffer();
+	script.append(",");
+	script.append(ScriptUtils.toScript(getParametersMap(component)));
+	script.append(");\n");
+	writer.write(script.toString());
+	
+	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");
+    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
@@ -265,10 +354,7 @@
 	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(")");
+	body.append("new ProgressBar('" + clientId + "').onComplete(data);");
 	functionDefinition.addToBody(body.toString());
 
 	return functionDefinition;
@@ -284,9 +370,8 @@
      * @throws IOException -
      *                 IOException
      */
-    public void encodeInitialState(FacesContext context, UIComponent component,
-	    Object data) throws IOException {
-	Boolean isAjax = Boolean.parseBoolean((String) data);
+    public void encodeInitialState(FacesContext context, UIComponent component)
+	    throws IOException {
 	String clientId = component.getClientId(context);
 	String initialClass = (String) component.getAttributes().get(
 		"initialClass");
@@ -295,14 +380,12 @@
 	writer.startElement(HTML.DIV_ELEM, component);
 	getUtils().writeAttribute(writer, HTML.class_ATTRIBUTE, initialClass);
 	getUtils().writeAttribute(writer, "style", style);
-	writer.writeAttribute(HTML.id_ATTRIBUTE, (isAjax ? clientId : clientId
-		+ ":initialState"), null);
-
+	writer.writeAttribute(HTML.id_ATTRIBUTE, clientId, null);
 	UIComponent initial = component.getFacet("initial");
 	if (initial != null) {
 	    renderChild(context, initial);
 	}
-	if (isAjax) {
+	if (isAjaxMode(component)) {
 	    encodePollScript(context, component);
 	}
 	writer.endElement(HTML.DIV_ELEM);
@@ -330,6 +413,33 @@
     }
 
     /**
+     * TODO Description goes here.
+     * @param context
+     * @param component
+     * @throws IOException
+     */
+    public void encodeProgressState(FacesContext context, UIComponent component)
+	    throws IOException {
+	ResponseWriter writer = context.getResponseWriter();
+	ComponentVariables variables = ComponentsVariableResolver.getVariables(
+		this, component);
+	String clientId = component.getClientId(context);
+	writer.startElement("div", component);
+	getUtils().writeAttribute(
+		writer,
+		"class",
+		"rich-progress-bar "
+			+ (String) variables.getVariable("styleClass"));
+	getUtils().writeAttribute(writer, "id", clientId);
+	getUtils().writeAttribute(writer, "style",
+		variables.getVariable("style"));
+	encodeInitialScript(context, component);
+	encodeProgressBar(context, component);
+	encodeLabelScript(context, component);
+	writer.endElement("div");
+    }
+
+    /**
      * Encodes progress state of component
      * 
      * @param context -
@@ -338,7 +448,7 @@
      *                component instance
      * @throws IOException
      */
-    public void encodeProgressState(FacesContext context, UIComponent component)
+    public void encodeProgressBar(FacesContext context, UIComponent component)
 	    throws IOException {
 	ResponseWriter writer = context.getResponseWriter();
 	String clientId = component.getClientId(context);
@@ -350,9 +460,6 @@
 	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 = (String) component.getAttributes().get(
@@ -366,11 +473,7 @@
 		    remainClass + " rich-progress-bar-base");
 	    getUtils().writeAttribute(writer, "id", clientId + ":remain");
 	    getUtils().writeAttribute(writer, "style", style);
-	    
 
-	    encodeChildren(context, component, clientId + ":remain");
-
-	    
 	    writer.endElement("div");
 	    writer.startElement("div", component);
 	    getUtils().writeAttribute(writer, "class",
@@ -387,59 +490,29 @@
 		    completeClass + " rich-progress-bar-base");
 	    getUtils().writeAttribute(writer, "id", clientId + ":complete");
 	    getUtils().writeAttribute(writer, "style", style);
-	    
-	    encodeChildren(context, component, clientId + ":complete");
 
 	    writer.endElement("div");
 
 	    writer.endElement("div");
+
+	    if (isAjaxMode(component)) {
+		encodePollScript(context, component);
+	    }
 	} else {
 	    writer.startElement("div", component);
 	    getUtils().writeAttribute(writer, "class",
 		    "rich-progress-bar-permanent");
 	    getUtils().writeAttribute(writer, "style", style + " width: 100%");
 	    writer.endElement("div");
+	    if (isAjaxMode(component)) {
+		encodePollScript(context, component);
+	    }
 	}
     }
 
     /**
-     * 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
      */
@@ -464,30 +537,6 @@
     }
 
     /**
-     * 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 "";
-    }
-
-    /**
      * Methods encodes finish facet of progress bar component
      * 
      * @param context -
@@ -497,9 +546,9 @@
      * @throws IOException -
      *                 IOException
      */
-    public void encodeCompletedState(FacesContext context,
-	    UIComponent component, Object data) throws IOException {
-	Boolean isAjax = Boolean.parseBoolean((String) data);
+    public void encodeCompletedState(FacesContext context, UIComponent component)
+	    throws IOException {
+	boolean isAjax = isAjaxMode(component);
 	if (isAjax) {
 	    Object rerenderAfterComplete = component.getAttributes().get(
 		    "reRenderAfterComplete");
@@ -514,9 +563,7 @@
 		}
 	    }
 	}
-	String clientId = (isAjax) ? component.getClientId(context) : component
-		.getClientId(context)
-		+ ":completedState";
+	String clientId = component.getClientId(context);
 	String finishClass = (String) component.getAttributes().get(
 		"finishClass");
 	String style = (String) component.getAttributes().get("style");
@@ -540,19 +587,6 @@
 	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
@@ -585,6 +619,7 @@
 
     /**
      * Returns JS script to stop polling
+     * 
      * @param clientId
      * @return
      */
@@ -604,17 +639,28 @@
 	return UIProgressBar.class;
     }
 
+    /*
+     * (non-Javadoc)
+     * 
+     * @see org.richfaces.renderkit.TemplateEncoderRendererBase#encodeChildren(javax.faces.context.FacesContext,
+     *      javax.faces.component.UIComponent)
+     */
+    @Override
+    public void encodeChildren(FacesContext context, UIComponent component)
+	    throws IOException {
+	;
+    }
 
-    /**
-     * Renders children components
+    /*
+     * (non-Javadoc)
      * 
-     * @param facesContext
-     * @param component
-     * @throws IOException
+     * @see org.ajax4jsf.renderkit.RendererBase#doEncodeChildren(javax.faces.context.ResponseWriter,
+     *      javax.faces.context.FacesContext, javax.faces.component.UIComponent)
      */
-    private void renderChildrenComponents(FacesContext facesContext,
+    @Override
+    public void doEncodeChildren(ResponseWriter writer, FacesContext context,
 	    UIComponent component) throws IOException {
-	super.renderChildren(facesContext, component);
+	;
     }
 
 }

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-24 14:57:55 UTC (rev 5589)
+++ trunk/sandbox/ui/progressBAR/src/main/resources/org/richfaces/renderkit/html/js/progressBar.js	2008-01-24 15:04:27 UTC (rev 5590)
@@ -1,13 +1,25 @@
 ProgressBar = {};
 ProgressBar = Class.create();
-
+ProgressBar.Pollers = {}
 Object.extend(ProgressBar.prototype, {
-    initialize: function(id) {
-    	this.id = id;
-    	this.maxValue = $(id + ":max").value;
-    	this.minValue = $(id + ":min").value;
-    	this.mode = "initial";
+    initialize: function() {
+    	if (arguments.length > 0) {
+    		var id = arguments[0];
+    		this.id = id;
+    		this.maxValue = ProgressBar.Pollers[id]['maxValue'];
+    		this.minValue = ProgressBar.Pollers[id]['minValue'];
+    	}
     },
+    init: function (id, containerId, formId, mode, minValue, maxValue, label , context) {
+    	ProgressBar.Pollers[id] = {};
+    	ProgressBar.Pollers[id]['containerId'] = containerId;
+    	ProgressBar.Pollers[id]['formId'] = formId;
+    	ProgressBar.Pollers[id]['mode'] = mode;
+    	ProgressBar.Pollers[id]['minValue'] = minValue;
+    	ProgressBar.Pollers[id]['maxValue'] = maxValue;
+    	ProgressBar.Pollers[id]['label'] = label;
+    	ProgressBar.Pollers[id]['context'] = context;
+    },
 	getValue: function () {
 		var d = $(this.id + ":upload");
 		if (d != null) {
@@ -18,26 +30,18 @@
 		}
 	return null;
 	},
-	onComplete: function (data, str, context) {
+	onComplete: function (data) {
 		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();
-			}*/
+			this.renderLabel();
+			this.poll();
 		}
 		
 	},
+	poll: function () {
+			A4J.AJAX.Poll(ProgressBar.Pollers[this.id]['containerId'], ProgressBar.Pollers[this.id]['formId'], ProgressBar.Pollers[this.id]['options']);
+	},
 	interpolate: function (placeholders) {
 		for(var k in this.context) {
 			var v = this.context[k];
@@ -79,41 +83,25 @@
 			}
 		}
 	},
+	renderLabel: function () {
+		var context = ProgressBar.Pollers[this.id]['context'];
+			context['minValue'] = ProgressBar.Pollers[this.id]['minValue'];
+			context['maxValue'] = ProgressBar.Pollers[this.id]['maxValue'];
+			context['value'] = (this.value ? this.value : this.getValue());
+		var markup = ProgressBar.Pollers[this.id]['label'];
+		var html = markup.invoke('getContent', context).join('');
+		$(this.id + ":remain").innerHTML = $(this.id + ":complete").innerHTML = html;
+	},
 	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;
+	getMode: function () {
+		return ProgressBar.Pollers[this.id]['mode'];
 	},
+	getMaxValue: function () {
+		return ProgressBar.Pollers[this.id]['maxValue'];	
+	},
 	isAjaxMode: function () {
-		return ($(this.id + ":progressState") == null);
+		return (this.getMode() == "ajax");
 	}, 
 	setValue: function (val) {
 		var p = val;
@@ -121,79 +109,38 @@
 			if (val.indexOf("%") < 0)
 				val = val + "%";
 		}
-
-		if ( parseFloat(p) >=  parseFloat(this.maxValue))  {
+		
+		if ( parseFloat(p) >=  parseFloat(this.getMaxValue()))  {
 			if (!this.isAjaxMode()) { 
-				this.switchMode("completed"); return;
+				//this.switchMode("completed"); return;
 			} 
 		}
 		
-		if (this.mode == "progress" || this.isAjaxMode()) {
-			d = $(this.id + ":upload");
-			if (d != null) d.style.width = val;
-		}
+		d = $(this.id + ":upload");
+		if (d != null) d.style.width = val;
 	
 	},
 	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();			
+			this.forceProgressState();
+		}else {
+			this.disable();
+			this.poll();			
 		}
 	},
 	disable: function () {
 		A4J.AJAX.StopPoll(this.id);
 	},
 	finish: function () {
-		this.switchMode("completed");
+		//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" : 
-				this.hideStates(this.id + ":progressState",this.id + ":completedState");
-				this.show($(this.id + ":initialState"));
-			break;
-		  	case "progress" :
-		  		this.hideStates(this.id + ":initialState",this.id + ":completedState");
-				this.show($(this.id + ":progressState"));
-		 	break;
-		 	case "completed" :
-		 		this.hideStates(this.id + ":progressState",this.id + ":initialState");
-				this.show($(this.id + ":completedState"));
-		 	break;		 	
-		}
-		this.mode = mode;
-	},
-	hideStates : function () {
-		for (i = 0 ; i < arguments.length; i++) {
-			d = $(arguments[i]);
-			if (d != null) this.hide(d); 
-		}
-	},
-	init : function () {
-	this.switchMode("initial");
-	},
-	hide : function (o) {
-	if (o != null)
-		o.style.display = 'none';
-	},
-	show : function (o) {
-	if (o != null)
-		o.style.display = '';
+	forceProgressState : function () {
+		var options = {};
+		options['parameters'] = {};
+		options['parameters'][this.id] = this.id;
+		options['parameters']['forceState'] = "progress";
+		options['actionUrl'] = ProgressBar.Pollers[this.id]['options']['actionUrl'];
+		options['oncomplete'] = function () { new ProgressBar(this.id).setValue("0") };
+		A4J.AJAX.SubmitRequest(ProgressBar.Pollers[this.id]['containerId'],ProgressBar.Pollers[this.id]['formId'],null,options);
 	}
 	});

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-24 14:57:55 UTC (rev 5589)
+++ trunk/sandbox/ui/progressBAR/src/main/templates/org/richfaces/progressBar.jspx	2008-01-24 15:04:27 UTC (rev 5590)
@@ -11,6 +11,7 @@
 			new org.ajax4jsf.javascript.PrototypeScript(),
 			new org.ajax4jsf.javascript.AjaxScript(),
 			/org/richfaces/renderkit/html/scripts/utils.js,
+			/org/richfaces/renderkit/html/scripts/json/json-dom.js,
 			/org/richfaces/renderkit/html/js/progressBar.js,
 		
 </h:scripts>
@@ -27,62 +28,26 @@
     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"));
-
+	String forcedState = getForcedState(component);
     
  ]]>
 </jsp:scriptlet>
 
 	<jsp:scriptlet>
 <![CDATA[
-if (isAjax) {
 setProgressVar(context, component, value);
-if (value.doubleValue() < minValue.doubleValue()) { 
-    encodeInitialState(context, component, "true");
-} else if (value.doubleValue() >= maxValue.doubleValue() ) {
-    encodeCompletedState(context, component, "true");
-} else {
+if (forcedState != null) {
+    renderForcedState(forcedState,context,component);
+}else if (value.doubleValue() < minValue.doubleValue()) { 
+    encodeInitialState(context, component);
+} else if (value.doubleValue() >= maxValue.doubleValue()) {
+    encodeCompletedState(context, component);
+} else  {
+    encodeProgressState(context, component);
+}
  ]]>
 </jsp:scriptlet>
-	<div class="rich-progress-bar #{styleClass}" style="#{style}" id="#{clientId}">
-		<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[
-}
-}else {
-]]>
-</jsp:scriptlet>
-<div id="#{clientId}" style="#{style}">
-	<div id="#{clientId}:progressState" class="rich-progress-bar #{styleClass}" style="#{style}">
-		<f:call name="encodeProgressState" />
-	</div>
-	<f:call name="encodeInitialState">
-		<f:parameter value="false"/>
-	</f:call>
-	<f:call name="encodeCompletedState">
-		<f:parameter value="false"/>
-	</f:call>
-	<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>
-		new ProgressBar('#{clientId}').init();
-	</script>
-</div>
-<jsp:scriptlet>
-<![CDATA[
-}
-]]>
-</jsp:scriptlet>
-
 </f:root>
\ No newline at end of file




More information about the richfaces-svn-commits mailing list