Search in sources :

Example 11 with ClientBehaviorContext

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

the class CoreRenderer method renderDomEvents.

private void renderDomEvents(FacesContext context, UIComponent component, List<String> eventAttrs, Map<String, List<ClientBehavior>> behaviors) throws IOException {
    if (eventAttrs == null || eventAttrs.isEmpty()) {
        return;
    }
    ResponseWriter writer = context.getResponseWriter();
    StringBuilder builder = null;
    for (int i = 0; i < eventAttrs.size(); i++) {
        String domEvent = eventAttrs.get(i);
        Object eventValue = component.getAttributes().get(domEvent);
        String behaviorEvent = domEvent.substring(2, domEvent.length());
        List<ClientBehavior> eventBehaviors = behaviors.get(behaviorEvent);
        boolean hasEventValue = (eventValue != null);
        boolean hasEventBehaviors = (eventBehaviors != null && !eventBehaviors.isEmpty());
        if ("onchange".equals(domEvent) && !hasEventBehaviors) {
            eventBehaviors = behaviors.get("valueChange");
            hasEventBehaviors = (eventBehaviors != null && !eventBehaviors.isEmpty());
            if (hasEventBehaviors) {
                behaviorEvent = "valueChange";
            }
        }
        if (hasEventValue || hasEventBehaviors) {
            if (builder == null) {
                builder = SharedStringBuilder.get(context, SB_RENDER_DOM_EVENTS);
            }
            if (hasEventBehaviors) {
                String clientId = component.getClientId(context);
                List<ClientBehaviorContext.Parameter> params = new ArrayList<>(1);
                params.add(new ClientBehaviorContext.Parameter(Constants.CLIENT_BEHAVIOR_RENDERING_MODE, ClientBehaviorRenderingMode.OBSTRUSIVE));
                ClientBehaviorContext cbc = ClientBehaviorContext.createClientBehaviorContext(context, component, behaviorEvent, clientId, params);
                int size = eventBehaviors.size();
                boolean chained = false;
                if (size > 1 || hasEventValue) {
                    builder.append("PrimeFaces.bcn(this,event,[");
                    if (hasEventValue) {
                        builder.append("function(event){").append(eventValue).append("}");
                        chained = true;
                    }
                    for (int j = 0; j < size; j++) {
                        ClientBehavior behavior = eventBehaviors.get(j);
                        String script = behavior.getScript(cbc);
                        if (script != null) {
                            if (chained) {
                                builder.append(",");
                            }
                            builder.append("function(event){").append(script).append("}");
                            chained = true;
                        }
                    }
                    builder.append("])");
                } else {
                    ClientBehavior behavior = eventBehaviors.get(0);
                    String script = behavior.getScript(cbc);
                    if (script != null) {
                        builder.append(script);
                    }
                }
            } else if (hasEventValue) {
                builder.append(eventValue);
            }
            if (builder.length() > 0) {
                writer.writeAttribute(domEvent, builder.toString(), domEvent);
                builder.setLength(0);
            }
        }
    }
}
Also used : ClientBehaviorContext(javax.faces.component.behavior.ClientBehaviorContext) ResponseWriter(javax.faces.context.ResponseWriter) ClientBehavior(javax.faces.component.behavior.ClientBehavior)

Example 12 with ClientBehaviorContext

use of javax.faces.component.behavior.ClientBehaviorContext in project liferay-faces-alloy by liferay.

the class TabViewRenderer method encodeJavaScriptCustom.

