Search in sources :

Example 1 with Command

use of org.apache.myfaces.tobago.internal.renderkit.Command in project myfaces-tobago by apache.

the class TreeSelectRenderer method encodeBeginInternal.

@Override
public void encodeBeginInternal(final FacesContext facesContext, final T component) throws IOException {
    final AbstractUITree tree = ComponentUtils.findAncestor(component, AbstractUITree.class);
    final AbstractUITreeNodeBase node = ComponentUtils.findAncestor(component, AbstractUITreeNodeBase.class);
    final AbstractUIData data = ComponentUtils.findAncestor(node, AbstractUIData.class);
    final TobagoResponseWriter writer = getResponseWriter(facesContext);
    if (data instanceof AbstractUITreeListbox) {
        writer.write(StringUtils.defaultString(component.getLabel()));
        return;
    }
    final String id = component.getClientId(facesContext);
    final String currentValue = getCurrentValue(facesContext, component);
    final boolean checked;
    if (component.isValueStoredInState()) {
        checked = data.getSelectedState().isSelected(node.getPath());
    } else {
        checked = "true".equals(currentValue);
    }
    final boolean folder = data.isFolder();
    final Selectable selectable = data.getSelectable();
    final boolean showFormCheck = component.isShowCheckbox() && selectable != Selectable.none && (!selectable.isLeafOnly() || !folder);
    writer.startElement(HtmlElements.TOBAGO_TREE_SELECT);
    writer.writeClassAttribute(component.getCustomClass(), showFormCheck ? BootstrapClass.FORM_CHECK_INLINE : null, showFormCheck && selectable.isMulti() ? BootstrapClass.FORM_CHECK : null, showFormCheck && selectable.isSingle() ? BootstrapClass.FORM_CHECK : null);
    HtmlRendererUtils.writeDataAttributes(facesContext, writer, component);
    if (showFormCheck) {
        writer.startElement(HtmlElements.INPUT);
        writer.writeClassAttribute(BootstrapClass.FORM_CHECK_INPUT);
        if (selectable.isSingle()) {
            writer.writeAttribute(HtmlAttributes.TYPE, HtmlInputTypes.RADIO);
            writer.writeNameAttribute(getClientIdWithoutRowIndex(data, id));
        } else {
            writer.writeAttribute(HtmlAttributes.TYPE, HtmlInputTypes.CHECKBOX);
            writer.writeNameAttribute(id);
        }
        writer.writeAttribute(HtmlAttributes.VALUE, id, false);
        // TODO: ID must be at the outer tag
        writer.writeIdAttribute(id);
        writer.writeAttribute(HtmlAttributes.CHECKED, checked);
        writer.endElement(HtmlElements.INPUT);
    }
    final String label = component.getLabel();
    writer.startElement(HtmlElements.LABEL);
    writer.writeClassAttribute(showFormCheck ? BootstrapClass.FORM_CHECK_LABEL : null);
    final String title = HtmlRendererUtils.getTitleFromTipAndMessages(facesContext, component);
    if (title != null) {
        writer.writeAttribute(HtmlAttributes.TITLE, title, true);
    }
    writer.writeAttribute(HtmlAttributes.FOR, id, false);
    writer.writeText(label);
    writer.endElement(HtmlElements.LABEL);
    if (showFormCheck) {
        final CommandMap behaviorCommands = getBehaviorCommands(facesContext, component);
        if (behaviorCommands != null) {
            Map<ClientBehaviors, Command> other = behaviorCommands.getOther();
            if (other != null) {
                Command change = other.get(ClientBehaviors.change);
                change.setExecute(change.getExecute() + " " + tree.getBaseClientId(facesContext));
                change.setRender(change.getRender() + " " + tree.getBaseClientId(facesContext));
            }
        }
        encodeBehavior(writer, behaviorCommands);
    }
    writer.endElement(HtmlElements.TOBAGO_TREE_SELECT);
}
Also used : AbstractUIData(org.apache.myfaces.tobago.internal.component.AbstractUIData) ClientBehaviors(org.apache.myfaces.tobago.component.ClientBehaviors) TobagoResponseWriter(org.apache.myfaces.tobago.webapp.TobagoResponseWriter) Selectable(org.apache.myfaces.tobago.model.Selectable) Command(org.apache.myfaces.tobago.internal.renderkit.Command) AbstractUITreeNodeBase(org.apache.myfaces.tobago.internal.component.AbstractUITreeNodeBase) AbstractUITreeListbox(org.apache.myfaces.tobago.internal.component.AbstractUITreeListbox) AbstractUITree(org.apache.myfaces.tobago.internal.component.AbstractUITree) CommandMap(org.apache.myfaces.tobago.internal.renderkit.CommandMap)

Example 2 with Command

use of org.apache.myfaces.tobago.internal.renderkit.Command in project myfaces-tobago by apache.

the class RendererBase method encodeBehavior.

/**
 * Renders the tobago-behavior tag.
 *
 * @since 5.0
 */
