Search in sources :

Example 1 with OutputScript

use of com.liferay.faces.alloy.component.outputscript.OutputScript 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)

Aggregations

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