Search in sources :

Example 1 with HtmlCommandLink

use of jakarta.faces.component.html.HtmlCommandLink in project myfaces by apache.

the class HtmlLinkRendererBase method renderJavaScriptAnchorStart.

protected void renderJavaScriptAnchorStart(FacesContext facesContext, ResponseWriter writer, UIComponent component, String clientId, UIComponent form) throws IOException {
    StringBuilder onClick = SharedStringBuilder.get(facesContext, SB_BUILD_ONCLICK);
    String commandOnclick;
    if (component instanceof HtmlCommandLink) {
        commandOnclick = ((HtmlCommandLink) component).getOnclick();
    } else {
        commandOnclick = (String) component.getAttributes().get(HTML.ONCLICK_ATTR);
    }
    if (commandOnclick != null) {
        onClick.append("var cf = function(){");
        onClick.append(commandOnclick);
        onClick.append('}');
        onClick.append(';');
        onClick.append("var oamSF = function(){");
    }
    StringBuilder params = addChildParameters(facesContext, component, form);
    String target = getTarget(component);
    onClick.append("return ").append(HtmlRendererUtils.SUBMIT_FORM_FN_NAME_JSF2).append("('").append(form.getClientId(facesContext)).append("','").append(clientId).append('\'');
    if (params.length() > 2 || target != null) {
        onClick.append(',').append(target == null ? "null" : ('\'' + target + '\'')).append(',').append(params);
    }
    onClick.append(");");
    if (commandOnclick != null) {
        onClick.append('}');
        onClick.append(';');
        onClick.append("return (cf.apply(this, [])==false)? false : oamSF.apply(this, []); ");
    }
    writer.startElement(HTML.ANCHOR_ELEM, component);
    writer.writeURIAttribute(HTML.HREF_ATTR, "#", null);
    writer.writeAttribute(HTML.ONCLICK_ATTR, onClick.toString(), null);
}
Also used : SharedStringBuilder(org.apache.myfaces.core.api.shared.lang.SharedStringBuilder) HtmlCommandLink(jakarta.faces.component.html.HtmlCommandLink)

Example 2 with HtmlCommandLink

use of jakarta.faces.component.html.HtmlCommandLink in project myfaces by apache.

the class HtmlCommandLinkClientBehaviorRendererTest method createComponentToTest.

@Override
protected UIComponent createComponentToTest() {
    UIComponent component = new HtmlCommandLink();
    UIComponent form = new HtmlForm();
    form.getChildren().add(component);
    facesContext.getViewRoot().getChildren().add(form);
    return component;
}
Also used : HtmlForm(jakarta.faces.component.html.HtmlForm) HtmlCommandLink(jakarta.faces.component.html.HtmlCommandLink) UIComponent(jakarta.faces.component.UIComponent)

Example 3 with HtmlCommandLink

use of jakarta.faces.component.html.HtmlCommandLink in project myfaces by apache.

the class CompositeComponentTestCase method testSimpleInsertChildrenAjax2.

