Author: nbelaevski
Date: 2009-11-06 14:18:31 -0500 (Fri, 06 Nov 2009)
New Revision: 15851
Modified:
root/framework/trunk/impl/src/main/java/org/ajax4jsf/component/AjaxActionComponent.java
root/framework/trunk/impl/src/main/java/org/ajax4jsf/renderkit/AjaxRendererUtils.java
root/framework/trunk/impl/src/main/java/org/richfaces/context/PartialViewContextImpl.java
root/framework/trunk/impl/src/main/resources/META-INF/resources/richfaces.js
root/ui/trunk/components/core/src/main/java/org/richfaces/renderkit/AjaxCommandRendererBase.java
Log:
https://jira.jboss.org/jira/browse/RF-7823
Modified:
root/framework/trunk/impl/src/main/java/org/ajax4jsf/component/AjaxActionComponent.java
===================================================================
---
root/framework/trunk/impl/src/main/java/org/ajax4jsf/component/AjaxActionComponent.java 2009-11-06
18:17:16 UTC (rev 15850)
+++
root/framework/trunk/impl/src/main/java/org/ajax4jsf/component/AjaxActionComponent.java 2009-11-06
19:18:31 UTC (rev 15851)
@@ -86,6 +86,7 @@
ajaxContext.getResponseDataMap().put(FOCUS_DATA_ID, focus);
}
+ ajaxContext.setOnbeforedomupdate(getOnbeforedomupdate());
ajaxContext.setOncomplete(getOncomplete());
}
}
Modified:
root/framework/trunk/impl/src/main/java/org/ajax4jsf/renderkit/AjaxRendererUtils.java
===================================================================
---
root/framework/trunk/impl/src/main/java/org/ajax4jsf/renderkit/AjaxRendererUtils.java 2009-11-06
18:17:16 UTC (rev 15850)
+++
root/framework/trunk/impl/src/main/java/org/ajax4jsf/renderkit/AjaxRendererUtils.java 2009-11-06
19:18:31 UTC (rev 15851)
@@ -27,6 +27,7 @@
import java.lang.reflect.Method;
import java.util.Arrays;
import java.util.Collection;
+import java.util.Collections;
import java.util.Iterator;
import java.util.LinkedHashSet;
import java.util.Locale;
@@ -38,6 +39,7 @@
import javax.faces.component.UIForm;
import javax.faces.context.ExternalContext;
import javax.faces.context.FacesContext;
+import javax.faces.context.PartialResponseWriter;
import javax.faces.context.ResponseWriter;
import javax.servlet.http.HttpServletResponse;
@@ -51,6 +53,7 @@
import org.ajax4jsf.javascript.JSFunction;
import org.ajax4jsf.javascript.JSFunctionDefinition;
import org.ajax4jsf.javascript.JSReference;
+import org.ajax4jsf.javascript.ScriptUtils;
import org.ajax4jsf.renderkit.RendererUtils.HTML;
import org.ajax4jsf.util.ServicesUtils;
import org.richfaces.log.RichfacesLogger;
@@ -62,7 +65,12 @@
* Some utilites for render AJAX components.
*/
public final class AjaxRendererUtils {
- public static final String AJAX_ABORT_ATTR = "ignoreDupResponses";
+
+ private static final String BEFOREDOMUPDATE_ELEMENT_NAME = "beforedomupdate";
+ private static final String COMPLETE_ELEMENT_NAME = "complete";
+ private static final String DATA_ELEMENT_NAME = "data";
+
+ public static final String AJAX_ABORT_ATTR = "ignoreDupResponses";
public static final String AJAX_AREAS_RENDERED =
"org.ajax4jsf.areas.rendered";
public static final String AJAX_DELAY_ATTR = "requestDelay";
@@ -115,6 +123,7 @@
public static final String STATUS_ATTR_NAME = "status";
public static final String VALUE_ATTR = "value";
+ public final static String EXTENSION_ID = "org.richfaces.extension";
public static final String AJAX_COMPONENT_ID_PARAMETER =
"org.richfaces.ajax.component";
public static final String BEHAVIOR_EVENT_PARAMETER =
"javax.faces.behavior.event";
@@ -1118,4 +1127,64 @@
public static void addRegionsFromComponent(UIComponent component, FacesContext
facesContext) {
AjaxContext.getCurrentInstance(facesContext).addRegionsFromComponent(component);
}
+
+ private static void startExtensionElementIfNecessary(
+ PartialResponseWriter partialResponseWriter,
+ Map<String, String> attributes,
+ boolean[] writingState) throws IOException {
+
+ if (!writingState[0]) {
+ writingState[0] = true;
+
+ partialResponseWriter.startExtension(attributes);
+ }
+ }
+
+ private static void endExtensionElementIfNecessary(
+ PartialResponseWriter partialResponseWriter,
+ boolean[] writingState) throws IOException {
+
+ if (writingState[0]) {
+ writingState[0] = false;
+
+ partialResponseWriter.endExtension();
+ }
+ }
+
+ public static void renderAjaxExtensions(FacesContext facesContext, UIComponent
component) throws IOException {
+ AjaxContext ajaxContext = AjaxContext.getCurrentInstance(facesContext);
+
+ Map<String, String> attributes = Collections.singletonMap(HTML.ID_ATTRIBUTE,
+ facesContext.getExternalContext().encodeNamespace(EXTENSION_ID));
+ PartialResponseWriter writer =
facesContext.getPartialViewContext().getPartialResponseWriter();
+ boolean[] writingState = new boolean[] {false};
+
+ 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);
+ }
+
+ 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);
+ }
+
+ Object responseData = ajaxContext.getResponseData();
+ if (responseData != null) {
+ startExtensionElementIfNecessary(writer, attributes, writingState);
+ writer.startElement(DATA_ELEMENT_NAME, component);
+ writer.writeText(ScriptUtils.toScript(responseData), null);
+ writer.endElement(DATA_ELEMENT_NAME);
+ }
+
+ endExtensionElementIfNecessary(writer, writingState);
+
+ }
+
}
Modified:
root/framework/trunk/impl/src/main/java/org/richfaces/context/PartialViewContextImpl.java
===================================================================
---
root/framework/trunk/impl/src/main/java/org/richfaces/context/PartialViewContextImpl.java 2009-11-06
18:17:16 UTC (rev 15850)
+++
root/framework/trunk/impl/src/main/java/org/richfaces/context/PartialViewContextImpl.java 2009-11-06
19:18:31 UTC (rev 15851)
@@ -47,9 +47,7 @@
import javax.faces.event.PhaseId;
import org.ajax4jsf.component.AjaxOutput;
-import org.ajax4jsf.context.AjaxContext;
-import org.ajax4jsf.javascript.ScriptUtils;
-import org.ajax4jsf.renderkit.RendererUtils.HTML;
+import org.ajax4jsf.renderkit.AjaxRendererUtils;
import org.richfaces.log.RichfacesLogger;
import org.slf4j.Logger;
@@ -65,12 +63,6 @@
private static final String PARTIAL_AJAX = "partial/ajax";
private static final String PARTIAL_PROCESS = "partial/process";
- private final static String EXTENSION_ID = "richfaces.extensions";
-
- 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 ORIGINAL_WRITER =
"org.richfaces.PartialViewContextImpl.ORIGINAL_WRITER";
private FacesContext facesContext;
@@ -254,11 +246,11 @@
processComponents(viewRoot, phaseId, ids, facesContext);
}
+ renderState(facesContext);
+
//TODO - render extensions for renderAll?
renderExtensions(facesContext, viewRoot);
- renderState(facesContext);
-
writer.endDocument();
} catch (IOException ex) {
//TODO - review?
@@ -400,35 +392,7 @@
}
protected void renderExtensions(FacesContext context, UIComponent component) throws
IOException {
- AjaxContext ajaxContext = AjaxContext.getCurrentInstance(facesContext);
- Map<String, String> attributes = Collections.singletonMap(HTML.ID_ATTRIBUTE,
- context.getExternalContext().encodeNamespace(EXTENSION_ID));
- PartialResponseWriter writer = getPartialResponseWriter();
-
- writer.startExtension(attributes);
-
- Object onbeforedomupdate = ajaxContext.getOnbeforedomupdate();
- if (onbeforedomupdate != null) {
- writer.startElement(BEFOREDOMUPDATE_ELEMENT_NAME, component);
- writer.writeText(onbeforedomupdate, null);
- writer.endElement(BEFOREDOMUPDATE_ELEMENT_NAME);
- }
-
- Object oncomplete = ajaxContext.getOncomplete();
- if (oncomplete != null) {
- writer.startElement(COMPLETE_ELEMENT_NAME, component);
- writer.writeText(oncomplete, null);
- writer.endElement(COMPLETE_ELEMENT_NAME);
- }
-
- Object responseData = ajaxContext.getResponseData();
- if (responseData != null) {
- writer.startElement(DATA_ELEMENT_NAME, component);
- writer.writeText(ScriptUtils.toScript(responseData), null);
- writer.endElement(DATA_ELEMENT_NAME);
- }
-
- writer.endExtension();
+ AjaxRendererUtils.renderAjaxExtensions(context, component);
}
private void assertNotReleased() {
Modified: root/framework/trunk/impl/src/main/resources/META-INF/resources/richfaces.js
===================================================================
---
root/framework/trunk/impl/src/main/resources/META-INF/resources/richfaces.js 2009-11-06
18:17:16 UTC (rev 15850)
+++
root/framework/trunk/impl/src/main/resources/META-INF/resources/richfaces.js 2009-11-06
19:18:31 UTC (rev 15851)
@@ -384,9 +384,60 @@
};
}
};
+
+ /**
+ * curry (g, a) (b) -> g(a, b)
+ */
+ var curry = function(g, a) {
+ var _g = g;
+ var _a = a;
+
+ return function(b) {
+ _g(_a, b);
+ };
+ };
+ var createEventHandler = function(handlerCode) {
+ if (handlerCode) {
+ return new Function("event", "data", handlerCode);
+ }
+
+ return null;
+ };
+
//TODO take events just from .java code using EL-expression
- var AJAX_EVENTS = ['begin', 'complete', 'beforedomupdate'];
+ var AJAX_EVENTS = (function() {
+ var serverEventHandler = function(clientHandler, event) {
+ var xml = jQuery("partial-response >
extension#org\\.richfaces\\.extension", 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 (serverHandler) {
+ serverHandler.call(window, event, data);
+ } else if (clientHandler) {
+ clientHandler.call(window, event, data);
+ }
+ };
+
+ return {
+ 'begin': null,
+ 'complete': serverEventHandler,
+ 'beforedomupdate': serverEventHandler
+ }
+ }());
richfaces.ajax = function(source, event, options) {
options = options || {};
@@ -402,13 +453,17 @@
var eventHandlers;
- for (var i = 0; i < AJAX_EVENTS.length; i++) {
- var eventName = AJAX_EVENTS[i];
- var eventHandlerOption = options[eventName];
+ for (var eventName in AJAX_EVENTS) {
+ var handler = createEventHandler(options[eventName]);
+
+ var serverHandler = AJAX_EVENTS[eventName];
+ if (serverHandler) {
+ handler = curry(serverHandler, handler);
+ }
- if (eventHandlerOption) {
+ if (handler) {
eventHandlers = eventHandlers || {};
- eventHandlers[eventName] = new Function('event', eventHandlerOption);
+ eventHandlers[eventName] = handler;
}
}
Modified:
root/ui/trunk/components/core/src/main/java/org/richfaces/renderkit/AjaxCommandRendererBase.java
===================================================================
---
root/ui/trunk/components/core/src/main/java/org/richfaces/renderkit/AjaxCommandRendererBase.java 2009-11-06
18:17:16 UTC (rev 15850)
+++
root/ui/trunk/components/core/src/main/java/org/richfaces/renderkit/AjaxCommandRendererBase.java 2009-11-06
19:18:31 UTC (rev 15851)
@@ -39,9 +39,7 @@
import org.ajax4jsf.renderkit.AjaxEventOptions;
import org.ajax4jsf.renderkit.HandlersChain;
import org.ajax4jsf.renderkit.RendererBase;
-
import org.richfaces.log.RichfacesLogger;
-
import org.slf4j.Logger;
/**