protected void encodeBehavior(final TobagoResponseWriter writer, final CommandMap behaviorCommands) throws IOException {
    if (behaviorCommands != null) {
        final Command click = behaviorCommands.getClick();
        if (click != null) {
            encodeBehavior(writer, ClientBehaviors.click, click);
        }
        final Map<ClientBehaviors, Command> other = behaviorCommands.getOther();
        if (other != null) {
            for (Map.Entry<ClientBehaviors, Command> entry : other.entrySet()) {
                encodeBehavior(writer, entry.getKey(), entry.getValue());
            }
        }
    }
}
Also used : ClientBehaviors(org.apache.myfaces.tobago.component.ClientBehaviors) AbstractUICommand(org.apache.myfaces.tobago.internal.component.AbstractUICommand) Command(org.apache.myfaces.tobago.internal.renderkit.Command) CommandMap(org.apache.myfaces.tobago.internal.renderkit.CommandMap) Map(java.util.Map)

Example 3 with Command

use of org.apache.myfaces.tobago.internal.renderkit.Command in project myfaces-tobago by apache.

the class JsonUtilsUnitTest method testCommandMap.

@Test
public void testCommandMap() {
    CommandMap map = new CommandMap();
    map.addCommand(ClientBehaviors.blur, new Command("doit", null, false, "field", "execute", "render", "Do \"you\" want?", 100, new Collapse(Collapse.Operation.hide, "box"), false));
    final String expected = ("{'blur':" + "{'clientId':'doit'," + "'transition':false," + "'target':'field'," + "'execute':'execute'," + "'render':'render'," + "'collapse':{'transition':'hide','forId':'box'}," + "'confirmation':'Do \\'you\\' want?'," + "'delay':100}}").replaceAll("'", "\"");
    Assertions.assertEquals(expected, JsonUtils.encode(map), "command map");
}
Also used : Collapse(org.apache.myfaces.tobago.internal.renderkit.Collapse) AbstractUICommand(org.apache.myfaces.tobago.internal.component.AbstractUICommand) Command(org.apache.myfaces.tobago.internal.renderkit.Command) CommandMap(org.apache.myfaces.tobago.internal.renderkit.CommandMap) Test(org.junit.jupiter.api.Test)

Example 4 with Command

use of org.apache.myfaces.tobago.internal.renderkit.Command in project myfaces-tobago by apache.

the class RendererBase method getBehaviorCommands.

protected CommandMap getBehaviorCommands(final FacesContext facesContext, final ClientBehaviorHolder clientBehaviorHolder) {
    CommandMap commandMap = null;
    for (final Map.Entry<String, List<ClientBehavior>> entry : clientBehaviorHolder.getClientBehaviors().entrySet()) {
        final String eventName = entry.getKey();
        final ClientBehaviorContext clientBehaviorContext = getClientBehaviorContext(facesContext, clientBehaviorHolder, eventName);
        for (final ClientBehavior clientBehavior : entry.getValue()) {
            if (clientBehavior instanceof EventBehavior) {
                final EventBehavior eventBehavior = (EventBehavior) clientBehavior;
                final AbstractUIEvent abstractUIEvent = RenderUtils.getAbstractUIEvent((UIComponent) clientBehaviorHolder, eventBehavior);
                if (abstractUIEvent != null && abstractUIEvent.isRendered() && !abstractUIEvent.isDisabled()) {
                    for (List<ClientBehavior> children : abstractUIEvent.getClientBehaviors().values()) {
                        for (ClientBehavior child : children) {
                            final CommandMap childMap = getCommandMap(facesContext, clientBehaviorContext, child);
                            commandMap = CommandMap.merge(commandMap, childMap);
                        }
                    }
                }
            }
            final CommandMap map = getCommandMap(facesContext, clientBehaviorContext, clientBehavior);
            commandMap = CommandMap.merge(commandMap, map);
        }
    }
    // if there is no explicit behavior (with f:ajax or tc:event), use the command properties as default.
    if ((commandMap == null || commandMap.isEmpty()) && clientBehaviorHolder instanceof AbstractUICommand) {
        if (commandMap == null) {
            commandMap = new CommandMap();
        }
        final AbstractUICommand holder = (AbstractUICommand) clientBehaviorHolder;
        commandMap.addCommand(ClientBehaviors.click, new Command(holder.getClientId(facesContext), holder.getFieldId(facesContext), holder.isTransition(), holder.getTarget(), null, null, ComponentUtils.getConfirmation(holder), null, TobagoClientBehaviorRenderer.createCollapsible(facesContext, holder), holder.isOmit()));
    }
    return commandMap;
}
Also used : AbstractUICommand(org.apache.myfaces.tobago.internal.component.AbstractUICommand) AbstractUICommand(org.apache.myfaces.tobago.internal.component.AbstractUICommand) Command(org.apache.myfaces.tobago.internal.renderkit.Command) List(java.util.List) EventBehavior(org.apache.myfaces.tobago.internal.behavior.EventBehavior) CommandMap(org.apache.myfaces.tobago.internal.renderkit.CommandMap) Map(java.util.Map) AbstractUIEvent(org.apache.myfaces.tobago.internal.component.AbstractUIEvent) ClientBehaviorContext(jakarta.faces.component.behavior.ClientBehaviorContext) ClientBehavior(jakarta.faces.component.behavior.ClientBehavior) CommandMap(org.apache.myfaces.tobago.internal.renderkit.CommandMap)

