Author: pyaschenko
Date: 2011-04-27 09:56:01 -0400 (Wed, 27 Apr 2011)
New Revision: 22444
Modified:
trunk/core/api/src/main/java/org/ajax4jsf/javascript/JSFunctionDefinition.java
trunk/ui/validator/ui/src/main/java/org/richfaces/renderkit/html/ClientOnlyScript.java
Log:
https://issues.jboss.org/browse/RF-10940
Client-side validation function is fixed, JSFunctionDefinition is refactored
Modified: trunk/core/api/src/main/java/org/ajax4jsf/javascript/JSFunctionDefinition.java
===================================================================
---
trunk/core/api/src/main/java/org/ajax4jsf/javascript/JSFunctionDefinition.java 2011-04-27
11:15:12 UTC (rev 22443)
+++
trunk/core/api/src/main/java/org/ajax4jsf/javascript/JSFunctionDefinition.java 2011-04-27
13:56:01 UTC (rev 22444)
@@ -26,7 +26,6 @@
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
-import java.util.Iterator;
import java.util.List;
/**
@@ -35,6 +34,18 @@
*
*/
public class JSFunctionDefinition extends ScriptStringBase implements ScriptString {
+ protected static final String FUNCTION = "function";
+ protected static final String EQUALS = "=";
+ protected static final String DOT = ".";
+ protected static final String COMMA = ",";
+ protected static final String EMPTY_STRING = " ";
+ protected static final String LEFT_CURLY_BRACKET = "{";
+ protected static final String RIGHT_CURLY_BRACKET = "}";
+ protected static final String LEFT_ROUND_BRACKET = "(";
+ protected static final String RIGHT_ROUND_BRACKET = ")";
+ protected static final String LEFT_SQUARE_BRACKET = "[";
+ protected static final String RIGHT_SQUARE_BRACKET = "]";
+ protected static final String COLON = ":";
private List<Object> parameters = new ArrayList<Object>();
private StringBuffer body = new StringBuffer();
private String name;
@@ -58,34 +69,12 @@
}
public void appendScript(Appendable target) throws IOException {
- target.append("function");
-
- if (null != name) {
- target.append(" ").append(name);
- }
-
- target.append("(");
-
- boolean first = true;
-
- for (Iterator<Object> param = parameters.iterator(); param.hasNext(); ) {
- Object element = param.next();
-
- if (!first) {
- target.append(',');
- }
-
- target.append(element.toString());
- first = false;
- }
-
- target.append("){");appendBody(target);
- target.append("}");
+ appendFunctionName(target);
+ appendParameters(target);
+ target.append(LEFT_CURLY_BRACKET);
+ appendBody(target);
+ target.append(RIGHT_CURLY_BRACKET);
}
-
- protected void appendBody(Appendable target) throws IOException {
- target.append(body);
- }
/**
* @return the name
@@ -101,4 +90,32 @@
this.name = name;
}
+ protected void appendFunctionName(Appendable target) throws IOException {
+ target.append(FUNCTION);
+
+ if (null != name) {
+ target.append(EMPTY_STRING).append(name);
+ }
+ }
+
+ protected void appendBody(Appendable target) throws IOException {
+ target.append(body);
+ }
+
+ private void appendParameters(Appendable target) throws IOException {
+ target.append(LEFT_ROUND_BRACKET);
+
+ boolean first = true;
+
+ for (Object element : parameters) {
+ if (!first) {
+ target.append(COMMA);
+ }
+
+ target.append(element.toString());
+ first = false;
+ }
+
+ target.append(RIGHT_ROUND_BRACKET);
+ }
}
Modified:
trunk/ui/validator/ui/src/main/java/org/richfaces/renderkit/html/ClientOnlyScript.java
===================================================================
---
trunk/ui/validator/ui/src/main/java/org/richfaces/renderkit/html/ClientOnlyScript.java 2011-04-27
11:15:12 UTC (rev 22443)
+++
trunk/ui/validator/ui/src/main/java/org/richfaces/renderkit/html/ClientOnlyScript.java 2011-04-27
13:56:01 UTC (rev 22444)
@@ -1,18 +1,13 @@
package org.richfaces.renderkit.html;
+import com.google.common.collect.*;
+import org.ajax4jsf.javascript.ScriptUtils;
+import org.richfaces.resource.ResourceKey;
+
import java.io.IOException;
import java.util.Collection;
import java.util.LinkedHashSet;
-import org.ajax4jsf.javascript.ScriptUtils;
-import org.richfaces.resource.ResourceKey;
-
-import com.google.common.collect.ImmutableList;
-import com.google.common.collect.ImmutableSet;
-import com.google.common.collect.Iterables;
-import com.google.common.collect.Sets;
-import com.google.common.collect.UnmodifiableIterator;
-
public class ClientOnlyScript extends ValidatorScriptBase {
public static final ResourceKey CSV_RESOURCE =
ResourceKey.create("csv.reslib", "org.richfaces");
@@ -41,23 +36,32 @@
}
@Override
+ public void appendFunctionName(Appendable target) throws IOException {
+ target.append("window")
+ .append(DOT)
+ .append(super.getName())
+ .append(EQUALS)
+ .append(FUNCTION);
+ }
+
+ @Override
protected void appendParameters(Appendable target) throws IOException {
if (null != converter) {
- target.append(CONVERTER).append(":");
+ target.append(CONVERTER).append(COLON);
appendConverter(target, converter);
- target.append(",");
+ target.append(COMMA);
}
- target.append(VALIDATORS).append(":[");
+ target.append(VALIDATORS).append(COLON + LEFT_SQUARE_BRACKET);
UnmodifiableIterator<? extends LibraryScriptFunction> iterator =
validators.iterator();
while (iterator.hasNext()) {
- LibraryScriptFunction validatorScript = (LibraryScriptFunction)
iterator.next();
+ LibraryScriptFunction validatorScript = iterator.next();
appendValidator(target, validatorScript);
if (iterator.hasNext()) {
- target.append(",");
+ target.append(COMMA);
}
}
- target.append("]");
+ target.append(RIGHT_SQUARE_BRACKET);
appendAjaxParameter(target);
}
@@ -66,11 +70,11 @@
}
protected void appendConverter(Appendable target, LibraryScriptFunction converter)
throws IOException {
-
target.append('{').append("f").append(':').append(converter.getName()).append(',');
- target.append(PARAMS).append(':');ScriptUtils.appendScript(target,
converter.getParameters());
- target.append(',');
- target.append(MESSAGE).append(':');ScriptUtils.appendScript(target,
converter.getMessage());
- target.append('}');
+
target.append(LEFT_CURLY_BRACKET).append("f").append(COLON).append(converter.getName()).append(COMMA);
+ target.append(PARAMS).append(COLON);ScriptUtils.appendScript(target,
converter.getParameters());
+ target.append(COMMA);
+ target.append(MESSAGE).append(COLON);ScriptUtils.appendScript(target,
converter.getMessage());
+ target.append(RIGHT_CURLY_BRACKET);
}
protected void appendAjaxParameter(Appendable target) throws IOException {