Search in sources :

Example 1 with AjaxBehavior

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);
}
Also used : TagAttributeException(jakarta.faces.view.facelets.TagAttributeException) AjaxBehaviorListener(jakarta.faces.event.AjaxBehaviorListener) UIComponent(jakarta.faces.component.UIComponent) ArrayList(java.util.ArrayList) ClientBehaviorHolder(jakarta.faces.component.behavior.ClientBehaviorHolder) MethodExpression(jakarta.el.MethodExpression) AjaxBehavior(jakarta.faces.component.behavior.AjaxBehavior) ClientBehaviorRedirectEventComponentWrapper(org.apache.myfaces.view.facelets.tag.composite.ClientBehaviorRedirectEventComponentWrapper) FaceletContext(jakarta.faces.view.facelets.FaceletContext) AbstractFaceletContext(org.apache.myfaces.view.facelets.AbstractFaceletContext) ArrayList(java.util.ArrayList) List(java.util.List) ClientBehavior(jakarta.faces.component.behavior.ClientBehavior)

Example 2 with 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());
    }
}
Also used : AjaxBehavior(jakarta.faces.component.behavior.AjaxBehavior)

Example 3 with AjaxBehavior

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());
    }
}
Also used : AjaxBehavior(jakarta.faces.component.behavior.AjaxBehavior)

Example 4 with AjaxBehavior

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(&apos;j_id0&apos;), event,
        // &apos;alert(\&apos;test\&apos;)&apos;);"
        Assert.assertTrue(output.contains("&apos;alert(\\&apos;test\\&apos;)&apos;"));
    } catch (Exception e) {
        Assert.fail(e.getMessage());
    }
}
Also used : IOException(java.io.IOException) ConverterException(jakarta.faces.convert.ConverterException) AjaxBehavior(jakarta.faces.component.behavior.AjaxBehavior)

Example 5 with AjaxBehavior

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());
        }
    }
}
Also used : UIComponent(jakarta.faces.component.UIComponent) ClientBehaviorHolder(jakarta.faces.component.behavior.ClientBehaviorHolder) AjaxBehavior(jakarta.faces.component.behavior.AjaxBehavior) Test(org.junit.Test)

Aggregations

AjaxBehavior (jakarta.faces.component.behavior.AjaxBehavior)57 UIComponent (jakarta.faces.component.UIComponent)14 PrintWriter (java.io.PrintWriter)11 ClientBehaviorHolder (jakarta.faces.component.behavior.ClientBehaviorHolder)9 UISelectItem (jakarta.faces.component.UISelectItem)6 FacesContext (jakarta.faces.context.FacesContext)6 ArrayList (java.util.ArrayList)6 IOException (java.io.IOException)5 Test (org.junit.Test)4 ClientBehavior (jakarta.faces.component.behavior.ClientBehavior)3 ConverterException (jakarta.faces.convert.ConverterException)3 EventBehavior (org.apache.myfaces.tobago.internal.behavior.EventBehavior)3 AbstractUIEvent (org.apache.myfaces.tobago.internal.component.AbstractUIEvent)3 AjaxBehaviors (com.sun.faces.component.behavior.AjaxBehaviors)2 MethodExpression (jakarta.el.MethodExpression)2 ValueExpression (jakarta.el.ValueExpression)2 UIViewRoot (jakarta.faces.component.UIViewRoot)2 AjaxBehaviorEvent (jakarta.faces.event.AjaxBehaviorEvent)2 AjaxBehaviorListener (jakarta.faces.event.AjaxBehaviorListener)2 FaceletContext (jakarta.faces.view.facelets.FaceletContext)2