@Test
public void testSimpleInsertChildrenAjax2() throws Exception {
    HelloWorld helloWorld = new HelloWorld();
    facesContext.getExternalContext().getRequestMap().put("helloWorldBean", helloWorld);
    UIViewRoot root = facesContext.getViewRoot();
    vdl.buildView(facesContext, root, "testSimpleInsertChildrenAjax2.xhtml");
    UIComponent panelGroup1 = root.findComponent("testGroup1");
    Assert.assertNotNull(panelGroup1);
    UINamingContainer compositeComponent1 = (UINamingContainer) panelGroup1.getChildren().get(0);
    Assert.assertNotNull(compositeComponent1);
    UIComponent facet1 = compositeComponent1.getFacet(UIComponent.COMPOSITE_FACET_NAME);
    Assert.assertNotNull(facet1);
    HtmlCommandLink link = (HtmlCommandLink) compositeComponent1.findComponent("link");
    Assert.assertNotNull(link);
    Assert.assertEquals(1, link.getClientBehaviors().size());
    Assert.assertEquals(1, link.getClientBehaviors().get(link.getDefaultEventName()).size());
/*
        StringWriter sw = new StringWriter();
        MockResponseWriter mrw = new MockResponseWriter(sw);
        facesContext.setResponseWriter(mrw);
        
        root.encodeAll(facesContext);
        sw.flush();

        String resp = sw.toString();
        */
// System.out.print(sw.toString());
}
Also used : HtmlCommandLink(jakarta.faces.component.html.HtmlCommandLink) UIComponent(jakarta.faces.component.UIComponent) UINamingContainer(jakarta.faces.component.UINamingContainer) UIViewRoot(jakarta.faces.component.UIViewRoot) HelloWorld(org.apache.myfaces.view.facelets.bean.HelloWorld) Test(org.junit.Test)

Example 4 with HtmlCommandLink

use of jakarta.faces.component.html.HtmlCommandLink in project myfaces by apache.

the class DefaultHtmlDecoratorTestCaseUrn method checkTags.