Example 5 with Command

use of org.apache.myfaces.tobago.internal.renderkit.Command in project myfaces-tobago by apache.

the class RenderUtils method getBehaviorCommands.

/**
 * @deprecated since 5.0.0
 */
@Deprecated
public static CommandMap getBehaviorCommands(final FacesContext facesContext, final ClientBehaviorHolder clientBehaviorHolder) {
    CommandMap commandMap = null;
    for (final Map.Entry<String, List<ClientBehavior>> entry : clientBehaviorHolder.getClientBehaviors().entrySet()) {
        final String eventName = entry.getKey();
        final ClientBehaviorContext clientBehaviorContext = getClientBehaviorContext(facesContext, clientBehaviorHolder, eventName);
        for (final ClientBehavior clientBehavior : entry.getValue()) {
            if (clientBehavior instanceof EventBehavior) {
                final EventBehavior eventBehavior = (EventBehavior) clientBehavior;
                final AbstractUIEvent abstractUIEvent = getAbstractUIEvent((UIComponent) clientBehaviorHolder, eventBehavior);
                if (abstractUIEvent != null && abstractUIEvent.isRendered() && !abstractUIEvent.isDisabled()) {
                    for (List<ClientBehavior> children : abstractUIEvent.getClientBehaviors().values()) {
                        for (ClientBehavior child : children) {
                            final CommandMap childMap = getCommandMap(facesContext, clientBehaviorContext, child);
                            commandMap = CommandMap.merge(commandMap, childMap);
                        }
                    }
                }
            }
            final CommandMap map = getCommandMap(facesContext, clientBehaviorContext, clientBehavior);
            commandMap = CommandMap.merge(commandMap, map);
        }
    }
    // if there is no explicit behavior (with f:ajax or tc:event), use the command properties as default.
    if ((commandMap == null || commandMap.isEmpty()) && clientBehaviorHolder instanceof AbstractUICommand) {
        if (commandMap == null) {
            commandMap = new CommandMap();
        }
        final AbstractUICommand holder = (AbstractUICommand) clientBehaviorHolder;
        commandMap.addCommand(ClientBehaviors.click, new Command(holder.getClientId(facesContext), holder.getFieldId(facesContext), holder.isTransition(), holder.getTarget(), null, null, ComponentUtils.getConfirmation(holder), null, TobagoClientBehaviorRenderer.createCollapsible(facesContext, holder), holder.isOmit()));
    }
    return commandMap;
}
Also used : AbstractUICommand(org.apache.myfaces.tobago.internal.component.AbstractUICommand) AbstractUICommand(org.apache.myfaces.tobago.internal.component.AbstractUICommand) Command(org.apache.myfaces.tobago.internal.renderkit.Command) List(java.util.List) EventBehavior(org.apache.myfaces.tobago.internal.behavior.EventBehavior) CommandMap(org.apache.myfaces.tobago.internal.renderkit.CommandMap) Map(java.util.Map) AbstractUIEvent(org.apache.myfaces.tobago.internal.component.AbstractUIEvent) ClientBehaviorContext(jakarta.faces.component.behavior.ClientBehaviorContext) ClientBehavior(jakarta.faces.component.behavior.ClientBehavior) CommandMap(org.apache.myfaces.tobago.internal.renderkit.CommandMap)

Aggregations

Command (org.apache.myfaces.tobago.internal.renderkit.Command)12 CommandMap (org.apache.myfaces.tobago.internal.renderkit.CommandMap)12 AbstractUICommand (org.apache.myfaces.tobago.internal.component.AbstractUICommand)10 Test (org.junit.jupiter.api.Test)6 Map (java.util.Map)4 ClientBehaviors (org.apache.myfaces.tobago.component.ClientBehaviors)4 EventBehavior (org.apache.myfaces.tobago.internal.behavior.EventBehavior)3 AbstractUIEvent (org.apache.myfaces.tobago.internal.component.AbstractUIEvent)3 Collapse (org.apache.myfaces.tobago.internal.renderkit.Collapse)3 ClientBehavior (jakarta.faces.component.behavior.ClientBehavior)2 ClientBehaviorContext (jakarta.faces.component.behavior.ClientBehaviorContext)2 List (java.util.List)2 UIComponent (jakarta.faces.component.UIComponent)1 AjaxBehavior (jakarta.faces.component.behavior.AjaxBehavior)1 FacesContext (jakarta.faces.context.FacesContext)1 SupportFieldId (org.apache.myfaces.tobago.component.SupportFieldId)1 AbstractUIData (org.apache.myfaces.tobago.internal.component.AbstractUIData)1 AbstractUITree (org.apache.myfaces.tobago.internal.component.AbstractUITree)1 AbstractUITreeListbox (org.apache.myfaces.tobago.internal.component.AbstractUITreeListbox)1 AbstractUITreeNodeBase (org.apache.myfaces.tobago.internal.component.AbstractUITreeNodeBase)1