use of jakarta.faces.component.UIForm in project myfaces by apache.
the class HtmlLinkRendererBase method decode.
@Override
public void decode(FacesContext facesContext, UIComponent component) {
// check for NP
super.decode(facesContext, component);
if (component instanceof UICommand) {
String clientId = component.getClientId(facesContext);
UIForm form = ComponentUtils.findClosest(UIForm.class, component);
boolean disabled = HtmlRendererUtils.isDisabled(component);
// MYFACES-3960 Decode, decode client behavior and queue action event at the end
boolean activateActionEvent = false;
if (form != null && !disabled) {
String reqValue = (String) facesContext.getExternalContext().getRequestParameterMap().get(HtmlRendererUtils.getHiddenCommandLinkFieldName(form, facesContext));
activateActionEvent = reqValue != null && reqValue.equals(clientId) || HtmlRendererUtils.isPartialOrBehaviorSubmit(facesContext, clientId);
}
if (component instanceof ClientBehaviorHolder && !disabled) {
ClientBehaviorRendererUtils.decodeClientBehaviors(facesContext, component);
}
if (activateActionEvent) {
component.queueEvent(new ActionEvent(component));
}
} else if (component instanceof UIOutput) {
// do nothing
if (component instanceof ClientBehaviorHolder && !HtmlRendererUtils.isDisabled(component)) {
ClientBehaviorRendererUtils.decodeClientBehaviors(facesContext, component);
}
} else {
throw new IllegalArgumentException("Unsupported component class " + component.getClass().getName());
}
}
use of jakarta.faces.component.UIForm in project myfaces by apache.
the class InvokeOnComponentTest method testInvokeOnFormPrependIdChild.
public void testInvokeOnFormPrependIdChild() throws Exception {
UIForm form = new UIForm();
form.setId("form");
form.setPrependId(true);
facesContext.getViewRoot().getChildren().add(form);
UIInput child1 = new UIInput();
child1.setId("child1");
form.getChildren().add(child1);
AtomicBoolean val = new AtomicBoolean(false);
this.facesContext.getViewRoot().invokeOnComponent(facesContext, "form:child1", (context, target) -> {
val.set(true);
});
Assert.assertTrue(val.get());
}
use of jakarta.faces.component.UIForm in project myfaces by apache.
the class InvokeOnComponentTest method testInvokeOnFormPrependIdFalse.
public void testInvokeOnFormPrependIdFalse() throws Exception {
UIForm form = new UIForm();
form.setId("form");
form.setPrependId(false);
facesContext.getViewRoot().getChildren().add(form);
AtomicBoolean val = new AtomicBoolean(false);
this.facesContext.getViewRoot().invokeOnComponent(facesContext, "form", (context, target) -> {
val.set(true);
});
Assert.assertTrue(val.get());
}
use of jakarta.faces.component.UIForm in project myfaces by apache.
the class InvokeOnComponentTest method atestInvokeOnComp.
public void atestInvokeOnComp() throws Exception {
UIForm form = new UIForm();
UIInput i1 = new UIInput();
i1.setId("_id1");
UIInput i2 = new UIInput();
i2.setId("_id2");
UIInput i3 = new UIInput();
i3.setId("_id3");
UIInput i4 = new UIInput();
i4.setId("_id4");
form.getChildren().add(i1);
form.getChildren().add(i4);
form.getChildren().add(i2);
form.getChildren().add(i3);
this.facesContext.getViewRoot().getChildren().add(form);
this.facesContext.getViewRoot().invokeOnComponent(facesContext, i2.getClientId(facesContext), cc);
Mockito.verify(cc, Mockito.times(1)).invokeContextCallback(facesContext, i2);
Mockito.verify(cc, Mockito.never()).invokeContextCallback(facesContext, i1);
Mockito.verify(cc, Mockito.never()).invokeContextCallback(facesContext, i3);
Mockito.verify(cc, Mockito.never()).invokeContextCallback(facesContext, i4);
}
use of jakarta.faces.component.UIForm in project myfaces by apache.
the class HtmlButtonRendererBase method encodeBegin.
@Override
public void encodeBegin(FacesContext facesContext, UIComponent uiComponent) throws IOException {
RendererUtils.checkParamValidity(facesContext, uiComponent, UICommand.class);
String clientId = uiComponent.getClientId(facesContext);
ResponseWriter writer = facesContext.getResponseWriter();
// commandButton does not need to be nested in a form since JSF 2.0
UIForm form = ComponentUtils.findClosest(UIForm.class, uiComponent);
boolean reset = isReset(uiComponent);
boolean button = isButton(uiComponent);
Map<String, List<ClientBehavior>> behaviors = null;
if (uiComponent instanceof ClientBehaviorHolder) {
behaviors = ((ClientBehaviorHolder) uiComponent).getClientBehaviors();
if (!behaviors.isEmpty()) {
ResourceUtils.renderDefaultJsfJsInlineIfNecessary(facesContext, writer);
}
}
List<UIParameter> validParams = HtmlRendererUtils.getValidUIParameterChildren(facesContext, getChildren(uiComponent), false, false);
String commandOnclick = (String) uiComponent.getAttributes().get(HTML.ONCLICK_ATTR);
if (commandOnclick != null && (validParams != null && !validParams.isEmpty())) {
ResourceUtils.renderDefaultJsfJsInlineIfNecessary(facesContext, writer);
}
writer.startElement(HTML.INPUT_ELEM, uiComponent);
writer.writeAttribute(HTML.ID_ATTR, clientId, ComponentAttrs.ID_ATTR);
writer.writeAttribute(HTML.NAME_ATTR, clientId, ComponentAttrs.ID_ATTR);
String image = RendererUtils.getIconSrc(facesContext, uiComponent, ComponentAttrs.IMAGE_ATTR);
if (image != null) {
writer.writeAttribute(HTML.TYPE_ATTR, HTML.INPUT_TYPE_IMAGE, ComponentAttrs.TYPE_ATTR);
writer.writeURIAttribute(HTML.SRC_ATTR, image, ComponentAttrs.IMAGE_ATTR);
} else {
String type = getType(uiComponent);
if (type == null || (!reset && !button)) {
type = HTML.INPUT_TYPE_SUBMIT;
}
writer.writeAttribute(HTML.TYPE_ATTR, type, ComponentAttrs.TYPE_ATTR);
Object value = getValue(uiComponent);
if (value != null) {
writer.writeAttribute(HTML.VALUE_ATTR, value, ComponentAttrs.VALUE_ATTR);
}
}
if (ClientBehaviorRendererUtils.hasClientBehavior(ClientBehaviorEvents.CLICK, behaviors) || ClientBehaviorRendererUtils.hasClientBehavior(ClientBehaviorEvents.ACTION, behaviors)) {
if (!reset && !button) {
String onClick = buildBehaviorizedOnClick(uiComponent, behaviors, facesContext, writer, form, validParams);
if (onClick.length() != 0) {
writer.writeAttribute(HTML.ONCLICK_ATTR, onClick, null);
}
} else {
Collection<ClientBehaviorContext.Parameter> paramList = ClientBehaviorRendererUtils.getClientBehaviorContextParameters(HtmlRendererUtils.mapAttachedParamsToStringValues(facesContext, uiComponent));
String onClick = ClientBehaviorRendererUtils.buildBehaviorChain(facesContext, uiComponent, ClientBehaviorEvents.CLICK, paramList, ClientBehaviorEvents.ACTION, paramList, behaviors, commandOnclick, null);
if (onClick.length() != 0) {
writer.writeAttribute(HTML.ONCLICK_ATTR, onClick, null);
}
}
Map<String, Object> attributes = uiComponent.getAttributes();
ClientBehaviorRendererUtils.buildBehaviorChain(facesContext, uiComponent, ClientBehaviorEvents.DBLCLICK, null, behaviors, (String) attributes.get(HTML.ONDBLCLICK_ATTR), "");
} else {
// fallback into the pre 2.0 code to keep backwards compatibility with libraries which rely on internals
if (!reset && !button) {
StringBuilder onClick = buildOnClick(uiComponent, facesContext, writer, validParams);
if (onClick.length() != 0) {
writer.writeAttribute(HTML.ONCLICK_ATTR, onClick.toString(), null);
}
} else {
HtmlRendererUtils.renderHTMLStringAttribute(writer, uiComponent, HTML.ONCLICK_ATTR, HTML.ONCLICK_ATTR);
}
}
if (isCommonPropertiesOptimizationEnabled(facesContext)) {
CommonHtmlAttributesUtil.renderButtonPassthroughPropertiesWithoutDisabledAndEvents(writer, CommonHtmlAttributesUtil.getMarkedAttributes(uiComponent), uiComponent);
} else {
HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, HTML.BUTTON_PASSTHROUGH_ATTRIBUTES_WITHOUT_DISABLED_AND_EVENTS);
}
if (behaviors != null && !behaviors.isEmpty()) {
HtmlRendererUtils.renderBehaviorizedEventHandlersWithoutOnclick(facesContext, writer, uiComponent, behaviors);
HtmlRendererUtils.renderBehaviorizedFieldEventHandlers(facesContext, writer, uiComponent, behaviors);
} else {
if (isCommonPropertiesOptimizationEnabled(facesContext)) {
long commonAttributesMarked = CommonHtmlAttributesUtil.getMarkedAttributes(uiComponent);
CommonHtmlAttributesUtil.renderEventPropertiesWithoutOnclick(writer, commonAttributesMarked, uiComponent);
CommonHtmlAttributesUtil.renderCommonFieldEventProperties(writer, commonAttributesMarked, uiComponent);
} else {
HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, HTML.EVENT_HANDLER_ATTRIBUTES_WITHOUT_ONCLICK);
HtmlRendererUtils.renderHTMLAttributes(writer, uiComponent, HTML.COMMON_FIELD_EVENT_ATTRIBUTES);
}
}
if (isDisabled(facesContext, uiComponent)) {
writer.writeAttribute(HTML.DISABLED_ATTR, HTML.DISABLED_ATTR, ComponentAttrs.DISABLED_ATTR);
}
if (isReadonly(facesContext, uiComponent)) {
writer.writeAttribute(HTML.READONLY_ATTR, HTML.READONLY_ATTR, ComponentAttrs.READONLY_ATTR);
}
}
Aggregations