Search in sources :

Example 1 with ProgressBar

use of com.liferay.faces.alloy.component.progressbar.ProgressBar in project liferay-faces-alloy by liferay.

the class ProgressBarRendererBase method encodeAlloyAttributes.

@Override
public void encodeAlloyAttributes(FacesContext facesContext, ResponseWriter responseWriter, UIComponent uiComponent) throws IOException {
    ProgressBar progressBar = (ProgressBar) uiComponent;
    boolean first = true;
    String height = progressBar.getHeight();
    if (height != null) {
        encodeHeight(responseWriter, progressBar, height, first);
        first = false;
    }
    String label = progressBar.getLabel();
    if (label != null) {
        encodeLabel(responseWriter, progressBar, label, first);
        first = false;
    }
    String layout = progressBar.getLayout();
    if (layout != null) {
        encodeOrientation(responseWriter, progressBar, layout, first);
        first = false;
    }
    Integer maxProgress = progressBar.getMaxProgress();
    if (maxProgress != null) {
        encodeMax(responseWriter, progressBar, maxProgress, first);
        first = false;
    }
    Integer minProgress = progressBar.getMinProgress();
    if (minProgress != null) {
        encodeMin(responseWriter, progressBar, minProgress, first);
        first = false;
    }
    Integer value = progressBar.getValue();
    if (value != null) {
        encodeValue(responseWriter, progressBar, value, first);
        first = false;
    }
    String width = progressBar.getWidth();
    if (width != null) {
        encodeWidth(responseWriter, progressBar, width, first);
        first = false;
    }
    encodeHiddenAttributes(facesContext, responseWriter, progressBar, first);
}
Also used : ProgressBar(com.liferay.faces.alloy.component.progressbar.ProgressBar)

Example 2 with ProgressBar

use of com.liferay.faces.alloy.component.progressbar.ProgressBar in project liferay-faces-alloy by liferay.

the class ProgressBarRenderer method encodeJavaScript.

/**
 * This method is being overridden in order to set the value of the progressBar when server-side polling occurs.
 * Otherwise, this method simply calls super.encodeJavaScript() in order to render the component normally.
 */
@Override
public void encodeJavaScript(FacesContext facesContext, UIComponent uiComponent) throws IOException {
    if (isAjaxPolling(facesContext, uiComponent)) {
        ProgressBar progressBar = (ProgressBar) uiComponent;
        String clientVarName = getClientVarName(facesContext, progressBar);
        String clientKey = progressBar.getClientKey();
        if (clientKey == null) {
            clientKey = clientVarName;
        }
        // Buffer all JavaScript so that it is rendered in the <eval> section of the partial response.
        BufferedScriptResponseWriter bufferedScriptResponseWriter = new BufferedScriptResponseWriter();
        String clientId = progressBar.getClientId(facesContext);
        String hiddenClientId = clientId.concat(HIDDEN_SUFFIX);
        // J-
        // Liferay.component('clientKey')
        // J+
        JavaScriptFragment liferayComponent = new JavaScriptFragment("Liferay.component('" + clientKey + "')");
        Integer value = progressBar.getValue();
        // J-
        // LFAI.setProgressBarServerValue('hiddenClientId', Liferay.component('clientKey'), value);
        // J+
        encodeFunctionCall(bufferedScriptResponseWriter, "LFAI.setProgressBarServerValue", hiddenClientId, liferayComponent, value);
        FacesRequestContext facesRequestContext = FacesRequestContext.getCurrentInstance();
        facesRequestContext.addScript(bufferedScriptResponseWriter.toString());
    } else {
        super.encodeJavaScript(facesContext, uiComponent);
    }
}
Also used : FacesRequestContext(com.liferay.faces.util.context.FacesRequestContext) BufferedScriptResponseWriter(com.liferay.faces.util.render.BufferedScriptResponseWriter) JavaScriptFragment(com.liferay.faces.util.render.JavaScriptFragment) ProgressBar(com.liferay.faces.alloy.component.progressbar.ProgressBar)

Example 3 with ProgressBar

use of com.liferay.faces.alloy.component.progressbar.ProgressBar 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 4 with ProgressBar

use of com.liferay.faces.alloy.component.progressbar.ProgressBar in project liferay-faces-alloy by liferay.

the class ProgressBarRenderer method isServerPollingEnabled.

private boolean isServerPollingEnabled(UIComponent uiComponent) {
    ProgressBar progressBar = (ProgressBar) uiComponent;
    Map<String, List<ClientBehavior>> clientBehaviorMap = progressBar.getClientBehaviors();
    List<ClientBehavior> clientBehaviorsForPolling = clientBehaviorMap.get("poll");
    return ((clientBehaviorsForPolling != null) && !clientBehaviorsForPolling.isEmpty());
}
Also used : List(java.util.List) ProgressBar(com.liferay.faces.alloy.component.progressbar.ProgressBar) ClientBehavior(javax.faces.component.behavior.ClientBehavior)

Aggregations

ProgressBar (com.liferay.faces.alloy.component.progressbar.ProgressBar)4 BufferedScriptResponseWriter (com.liferay.faces.util.render.BufferedScriptResponseWriter)2 JavaScriptFragment (com.liferay.faces.util.render.JavaScriptFragment)2 List (java.util.List)2 ClientBehavior (javax.faces.component.behavior.ClientBehavior)2 FacesRequestContext (com.liferay.faces.util.context.FacesRequestContext)1 AjaxBehavior (javax.faces.component.behavior.AjaxBehavior)1 ClientBehaviorContext (javax.faces.component.behavior.ClientBehaviorContext)1 ResponseWriter (javax.faces.context.ResponseWriter)1