Search in sources :

Example 6 with ClientBehaviorContext

use of javax.faces.component.behavior.ClientBehaviorContext in project primefaces by primefaces.

the class CoreRenderer method buildDomEvent.

protected String buildDomEvent(FacesContext context, UIComponent component, String domEvent, String behaviorEvent, String behaviorEventAlias, String command) {
    StringBuilder builder = null;
    boolean hasCommand = (command != null);
    Map<String, List<ClientBehavior>> allBehaviors = null;
    if (component instanceof ClientBehaviorHolder) {
        allBehaviors = ((ClientBehaviorHolder) component).getClientBehaviors();
    }
    Object event = component.getAttributes().get(domEvent);
    boolean hasEvent = (event != null);
    String behaviorEventName = behaviorEventAlias;
    if (allBehaviors != null && allBehaviors.containsKey(behaviorEvent)) {
        behaviorEventName = behaviorEvent;
    }
    List<ClientBehavior> behaviors = (allBehaviors == null) ? null : allBehaviors.get(behaviorEventName);
    boolean hasBehaviors = (behaviors != null && !behaviors.isEmpty());
    if (hasEvent || hasBehaviors || hasCommand) {
        if (builder == null) {
            builder = SharedStringBuilder.get(context, SB_RENDER_DOM_EVENTS);
        }
        int commandSize = 0;
        if (hasBehaviors) {
            commandSize += behaviors.size();
        }
        if (hasEvent) {
            commandSize++;
        }
        if (hasCommand) {
            commandSize++;
        }
        if (commandSize > 1) {
            boolean first = true;
            builder.append("PrimeFaces.bcn(this,event,[");
            if (hasEvent) {
                builder.append("function(event){").append(event).append("}");
                first = false;
            }
            if (hasBehaviors) {
                ClientBehaviorContext cbc = null;
                for (int i = 0; i < behaviors.size(); i++) {
                    ClientBehavior behavior = behaviors.get(i);
                    if (cbc == null) {
                        cbc = ClientBehaviorContext.createClientBehaviorContext(context, component, behaviorEventName, component.getClientId(context), Collections.<ClientBehaviorContext.Parameter>emptyList());
                    }
                    String script = behavior.getScript(cbc);
                    if (script != null) {
                        if (!first) {
                            builder.append(",");
                        }
                        builder.append("function(event){").append(script).append("}");
                        first = false;
                    }
                }
            }
            if (hasCommand) {
                if (!first) {
                    builder.append(",");
                }
                builder.append("function(event){").append(command).append("}");
                first = false;
            }
            builder.append("]);");
        } else {
            if (hasBehaviors) {
                ClientBehaviorContext cbc = ClientBehaviorContext.createClientBehaviorContext(context, component, behaviorEventName, component.getClientId(context), Collections.<ClientBehaviorContext.Parameter>emptyList());
                ClientBehavior behavior = behaviors.get(0);
                String script = behavior.getScript(cbc);
                if (script != null) {
                    builder.append(script);
                }
            } else if (hasCommand) {
                builder.append(command);
            } else if (hasEvent) {
                builder.append(event);
            }
        }
    }
    return (builder == null) ? null : builder.toString();
}
Also used : ClientBehaviorHolder(javax.faces.component.behavior.ClientBehaviorHolder) MixedClientBehaviorHolder(org.primefaces.component.api.MixedClientBehaviorHolder) ClientBehavior(javax.faces.component.behavior.ClientBehavior) ClientBehaviorContext(javax.faces.component.behavior.ClientBehaviorContext)

Example 7 with ClientBehaviorContext

use of javax.faces.component.behavior.ClientBehaviorContext in project primefaces by primefaces.

the class MenuItemAwareRenderer method setConfirmationScript.

protected void setConfirmationScript(FacesContext context, MenuItem item) {
    if (item instanceof ClientBehaviorHolder) {
        Map<String, List<ClientBehavior>> behaviors = ((ClientBehaviorHolder) item).getClientBehaviors();
        List<ClientBehavior> clickBehaviors = (behaviors == null) ? null : behaviors.get("click");
        if (clickBehaviors != null && !clickBehaviors.isEmpty()) {
            for (int i = 0; i < clickBehaviors.size(); i++) {
                ClientBehavior clientBehavior = clickBehaviors.get(i);
                if (clientBehavior instanceof ConfirmBehavior) {
                    ClientBehaviorContext cbc = ClientBehaviorContext.createClientBehaviorContext(context, (UIComponent) item, "click", item.getClientId(), Collections.emptyList());
                    clientBehavior.getScript(cbc);
                    break;
                }
            }
        }
    }
}
Also used : ClientBehaviorHolder(javax.faces.component.behavior.ClientBehaviorHolder) ConfirmBehavior(org.primefaces.behavior.confirm.ConfirmBehavior) ClientBehavior(javax.faces.component.behavior.ClientBehavior) ClientBehaviorContext(javax.faces.component.behavior.ClientBehaviorContext)

