Search in sources :

Example 1 with MixedClientBehaviorHolder

use of org.primefaces.component.api.MixedClientBehaviorHolder 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)

Aggregations

ClientBehavior (javax.faces.component.behavior.ClientBehavior)1 ClientBehaviorContext (javax.faces.component.behavior.ClientBehaviorContext)1 ResponseWriter (javax.faces.context.ResponseWriter)1 MixedClientBehaviorHolder (org.primefaces.component.api.MixedClientBehaviorHolder)1