Search in sources :

Example 1 with EventBehavior

use of org.apache.myfaces.tobago.internal.behavior.EventBehavior in project myfaces-tobago by apache.

the class TobagoClientBehaviorRenderer method decode.

@Override
public void decode(final FacesContext facesContext, final UIComponent component, final ClientBehavior clientBehavior) {
    if (clientBehavior instanceof AjaxBehavior) {
        AjaxBehavior ajaxBehavior = (AjaxBehavior) clientBehavior;
        if (!component.isRendered() || ajaxBehavior.isDisabled()) {
            return;
        }
        dispatchBehaviorEvent(component, ajaxBehavior);
    } else if (clientBehavior instanceof EventBehavior) {
        final EventBehavior eventBehavior = (EventBehavior) clientBehavior;
        final AbstractUIEvent abstractUIEvent = RenderUtils.getAbstractUIEvent(component, eventBehavior);
        if (!component.isRendered() || abstractUIEvent == null || !abstractUIEvent.isRendered() || abstractUIEvent.isDisabled()) {
            return;
        }
        for (List<ClientBehavior> children : abstractUIEvent.getClientBehaviors().values()) {
            for (ClientBehavior child : children) {
                decode(facesContext, component, child);
            }
        }
        dispatchBehaviorEvent(component, eventBehavior);
    }
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) EventBehavior(org.apache.myfaces.tobago.internal.behavior.EventBehavior) AbstractUIEvent(org.apache.myfaces.tobago.internal.component.AbstractUIEvent) ClientBehavior(jakarta.faces.component.behavior.ClientBehavior) AjaxBehavior(jakarta.faces.component.behavior.AjaxBehavior)

Example 2 with EventBehavior

use of org.apache.myfaces.tobago.internal.behavior.EventBehavior 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 3 with EventBehavior

use of org.apache.myfaces.tobago.internal.behavior.EventBehavior 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)

Example 4 with EventBehavior

use of org.apache.myfaces.tobago.internal.behavior.EventBehavior in project myfaces-tobago by apache.

the class TabGroupRenderer method processEvent.

@Override
public void processEvent(final ComponentSystemEvent event) {
    final AbstractUITabGroup tabGroup = (AbstractUITabGroup) event.getComponent();
    for (final UIComponent child : tabGroup.getChildren()) {
        if (child instanceof AbstractUITab) {
            final AbstractUITab tab = (AbstractUITab) child;
            final FacesContext facesContext = FacesContext.getCurrentInstance();
            final ClientBehaviors click = ClientBehaviors.click;
            switch(tabGroup.getSwitchType()) {
                case none:
                    break;
                case client:
                    // todo: implement a client behavior which can call local scripts (respect CSP)
                    break;
                case reloadTab:
                    final AjaxBehavior ajaxBehavior = new AjaxBehavior();
                    final Collection<String> ids = Collections.singleton(UINamingContainer.getSeparatorChar(facesContext) + tabGroup.getClientId(facesContext));
                    ajaxBehavior.setExecute(ids);
                    ajaxBehavior.setRender(ids);
                    tab.addClientBehavior(click.name(), ajaxBehavior);
                    break;
                case reloadPage:
                    final AbstractUIEvent component = (AbstractUIEvent) ComponentUtils.createComponent(facesContext, Tags.event.componentType(), RendererTypes.Event, "_click");
                    component.setEvent(click);
                    tab.getChildren().add(component);
                    final EventBehavior eventBehavior = new EventBehavior();
                    eventBehavior.setFor(component.getId());
                    tab.addClientBehavior(click.name(), eventBehavior);
                    break;
                default:
                    LOG.error("Unknown switch type: '{}'", tabGroup.getSwitchType());
            }
        }
    }
}
Also used : FacesContext(jakarta.faces.context.FacesContext) ClientBehaviors(org.apache.myfaces.tobago.component.ClientBehaviors) UIComponent(jakarta.faces.component.UIComponent) EventBehavior(org.apache.myfaces.tobago.internal.behavior.EventBehavior) AbstractUIEvent(org.apache.myfaces.tobago.internal.component.AbstractUIEvent) AbstractUITabGroup(org.apache.myfaces.tobago.internal.component.AbstractUITabGroup) AbstractUITab(org.apache.myfaces.tobago.internal.component.AbstractUITab) AjaxBehavior(jakarta.faces.component.behavior.AjaxBehavior)

Example 5 with EventBehavior

use of org.apache.myfaces.tobago.internal.behavior.EventBehavior in project myfaces-tobago by apache.

the class TobagoClientBehaviorRenderer method getScript.

/**
 * In standard JSF this method returns a JavaScript string. Because of CSP, Tobago doesn't render JavaScript
 * into the HTML content. It transports the return value a bit hacky by {@link CommandMap#storeCommandMap}.
 *
 * @return "dummy" string or null, if nothing to do.
 */