Example 8 with ClientBehaviorContext

use of javax.faces.component.behavior.ClientBehaviorContext in project primefaces by primefaces.

the class AjaxBehaviorRenderer method getScript.

@Override
public String getScript(ClientBehaviorContext behaviorContext, ClientBehavior behavior) {
    AjaxBehavior ajaxBehavior = (AjaxBehavior) behavior;
    if (ajaxBehavior.isDisabled()) {
        return null;
    }
    UIComponent component = behaviorContext.getComponent();
    ClientBehaviorRenderingMode renderingMode = ClientBehaviorRenderingMode.OBSTRUSIVE;
    Collection<ClientBehaviorContext.Parameter> behaviorParameters = behaviorContext.getParameters();
    if (behaviorParameters != null && !behaviorParameters.isEmpty()) {
        // perf optimzation
        if (behaviorParameters instanceof ArrayList) {
            for (int i = 0; i < behaviorParameters.size(); i++) {
                ClientBehaviorContext.Parameter behaviorParameter = ((ArrayList<ClientBehaviorContext.Parameter>) behaviorParameters).get(i);
                if (behaviorParameter.getValue() instanceof ClientBehaviorRenderingMode) {
                    renderingMode = (ClientBehaviorRenderingMode) behaviorParameter.getValue();
                    break;
                }
            }
        } else {
            for (ClientBehaviorContext.Parameter behaviorParameter : behaviorParameters) {
                if (behaviorParameter.getValue() instanceof ClientBehaviorRenderingMode) {
                    renderingMode = (ClientBehaviorRenderingMode) behaviorParameter.getValue();
                    break;
                }
            }
        }
    }
    String source = behaviorContext.getSourceId();
    String process = ajaxBehavior.getProcess();
    if (process == null) {
        process = "@this";
    }
    AjaxRequestBuilder builder = PrimeRequestContext.getCurrentInstance().getAjaxRequestBuilder();
    String request = builder.init().source(source).event(behaviorContext.getEventName()).form(ajaxBehavior, component).process(component, process, ajaxBehavior.isIgnoreComponentNotFound()).update(component, ajaxBehavior.getUpdate(), ajaxBehavior.isIgnoreComponentNotFound()).async(ajaxBehavior.isAsync()).global(ajaxBehavior.isGlobal()).delay(ajaxBehavior.getDelay()).timeout(ajaxBehavior.getTimeout()).partialSubmit(ajaxBehavior.isPartialSubmit(), ajaxBehavior.isPartialSubmitSet(), ajaxBehavior.getPartialSubmitFilter()).resetValues(ajaxBehavior.isResetValues(), ajaxBehavior.isResetValuesSet()).ignoreAutoUpdate(ajaxBehavior.isIgnoreAutoUpdate()).skipChildren(ajaxBehavior.isSkipChildren()).onstart(ajaxBehavior.getOnstart()).onerror(ajaxBehavior.getOnerror()).onsuccess(ajaxBehavior.getOnsuccess()).oncomplete(ajaxBehavior.getOncomplete()).params(component).buildBehavior(renderingMode);
    return request;
}
Also used : ClientBehaviorRenderingMode(org.primefaces.component.api.ClientBehaviorRenderingMode) UIComponent(javax.faces.component.UIComponent) ArrayList(java.util.ArrayList) AjaxRequestBuilder(org.primefaces.util.AjaxRequestBuilder) ClientBehaviorContext(javax.faces.component.behavior.ClientBehaviorContext)

Example 9 with ClientBehaviorContext

use of javax.faces.component.behavior.ClientBehaviorContext in project primefaces by primefaces.

the class CoreRenderer method encodeClientBehaviors.