@Override
public void encodeJavaScriptCustom(FacesContext facesContext, UIComponent uiComponent) throws IOException {
    // Encode a script that will handle the client-side state of the selected tab index, as well as submitting
    // Ajax requests to support server-side events.
    TabView tabView = (TabView) uiComponent;
    // var tabViewClientVarName = Liferay.component('{clientKey}');
    String clientVarName = getClientVarName(facesContext, tabView);
    String clientKey = tabView.getClientKey();
    if (clientKey == null) {
        clientKey = clientVarName;
    }
    ResponseWriter responseWriter = facesContext.getResponseWriter();
    encodeLiferayComponentVar(responseWriter, clientVarName, clientKey);
    // Enable the enabled tabs, disable the disabled tabs, and select the selected tab on the client side.
    Integer selectedIndex = tabView.getSelectedIndex();
    DataEncoderBase dataEncoder = new DataEncoderTabJavaScriptImpl(selectedIndex, clientVarName);
    dataEncoder.encodeColumns(facesContext, tabView);
    // LFAI.initTabView(tabViewClientVarName, '{clientId}selectedIndex', '{clientId}',
    // [tabSelectClientBehaviorScriptFunction1, tabSelectClientBehaviorScriptFunction2,
    // tabSelectClientBehaviorScriptFunction3]);
    String clientId = uiComponent.getClientId(facesContext);
    String hiddenFieldId = clientId + "selectedIndex";
    StringBuilder clientBehaviorsArrayStringBuilder = new StringBuilder();
    clientBehaviorsArrayStringBuilder.append("[");
    boolean first = true;
    Map<String, List<ClientBehavior>> clientBehaviorMap = tabView.getClientBehaviors();
    Collection<String> eventNames = tabView.getEventNames();
    for (String eventName : eventNames) {
        List<ClientBehavior> clientBehaviorsForEvent = clientBehaviorMap.get(eventName);
        if (clientBehaviorsForEvent != null) {
            for (ClientBehavior clientBehavior : clientBehaviorsForEvent) {
                ClientBehaviorContext clientBehaviorContext = ClientBehaviorContext.createClientBehaviorContext(facesContext, tabView, eventName, clientId, null);
                // an Ajax request.
                if (TabSelectEvent.TAB_SELECT.equals(eventName)) {
                    if (!first) {
                        clientBehaviorsArrayStringBuilder.append(",");
                    }
                    // J-
                    // function(event) {
                    // jsf.ajax.request(this, event, {'javax.faces.behavior.event': 'tabSelect'})
                    // };
                    // J+
                    clientBehaviorsArrayStringBuilder.append("function(event){");
                    clientBehaviorsArrayStringBuilder.append(clientBehavior.getScript(clientBehaviorContext));
                    clientBehaviorsArrayStringBuilder.append("}");
                    first = false;
                }
            }
        }
    }
    clientBehaviorsArrayStringBuilder.append("]");
    encodeFunctionCall(responseWriter, "LFAI.initTabView", new JavaScriptFragment(clientVarName), hiddenFieldId, clientId, new JavaScriptFragment(clientBehaviorsArrayStringBuilder.toString()));
}
Also used : TabView(com.liferay.faces.alloy.component.tabview.TabView) ClientBehaviorContext(javax.faces.component.behavior.ClientBehaviorContext) ResponseWriter(javax.faces.context.ResponseWriter) JavaScriptFragment(com.liferay.faces.util.render.JavaScriptFragment) DataEncoderBase(com.liferay.faces.alloy.component.data.internal.DataEncoderBase) List(java.util.List) ClientBehavior(javax.faces.component.behavior.ClientBehavior)

Example 13 with ClientBehaviorContext

use of javax.faces.component.behavior.ClientBehaviorContext in project liferay-faces-alloy by liferay.

the class ProgressBarRenderer method encodeJavaScriptCustom.

