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);
}
}
}
Aggregations