Search in sources :

Example 16 with JavaScriptFragment

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

JavaScriptFragment (com.liferay.faces.util.render.JavaScriptFragment)16 ResponseWriter (javax.faces.context.ResponseWriter)9 List (java.util.List)6 ClientBehavior (javax.faces.component.behavior.ClientBehavior)6 EscapedClientId (com.liferay.faces.alloy.render.internal.EscapedClientId)4 BufferedScriptResponseWriter (com.liferay.faces.util.render.BufferedScriptResponseWriter)4 ClientBehaviorContext (javax.faces.component.behavior.ClientBehaviorContext)4 ClientComponent (com.liferay.faces.util.component.ClientComponent)3 Locale (java.util.Locale)3 DataEncoderBase (com.liferay.faces.alloy.component.data.internal.DataEncoderBase)2 InputDateTimeResponseWriter (com.liferay.faces.alloy.component.inputdatetime.internal.InputDateTimeResponseWriter)2 ProgressBar (com.liferay.faces.alloy.component.progressbar.ProgressBar)2 BrowserSniffer (com.liferay.faces.util.client.BrowserSniffer)2 IOException (java.io.IOException)2 SimpleDateFormat (java.text.SimpleDateFormat)2 Date (java.util.Date)2 TimeZone (java.util.TimeZone)2 NamingContainer (javax.faces.component.NamingContainer)2 UIViewRoot (javax.faces.component.UIViewRoot)2 ExternalContext (javax.faces.context.ExternalContext)2