private void checkTags() throws Exception {
    UIViewRoot root = facesContext.getViewRoot();
    HtmlHead head = (HtmlHead) root.findComponent("idHead");
    int linksFound = 0;
    int scriptsFound = 0;
    for (UIComponent child : head.getChildren()) {
        if (child instanceof UIOutput) {
            if (ResourceUtils.DEFAULT_SCRIPT_RENDERER_TYPE.equals(child.getRendererType())) {
                Assert.assertEquals("osc", child.getId());
                scriptsFound++;
            }
            if (ResourceUtils.DEFAULT_STYLESHEET_RENDERER_TYPE.equals(child.getRendererType())) {
                Assert.assertEquals("osh", child.getId());
                linksFound++;
            }
        }
    }
    for (UIComponent child : root.getComponentResources(facesContext, "head")) {
        if ("jakarta.faces.resource.Stylesheet".equals(child.getRendererType())) {
            Assert.assertEquals("osh", child.getId());
            linksFound++;
        }
    }
    Assert.assertEquals(1, linksFound);
    Assert.assertEquals(1, scriptsFound);
    HtmlBody body = (HtmlBody) root.findComponent("idBody");
    Assert.assertNotNull(body);
    UIForm form = (UIForm) root.findComponent("myForm");
    Assert.assertNotNull(form);
    HtmlCommandLink link1 = (HtmlCommandLink) form.findComponent("link1");
    Assert.assertNotNull(link1);
    Assert.assertEquals("#{test.testAction}", link1.getActionExpression().getExpressionString());
    HtmlCommandLink link2 = (HtmlCommandLink) form.findComponent("link2");
    Assert.assertNotNull(link2);
    Assert.assertEquals(1, link2.getActionListeners().length);
    HtmlOutputLink link3 = (HtmlOutputLink) form.findComponent("link3");
    Assert.assertNotNull(link3);
    Assert.assertEquals("/my/new/location.txt", link3.getValue());
    HtmlOutcomeTargetLink link4 = (HtmlOutcomeTargetLink) form.findComponent("link4");
    Assert.assertNotNull(link4);
    Assert.assertEquals("rollback", link4.getOutcome());
    HtmlCommandButton button = (HtmlCommandButton) form.findComponent("button1");
    Assert.assertNotNull(button);
    Assert.assertEquals("#{test.testAction}", button.getActionExpression().getExpressionString());
    HtmlOutcomeTargetButton button2 = (HtmlOutcomeTargetButton) form.findComponent("button2");
    Assert.assertNotNull(button2);
    Assert.assertEquals("rollback", button2.getOutcome());
    HtmlGraphicImage img1 = (HtmlGraphicImage) form.findComponent("img1");
    Assert.assertNotNull(img1);
    Assert.assertEquals("/my/image.png", img1.getUrl());
    HtmlCommandButton input1 = (HtmlCommandButton) form.findComponent("input1");
    Assert.assertNotNull(input1);
    HtmlSelectBooleanCheckbox input2 = (HtmlSelectBooleanCheckbox) form.findComponent("input2");
    Assert.assertNotNull(input2);
    HtmlInputText input3 = (HtmlInputText) form.findComponent("input3");
    Assert.assertNotNull(input3);
    HtmlInputText input4 = (HtmlInputText) form.findComponent("input4");
    Assert.assertNotNull(input4);
    HtmlInputText input5 = (HtmlInputText) form.findComponent("input5");
    Assert.assertNotNull(input5);
    HtmlInputText input6 = (HtmlInputText) form.findComponent("input6");
    Assert.assertNotNull(input6);
    HtmlInputText input7 = (HtmlInputText) form.findComponent("input7");
    Assert.assertNotNull(input7);
    HtmlInputText input8 = (HtmlInputText) form.findComponent("input8");
    Assert.assertNotNull(input8);
    HtmlInputText input9 = (HtmlInputText) form.findComponent("input9");
    Assert.assertNotNull(input9);
    HtmlInputText input10 = (HtmlInputText) form.findComponent("input10");
    Assert.assertNotNull(input10);
    HtmlInputText input11 = (HtmlInputText) form.findComponent("input11");
    Assert.assertNotNull(input11);
    HtmlInputText input12 = (HtmlInputText) form.findComponent("input12");
    Assert.assertNotNull(input12);
    HtmlInputText input13 = (HtmlInputText) form.findComponent("input13");
    Assert.assertNotNull(input13);
    HtmlInputText input14 = (HtmlInputText) form.findComponent("input14");
    Assert.assertNotNull(input14);
    HtmlInputFile input15 = (HtmlInputFile) form.findComponent("input15");
    Assert.assertNotNull(input15);
    HtmlInputHidden input16 = (HtmlInputHidden) form.findComponent("input16");
    Assert.assertNotNull(input16);
    HtmlInputSecret input17 = (HtmlInputSecret) form.findComponent("input17");
    Assert.assertNotNull(input17);
    HtmlCommandButton input18 = (HtmlCommandButton) form.findComponent("input18");
    Assert.assertNotNull(input18);
    HtmlCommandButton input19 = (HtmlCommandButton) form.findComponent("input19");
    Assert.assertNotNull(input19);
    HtmlInputText input20 = (HtmlInputText) form.findComponent("input20");
    Assert.assertNotNull(input20);
    HtmlOutputLabel label1 = (HtmlOutputLabel) form.findComponent("label1");
    Assert.assertNotNull(label1);
    HtmlSelectOneListbox select1 = (HtmlSelectOneListbox) form.findComponent("select1");
    Assert.assertNotNull(select1);
    HtmlSelectManyListbox select2 = (HtmlSelectManyListbox) form.findComponent("select2");
    Assert.assertNotNull(select2);
    HtmlInputTextarea textarea1 = (HtmlInputTextarea) form.findComponent("textarea1");
    Assert.assertNotNull(textarea1);
}
Also used : HtmlSelectBooleanCheckbox(jakarta.faces.component.html.HtmlSelectBooleanCheckbox) HtmlInputTextarea(jakarta.faces.component.html.HtmlInputTextarea) HtmlInputFile(jakarta.faces.component.html.HtmlInputFile) HtmlSelectOneListbox(jakarta.faces.component.html.HtmlSelectOneListbox) HtmlCommandLink(jakarta.faces.component.html.HtmlCommandLink) HtmlOutcomeTargetLink(jakarta.faces.component.html.HtmlOutcomeTargetLink) HtmlGraphicImage(jakarta.faces.component.html.HtmlGraphicImage) UIComponent(jakarta.faces.component.UIComponent) HtmlInputText(jakarta.faces.component.html.HtmlInputText) UIForm(jakarta.faces.component.UIForm) HtmlCommandButton(jakarta.faces.component.html.HtmlCommandButton) HtmlOutputLabel(jakarta.faces.component.html.HtmlOutputLabel) HtmlInputSecret(jakarta.faces.component.html.HtmlInputSecret) HtmlHead(jakarta.faces.component.html.HtmlHead) HtmlBody(jakarta.faces.component.html.HtmlBody) UIOutput(jakarta.faces.component.UIOutput) HtmlSelectManyListbox(jakarta.faces.component.html.HtmlSelectManyListbox) HtmlOutputLink(jakarta.faces.component.html.HtmlOutputLink) UIViewRoot(jakarta.faces.component.UIViewRoot) HtmlOutcomeTargetButton(jakarta.faces.component.html.HtmlOutcomeTargetButton) HtmlInputHidden(jakarta.faces.component.html.HtmlInputHidden)

