Author: pyaschenko
Date: 2011-01-14 12:23:26 -0500 (Fri, 14 Jan 2011)
New Revision: 21036
Modified:
branches/RF-9797/ui/validator/ui/src/main/java/org/richfaces/renderkit/html/ClientOnlyScript.java
branches/RF-9797/ui/validator/ui/src/main/java/org/richfaces/renderkit/html/LibraryScriptFunction.java
branches/RF-9797/ui/validator/ui/src/main/java/org/richfaces/validator/FacesValidatorServiceImpl.java
branches/RF-9797/ui/validator/ui/src/main/resources/META-INF/resources/org.richfaces/richfaces-csv.js
Log:
RF-9797
validators and converter hash script rendering were added
validators and converters were moved to RichFaces.csv container
bugfix
Modified:
branches/RF-9797/ui/validator/ui/src/main/java/org/richfaces/renderkit/html/ClientOnlyScript.java
===================================================================
---
branches/RF-9797/ui/validator/ui/src/main/java/org/richfaces/renderkit/html/ClientOnlyScript.java 2011-01-14
17:05:06 UTC (rev 21035)
+++
branches/RF-9797/ui/validator/ui/src/main/java/org/richfaces/renderkit/html/ClientOnlyScript.java 2011-01-14
17:23:26 UTC (rev 21036)
@@ -1,7 +1,9 @@
package org.richfaces.renderkit.html;
+import java.util.Arrays;
import java.util.Collection;
import java.util.LinkedHashSet;
+import java.util.Map;
import org.ajax4jsf.javascript.JSFunction;
import org.ajax4jsf.javascript.JSReference;
@@ -50,10 +52,28 @@
* finishValidation(body); body.append("return true;\n"); // Catch
errors body.append("} catch(e) {\n");
* SEND_ERROR_FUNCTION.appendScriptToStringBuilder(body);body.append(EOL);
body.append("return false;\n}");
*/
+ body.append("var c;");
+ if (null!=converter) {
+ body.append("c=");
+ body.append(converter.getClientScript());
+ body.append("EOL");
+ }
+ body.append("var v=[");
+
+ int i = validators.size();
+ for (LibraryScriptFunction validatorScript : validators) {
+ body.append(validatorScript.getClientScript());
+ if (--i>0) {
+ body.append(",");
+ }
+ }
+ body.append("];");
+
JSFunction callValidator =
new JSFunction("RichFaces.csv.validate", new
JSReference("event"), new JSReference("id"), new JSReference(
- "element"), new JSReference("c"), new
JSReference("v"), new JSReference("p"));
+ "element"), new JSReference("c"), new
JSReference("v"));
body.append(callValidator);
+ body.append(EOL);
return body;
}
Modified:
branches/RF-9797/ui/validator/ui/src/main/java/org/richfaces/renderkit/html/LibraryScriptFunction.java
===================================================================
---
branches/RF-9797/ui/validator/ui/src/main/java/org/richfaces/renderkit/html/LibraryScriptFunction.java 2011-01-14
17:05:06 UTC (rev 21035)
+++
branches/RF-9797/ui/validator/ui/src/main/java/org/richfaces/renderkit/html/LibraryScriptFunction.java 2011-01-14
17:23:26 UTC (rev 21036)
@@ -23,12 +23,18 @@
package org.richfaces.renderkit.html;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.ajax4jsf.javascript.JSReference;
+import org.ajax4jsf.javascript.ScriptUtils;
import org.richfaces.javascript.LibraryFunction;
import org.richfaces.javascript.Message;
import org.richfaces.resource.ResourceKey;
import org.richfaces.resource.ResourceLibrary;
import com.google.common.collect.ImmutableSet;
+import com.google.common.collect.Maps;
/**
* <p class="changed_added_4_0">
@@ -84,6 +90,15 @@
public Object getParameters() {
return this.parameters;
}
+
+ public String getClientScript() {
+ Map <String, Object> hash = Maps.newHashMap();
+ hash.put("f", new JSReference(name));
+ HashMap <String, Object> params = new HashMap<String, Object>((Map
<String, Object>) parameters);
+ params.put("customMessage", message.getSummary());
+ hash.put("params", params);
+ return ScriptUtils.toScript(hash);
+ }
/*
* (non-Javadoc)
Modified:
branches/RF-9797/ui/validator/ui/src/main/java/org/richfaces/validator/FacesValidatorServiceImpl.java
===================================================================
---
branches/RF-9797/ui/validator/ui/src/main/java/org/richfaces/validator/FacesValidatorServiceImpl.java 2011-01-14
17:05:06 UTC (rev 21035)
+++
branches/RF-9797/ui/validator/ui/src/main/java/org/richfaces/validator/FacesValidatorServiceImpl.java 2011-01-14
17:23:26 UTC (rev 21036)
@@ -35,11 +35,12 @@
@Override
protected String getMessageId(Validator component) {
+ // TODO: all messages should be passed to client side using js function
RichFaces.csv.addMessage
String messageId;
if (component instanceof DoubleRangeValidator) {
messageId = DoubleRangeValidator.NOT_IN_RANGE_MESSAGE_ID;
} else if (component instanceof LengthValidator) {
- messageId = DoubleRangeValidator.NOT_IN_RANGE_MESSAGE_ID;
+ messageId = LengthValidator.MAXIMUM_MESSAGE_ID;
} else if (component instanceof LongRangeValidator) {
messageId = LongRangeValidator.NOT_IN_RANGE_MESSAGE_ID;
} else if (component instanceof RegexValidator) {
Modified:
branches/RF-9797/ui/validator/ui/src/main/resources/META-INF/resources/org.richfaces/richfaces-csv.js
===================================================================
---
branches/RF-9797/ui/validator/ui/src/main/resources/META-INF/resources/org.richfaces/richfaces-csv.js 2011-01-14
17:05:06 UTC (rev 21035)
+++
branches/RF-9797/ui/validator/ui/src/main/resources/META-INF/resources/org.richfaces/richfaces-csv.js 2011-01-14
17:23:26 UTC (rev 21036)
@@ -49,28 +49,15 @@
sendMessage: function (componentId, message) {
rf.Event.fire(window.document, rf.Event.MESSAGE_EVENT_TYPE,
{'sourceId':componentId, 'message':message});
},
- // Converters API
- addConverter: function (converterFunctions) {
- $.extend(_converters, converterFunctions);
- },
- getConverter: function (name) {
- return _converters[name];
- },
- // Validators API
- addValidator: function (validatorFunctions) {
- $.extend(_validators, validatorFunctions);
- },
- getValidator: function (name) {
- return _validators[name];
- },
validate: function (event, id, element, converter, validators, params) {
var value = __getValue(element || id);
if (converter) {
try {
converter.options.componentId = id;
- value = getConverter([converter.name])(value, converter.options);
+ if (converter.f)
+ value = converter.f(value, converter.params);
} catch (e){
- sendMessage(id, e.message);
+ rf.csv.sendMessage(id, e);
return false;
}
}
@@ -78,13 +65,13 @@
var validatorFunction;
try {
for (i=0;i<validators.length;i++) {
- validatorFunction = getValidator(validators[i].type);
+ validatorFunction = validators[i].f;
if (validatorFunction) {
- validatorFunction(id, value, validators[i]);
+ validatorFunction(id, value, validators[i].params);
}
}
} catch (e) {
- sendMessage(id, result);
+ rf.csv.sendMessage(id, e);
return false;
}
}
@@ -105,38 +92,32 @@
}}
*/
/*
- * c: {name:, options:}
- v: [{type:, customMessage:, [validator params]}]
+ * c: {name:, params:}
+ v: [{f:, customMessage:, [validator params]}]
function form_3Atext_3Av(id, el, e, c, v, p) {
RichFaces.csv.validate(e, id, el, c, v, p);
}
*/
- addFormValidators: function (formId, callValidatorFunctions) {
-
- }
});
-})(jQuery, window.RichFaces || (window.RichFaces={}));
-
-/*
- * Converters implementation
- */
-(function($, rf) {
- RichFaces.csv.addConverter({
- "boolean": function (value, params) {
+ /*
+ * Converters implementation
+ */
+ $.extend(rf.csv, {
+ "convertBoolean": function (value, params) {
var result; value = $.trim(value).toLowerCase();
result = value=='true' ? true : value.length<1 ? null : false;
return result;
},
- "byte": function (value, params) {
+ "convertByte": function (value, params) {
var result; value = $.trim(value);
if (!rf.csv.RE_DIGITS.test(value) || (result=parseInt(value,10))<-128 ||
result>127) {
throw rf.csv.getMessage(params.customMessage, 'BYTE_CONVERTER_BYTE', [value,
0, params.componentId]);
}
return result;
},
- "number": function (value, params) {
+ "convertNumber": function (value, params) {
var result; value=$.trim(value);
if (isNaN(value)) {
throw rf.csv.getMessage(params.customMessage, 'NUMBER_CONVERTER_NUMBER',
[value, 0, params.componentId]);
@@ -145,7 +126,7 @@
}
return result;
},
- "short": function (value, params) {
+ "convertShort": function (value, params) {
var result; value = $.trim(value);
if (!rf.csv.RE_DIGITS.test(value) || (result=parseInt(value,10))<-32768 ||
result>32767) {
throw rf.csv.getMessage(params.customMessage, 'SHORT_CONVERTER_SHORT',
[value, 0, params.componentId]);
@@ -153,14 +134,12 @@
return result;
}
});
-})(jQuery, window.RichFaces || (window.RichFaces={}));
-
-/*
- * Validators implementation
- */
-(function($, rf) {
- RichFaces.csv.addValidator({
- "long-range": function (componentId, value, params) {
+
+ /*
+ * Validators implementation
+ */
+ $.extend(rf.csv, {
+ "validateLongRange": function (componentId, value, params) {
var type = typeof value;
if (type != "number") {
if (type != "string") {
@@ -191,7 +170,7 @@
}
}
},
- "double-range": function (componentId, value, params) {
+ "validateDoubleRange": function (componentId, value, params) {
var type = typeof value;
if (type != "number") {
if (type != "string") {
@@ -222,7 +201,7 @@
}
}
},
- "length": function (componentId, value, params) {
+ "validateLength": function (componentId, value, params) {
if (params.maximum && value.length > params.maximum) {
throw rf.csv.getMessage(params.customMessage, 'LENGTH_VALIDATOR_MAXIMUM',
[params.maximum, componentId]);
}
@@ -230,7 +209,7 @@
throw rf.csv.getMessage(params.customMessage, 'LENGTH_VALIDATOR_MINIMUM',
[params.minimum, componentId]);
}
},
- "regex": function (componentId, value, params) {
+ "validateRegex": function (componentId, value, params) {
if (typeof params.pattern != "string" || params.pattern.length == 0) {
throw rf.csv.getMessage(params.customMessage,
'REGEX_VALIDATOR_PATTERN_NOT_SET', []);
}
@@ -245,10 +224,11 @@
throw rf.csv.getMessage(params.customMessage, 'REGEX_VALIDATOR_NOT_MATCHED',
[params.pattern]);
}
},
- "required": function (componentId, value, params) {
+ "validateRequired": function (componentId, value, params) {
if (value.length==0) {
throw rf.csv.getMessage(params.customMessage, 'UIINPUT_REQUIRED',
[componentId]);
}
}
});
+
})(jQuery, window.RichFaces || (window.RichFaces={}));
\ No newline at end of file