use of jakarta.faces.component.behavior.AjaxBehavior in project myfaces by apache.
the class AjaxHandler method applyAttachedObject.
/**
* This method should create an AjaxBehavior object and attach it to the
* parent component.
*
* Also, it should check if the parent can apply the selected AjaxBehavior
* to the selected component through ClientBehaviorHolder.getEventNames() or
* ClientBehaviorHolder.getDefaultEventName()
*/
@Override
public void applyAttachedObject(FacesContext context, UIComponent parent) {
// Retrieve the current FaceletContext from FacesContext object
FaceletContext faceletContext = (FaceletContext) context.getAttributes().get(FaceletContext.FACELET_CONTEXT_KEY);
// cast to a ClientBehaviorHolder
ClientBehaviorHolder cvh = (ClientBehaviorHolder) parent;
String eventName = null;
if (_event != null) {
if (_event.isLiteral()) {
eventName = _event.getValue();
} else {
eventName = (String) _event.getValueExpression(faceletContext, String.class).getValue(faceletContext);
}
}
if (eventName == null) {
eventName = cvh.getDefaultEventName();
if (eventName == null) {
if (_wrapMode) {
// for this attached object.
return;
} else {
throw new TagAttributeException(_event, "eventName could not be defined for f:ajax tag with no wrap mode.");
}
}
} else if (!cvh.getEventNames().contains(eventName)) {
if (_wrapMode) {
// component could not be the target for this attached object.
return;
} else {
throw new TagAttributeException(_event, "event it is not a valid eventName defined for this component");
}
}
Map<String, List<ClientBehavior>> clientBehaviors = cvh.getClientBehaviors();
List<ClientBehavior> clientBehaviorList = clientBehaviors.get(eventName);
if (clientBehaviorList != null && !clientBehaviorList.isEmpty()) {
for (ClientBehavior cb : clientBehaviorList) {
if (cb instanceof AjaxBehavior) {
// the outer one.
return;
}
}
}
AjaxBehavior ajaxBehavior = createBehavior(context);
if (_disabled != null) {
if (_disabled.isLiteral()) {
ajaxBehavior.setDisabled(_disabled.getBoolean(faceletContext));
} else {
ajaxBehavior.setValueExpression("disabled", _disabled.getValueExpression(faceletContext, Boolean.class));
}
}
if (_execute != null) {
ajaxBehavior.setValueExpression("execute", _execute.getValueExpression(faceletContext, Object.class));
}
if (_immediate != null) {
if (_immediate.isLiteral()) {
ajaxBehavior.setImmediate(_immediate.getBoolean(faceletContext));
} else {
ajaxBehavior.setValueExpression("immediate", _immediate.getValueExpression(faceletContext, Boolean.class));
}
}
if (_listener != null) {
MethodExpression expr = _listener.getMethodExpression(faceletContext, Void.TYPE, AJAX_BEHAVIOR_LISTENER_SIG);
AjaxBehaviorListener abl = new AjaxBehaviorListenerImpl(expr);
ajaxBehavior.addAjaxBehaviorListener(abl);
}
if (_onerror != null) {
if (_onerror.isLiteral()) {
ajaxBehavior.setOnerror(_onerror.getValue(faceletContext));
} else {
ajaxBehavior.setValueExpression("onerror", _onerror.getValueExpression(faceletContext, String.class));
}
}
if (_onevent != null) {
if (_onevent.isLiteral()) {
ajaxBehavior.setOnevent(_onevent.getValue(faceletContext));
} else {
ajaxBehavior.setValueExpression("onevent", _onevent.getValueExpression(faceletContext, String.class));
}
}
if (_render != null) {
ajaxBehavior.setValueExpression("render", _render.getValueExpression(faceletContext, Object.class));
}
if (_delay != null) {
if (_delay.isLiteral()) {
ajaxBehavior.setDelay(_delay.getValue(faceletContext));
} else {
ajaxBehavior.setValueExpression("delay", _delay.getValueExpression(faceletContext, String.class));
}
}
if (_resetValues != null) {
if (_resetValues.isLiteral()) {
ajaxBehavior.setResetValues(_resetValues.getBoolean(faceletContext));
} else {
ajaxBehavior.setValueExpression("resetValues", _resetValues.getValueExpression(faceletContext, Boolean.class));
}
}
// map @this in a composite to @composite
if (parent instanceof ClientBehaviorRedirectEventComponentWrapper) {
UIComponent composite = ((ClientBehaviorRedirectEventComponentWrapper) parent).getComposite();
if (composite != null) {
Collection<String> execute = ajaxBehavior.getExecute();
if (execute != null && execute.contains("@this")) {
Collection<String> newExecute = new ArrayList<>(execute);
newExecute.remove("@this");
newExecute.add("@composite");
ajaxBehavior.setExecute(newExecute);
}
Collection<String> render = ajaxBehavior.getRender();
if (render != null && render.contains("@this")) {
Collection<String> newRender = new ArrayList<>(render);
newRender.remove("@this");
newRender.add("@composite");
ajaxBehavior.setRender(newRender);
}
}
}
cvh.addClientBehavior(eventName, ajaxBehavior);
}
use of jakarta.faces.component.behavior.AjaxBehavior in project myfaces by apache.
the class HtmlSecretRendererTest method testClientBehaviorHolderRendersIdAndName.
/**
* Components that render client behaviors should always render "id" and "name" attribute
*/
public void testClientBehaviorHolderRendersIdAndName() {
inputText.addClientBehavior("keypress", new AjaxBehavior());
try {
inputText.encodeAll(facesContext);
String output = ((StringWriter) writer.getWriter()).getBuffer().toString();
Assert.assertTrue(output.matches(".+id=\".+\".+"));
Assert.assertTrue(output.matches(".+name=\".+\".+"));
} catch (Exception e) {
Assert.fail(e.getMessage());
}
}
use of jakarta.faces.component.behavior.AjaxBehavior in project myfaces by apache.
the class HtmlTableRendererTest method testClientBehaviorHolderRendersIdAndName.
/**
* Components that render client behaviors should always render "id" and "name" attribute
*/
public void testClientBehaviorHolderRendersIdAndName() {
dataTable.addClientBehavior("keypress", new AjaxBehavior());
try {
dataTable.encodeAll(facesContext);
String output = ((StringWriter) writer.getWriter()).getBuffer().toString();
Assert.assertTrue(output.matches("(?s).+id=\".+\".+"));
Assert.assertTrue(output.matches("(?s).+name=\".+\".+"));
} catch (Exception e) {
Assert.fail(e.getMessage());
}
}
use of jakarta.faces.component.behavior.AjaxBehavior in project myfaces by apache.
the class HtmlTextRendererTest method testClientBehaviorUserCodeJavaScriptEscaping.
/**
* Tests if a JavaScript user code is correctly escaped.
* e.g. alert('test') has to become alert(\'test\')
*/
public void testClientBehaviorUserCodeJavaScriptEscaping() {
inputText.getAttributes().put("onchange", "alert('test')");
inputText.addClientBehavior("change", new AjaxBehavior());
try {
inputText.encodeAll(facesContext);
String output = ((StringWriter) writer.getWriter()).getBuffer().toString();
// onchange="faces.util.chain(document.getElementById('j_id0'), event,
// 'alert(\'test\')');"
Assert.assertTrue(output.contains("'alert(\\'test\\')'"));
} catch (Exception e) {
Assert.fail(e.getMessage());
}
}
use of jakarta.faces.component.behavior.AjaxBehavior in project myfaces by apache.
the class AbstractClientBehaviorTestCase method testClientBehaviorRenderedWithHtmlAttribute.
@Test
public void testClientBehaviorRenderedWithHtmlAttribute() {
HtmlRenderedClientEventAttr[] attrs = getClientBehaviorHtmlRenderedAttributes();
for (int i = 0; i < attrs.length; i++) {
UIComponent component = createComponentToTest();
ClientBehaviorHolder clientBehaviorHolder = (ClientBehaviorHolder) component;
clientBehaviorHolder.addClientBehavior(attrs[i].getClientEvent(), new AjaxBehavior());
component.getAttributes().put(attrs[i].getName(), "htmlValue");
try {
component.encodeAll(facesContext);
String output = outputWriter.toString();
// faces.ajax.request('j_id0',event,{'jakarta.faces.behavior.event':'click'})
// Only check if the property starts with faces.ajax.request( is enough
// Assert.assertTrue("output does not match expected output faces.ajax.request(.... for property "+attrs[i].getName(),
// output.matches(".+ "+attrs[i].getName()+"=\"faces\\.ajax\\.request\\(.+"));
int index = checkClientBehaviorRenderedOnClientEventPropertyAndHtmlValue(output, 0, attrs[i], "htmlValue");
outputWriter.reset();
} catch (Exception e) {
Assert.fail(e.getMessage());
}
}
}
Aggregations