Search in sources :

Example 1 with AutoComplete

use of com.liferay.faces.alloy.component.autocomplete.AutoComplete in project liferay-faces-alloy by liferay.

the class AutoCompleteRenderer method getModules.

@Override
public String[] getModules(FacesContext facesContext, UIComponent uiComponent) {
    String[] modules = MODULES;
    AutoComplete autoComplete = (AutoComplete) uiComponent;
    String clientFilterType = autoComplete.getClientFilterType();
    String clientCustomFilter = autoComplete.getClientCustomFilter();
    // "autocomplete-filters" module.
    if (!isServerFilteringEnabled(autoComplete) && (clientFilterType != null) && (clientFilterType.length() > 0) && (clientCustomFilter == null)) {
        modules = StringHelper.append(modules, AUTOCOMPLETE_FILTERS);
    }
    String highlighterType = autoComplete.getHighlighterType();
    if (highlighterType != null) {
        modules = StringHelper.append(modules, AUTOCOMPLETE_HIGHLIGHTERS);
    }
    Map<String, List<ClientBehavior>> clientBehaviorMap = autoComplete.getClientBehaviors();
    List<ClientBehavior> valueChangeClientBehaviors = clientBehaviorMap.get(VALUE_CHANGE);
    if ((valueChangeClientBehaviors != null) && !valueChangeClientBehaviors.isEmpty()) {
        modules = StringHelper.append(modules, NODE_EVENT_SIMULATE);
    }
    return modules;
}
Also used : ArrayList(java.util.ArrayList) List(java.util.List) AutoComplete(com.liferay.faces.alloy.component.autocomplete.AutoComplete) ClientBehavior(javax.faces.component.behavior.ClientBehavior)

Example 2 with AutoComplete

use of com.liferay.faces.alloy.component.autocomplete.AutoComplete in project liferay-faces-alloy by liferay.

the class AutoCompleteRendererBase method encodeAlloyAttributes.

@Override
public void encodeAlloyAttributes(FacesContext facesContext, ResponseWriter responseWriter, UIComponent uiComponent) throws IOException {
    AutoComplete autoComplete = (AutoComplete) uiComponent;
    boolean first = true;
    Boolean activateFirstItem = autoComplete.isActivateFirstItem();
    if (activateFirstItem != null) {
        encodeActivateFirstItem(responseWriter, autoComplete, activateFirstItem, first);
        first = false;
    }
    Boolean autoScroll = autoComplete.getAutoScroll();
    if (autoScroll != null) {
        encodeScrollIntoView(responseWriter, autoComplete, autoScroll, first);
        first = false;
    }
    Boolean circular = autoComplete.getCircular();
    if (circular != null) {
        encodeCircular(responseWriter, autoComplete, circular, first);
        first = false;
    }
    String clientCustomFilter = autoComplete.getClientCustomFilter();
    if (clientCustomFilter != null) {
        encodeResultFilters(responseWriter, autoComplete, clientCustomFilter, first);
        first = false;
    }
    String clientFilterType = autoComplete.getClientFilterType();
    if (clientFilterType != null) {
        encodeClientFilterType(responseWriter, autoComplete, clientFilterType, first);
        first = false;
    }
    Integer delay = autoComplete.getDelay();
    if (delay != null) {
        encodeQueryDelay(responseWriter, autoComplete, delay, first);
        first = false;
    }
    String delimiter = autoComplete.getDelimiter();
    if (delimiter != null) {
        encodeQueryDelimiter(responseWriter, autoComplete, delimiter, first);
        first = false;
    }
    String height = autoComplete.getHeight();
    if (height != null) {
        encodeHeight(responseWriter, autoComplete, height, first);
        first = false;
    }
    String highlighterType = autoComplete.getHighlighterType();
    if (highlighterType != null) {
        encodeResultHighlighter(responseWriter, autoComplete, highlighterType, first);
        first = false;
    }
    Boolean listItemRequired = autoComplete.isListItemRequired();
    if (listItemRequired != null) {
        encodeListItemRequired(responseWriter, autoComplete, listItemRequired, first);
        first = false;
    }
    Integer maxItems = autoComplete.getMaxItems();
    if (maxItems != null) {
        encodeMaxResults(responseWriter, autoComplete, maxItems, first);
        first = false;
    }
    Integer minChars = autoComplete.getMinChars();
    if (minChars != null) {
        encodeMinQueryLength(responseWriter, autoComplete, minChars, first);
        first = false;
    }
    Boolean tabSelect = autoComplete.isTabSelect();
    if (tabSelect != null) {
        encodeTabSelect(responseWriter, autoComplete, tabSelect, first);
        first = false;
    }
    String width = autoComplete.getWidth();
    if (width != null) {
        encodeWidth(responseWriter, autoComplete, width, first);
        first = false;
    }
    encodeHiddenAttributes(facesContext, responseWriter, autoComplete, first);
}
Also used : AutoComplete(com.liferay.faces.alloy.component.autocomplete.AutoComplete)

Example 3 with AutoComplete

use of com.liferay.faces.alloy.component.autocomplete.AutoComplete 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

AutoComplete (com.liferay.faces.alloy.component.autocomplete.AutoComplete)3 ClientComponent (com.liferay.faces.util.component.ClientComponent)1 BufferedScriptResponseWriter (com.liferay.faces.util.render.BufferedScriptResponseWriter)1 JavaScriptFragment (com.liferay.faces.util.render.JavaScriptFragment)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 List (java.util.List)1 Locale (java.util.Locale)1 MethodExpression (javax.el.MethodExpression)1 ClientBehavior (javax.faces.component.behavior.ClientBehavior)1 ExternalContext (javax.faces.context.ExternalContext)1