Support for Ajax based redirects, and modal view states.

This commit is contained in:
Jeremy Grelle
2008-01-23 22:52:53 +00:00
parent e939e7d546
commit 856f9ce201
15 changed files with 564 additions and 51 deletions

View File

@@ -104,6 +104,23 @@ SpringFaces.DojoAjaxHandler.prototype = {
handleResponse: function(response, ioArgs) {
//alert("handling the response");
//First check if this response should redirect
var redirectURL = ioArgs.xhr.getResponseHeader('Flow-Redirect-URL');
var modalViewHeader = ioArgs.xhr.getResponseHeader('Flow-Modal-View');
var modalView = dojo.isString(modalViewHeader) && modalViewHeader.length > 0;
if (dojo.isString(redirectURL) && redirectURL.length > 0) {
if (modalView) {
//render a popup with the new URL
SpringFaces.AjaxHandler.renderURLToModalDialog(redirectURL, ioArgs);
return response;
}
else {
window.location.pathname = redirectURL;
return response;
}
}
//Extract and store all <script> elements from the response
var scriptPattern = '(?:<script.*?>)((\n|\r|.)*?)(?:<\/script>)';
var extractedScriptNodes = [];
@@ -131,6 +148,12 @@ SpringFaces.DojoAjaxHandler.prototype = {
var newNodes = tempContainer.addContent(response, "first").query("#ajaxResponse > *").orphan();
tempContainer.orphan();
//For a modal view, just dump the new nodes into a modal dialog
if (modalView) {
SpringFaces.AjaxHandler.renderNodeListToModalDialog(newNodes);
return response;
}
//Insert the new DOM nodes and update the Form's action URL
newNodes.forEach(function(item) {
if (item.id != null && item.id != "") {
@@ -151,6 +174,24 @@ SpringFaces.DojoAjaxHandler.prototype = {
//alert("handling an error");
console.error("HTTP status code: ", ioArgs.xhr.status);
return response;
},
renderURLToModalDialog: function(url, ioArgs) {
dojo.require("dijit.Dialog");
url = url + "?"+dojo.objectToQuery(ioArgs.args.content);
var dialog = new dijit.Dialog({href: url});
dialog.show();
},
renderNodeListToModalDialog: function(nodes) {
dojo.require("dijit.Dialog");
var dialog = new dijit.Dialog({});
dialog.setContent(nodes);
dialog.show();
}
};

View File

@@ -0,0 +1,252 @@
SpringUI.DojoValidatingFieldAdvisor = function(config){
dojo.mixin(this, config);
};
SpringUI.DojoValidatingFieldAdvisor.prototype = {
targetElId : "",
decoratorType : "",
decorator : null,
decoratorAttrs : "",
apply : function(){
this.decorator = eval("new "+ this.decoratorType + "(" + this.decoratorAttrs +", dojo.byId('"+this.targetElId+"'));" );
this.decorator.startup();
//return this to support method chaining
return this;
},
validate : function(){
var isValid = this.decorator.isValid(false);
if (!isValid) {
this.decorator.state = "Error";
this.decorator._setStateClass();
}
return isValid;
}
};
SpringUI.ValidatingFieldAdvisor = SpringUI.DojoValidatingFieldAdvisor;
SpringUI.DojoRemoteEventAdvisor = function(config){
dojo.mixin(this, config);
};
SpringUI.DojoRemoteEventAdvisor.prototype = {
event : "",
targetId : "",
sourceId : "",
formId : "",
processIds : "",
renderIds : "",
params : [],
connection : null,
apply : function() {
connection = dojo.connect(dojo.byId(this.targetId), this.event, this, "submit");
return this;
},
cleanup : function(){
dojo.disconnect(this.connection);
},
submit : function(event){
if (this.sourceId == ""){
this.sourceId = this.targetId;
}
if(this.formId == ""){
SpringUI.RemotingHandler.getResource(this.sourceId, this.processIds, this.renderIds);
} else {
SpringUI.RemotingHandler.submitForm(this.sourceId, this.formId, this.processIds, this.renderIds, this.params);
}
dojo.stopEvent(event);
}
};
SpringUI.RemoteEventAdvisor = SpringUI.DojoRemoteEventAdvisor;
SpringUI.DojoRemotingHandler = function(){};
SpringUI.DojoRemotingHandler.prototype = {
submitForm : function(/*String */ sourceId, /*String*/formId, /*String*/ processIds, /*String*/renderIds, /*Array*/ params) {
var content = new Object();
var sourceComponent = dojo.byId(sourceId);
content['processIds'] = processIds;
content['renderIds'] = renderIds;
if (sourceComponent != null){
if(sourceComponent.value) {
content[sourceComponent.name] = sourceComponent.value;
} else {
content[sourceComponent.name] = sourceId;
}
}
dojo.forEach(params, function(param){
content[param.name] = param.value;
});
content['ajaxSource'] = sourceId;
dojo.xhrPost({
content: content,
form: formId,
handleAs: "text",
headers: {"Accept" : "text/html;type=ajax"},
// The LOAD function will be called on a successful response.
load: this.handleResponse,
// The ERROR function will be called in an error case.
error: this.handleError
});
},
getResource: function(/*String */ sourceId, /*String*/ processIds, /*String*/renderIds) {
var content = new Object();
var sourceComponent = dojo.byId(sourceId);
content['processIds'] = processIds;
content['renderIds'] = renderIds;
content['ajaxSource'] = sourceId;
dojo.xhrGet({
url: sourceComponent.href,
content: content,
handleAs: "text",
headers: {"Accept" : "text/html;type=ajax"},
load: this.handleResponse,
error: this.handleError
});
},
handleResponse: function(response, ioArgs) {
//alert("handling the response");
//Extract and store all <script> elements from the response
var scriptPattern = '(?:<script.*?>)((\n|\r|.)*?)(?:<\/script>)';
var extractedScriptNodes = [];
var matchAll = new RegExp(scriptPattern, 'img');
var matchOne = new RegExp(scriptPattern, 'im');
var scriptNodes = response.match(matchAll);
if (scriptNodes != null)
{
for (var i=0; i<scriptNodes.length; i++)
{
var script = (scriptNodes[i].match(matchOne) || ['',''])[1];
script = script.replace(/<!--/mg,'').replace(/\/\/-->/mg,'');
extractedScriptNodes.push(script);
}
}
response = response.replace(matchAll, '');
//Extract the new DOM nodes from the response
var tempDiv = dojo.doc.createElement("div");
tempDiv.id="ajaxResponse";
tempDiv.style.visibility= "hidden";
document.body.appendChild(tempDiv);
var tempContainer = new dojo.NodeList(tempDiv);
var newNodes = tempContainer.addContent(response, "first").query("#ajaxResponse > *").orphan();
tempContainer.orphan();
//Insert the new DOM nodes and update the Form's action URL
newNodes.forEach(function(item) {
if (item.id == 'flowExecutionUrl'){
dojo.query("form").forEach(function (formNode) {
formNode.action = item.firstChild.nodeValue;
});
} else if (item.id != null && item.id != "") {
var target = dojo.byId(item.id);
target.parentNode.replaceChild(item, target);
}
});
//Evaluate any script code
dojo.forEach(extractedScriptNodes, function(script){
dojo.eval(script);
});
return response;
},
handleError: function(response, ioArgs) {
//alert("handling an error");
console.error("HTTP status code: ", ioArgs.xhr.status);
return response;
}
};
SpringUI.RemotingHandler = new SpringUI.DojoRemotingHandler();
SpringUI.DojoSubmitLinkAdvisor = function(config){
dojo.mixin(this, config);
};
SpringUI.DojoSubmitLinkAdvisor.prototype = {
targetElId : "",
linkHtml : "",
apply : function(){
var advisedNode = dojo.byId(this.targetElId);
if (!dojo.hasClass(advisedNode, "progressiveLink")) {
//Node must be replaced
var nodeToReplace = new dojo.NodeList(advisedNode);
nodeToReplace.addContent(this.linkHtml, "after").orphan("*");
//Get the new node
advisedNode = dojo.byId(this.targetElId);
}
advisedNode.submitFormFromLink = this.submitFormFromLink;
//return this to support method chaining
return this;
},
submitFormFromLink : function(/*String*/ formId, /*String*/ sourceId, /*Array of name,value params*/ params){
var addedNodes = [];
var formNode = dojo.byId(formId);
var linkNode = document.createElement("input");
linkNode.name = sourceId;
linkNode.value = "submitted";
addedNodes.push(linkNode);
dojo.forEach(params, function(param){
var paramNode = document.createElement("input");
paramNode.name=param.name;
paramNode.value=param.value;
addedNodes.push(paramNode);
});
dojo.forEach(addedNodes, function(nodeToAdd){
dojo.addClass(nodeToAdd, "SpringUILinkInput");
dojo.place(nodeToAdd, formNode, "last");
});
if ((formNode.onsubmit ? !formNode.onsubmit() : false) || !formNode.submit()) {
dojo.forEach(addedNodes, function(hiddenNode){
formNode.removeChild(hiddenNode);
});
}
}
};
SpringUI.SubmitLinkAdvisor = SpringUI.DojoSubmitLinkAdvisor;
dojo.addOnLoad(SpringUI.applyAdvisors);

View File

@@ -0,0 +1,85 @@
SpringUI = {};
SpringUI.advisors = [];
SpringUI.advisorsApplied = false;
SpringUI.applyAdvisors = function(){
if (!SpringUI.advisorsApplied) {
for (var x=0; x<SpringUI.advisors.length; x++) {
SpringUI.advisors[x].apply();
}
SpringUI.advisorsApplied = true;
}
};
SpringUI.validateAll = function(){
var valid = true;
for(x in SpringUI.advisors) {
if (SpringUI.advisors[x].decorator &&
!SpringUI.advisors[x].validate()) {
valid = false;
}
}
return valid;
};
SpringUI.validateRequired = function(){
var valid = true;
for(x in SpringUI.advisors) {
if(SpringUI.advisors[x].decorator &&
SpringUI.advisors[x].isRequired() &&
!SpringUI.advisors[x].validate())
valid = false;
}
return valid;
};
SpringUI.ValidatingFieldAdvisor = function(){};
SpringUI.ValidatingFieldAdvisor.prototype = {
targetElId : "",
decoratorType : "",
decorator : null,
decoratorAttrs : "",
apply : function(){},
validate : function(){},
isRequired : function(){}
};
SpringUI.RemoteEventAdvisor = function(){};
SpringUI.RemoteEventAdvisor.prototype = {
event : "",
targetId : "",
sourceId : "",
formId : "",
processIds : "",
renderIds : "",
params : [],
connection : null,
apply : function(){},
cleanup : function(){},
submit : function(event){}
};
SpringUI.RemotingHandler = function(){};
SpringUI.RemotingHandler.prototype = {
submitForm : function(/*String */ sourceId, /*String*/formId, /*String*/ processIds, /*String*/renderIds, /*Array*/ params){},
getResource : function(/*String */ sourceId, /*String*/ processIds, /*String*/renderIds) {},
handleResponse : function() {},
handleError : function() {}
};

View File

@@ -35,6 +35,8 @@ public class AjaxViewRoot extends DelegatingViewRoot {
protected static final String FORM_RENDERED = "formRendered";
protected static final String PROCESS_ALL = "*";
private List events = new ArrayList();
private String[] processIds;
@@ -150,8 +152,12 @@ public class AjaxViewRoot extends DelegatingViewRoot {
private void processRequestParams(FacesContext context) {
String processIdsParam = (String) context.getExternalContext().getRequestParameterMap().get(PROCESS_IDS_PARAM);
processIds = StringUtils.delimitedListToStringArray(processIdsParam, ",", " ");
processIds = removeNestedChildren(context, processIds);
if (StringUtils.hasText(processIdsParam) && processIdsParam.contains("*")) {
processIds = new String[] { context.getViewRoot().getClientId(context) };
} else {
processIds = StringUtils.delimitedListToStringArray(processIdsParam, ",", " ");
processIds = removeNestedChildren(context, processIds);
}
String renderIdsParam = (String) context.getExternalContext().getRequestParameterMap().get(RENDER_IDS_PARAM);
renderIds = StringUtils.delimitedListToStringArray(renderIdsParam, ",", " ");

View File

@@ -59,7 +59,7 @@ public class JsfUtils {
}
public static boolean isAsynchronousFlowRequest() {
if (isFlowRequest() && RequestContextHolder.getRequestContext().getRequestParameters().contains("ajaxSource")) {
if (isFlowRequest() && RequestContextHolder.getRequestContext().getExternalContext().isAjaxRequest()) {
return true;
} else {
return false;

View File

@@ -118,7 +118,7 @@ public class JsfViewFactory implements ViewFactory {
}
private JsfView createJsfView(UIViewRoot root, Lifecycle lifecycle, RequestContext context) {
if (JsfUtils.isAsynchronousFlowRequest()) {
if (context.getExternalContext().isAjaxRequest()) {
return new JsfView(new AjaxViewRoot(root), lifecycle, context);
} else {
return new JsfView(root, lifecycle, context);