@Override
public void encodeJavaScriptCustom(FacesContext facesContext, UIComponent uiComponent) throws IOException {
    ResponseWriter responseWriter = facesContext.getResponseWriter();
    ProgressBar progressBar = (ProgressBar) uiComponent;
    String clientVarName = getClientVarName(facesContext, progressBar);
    String clientKey = progressBar.getClientKey();
    if (clientKey == null) {
        clientKey = clientVarName;
    }
    Map<String, List<ClientBehavior>> clientBehaviorMap = progressBar.getClientBehaviors();
    List<ClientBehavior> pollEventClientBehaviors = clientBehaviorMap.get("poll");
    // If the developer has specified <f:ajax event="poll" />, then
    String javaScriptText = "Liferay.component('".concat(clientKey).concat("')");
    if ((pollEventClientBehaviors != null) && !pollEventClientBehaviors.isEmpty()) {
        // Build up an anonymous function, which contains all clientBehaviors for the "poll" event, so that it can
        // be passed to LFAI.initProgressBarServerMode().
        // J-
        // function(pollingFunction) {
        // var event = null;
        // jsf.ajax.request('clientId', event, 'poll', {
        // render: 'clientId ' + render,
        // execute: 'clientId ' + execute,
        // onevent: function(data){
        // if(data.status==='success'){
        // pollingFunction();
        // }
        // onevent();
        // },
        // onerror: function(data){
        // Liferay.component('clientKey').stopPolling();
        // onerror();
        // }
        // });
        // jsf.ajax.request(...);
        // ...
        // jsf.ajax.request(...);
        // }
        // J+
        StringBuilder buf = new StringBuilder();
        buf.append("function(pollingFunction){");
        // ClientBehavior.getScript(ClientBehaviorContext clientBehaviorContext) renders javascript which expects a
        // javascript variable named 'event' to have been initilized and contain the DOM event which triggered the
        // request. Since there is no DOM event which triggers the progressBar events, render javascript
        // initializing event to null;
        buf.append("var event = null;");
        String clientId = progressBar.getClientId(facesContext);
        ClientBehaviorContext clientBehaviorContext = ClientBehaviorContext.createClientBehaviorContext(facesContext, progressBar, "poll", clientId, null);
        int size = pollEventClientBehaviors.size();
        // J-
        // Liferay.component('clientKey')
        // J+
        JavaScriptFragment liferayComponent = new JavaScriptFragment(javaScriptText);
        // It is possible to specify multiple <f:ajax event="poll" /> tags (even though there is no benefit).
        for (int i = 0; i < size; i++) {
            ClientBehavior pollEventClientBehavior = pollEventClientBehaviors.get(i);
            if (i == 0) {
                AjaxBehavior firstPollEventAjaxBehavior = (AjaxBehavior) pollEventClientBehavior;
                String stopPollingFunction = "function(){".concat(liferayComponent.toString()).concat(".stopPolling();}");
                // Ensure that render is '@this', execute is '@this', the pollingFunction is called onsuccess, and
                // the stopPolling function is called onerror.
                pollEventClientBehavior = new ProgressBarAjaxBehavior(firstPollEventAjaxBehavior, "pollingFunction", stopPollingFunction);
            }
            buf.append(pollEventClientBehavior.getScript(clientBehaviorContext));
            buf.append(";");
        }
        buf.append("}");
        JavaScriptFragment anonymousFunction = new JavaScriptFragment(buf.toString());
        Integer pollingDelay = progressBar.getPollingDelay();
        // J-
        // LFAI.initProgressBarServerMode(Liferay.component('clientKey'), 'clientId', pollingDelay,
        // function(pollingFunction) {
        // var event = null;
        // jsf.ajax.request('clientId', event, 'poll', {
        // render: 'clientId ' + render,
        // execute: 'clientId ' + execute,
        // onevent: function(data){
        // if(data.status==='success'){
        // pollingFunction();
        // }
        // onevent();
        // },
        // onerror: function(data){
        // Liferay.component('clientKey').stopPolling();
        // onerror();
        // }
        // });
        // jsf.ajax.request(...);
        // ...
        // jsf.ajax.request(...);
        // }
        // );
        // J+
        encodeFunctionCall(responseWriter, "LFAI.initProgressBarServerMode", liferayComponent, clientId, pollingDelay, anonymousFunction);
    } else // Otherwise the component is in client mode.
    {
        // J-
        // LFAI.initProgressBarClientMode(Liferay.component('clientKey'), projectStageDevelopment);
        // J+
        JavaScriptFragment liferayComponent = new JavaScriptFragment(javaScriptText);
        encodeFunctionCall(responseWriter, "LFAI.initProgressBarClientMode", liferayComponent);
    }
}
Also used : ClientBehaviorContext(javax.faces.component.behavior.ClientBehaviorContext) AjaxBehavior(javax.faces.component.behavior.AjaxBehavior) BufferedScriptResponseWriter(com.liferay.faces.util.render.BufferedScriptResponseWriter) ResponseWriter(javax.faces.context.ResponseWriter) JavaScriptFragment(com.liferay.faces.util.render.JavaScriptFragment) List(java.util.List) ProgressBar(com.liferay.faces.alloy.component.progressbar.ProgressBar) ClientBehavior(javax.faces.component.behavior.ClientBehavior)

Example 14 with ClientBehaviorContext

use of javax.faces.component.behavior.ClientBehaviorContext in project liferay-faces-alloy by liferay.

the class DataTableRenderer method getRowEventClientBehaviorScript.

protected JavaScriptFragment getRowEventClientBehaviorScript(FacesContext facesContext, DataTable dataTable, String dataTableClientId, String eventName, String parameterName) {
    StringBuilder scriptBuilder = new StringBuilder();
    scriptBuilder.append("function(");
    scriptBuilder.append(parameterName);
    scriptBuilder.append(", event){");
    Map<String, List<ClientBehavior>> clientBehaviorMap = dataTable.getClientBehaviors();
    List<ClientBehavior> clientBehaviorsForEvent = clientBehaviorMap.get(eventName);
    if (clientBehaviorsForEvent != null) {
        for (ClientBehavior clientBehavior : clientBehaviorsForEvent) {
            List<ClientBehaviorContext.Parameter> parameters = new ArrayList<ClientBehaviorContext.Parameter>();
            String eventMetaKeyParamName = dataTableClientId.concat("_").concat(parameterName);
            parameters.add(new ClientBehaviorContext.Parameter(eventMetaKeyParamName, parameterName));
            ClientBehaviorContext clientBehaviorContext = ClientBehaviorContext.createClientBehaviorContext(facesContext, dataTable, eventName, dataTableClientId, parameters);
            String script = clientBehavior.getScript(clientBehaviorContext);
            if (script != null) {
                String quotedParamName = "'".concat(parameterName).concat("'");
                script = script.replaceFirst(quotedParamName, parameterName);
            }
            scriptBuilder.append(script);
        }
    }
    scriptBuilder.append("}");
    return new JavaScriptFragment(scriptBuilder.toString());
}
Also used : ArrayList(java.util.ArrayList) JavaScriptFragment(com.liferay.faces.util.render.JavaScriptFragment) UIParameter(javax.faces.component.UIParameter) ArrayList(java.util.ArrayList) List(java.util.List) 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