Search in sources :

Example 1 with BufferedScriptResponseWriter

use of com.liferay.faces.util.render.BufferedScriptResponseWriter in project liferay-faces-alloy by liferay.

the class AlloyRendererCommon method encodeJavaScript.

/* package-private */
static void encodeJavaScript(FacesContext facesContext, UIComponent uiComponent, AlloyRenderer alloyRenderer) throws IOException {
    ResponseWriter responseWriter = facesContext.getResponseWriter();
    BufferedScriptResponseWriter bufferedScriptResponseWriter = new BufferedScriptResponseWriter();
    facesContext.setResponseWriter(bufferedScriptResponseWriter);
    alloyRenderer.encodeJavaScriptBegin(facesContext, uiComponent);
    alloyRenderer.encodeJavaScriptMain(facesContext, uiComponent);
    alloyRenderer.encodeJavaScriptCustom(facesContext, uiComponent);
    alloyRenderer.encodeJavaScriptEnd(facesContext, uiComponent);
    String[] modules = null;
    if (!alloyRenderer.isSandboxed(facesContext, uiComponent)) {
        modules = alloyRenderer.getModules(facesContext, uiComponent);
    }
    alloyRenderer.renderScript(facesContext, bufferedScriptResponseWriter.toString(), modules, Script.ModulesType.ALLOY);
    facesContext.setResponseWriter(responseWriter);
}
Also used : BufferedScriptResponseWriter(com.liferay.faces.util.render.BufferedScriptResponseWriter) ResponseWriter(javax.faces.context.ResponseWriter) BufferedScriptResponseWriter(com.liferay.faces.util.render.BufferedScriptResponseWriter)

Example 2 with BufferedScriptResponseWriter

use of com.liferay.faces.util.render.BufferedScriptResponseWriter 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 3 with BufferedScriptResponseWriter

use of com.liferay.faces.util.render.BufferedScriptResponseWriter 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 4 with BufferedScriptResponseWriter

use of com.liferay.faces.util.render.BufferedScriptResponseWriter in project liferay-faces-alloy by liferay.

the class AutoCompleteRenderer method encodeJavaScript.

/**
 * This method is being overridden in order to allow server-side filtering of autoComplete items when server-side
 * filtering is enabled during an Ajax request and there is a query. Otherwise, this method simply calls
 * super.encodeJavaScript() in order to render the component normally.
 */
@Override
public void encodeJavaScript(FacesContext facesContext, UIComponent uiComponent) throws IOException {
    AutoComplete autoComplete = (AutoComplete) uiComponent;
    // If the developer has specified a server-side filtering during Ajax, then
    if (isServerFilteringEnabled(autoComplete) && facesContext.getPartialViewContext().isAjaxRequest()) {
        // If the user has specified a query, then
        ExternalContext externalContext = facesContext.getExternalContext();
        Map<String, String> requestParameterMap = externalContext.getRequestParameterMap();
        String clientId = uiComponent.getClientId(facesContext);
        String hiddenClientId = clientId + HIDDEN_SUFFIX;
        String query = requestParameterMap.get(hiddenClientId);
        if ((query != null) && (query.length() > 0)) {
            // Get the entire list of completion items.
            List<String> items = autoComplete.getAllItems(facesContext);
            // If the developer has specified a serverCustomFilter, then call their custom filtering method.
            MethodExpression serverCustomFilter = autoComplete.getServerCustomFilter();
            if (serverCustomFilter != null) {
                items = invokeServerCustomFilter(facesContext.getELContext(), serverCustomFilter, query, items);
            } else // Otherwise, if the developer has specified a serverFilterType, then call the corresponding filtering
            // method.
            {
                String serverFilterType = autoComplete.getServerFilterType();
                if (serverFilterType != null) {
                    Locale locale = facesContext.getViewRoot().getLocale();
                    AutoCompleteFilterFactory autoCompleteFilterFactory = new AutoCompleteFilterFactoryImpl();
                    AutoCompleteFilter autoCompleteFilter = autoCompleteFilterFactory.getAutoCompleteFilter(serverFilterType);
                    if (autoCompleteFilter != null) {
                        boolean caseSensitive = serverFilterType.contains("Case");
                        items = autoCompleteFilter.doFilter(query, items, caseSensitive, locale);
                    } else {
                        throw new IOException(serverFilterType + " is not a valid serverFilterType.");
                    }
                }
            }
            // Build up a fragment of JavaScript that gets the client-side component.
            ClientComponent clientComponent = (ClientComponent) uiComponent;
            String clientVarName = getClientVarName(facesContext, clientComponent);
            String clientKey = clientComponent.getClientKey();
            if (clientKey == null) {
                clientKey = clientVarName;
            }
            // J-
            // Liferay.component('clientKey')
            // J+
            JavaScriptFragment liferayComponentJavaScriptFragment = new JavaScriptFragment("Liferay.component('" + clientKey + "')");
            // Build up a fragment of JavaScript that contains an array of the results.
            // J-
            // ['item1', 'item2', 'item3', ... 'itemN']
            // J+
            JavaScriptFragment[] results = AlloyRendererUtil.toEscapedJavaScriptStringArray(items);
            // Buffer all JavaScript so that it is rendered in the <eval> section of the partial response.
            BufferedScriptResponseWriter bufferedScriptResponseWriter = new BufferedScriptResponseWriter();
            // J-
            // LFAI.recieveAutoCompleteResults(Liferay.component('clientKey'), ['item1', 'item2', 'item3'],
            // 'hiddenClientId')
            // J+
            encodeFunctionCall(bufferedScriptResponseWriter, "LFAI.setAutoCompleteServerResults", liferayComponentJavaScriptFragment, results, hiddenClientId);
            String[] modules = getModules(facesContext, uiComponent);
            renderScript(facesContext, bufferedScriptResponseWriter.toString(), modules, Script.ModulesType.ALLOY);
        } else {
            super.encodeJavaScript(facesContext, uiComponent);
        }
    } else {
        super.encodeJavaScript(facesContext, uiComponent);
    }
}
Also used : Locale(java.util.Locale) BufferedScriptResponseWriter(com.liferay.faces.util.render.BufferedScriptResponseWriter) IOException(java.io.IOException) MethodExpression(javax.el.MethodExpression) ClientComponent(com.liferay.faces.util.component.ClientComponent) ExternalContext(javax.faces.context.ExternalContext) JavaScriptFragment(com.liferay.faces.util.render.JavaScriptFragment) AutoComplete(com.liferay.faces.alloy.component.autocomplete.AutoComplete)

Aggregations

BufferedScriptResponseWriter (com.liferay.faces.util.render.BufferedScriptResponseWriter)4 FacesRequestContext (com.liferay.faces.util.context.FacesRequestContext)2 JavaScriptFragment (com.liferay.faces.util.render.JavaScriptFragment)2 ExternalContext (javax.faces.context.ExternalContext)2 ResponseWriter (javax.faces.context.ResponseWriter)2 AutoComplete (com.liferay.faces.alloy.component.autocomplete.AutoComplete)1 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 ClientComponent (com.liferay.faces.util.component.ClientComponent)1 IOException (java.io.IOException)1 Locale (java.util.Locale)1 MethodExpression (javax.el.MethodExpression)1