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