Skip to content

Commit

Permalink
add base classes for normal and ajax dynamic elements
Browse files Browse the repository at this point in the history
  • Loading branch information
darkv committed Jun 11, 2012
1 parent 366b543 commit 84661f5
Show file tree
Hide file tree
Showing 2 changed files with 401 additions and 0 deletions.
146 changes: 146 additions & 0 deletions Frameworks/Ajax/Ajax/Sources/er/ajax/ERXAjaxDynamicElement.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
package er.ajax;

import com.webobjects.appserver.WOActionResults;
import com.webobjects.appserver.WOAssociation;
import com.webobjects.appserver.WOContext;
import com.webobjects.appserver.WOElement;
import com.webobjects.appserver.WORequest;
import com.webobjects.appserver.WOResponse;
import com.webobjects.foundation.NSDictionary;
import com.webobjects.foundation.NSMutableArray;

import er.extensions.appserver.ajax.ERXAjaxApplication;
import er.extensions.components.ERXDynamicElement;

/**
* ERXAjaxDynamicElement provides a common base class for dynamic Ajax elements.
*
* @author jw
*/
public abstract class ERXAjaxDynamicElement extends ERXDynamicElement implements IAjaxElement {

public ERXAjaxDynamicElement(String name, NSDictionary<String, WOAssociation> associations, WOElement template) {
super(name, associations, template);
}

public ERXAjaxDynamicElement(String name, NSDictionary<String, WOAssociation> associations, NSMutableArray<WOElement> children) {
super(name, associations, children);
}

/**
* Execute the request, if it's coming from our action, then invoke the ajax handler and put the key
* <code>AJAX_REQUEST_KEY</code> in the request userInfo dictionary (<code>request.userInfo()</code>).
*
* @param request the current request
* @param context context of the transaction
* @return the action results
*/
@Override
public WOActionResults invokeAction(WORequest request, WOContext context) {
WOActionResults result = null;
if (shouldHandleRequest(request, context)) {
result = handleRequest(request, context);
AjaxUtils.updateMutableUserInfoWithAjaxInfo(context);
if (ERXAjaxApplication.shouldIgnoreResults(request, context, result)) {
log.warn("An Ajax request attempted to return the page, which is almost certainly an error.");
result = null;
}
if (result == null && !ERXAjaxApplication.isAjaxReplacement(request)) {
result = AjaxUtils.createResponse(request, context);
}
} else if (hasChildrenElements()) {
result = super.invokeAction(request, context);
}
return result;
}

/**
* Checks if the current request should be handled by this element.
*
* @param request the current request
* @param context context of the transaction
* @return <code>true</code> if we should handle the request
*/
protected boolean shouldHandleRequest(WORequest request, WOContext context) {
return AjaxUtils.shouldHandleRequest(request, context, id(context));
}

/**
* Overridden to call {@link #addRequiredWebResources(WOResponse, WOContext)}.
*
* @param response the current response
* @param context context of the transaction
*/
@Override
public void appendToResponse(WOResponse response, WOContext context) {
addRequiredWebResources(response, context);
}

/**
* Override this method to append the needed scripts for this component.
*
* @param request the current request
* @param context context of the transaction
*/
protected abstract void addRequiredWebResources(WOResponse response, WOContext context);

/**
* Override this method to return the response for an Ajax request.
*
* @param request the current request
* @param context context of the transaction
*/
public abstract WOActionResults handleRequest(WORequest request, WOContext context);

/**
* Adds a script link tag with a correct resource URL in the HTML head tag if it isn't
* already present in the response.
*
* @param context context of the transaction
* @param response the current response
* @param framework name of the framework that contains the file
* @param fileName script file name
*/
protected void addScriptResourceInHead(WOContext context, WOResponse response, String framework, String fileName) {
AjaxUtils.addScriptResourceInHead(context, response, framework, fileName);
}

/**
* Adds a script link tag with a correct resource URL in the HTML head tag if it isn't
* already present in the response. The script file will be searched within the
* Ajax framework.
*
* @param context context of the transaction
* @param response the current response
* @param fileName script file name
*/
protected void addScriptResourceInHead(WOContext context, WOResponse response, String fileName) {
AjaxUtils.addScriptResourceInHead(context, response, fileName);
}

/**
* Adds a stylesheet link tag with a correct resource URL in the HTML head
* tag if it isn't already present in the response.
*
* @param context context of the transaction
* @param response the current response
* @param framework name of the framework that contains the file
* @param fileName CSS file name
*/
protected void addStylesheetResourceInHead(WOContext context, WOResponse response, String framework, String fileName) {
AjaxUtils.addStylesheetResourceInHead(context, response, framework, fileName);
}

/**
* Adds a stylesheet link tag with a correct resource URL in the HTML head
* tag if it isn't already present in the response. The CSS file will be searched
* within the Ajax framework.
*
* @param context context of the transaction
* @param response the current response
* @param fileName CSS file name
*/
protected void addStylesheetResourceInHead(WOContext context, WOResponse response, String fileName) {
AjaxUtils.addStylesheetResourceInHead(context, response, fileName);
}
}
Loading

0 comments on commit 84661f5

Please sign in to comment.