protected void encodeClientBehaviors(FacesContext context, ClientBehaviorHolder component) throws IOException {
    ResponseWriter writer = context.getResponseWriter();
    Map<String, List<ClientBehavior>> clientBehaviors = component.getClientBehaviors();
    if (clientBehaviors != null && !clientBehaviors.isEmpty()) {
        boolean written = false;
        Collection<String> eventNames = (component instanceof MixedClientBehaviorHolder) ? ((MixedClientBehaviorHolder) component).getUnobstrusiveEventNames() : clientBehaviors.keySet();
        String clientId = ((UIComponent) component).getClientId(context);
        List<ClientBehaviorContext.Parameter> params = new ArrayList<>(1);
        params.add(new ClientBehaviorContext.Parameter(Constants.CLIENT_BEHAVIOR_RENDERING_MODE, ClientBehaviorRenderingMode.UNOBSTRUSIVE));
        writer.write(",behaviors:{");
        for (Iterator<String> eventNameIterator = eventNames.iterator(); eventNameIterator.hasNext(); ) {
            String eventName = eventNameIterator.next();
            List<ClientBehavior> eventBehaviors = clientBehaviors.get(eventName);
            if (eventBehaviors != null && !eventBehaviors.isEmpty()) {
                if (written) {
                    writer.write(",");
                }
                int eventBehaviorsSize = eventBehaviors.size();
                writer.write(eventName + ":");
                writer.write("function(ext,event) {");
                if (eventBehaviorsSize > 1) {
                    boolean chained = false;
                    writer.write("PrimeFaces.bcnu(ext,event,[");
                    for (int i = 0; i < eventBehaviorsSize; i++) {
                        ClientBehavior behavior = eventBehaviors.get(i);
                        ClientBehaviorContext cbc = ClientBehaviorContext.createClientBehaviorContext(context, (UIComponent) component, eventName, clientId, params);
                        String script = behavior.getScript(cbc);
                        if (script != null) {
                            if (chained) {
                                writer.write(",");
                            }
                            writer.write("function(ext,event) {");
                            writer.write(script);
                            writer.write("}");
                            chained = true;
                        }
                    }
                    writer.write("]);");
                } else {
                    ClientBehavior behavior = eventBehaviors.get(0);
                    ClientBehaviorContext cbc = ClientBehaviorContext.createClientBehaviorContext(context, (UIComponent) component, eventName, clientId, params);
                    String script = behavior.getScript(cbc);
                    if (script != null) {
                        writer.write(script);
                    }
                }
                writer.write("}");
                written = true;
            }
        }
        writer.write("}");
    }
}
Also used : MixedClientBehaviorHolder(org.primefaces.component.api.MixedClientBehaviorHolder) ClientBehaviorContext(javax.faces.component.behavior.ClientBehaviorContext) ResponseWriter(javax.faces.context.ResponseWriter) ClientBehavior(javax.faces.component.behavior.ClientBehavior)

Example 10 with ClientBehaviorContext

use of javax.faces.component.behavior.ClientBehaviorContext in project primefaces by primefaces.

the class CoreRenderer method getEventBehaviors.

protected String getEventBehaviors(FacesContext context, ClientBehaviorHolder cbh, String event, List<ClientBehaviorContext.Parameter> parameters) {
    List<ClientBehavior> behaviors = cbh.getClientBehaviors().get(event);
    StringBuilder sb = SharedStringBuilder.get(context, SB_GET_EVENT_BEHAVIORS);
    if (behaviors != null && !behaviors.isEmpty()) {
        UIComponent component = (UIComponent) cbh;
        String clientId = component.getClientId(context);
        List<ClientBehaviorContext.Parameter> params;
        if (parameters != null && !parameters.isEmpty()) {
            params = parameters;
        } else {
            params = Collections.emptyList();
        }
        for (int i = 0; i < behaviors.size(); i++) {
            ClientBehavior behavior = behaviors.get(i);
            ClientBehaviorContext cbc = ClientBehaviorContext.createClientBehaviorContext(context, component, event, clientId, params);
            String script = behavior.getScript(cbc);
            if (script != null) {
                sb.append(script).append(";");
            }
        }
    }
    return sb.length() == 0 ? null : sb.toString();
}
Also used : ClientBehavior(javax.faces.component.behavior.ClientBehavior) ClientBehaviorContext(javax.faces.component.behavior.ClientBehaviorContext)

Aggregations

ClientBehaviorContext (javax.faces.component.behavior.ClientBehaviorContext)14 ClientBehavior (javax.faces.component.behavior.ClientBehavior)12 List (java.util.List)8 ResponseWriter (javax.faces.context.ResponseWriter)5 JavaScriptFragment (com.liferay.faces.util.render.JavaScriptFragment)4 ArrayList (java.util.ArrayList)4 UIParameter (javax.faces.component.UIParameter)3 DataEncoderBase (com.liferay.faces.alloy.component.data.internal.DataEncoderBase)2 ClientBehaviorHolder (javax.faces.component.behavior.ClientBehaviorHolder)2 MixedClientBehaviorHolder (org.primefaces.component.api.MixedClientBehaviorHolder)2 Accordion (com.liferay.faces.alloy.component.accordion.Accordion)1 ProgressBar (com.liferay.faces.alloy.component.progressbar.ProgressBar)1 TabView (com.liferay.faces.alloy.component.tabview.TabView)1 BufferedScriptResponseWriter (com.liferay.faces.util.render.BufferedScriptResponseWriter)1 UIComponent (javax.faces.component.UIComponent)1 AjaxBehavior (javax.faces.component.behavior.AjaxBehavior)1 ConfirmBehavior (org.primefaces.behavior.confirm.ConfirmBehavior)1 ClientBehaviorRenderingMode (org.primefaces.component.api.ClientBehaviorRenderingMode)1 AjaxRequestBuilder (org.primefaces.util.AjaxRequestBuilder)1