Search in sources :

Example 1 with FacesRequestContext

use of com.liferay.faces.util.context.FacesRequestContext in project liferay-faces-alloy by liferay.

the class OutputScriptRenderer method encodeChildren.

@Override
public void encodeChildren(FacesContext facesContext, UIComponent uiComponent) throws IOException {
    OutputScript outputScript = (OutputScript) uiComponent;
    String name = outputScript.getName();
    // render a JSF resource.
    if ((name != null) && (name.length() > 0)) {
        super.encodeChildren(facesContext, uiComponent);
    } else // Otherwise, since it is not a JSF resource:
    {
        // If target="body" and this is not an Ajax request, then
        String use = outputScript.getUse();
        String target = outputScript.getTarget();
        if ("body".equals(target) && !facesContext.getPartialViewContext().isAjaxRequest()) {
            ResponseWriter responseWriter = facesContext.getResponseWriter();
            BufferedScriptResponseWriter bufferedScriptResponseWriter = new BufferedScriptResponseWriter();
            facesContext.setResponseWriter(bufferedScriptResponseWriter);
            super.encodeChildren(facesContext, uiComponent);
            facesContext.setResponseWriter(responseWriter);
            Script script;
            String bufferedScriptString = bufferedScriptResponseWriter.toString();
            ExternalContext externalContext = facesContext.getExternalContext();
            if ((use != null) && (use.length() > 0)) {
                String[] modules = use.split(",");
                script = ScriptFactory.getScriptInstance(externalContext, bufferedScriptString, modules, Script.ModulesType.ALLOY);
            } else {
                script = ScriptFactory.getScriptInstance(externalContext, bufferedScriptString);
            }
            // Render the script at the bottom of the page immediately before the closing </body> tag.
            FacesRequestContext facesRequestContext = FacesRequestContext.getCurrentInstance();
            facesRequestContext.addScript(script);
        } else // correct modules around the script.
        if ((use != null) && (use.length() > 0)) {
            // Delegate to the JSF runtime Renderer, but replace the JSF runtime ResponseWriter with an
            // OutputScriptResponseWriter which will render the YUI sandbox code around the script.
            ResponseWriter responseWriter = facesContext.getResponseWriter();
            // In order to determine the exact YUI sandbox string to write, the modules and browser information
            // must be passed to RendererUtil.getAlloyBeginScript().
            String[] modules = use.split(",");
            BrowserSniffer browserSniffer = BrowserSnifferFactory.getBrowserSnifferInstance(facesContext.getExternalContext());
            String alloyBeginScript = AlloyRendererUtil.getAlloyBeginScript(modules, null, browserSniffer);
            OutputScriptResponseWriter outputScriptResponseWriter = new OutputScriptResponseWriter(responseWriter, alloyBeginScript);
            super.encodeChildren(facesContext, uiComponent, outputScriptResponseWriter);
        } else // Otherwise, simply delegate to the JSF runtime renderer since target="head" or target="form".
        {
            super.encodeChildren(facesContext, uiComponent);
        }
    }
}
Also used : FacesRequestContext(com.liferay.faces.util.context.FacesRequestContext) OutputScript(com.liferay.faces.alloy.component.outputscript.OutputScript) Script(com.liferay.faces.util.client.Script) BrowserSniffer(com.liferay.faces.util.client.BrowserSniffer) BufferedScriptResponseWriter(com.liferay.faces.util.render.BufferedScriptResponseWriter) BufferedScriptResponseWriter(com.liferay.faces.util.render.BufferedScriptResponseWriter) ResponseWriter(javax.faces.context.ResponseWriter) ExternalContext(javax.faces.context.ExternalContext) OutputScript(com.liferay.faces.alloy.component.outputscript.OutputScript)

Example 2 with FacesRequestContext

use of com.liferay.faces.util.context.FacesRequestContext in project liferay-faces-bridge-impl by liferay.

the class ResourceRendererImpl method encodeBegin.

@Override
public void encodeBegin(FacesContext facesContext, UIComponent uiComponentResource) throws IOException {
    String resourceInfo = "<span>" + ResourceUtil.getResourceId(uiComponentResource) + "</span>";
    ExternalContext externalContext = facesContext.getExternalContext();
    if (ResourceVerifierFactory.getResourceVerifierInstance(externalContext).isDependencySatisfied(facesContext, uiComponentResource)) {
        resourceInfo += " was suppressed.";
    } else {
        resourceInfo += " was rendered.";
    }
    FacesRequestContext facesRequestContext = FacesRequestContext.getCurrentInstance();
    String resourceInfoScript = SCRIPT.replace("{0}", resourceInfo);
    facesRequestContext.addScript(resourceInfoScript);
    super.encodeBegin(facesContext, uiComponentResource);
}
Also used : FacesRequestContext(com.liferay.faces.util.context.FacesRequestContext) ExternalContext(javax.faces.context.ExternalContext)

Example 3 with FacesRequestContext

use of com.liferay.faces.util.context.FacesRequestContext 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)

Aggregations

FacesRequestContext (com.liferay.faces.util.context.FacesRequestContext)3 BufferedScriptResponseWriter (com.liferay.faces.util.render.BufferedScriptResponseWriter)2 ExternalContext (javax.faces.context.ExternalContext)2 OutputScript (com.liferay.faces.alloy.component.outputscript.OutputScript)1 ProgressBar (com.liferay.faces.alloy.component.progressbar.ProgressBar)1 BrowserSniffer (com.liferay.faces.util.client.BrowserSniffer)1 Script (com.liferay.faces.util.client.Script)1 JavaScriptFragment (com.liferay.faces.util.render.JavaScriptFragment)1 ResponseWriter (javax.faces.context.ResponseWriter)1