Example 5 with HtmlCommandLink

use of jakarta.faces.component.html.HtmlCommandLink in project myfaces by apache.

the class HtmlLinkRendererBase method renderCommandLinkStart.

protected void renderCommandLinkStart(FacesContext facesContext, UIComponent component, String clientId, Object value, String style, String styleClass) throws IOException {
    ResponseWriter writer = facesContext.getResponseWriter();
    Map<String, List<ClientBehavior>> behaviors = null;
    // h:commandLink can be rendered outside a form, but with warning (jsf 2.0 TCK)
    UIForm form = ComponentUtils.findClosest(UIForm.class, component);
    boolean disabled = HtmlRendererUtils.isDisabled(component);
    if (disabled || form == null) {
        writer.startElement(HTML.SPAN_ELEM, component);
        if (component instanceof ClientBehaviorHolder) {
            behaviors = ((ClientBehaviorHolder) component).getClientBehaviors();
            if (!behaviors.isEmpty()) {
                HtmlRendererUtils.writeIdAndName(writer, component, facesContext);
            } else {
                HtmlRendererUtils.writeIdIfNecessary(writer, component, facesContext);
            }
            long commonPropertiesMarked = 0L;
            if (isCommonPropertiesOptimizationEnabled(facesContext)) {
                commonPropertiesMarked = CommonHtmlAttributesUtil.getMarkedAttributes(component);
            }
            // only render onclick if != disabled
            if (!disabled) {
                if (behaviors.isEmpty() && isCommonPropertiesOptimizationEnabled(facesContext)) {
                    CommonHtmlAttributesUtil.renderEventProperties(writer, commonPropertiesMarked, component);
                    CommonHtmlAttributesUtil.renderFocusBlurEventProperties(writer, commonPropertiesMarked, component);
                } else {
                    if (isCommonEventsOptimizationEnabled(facesContext)) {
                        Long commonEventsMarked = CommonHtmlEventsUtil.getMarkedEvents(component);
                        CommonHtmlEventsUtil.renderBehaviorizedEventHandlers(facesContext, writer, commonPropertiesMarked, commonEventsMarked, component, behaviors);
                        CommonHtmlEventsUtil.renderBehaviorizedFieldEventHandlersWithoutOnchangeAndOnselect(facesContext, writer, commonPropertiesMarked, commonEventsMarked, component, behaviors);
                    } else {
                        HtmlRendererUtils.renderBehaviorizedEventHandlers(facesContext, writer, component, behaviors);
                        HtmlRendererUtils.renderBehaviorizedFieldEventHandlersWithoutOnchangeAndOnselect(facesContext, writer, component, behaviors);
                    }
                }
            } else {
                if (behaviors.isEmpty() && isCommonPropertiesOptimizationEnabled(facesContext)) {
                    CommonHtmlAttributesUtil.renderEventPropertiesWithoutOnclick(writer, commonPropertiesMarked, component);
                    CommonHtmlAttributesUtil.renderFocusBlurEventProperties(writer, commonPropertiesMarked, component);
                } else {
                    if (isCommonEventsOptimizationEnabled(facesContext)) {
                        Long commonEventsMarked = CommonHtmlEventsUtil.getMarkedEvents(component);
                        CommonHtmlEventsUtil.renderBehaviorizedEventHandlersWithoutOnclick(facesContext, writer, commonPropertiesMarked, commonEventsMarked, component, behaviors);
                        CommonHtmlEventsUtil.renderBehaviorizedFieldEventHandlersWithoutOnchangeAndOnselect(facesContext, writer, commonPropertiesMarked, commonEventsMarked, component, behaviors);
                    } else {
                        HtmlRendererUtils.renderBehaviorizedEventHandlersWithoutOnclick(facesContext, writer, component, behaviors);
                        HtmlRendererUtils.renderBehaviorizedFieldEventHandlersWithoutOnchangeAndOnselect(facesContext, writer, component, behaviors);
                    }
                }
            }
            if (isCommonPropertiesOptimizationEnabled(facesContext)) {
                CommonHtmlAttributesUtil.renderAnchorPassthroughPropertiesDisabledWithoutEvents(writer, commonPropertiesMarked, component);
            } else {
                HtmlRendererUtils.renderHTMLAttributes(writer, component, HTML.ANCHOR_PASSTHROUGH_ATTRIBUTES_DISABLED_WITHOUT_EVENTS);
            }
        } else {
            HtmlRendererUtils.writeIdIfNecessary(writer, component, facesContext);
            if (isCommonPropertiesOptimizationEnabled(facesContext)) {
                CommonHtmlAttributesUtil.renderAnchorPassthroughPropertiesDisabled(writer, CommonHtmlAttributesUtil.getMarkedAttributes(component), component);
            } else {
                HtmlRendererUtils.renderHTMLAttributes(writer, component, HTML.ANCHOR_PASSTHROUGH_ATTRIBUTES_DISABLED);
            }
        }
    } else {
        if (component instanceof ClientBehaviorHolder) {
            behaviors = ((ClientBehaviorHolder) component).getClientBehaviors();
            renderBehaviorizedJavaScriptAnchorStart(facesContext, writer, component, clientId, behaviors, form);
            if (!behaviors.isEmpty()) {
                HtmlRendererUtils.writeIdAndName(writer, component, facesContext);
            } else {
                // If onclick is not null, both onclick and server side script are rendered
                // using faces.util.chain(...) js function. We need to check that case and force
                // id/name rendering. It is possible to do something else in that case and
                // do not render the script using faces.util.chain, but for now it is ok.
                String commandOnclick;
                if (component instanceof HtmlCommandLink) {
                    commandOnclick = ((HtmlCommandLink) component).getOnclick();
                } else {
                    commandOnclick = (String) component.getAttributes().get(HTML.ONCLICK_ATTR);
                }
                if (commandOnclick != null) {
                    HtmlRendererUtils.writeIdAndName(writer, component, facesContext);
                } else {
                    HtmlRendererUtils.writeIdIfNecessary(writer, component, facesContext);
                }
            }
            long commonPropertiesMarked = 0L;
            if (isCommonPropertiesOptimizationEnabled(facesContext)) {
                commonPropertiesMarked = CommonHtmlAttributesUtil.getMarkedAttributes(component);
            }
            if (behaviors.isEmpty() && isCommonPropertiesOptimizationEnabled(facesContext)) {
                CommonHtmlAttributesUtil.renderEventPropertiesWithoutOnclick(writer, commonPropertiesMarked, component);
                CommonHtmlAttributesUtil.renderFocusBlurEventProperties(writer, commonPropertiesMarked, component);
            } else {
                HtmlRendererUtils.renderBehaviorizedEventHandlersWithoutOnclick(facesContext, writer, component, behaviors);
                HtmlRendererUtils.renderBehaviorizedFieldEventHandlersWithoutOnchangeAndOnselect(facesContext, writer, component, behaviors);
            }
            if (isCommonPropertiesOptimizationEnabled(facesContext)) {
                CommonHtmlAttributesUtil.renderAnchorPassthroughPropertiesWithoutStyleAndEvents(writer, commonPropertiesMarked, component);
            } else {
                HtmlRendererUtils.renderHTMLAttributes(writer, component, HTML.ANCHOR_PASSTHROUGH_ATTRIBUTES_WITHOUT_STYLE_AND_EVENTS);
            }
        } else {
            renderJavaScriptAnchorStart(facesContext, writer, component, clientId, form);
            HtmlRendererUtils.writeIdIfNecessary(writer, component, facesContext);
            if (isCommonPropertiesOptimizationEnabled(facesContext)) {
                CommonHtmlAttributesUtil.renderAnchorPassthroughPropertiesWithoutOnclickAndStyle(writer, CommonHtmlAttributesUtil.getMarkedAttributes(component), component);
            } else {
                HtmlRendererUtils.renderHTMLAttributes(writer, component, HTML.ANCHOR_PASSTHROUGH_ATTRIBUTES_WITHOUT_ONCLICK_WITHOUT_STYLE);
            }
        }
        HtmlRendererUtils.renderHTMLAttribute(writer, HTML.STYLE_ATTR, HTML.STYLE_ATTR, style);
        HtmlRendererUtils.renderHTMLAttribute(writer, HTML.STYLE_CLASS_ATTR, HTML.STYLE_CLASS_ATTR, styleClass);
    }
    // render value as required by JSF 1.1 renderkitdocs
    if (value != null) {
        writer.writeText(value.toString(), ComponentAttrs.VALUE_ATTR);
    }
    // render warning message for a h:commandLink with no nesting form
    if (form == null) {
        writer.writeText(": This link is deactivated, because it is not embedded in a JSF form.", null);
    }
}
Also used : ResponseWriter(jakarta.faces.context.ResponseWriter) HtmlCommandLink(jakarta.faces.component.html.HtmlCommandLink) List(java.util.List) UIForm(jakarta.faces.component.UIForm) ClientBehaviorHolder(jakarta.faces.component.behavior.ClientBehaviorHolder)

