Search in sources :

Example 1 with AjaxBehavior

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

the class CommandLinkRenderer method processEvent.

@Override
public void processEvent(ComponentSystemEvent componentSystemEvent) throws AbortProcessingException {
    CommandLink commandLink = (CommandLink) componentSystemEvent.getComponent();
    // If the specified event indicates that the command link was added to the component tree, then
    if (componentSystemEvent instanceof PostAddToViewEvent) {
        // Add the default Ajax behavior in order to support the ajax attribute. This effectively simulates the
        // presence of a child f:ajax tag.
        RendererUtil.addDefaultAjaxBehavior(commandLink, commandLink.getExecute(), commandLink.getProcess(), "@all", commandLink.getRender(), commandLink.getUpdate(), "@none");
    } else // Otherwise, the specified event indicates that the command link is about to be rendered. If the ajax
    // attribute is true, then ensure that the AjaxBehavior is rendered. Otherwise, ensure that the AjaxBehavior is
    // not rendered.
    {
        AjaxBehavior ajaxBehavior = RendererUtil.getDefaultAjaxBehavior(commandLink);
        ajaxBehavior.setDisabled(!commandLink.isAjax());
    }
}
Also used : PostAddToViewEvent(javax.faces.event.PostAddToViewEvent) CommandLink(com.liferay.faces.alloy.component.commandlink.CommandLink) AjaxBehavior(javax.faces.component.behavior.AjaxBehavior)

Example 2 with AjaxBehavior

use of javax.faces.component.behavior.AjaxBehavior 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 3 with AjaxBehavior

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

the class CommandButtonRenderer method processEvent.

@Override
public void processEvent(ComponentSystemEvent componentSystemEvent) throws AbortProcessingException {
    CommandButton commandButton = (CommandButton) componentSystemEvent.getComponent();
    // If the specified event indicates that the command button was added to the component tree, then
    if (componentSystemEvent instanceof PostAddToViewEvent) {
        // Add the default Ajax behavior in order to support the ajax attribute. This effectively simulates the
        // presence of a child f:ajax tag.
        RendererUtil.addDefaultAjaxBehavior(commandButton, commandButton.getExecute(), commandButton.getProcess(), "@all", commandButton.getRender(), commandButton.getUpdate(), "@none");
    } else // Otherwise, the specified event indicates that the command button is about to be rendered. If the ajax
    // attribute is true, then ensure that the AjaxBehavior is rendered. Otherwise, ensure that the AjaxBehavior is
    // not rendered.
    {
        AjaxBehavior ajaxBehavior = RendererUtil.getDefaultAjaxBehavior(commandButton);
        ajaxBehavior.setDisabled(!commandButton.isAjax());
    }
}
Also used : PostAddToViewEvent(javax.faces.event.PostAddToViewEvent) CommandButton(com.liferay.faces.alloy.component.commandbutton.CommandButton) AjaxBehavior(javax.faces.component.behavior.AjaxBehavior)

Aggregations

AjaxBehavior (javax.faces.component.behavior.AjaxBehavior)3 PostAddToViewEvent (javax.faces.event.PostAddToViewEvent)2 CommandButton (com.liferay.faces.alloy.component.commandbutton.CommandButton)1 CommandLink (com.liferay.faces.alloy.component.commandlink.CommandLink)1 ProgressBar (com.liferay.faces.alloy.component.progressbar.ProgressBar)1 BufferedScriptResponseWriter (com.liferay.faces.util.render.BufferedScriptResponseWriter)1 JavaScriptFragment (com.liferay.faces.util.render.JavaScriptFragment)1 List (java.util.List)1 ClientBehavior (javax.faces.component.behavior.ClientBehavior)1 ClientBehaviorContext (javax.faces.component.behavior.ClientBehaviorContext)1 ResponseWriter (javax.faces.context.ResponseWriter)1