Author: nbelaevski
Date: 2010-07-21 09:01:03 -0400 (Wed, 21 Jul 2010)
New Revision: 18174
Modified:
root/core/trunk/api/src/main/java/org/ajax4jsf/context/AjaxContext.java
root/core/trunk/api/src/main/java/org/ajax4jsf/javascript/ScriptUtils.java
root/core/trunk/api/src/test/java/org/ajax4jsf/javascript/ScriptUtilsTest.java
root/core/trunk/impl/src/main/java/org/ajax4jsf/context/AjaxContextImpl.java
root/core/trunk/impl/src/main/java/org/ajax4jsf/renderkit/AjaxRendererUtils.java
root/core/trunk/impl/src/main/resources/META-INF/resources/richfaces.js
Log:
https://jira.jboss.org/browse/RF-8946
Modified: root/core/trunk/api/src/main/java/org/ajax4jsf/context/AjaxContext.java
===================================================================
--- root/core/trunk/api/src/main/java/org/ajax4jsf/context/AjaxContext.java 2010-07-21
12:27:44 UTC (rev 18173)
+++ root/core/trunk/api/src/main/java/org/ajax4jsf/context/AjaxContext.java 2010-07-21
13:01:03 UTC (rev 18174)
@@ -27,7 +27,6 @@
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
-
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
@@ -44,7 +43,6 @@
* Key for keep request state information in request-scope attributes.
*/
public static final String AJAX_CONTEXT_KEY = "ajaxContext";
- public static final String RESPONSE_DATA_KEY = "_ajax:data";
static final String SERVICE_RESOURCE = "META-INF/services/" +
AjaxContext.class.getName();
private static final String DEFAULT_CONTEXT_CLASS =
"org.ajax4jsf.context.AjaxContextImpl";
private static Map<ClassLoader, Class<? extends AjaxContext>>
ajaxContextClasses =
@@ -113,7 +111,7 @@
public abstract void release();
- public abstract Map<String, Object> getResponseDataMap();
+ public abstract Map<String, Object> getResponseComponentDataMap();
public abstract void setAjaxRequest(boolean b);
Modified: root/core/trunk/api/src/main/java/org/ajax4jsf/javascript/ScriptUtils.java
===================================================================
--- root/core/trunk/api/src/main/java/org/ajax4jsf/javascript/ScriptUtils.java 2010-07-21
12:27:44 UTC (rev 18173)
+++ root/core/trunk/api/src/main/java/org/ajax4jsf/javascript/ScriptUtils.java 2010-07-21
13:01:03 UTC (rev 18174)
@@ -243,9 +243,9 @@
}
public static void writeEncodedString(Writer w, Object obj) throws IOException {
- w.write("'");
+ w.write("\"");
writeEncoded(w, obj);
- w.write("'");
+ w.write("\"");
}
public static void addEncodedString(StringBuilder buff, Object obj) {
Modified: root/core/trunk/api/src/test/java/org/ajax4jsf/javascript/ScriptUtilsTest.java
===================================================================
---
root/core/trunk/api/src/test/java/org/ajax4jsf/javascript/ScriptUtilsTest.java 2010-07-21
12:27:44 UTC (rev 18173)
+++
root/core/trunk/api/src/test/java/org/ajax4jsf/javascript/ScriptUtilsTest.java 2010-07-21
13:01:03 UTC (rev 18174)
@@ -125,7 +125,7 @@
public void testStringToScript() {
Object obj = "foo";
- assertEquals("'foo'", ScriptUtils.toScript(obj));
+ assertEquals("\"foo\"", ScriptUtils.toScript(obj));
}
/**
@@ -174,13 +174,13 @@
{"one", "two"}, {"three", "four"}
};
- assertEquals("[['one','two']
,['three','four'] ] ", ScriptUtils.toScript(obj));
+ assertEquals("[[\"one\",\"two\"]
,[\"three\",\"four\"] ] ", ScriptUtils.toScript(obj));
Map<String, Object> map = new TreeMap<String, Object>();
map.put("a", obj);
map.put("b", "c");
- assertEquals("{'a':[['one','two']
,['three','four'] ] ,'b':'c'} ",
ScriptUtils.toScript(map));
+ assertEquals("{\"a\":[[\"one\",\"two\"]
,[\"three\",\"four\"] ] ,\"b\":\"c\"} ",
ScriptUtils.toScript(map));
}
/**
@@ -217,7 +217,7 @@
public void testObjectArrayToScript() {
Bean[] obj = {new Bean(1, true, "foo"), new Bean(2, false,
"bar")};
-
assertEquals("[{'bool':true,'foo':'foo',\'integer\':1}
,{'bool':false,'foo':'bar','integer':2} ] ",
+
assertEquals("[{\"bool\":true,\"foo\":\"foo\",\"integer\":1}
,{\"bool\":false,\"foo\":\"bar\",\"integer\":2} ]
",
ScriptUtils.toScript(obj));
}
@@ -228,7 +228,7 @@
Bean[] array = {new Bean(1, true, "foo"), new Bean(2, false,
"bar")};
List<Bean> obj = Arrays.asList(array);
-
assertEquals("[{'bool':true,'foo':'foo',\'integer\':1}
,{'bool':false,'foo':'bar','integer':2} ] ",
+
assertEquals("[{\"bool\":true,\"foo\":\"foo\",\"integer\":1}
,{\"bool\":false,\"foo\":\"bar\",\"integer\":2} ]
",
ScriptUtils.toScript(obj));
}
@@ -241,7 +241,7 @@
obj.put("a", "foo");
obj.put("b", "bar");
obj.put("c", "baz");
-
assertEquals("{'a':'foo','b':'bar','c':'baz'}
", ScriptUtils.toScript(obj));
+
assertEquals("{\"a\":\"foo\",\"b\":\"bar\",\"c\":\"baz\"}
", ScriptUtils.toScript(obj));
}
/**
@@ -251,7 +251,7 @@
StringBuilder buff = new StringBuilder();
ScriptUtils.addEncodedString(buff, "foo");
- assertEquals("'foo'", buff.toString());
+ assertEquals("\"foo\"", buff.toString());
}
/**
@@ -282,7 +282,7 @@
* Test method for {@link ScriptUtils#toScript(Object)}
*/
public void testEnum() throws Exception {
- assertEquals("'TestEnum: B'",
ScriptUtils.toScript(TestEnum.B));
+ assertEquals("\"TestEnum: B\"",
ScriptUtils.toScript(TestEnum.B));
}
private void assertCaptureEquals(Capture<? extends Object> capture, String
expected) {
@@ -329,7 +329,7 @@
environment.replay();
ScriptUtils.writeToStream(mockWriter, Collections.singletonMap("delay",
Integer.valueOf(1500)));
environment.verify();
- assertCaptureEquals(capture, "{'delay':1500}");
+ assertCaptureEquals(capture, "{\"delay\":1500}");
environment.release();
}
@@ -401,7 +401,7 @@
ReferenceHolderBean parent = new ReferenceHolderBean("parent");
ReferencedBean child = new ReferencedBean("child", parent);
- assertEquals(dehydrate("{'name': 'child', 'parent':
{'name': 'parent', 'reference': null}}"),
+ assertEquals(dehydrate("{\"name\": \"child\",
\"parent\": {\"name\": \"parent\", \"reference\":
null}}"),
dehydrate(ScriptUtils.toScript(child)));
}
@@ -411,7 +411,7 @@
parent.setReference(child);
- assertEquals(dehydrate("{'name': 'parent',
'reference': {'name': 'child', 'parent': null}}"),
+ assertEquals(dehydrate("{\"name\": \"parent\",
\"reference\": {\"name\": \"child\", \"parent\":
null}}"),
dehydrate(ScriptUtils.toScript(parent)));
}
@@ -421,7 +421,7 @@
parent.setReference(new Object[] {child});
- assertEquals(dehydrate("{'name': 'parent',
'reference': [{'name': 'child', 'parent': null}]}"),
+ assertEquals(dehydrate("{\"name\": \"parent\",
\"reference\": [{\"name\": \"child\", \"parent\":
null}]}"),
dehydrate(ScriptUtils.toScript(parent)));
}
@@ -433,7 +433,7 @@
set.add(child);
parent.setReference(set);
- assertEquals(dehydrate("{'name': 'parent',
'reference': [{'name': 'child', 'parent': null}]}"),
+ assertEquals(dehydrate("{\"name\": \"parent\",
\"reference\": [{\"name\": \"child\", \"parent\":
null}]}"),
dehydrate(ScriptUtils.toScript(parent)));
}
@@ -445,7 +445,7 @@
map.put("key", child);
parent.setReference(map);
- assertEquals(dehydrate("{'name': 'parent',
'reference': {'key': {'name': 'child', 'parent':
null}}}"),
+ assertEquals(dehydrate("{\"name\": \"parent\",
\"reference\": {\"key\": {\"name\": \"child\",
\"parent\": null}}}"),
dehydrate(ScriptUtils.toScript(parent)));
}
Modified: root/core/trunk/impl/src/main/java/org/ajax4jsf/context/AjaxContextImpl.java
===================================================================
---
root/core/trunk/impl/src/main/java/org/ajax4jsf/context/AjaxContextImpl.java 2010-07-21
12:27:44 UTC (rev 18173)
+++
root/core/trunk/impl/src/main/java/org/ajax4jsf/context/AjaxContextImpl.java 2010-07-21
13:01:03 UTC (rev 18174)
@@ -21,14 +21,17 @@
package org.ajax4jsf.context;
-import org.ajax4jsf.Messages;
-import org.ajax4jsf.application.AjaxViewHandler;
-import org.ajax4jsf.renderkit.AjaxContainerRenderer;
-import org.ajax4jsf.renderkit.AjaxRendererUtils;
-import org.ajax4jsf.renderkit.RendererUtils;
-import org.ajax4jsf.renderkit.RendererUtils.HTML;
-import org.richfaces.log.RichfacesLogger;
-import org.slf4j.Logger;
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.io.StringWriter;
+import java.lang.reflect.Method;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.Iterator;
+import java.util.LinkedHashSet;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Set;
import javax.faces.FacesException;
import javax.faces.FactoryFinder;
@@ -44,18 +47,16 @@
import javax.faces.render.RenderKitFactory;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
-import java.io.IOException;
-import java.io.PrintWriter;
-import java.io.StringWriter;
-import java.lang.reflect.Method;
-import java.util.HashMap;
-import java.util.HashSet;
-import java.util.Iterator;
-import java.util.LinkedHashSet;
-import java.util.Locale;
-import java.util.Map;
-import java.util.Set;
+import org.ajax4jsf.Messages;
+import org.ajax4jsf.application.AjaxViewHandler;
+import org.ajax4jsf.renderkit.AjaxContainerRenderer;
+import org.ajax4jsf.renderkit.AjaxRendererUtils;
+import org.ajax4jsf.renderkit.RendererUtils;
+import org.ajax4jsf.renderkit.RendererUtils.HTML;
+import org.richfaces.log.RichfacesLogger;
+import org.slf4j.Logger;
+
/**
* This class incapsulated
*
@@ -76,9 +77,10 @@
String submittedRegionClientId = null;
ViewIdHolder viewIdHolder = null;
Integer viewSequence = new Integer(1);
- Map<String, Object> responseDataMap = new HashMap<String, Object>();
+ Map<String, Object> responseComponentDataMap = new HashMap<String,
Object>();
Map<String, Object> commonAjaxParameters = new HashMap<String,
Object>();
-
+ Object responseData = null;
+
public void release() {
ajaxAreasToRender = new LinkedHashSet<String>();
ajaxAreasToProcess = null;
@@ -88,8 +90,9 @@
viewSequence = new Integer(1);
submittedRegionClientId = null;
viewIdHolder = null;
- responseDataMap = new HashMap<String, Object>();
+ responseComponentDataMap = new HashMap<String, Object>();
commonAjaxParameters = new HashMap<String, Object>();
+ responseData = null;
}
/*
@@ -476,21 +479,21 @@
* @return the responseData
*/
public Object getResponseData() {
- return responseDataMap.get(RESPONSE_DATA_KEY);
+ return responseData;
}
/**
* @param responseData the responseData to set
*/
public void setResponseData(Object responseData) {
- this.responseDataMap.put(RESPONSE_DATA_KEY, responseData);
+ this.responseData = responseData;
}
/**
* @return the responseDataMap
*/
- public Map<String, Object> getResponseDataMap() {
- return responseDataMap;
+ public Map<String, Object> getResponseComponentDataMap() {
+ return responseComponentDataMap;
}
/**
Modified:
root/core/trunk/impl/src/main/java/org/ajax4jsf/renderkit/AjaxRendererUtils.java
===================================================================
---
root/core/trunk/impl/src/main/java/org/ajax4jsf/renderkit/AjaxRendererUtils.java 2010-07-21
12:27:44 UTC (rev 18173)
+++
root/core/trunk/impl/src/main/java/org/ajax4jsf/renderkit/AjaxRendererUtils.java 2010-07-21
13:01:03 UTC (rev 18174)
@@ -136,6 +136,7 @@
private static final String BEFOREDOMUPDATE_ELEMENT_NAME =
"beforedomupdate";
private static final String COMPLETE_ELEMENT_NAME = "complete";
private static final String DATA_ELEMENT_NAME = "data";
+ private static final String COMPONENT_DATA_ELEMENT_NAME = "componentData";
private static final RendererUtils RENDERER_UTILS = RendererUtils.getInstance();
private static final Class<?> OBJECT_ARRAY_CLASS = new Object[0].getClass();
@@ -974,7 +975,7 @@
}
}
- Map<String, Object> responseDataMap = ajaxContext.getResponseDataMap();
+ Map<String, Object> responseDataMap =
ajaxContext.getResponseComponentDataMap();
// Get data serializer instance
AJAXDataSerializer serializer = ServiceTracker.getService(context,
AJAXDataSerializer.class);
@@ -1168,18 +1169,24 @@
Object onbeforedomupdate = ajaxContext.getOnbeforedomupdate();
if (onbeforedomupdate != null) {
- startExtensionElementIfNecessary(writer, attributes, writingState);
- writer.startElement(BEFOREDOMUPDATE_ELEMENT_NAME, component);
- writer.writeText(onbeforedomupdate, null);
- writer.endElement(BEFOREDOMUPDATE_ELEMENT_NAME);
+ String string = onbeforedomupdate.toString();
+ if (string.length() != 0) {
+ startExtensionElementIfNecessary(writer, attributes, writingState);
+ writer.startElement(BEFOREDOMUPDATE_ELEMENT_NAME, component);
+ writer.writeText(onbeforedomupdate, null);
+ writer.endElement(BEFOREDOMUPDATE_ELEMENT_NAME);
+ }
}
Object oncomplete = ajaxContext.getOncomplete();
if (oncomplete != null) {
- startExtensionElementIfNecessary(writer, attributes, writingState);
- writer.startElement(COMPLETE_ELEMENT_NAME, component);
- writer.writeText(oncomplete, null);
- writer.endElement(COMPLETE_ELEMENT_NAME);
+ String string = oncomplete.toString();
+ if (string.length() != 0) {
+ startExtensionElementIfNecessary(writer, attributes, writingState);
+ writer.startElement(COMPLETE_ELEMENT_NAME, component);
+ writer.writeText(oncomplete, null);
+ writer.endElement(COMPLETE_ELEMENT_NAME);
+ }
}
Object responseData = ajaxContext.getResponseData();
@@ -1187,13 +1194,23 @@
startExtensionElementIfNecessary(writer, attributes, writingState);
writer.startElement(DATA_ELEMENT_NAME, component);
- //TODO - encode response data map
AJAXDataSerializer serializer = ServiceTracker.getService(facesContext,
AJAXDataSerializer.class);
writer.writeText(serializer.asString(responseData), null);
writer.endElement(DATA_ELEMENT_NAME);
}
+ Map<String, Object> responseComponentDataMap =
ajaxContext.getResponseComponentDataMap();
+ if (responseComponentDataMap != null &&
!responseComponentDataMap.isEmpty()) {
+ startExtensionElementIfNecessary(writer, attributes, writingState);
+ writer.startElement(COMPONENT_DATA_ELEMENT_NAME, component);
+
+ AJAXDataSerializer serializer = ServiceTracker.getService(facesContext,
AJAXDataSerializer.class);
+ writer.writeText(serializer.asString(responseComponentDataMap), null);
+
+ writer.endElement(COMPONENT_DATA_ELEMENT_NAME);
+ }
+
endExtensionElementIfNecessary(writer, writingState);
}
Modified: root/core/trunk/impl/src/main/resources/META-INF/resources/richfaces.js
===================================================================
--- root/core/trunk/impl/src/main/resources/META-INF/resources/richfaces.js 2010-07-21
12:27:44 UTC (rev 18173)
+++ root/core/trunk/impl/src/main/resources/META-INF/resources/richfaces.js 2010-07-21
13:01:03 UTC (rev 18174)
@@ -432,6 +432,30 @@
error: ['error', 'complete']
};
+ var getExtensionResponseElement = function(responseXML) {
+ return jQuery("partial-response > extension#org\\.richfaces\\.extension",
responseXML);
+ };
+
+ var JSON_STRING_START = /^\s*(\[|\{)/;
+
+ var getJSONData = function(extensionElement, elementName) {
+ var dataString = jQuery.trim(extensionElement.children(elementName).text());
+ extensionElement.end();
+ try {
+ if (dataString) {
+ if (JSON_STRING_START.test(dataString)) {
+ return jQuery.parseJSON(dataString);
+ } else {
+ var parsedData = jQuery.parseJSON("{\"root\": " + dataString +
"}");
+ return parsedData.root;
+ }
+ }
+ } catch (e) {
+ richfaces.log.warn("Error evaluating JSON data from element <" +
elementName + ">: " + e.message);
+ }
+ return null;
+ };
+
richfaces.createJSFEventsAdapter = function(handlers) {
//hash of handlers
//supported are:
@@ -461,6 +485,15 @@
event.type = eventType;
if (type != 'error') {
delete event.status;
+
+ if (event.responseXML) {
+ var xml = getExtensionResponseElement(event.responseXML);
+ var data = getJSONData(xml, "data");
+ var componentData = getJSONData(xml, "componentData");
+
+ event.data = data;
+ event.componentData = componentData || {};
+ }
}
handler.call(source, event);
@@ -520,7 +553,7 @@
var createEventHandler = function(handlerCode) {
if (handlerCode) {
- return new Function("event", "data", handlerCode);
+ return new Function("event", handlerCode);
}
return null;
@@ -529,29 +562,17 @@
//TODO take events just from .java code using EL-expression
var AJAX_EVENTS = (function() {
var serverEventHandler = function(clientHandler, event) {
- var xml = jQuery("partial-response >
extension#org\\.richfaces\\.extension", event.responseXML);
+ var xml = getExtensionResponseElement(event.responseXML);
var serverHandler = createEventHandler(xml.children(event.type).text());
xml.end();
- var dataString = xml.children("data").text();
- xml.end();
-
- var data = null;
- if (dataString) {
- try {
- data = window["eval"]("(" + dataString + ")");
- } catch (e) {
- richfaces.log.warn("Error evaluating custom response data: " +
e.message);
- }
- }
-
if (clientHandler) {
- clientHandler.call(window, event, data);
+ clientHandler.call(window, event);
}
if (serverHandler) {
- serverHandler.call(window, event, data);
+ serverHandler.call(window, event);
}
};