@Override
public String getScript(final ClientBehaviorContext behaviorContext, final ClientBehavior behavior) {
    final FacesContext facesContext = behaviorContext.getFacesContext();
    final UIComponent uiComponent = behaviorContext.getComponent();
    final ClientBehaviors eventName = ClientBehaviors.valueOf(behaviorContext.getEventName());
    // // TBD: is this nice? May be implemented with a JSF behavior?
    Collapse collapse = createCollapsible(facesContext, uiComponent);
    String executeIds = null;
    String renderIds = null;
    Boolean transition = null;
    String target = null;
    String clientId = null;
    String fieldId = null;
    boolean omit = false;
    final String confirmation = ComponentUtils.getConfirmation(uiComponent);
    if (behavior instanceof AjaxBehavior) {
        final AjaxBehavior ajaxBehavior = (AjaxBehavior) behavior;
        if (ajaxBehavior.isDisabled()) {
            return null;
        }
        final Collection<String> execute = ajaxBehavior.getExecute();
        final Collection<String> render = ajaxBehavior.getRender();
        clientId = uiComponent.getClientId(facesContext);
        if (uiComponent instanceof SupportFieldId) {
            fieldId = ((SupportFieldId) uiComponent).getFieldId(facesContext);
        }
        executeIds = ComponentUtils.evaluateClientIds(facesContext, uiComponent, execute.toArray(new String[0]));
        if (executeIds != null) {
            executeIds = executeIds + " " + clientId;
        } else {
            executeIds = clientId;
        }
        if (uiComponent instanceof AbstractUICommand) {
            // <f:ajax> inside of a command
            final AbstractUICommand command = (AbstractUICommand) uiComponent;
            transition = command.isTransition();
            target = command.getTarget();
            omit = command.isOmit() || StringUtils.isNotBlank(RenderUtils.generateUrl(facesContext, command));
        }
        renderIds = ComponentUtils.evaluateClientIds(facesContext, uiComponent, render.toArray(new String[0]));
    } else if (behavior instanceof EventBehavior) {
        // <tc:event>
        final EventBehavior eventBehavior = (EventBehavior) behavior;
        final AbstractUIEvent event = RenderUtils.getAbstractUIEvent(uiComponent, eventBehavior);
        if (event != null) {
            if (!event.isRendered() || event.isDisabled()) {
                return null;
            } else {
                transition = event.isTransition();
                target = event.getTarget();
                clientId = event.getClientId(facesContext);
                if (collapse == null) {
                    collapse = createCollapsible(facesContext, event);
                }
                omit = event.isOmit() || StringUtils.isNotBlank(RenderUtils.generateUrl(facesContext, event));
            }
        }
    } else {
        LOG.warn("Unknown behavior '{}'!", behavior.getClass().getName());
    }
    final Command command = new Command(clientId, fieldId, transition, target, executeIds, renderIds, confirmation, null, collapse, omit);
    final CommandMap map = new CommandMap();
    map.addCommand(eventName, command);
    CommandMap.storeCommandMap(facesContext, map);
    // XXX the return value is a string, but we should use a CommandMap
    return "dummy";
}
Also used : Collapse(org.apache.myfaces.tobago.internal.renderkit.Collapse) FacesContext(jakarta.faces.context.FacesContext) ClientBehaviors(org.apache.myfaces.tobago.component.ClientBehaviors) AbstractUICommand(org.apache.myfaces.tobago.internal.component.AbstractUICommand) UIComponent(jakarta.faces.component.UIComponent) SupportFieldId(org.apache.myfaces.tobago.component.SupportFieldId) EventBehavior(org.apache.myfaces.tobago.internal.behavior.EventBehavior) AbstractUIEvent(org.apache.myfaces.tobago.internal.component.AbstractUIEvent) AjaxBehavior(jakarta.faces.component.behavior.AjaxBehavior) AbstractUICommand(org.apache.myfaces.tobago.internal.component.AbstractUICommand) Command(org.apache.myfaces.tobago.internal.renderkit.Command) CommandMap(org.apache.myfaces.tobago.internal.renderkit.CommandMap)

Aggregations

EventBehavior (org.apache.myfaces.tobago.internal.behavior.EventBehavior)6 AbstractUIEvent (org.apache.myfaces.tobago.internal.component.AbstractUIEvent)6 UIComponent (jakarta.faces.component.UIComponent)3 AjaxBehavior (jakarta.faces.component.behavior.AjaxBehavior)3 ClientBehavior (jakarta.faces.component.behavior.ClientBehavior)3 List (java.util.List)3 AbstractUICommand (org.apache.myfaces.tobago.internal.component.AbstractUICommand)3 Command (org.apache.myfaces.tobago.internal.renderkit.Command)3 CommandMap (org.apache.myfaces.tobago.internal.renderkit.CommandMap)3 ClientBehaviorContext (jakarta.faces.component.behavior.ClientBehaviorContext)2 FacesContext (jakarta.faces.context.FacesContext)2 Map (java.util.Map)2 ClientBehaviors (org.apache.myfaces.tobago.component.ClientBehaviors)2 ClientBehaviorHolder (jakarta.faces.component.behavior.ClientBehaviorHolder)1 FaceletContext (jakarta.faces.view.facelets.FaceletContext)1 TagAttributeException (jakarta.faces.view.facelets.TagAttributeException)1 ArrayList (java.util.ArrayList)1 SupportFieldId (org.apache.myfaces.tobago.component.SupportFieldId)1 AbstractUITab (org.apache.myfaces.tobago.internal.component.AbstractUITab)1 AbstractUITabGroup (org.apache.myfaces.tobago.internal.component.AbstractUITabGroup)1