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