Aggregations

HtmlCommandLink (jakarta.faces.component.html.HtmlCommandLink)9 UIComponent (jakarta.faces.component.UIComponent)6 UIViewRoot (jakarta.faces.component.UIViewRoot)5 UIForm (jakarta.faces.component.UIForm)4 UINamingContainer (jakarta.faces.component.UINamingContainer)3 HtmlOutcomeTargetLink (jakarta.faces.component.html.HtmlOutcomeTargetLink)3 HtmlOutputLink (jakarta.faces.component.html.HtmlOutputLink)3 HelloWorld (org.apache.myfaces.view.facelets.bean.HelloWorld)3 Test (org.junit.Test)3 UIOutput (jakarta.faces.component.UIOutput)2 HtmlBody (jakarta.faces.component.html.HtmlBody)2 HtmlCommandButton (jakarta.faces.component.html.HtmlCommandButton)2 HtmlGraphicImage (jakarta.faces.component.html.HtmlGraphicImage)2 HtmlHead (jakarta.faces.component.html.HtmlHead)2 HtmlInputFile (jakarta.faces.component.html.HtmlInputFile)2 HtmlInputHidden (jakarta.faces.component.html.HtmlInputHidden)2 HtmlInputSecret (jakarta.faces.component.html.HtmlInputSecret)2 HtmlInputText (jakarta.faces.component.html.HtmlInputText)2 HtmlInputTextarea (jakarta.faces.component.html.HtmlInputTextarea)2 HtmlOutcomeTargetButton (jakarta.faces.component.html.HtmlOutcomeTargetButton)2