org.primefaces.component.autocomplete.AutoComplete Maven / Gradle / Ivy
/*
* The MIT License
*
* Copyright (c) 2009-2023 PrimeTek Informatics
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
package org.primefaces.component.autocomplete;
import java.util.*;
import javax.el.MethodExpression;
import javax.faces.FacesException;
import javax.faces.application.ResourceDependency;
import javax.faces.component.UIComponent;
import javax.faces.context.FacesContext;
import javax.faces.event.AjaxBehaviorEvent;
import javax.faces.event.FacesEvent;
import org.primefaces.component.api.AbstractPrimeHtmlInputText;
import org.primefaces.component.column.Column;
import org.primefaces.event.SelectEvent;
import org.primefaces.event.UnselectEvent;
import org.primefaces.model.FilterMeta;
import org.primefaces.model.LazyDataModel;
import org.primefaces.model.MatchMode;
import org.primefaces.model.SortMeta;
import org.primefaces.model.SortOrder;
import org.primefaces.util.ComponentUtils;
import org.primefaces.util.Constants;
import org.primefaces.util.LangUtils;
@ResourceDependency(library = "primefaces", name = "components.css")
@ResourceDependency(library = "primefaces", name = "jquery/jquery.js")
@ResourceDependency(library = "primefaces", name = "jquery/jquery-plugins.js")
@ResourceDependency(library = "primefaces", name = "core.js")
@ResourceDependency(library = "primefaces", name = "components.js")
public class AutoComplete extends AutoCompleteBase {
public static final String COMPONENT_TYPE = "org.primefaces.component.AutoComplete";
public static final String STYLE_CLASS = "ui-autocomplete";
public static final String MULTIPLE_STYLE_CLASS = "ui-autocomplete ui-autocomplete-multiple";
public static final String DROPDOWN_SYLE_CLASS = "ui-autocomplete-dd";
public static final String INPUT_CLASS = "ui-autocomplete-input ui-inputfield ui-widget ui-state-default ui-corner-all";
public static final String INPUT_WITH_DROPDOWN_CLASS = "ui-autocomplete-input ui-autocomplete-dd-input ui-inputfield ui-widget ui-state-default ui-corner-left";
public static final String DROPDOWN_CLASS = "ui-autocomplete-dropdown ui-button ui-widget ui-state-default ui-corner-right ui-button-icon-only";
public static final String PANEL_CLASS = "ui-autocomplete-panel ui-widget-content ui-corner-all ui-helper-hidden ui-shadow ui-input-overlay";
public static final String LIST_CLASS = "ui-autocomplete-items ui-autocomplete-list ui-widget-content ui-widget ui-corner-all ui-helper-reset";
public static final String TABLE_CLASS = "ui-autocomplete-items ui-autocomplete-table ui-widget-content ui-widget ui-corner-all ui-helper-reset";
public static final String ITEM_CLASS = "ui-autocomplete-item ui-autocomplete-list-item ui-corner-all";
public static final String ROW_CLASS = "ui-autocomplete-item ui-autocomplete-row ui-widget-content ui-corner-all";
public static final String TOKEN_DISPLAY_CLASS = "ui-autocomplete-token ui-state-active ui-corner-all";
public static final String TOKEN_LABEL_CLASS = "ui-autocomplete-token-label";
public static final String TOKEN_LABEL_DISABLED_CLASS = "ui-autocomplete-token-label-disabled";
public static final String TOKEN_ICON_CLASS = "ui-autocomplete-token-icon ui-icon ui-icon-close";
public static final String TOKEN_INPUT_CLASS = "ui-autocomplete-input-token";
public static final String MULTIPLE_CONTAINER_CLASS = "ui-autocomplete-multiple-container ui-widget ui-inputfield ui-state-default ui-corner-all";
public static final String MULTIPLE_CONTAINER_WITH_DROPDOWN_CLASS = "ui-autocomplete-multiple-container ui-autocomplete-dd-multiple-container ui-widget ui-inputfield ui-state-default ui-corner-left";
public static final String ITEMTIP_CONTENT_CLASS = "ui-autocomplete-itemtip-content";
public static final String MORE_TEXT_LIST_CLASS = "ui-autocomplete-item ui-autocomplete-moretext ui-corner-all";
public static final String MORE_TEXT_TABLE_CLASS = "ui-autocomplete-item ui-autocomplete-moretext ui-widget-content ui-corner-all";
protected static final List UNOBSTRUSIVE_EVENT_NAMES = LangUtils.unmodifiableList("itemSelect", "itemUnselect", "query",
"moreTextSelect", "emptyMessageSelect", "clear");
protected static final Collection EVENT_NAMES = LangUtils.concat(AbstractPrimeHtmlInputText.EVENT_NAMES, UNOBSTRUSIVE_EVENT_NAMES);
private Object suggestions;
private Integer suggestionsCount;
@Override
public Collection getEventNames() {
return EVENT_NAMES;
}
@Override
public Collection getUnobstrusiveEventNames() {
return UNOBSTRUSIVE_EVENT_NAMES;
}
public boolean isMoreTextRequest(FacesContext context) {
return context.getExternalContext().getRequestParameterMap().containsKey(getClientId(context) + "_moreText");
}
public boolean isDynamicLoadRequest(FacesContext context) {
return context.getExternalContext().getRequestParameterMap().containsKey(getClientId(context) + "_dynamicload");
}
public boolean isClientCacheRequest(FacesContext context) {
return context.getExternalContext().getRequestParameterMap().containsKey(getClientId(context) + "_clientCache");
}
@Override
public void queueEvent(FacesEvent event) {
FacesContext context = getFacesContext();
Map params = context.getExternalContext().getRequestParameterMap();
String eventName = params.get(Constants.RequestParams.PARTIAL_BEHAVIOR_EVENT_PARAM);
if (eventName != null && event instanceof AjaxBehaviorEvent) {
AjaxBehaviorEvent ajaxBehaviorEvent = (AjaxBehaviorEvent) event;
if ("itemSelect".equals(eventName)) {
Object selectedItemValue = ComponentUtils.getConvertedValue(context, this, params.get(getClientId(context) + "_itemSelect"));
SelectEvent selectEvent = new SelectEvent(this, ajaxBehaviorEvent.getBehavior(), selectedItemValue);
selectEvent.setPhaseId(ajaxBehaviorEvent.getPhaseId());
super.queueEvent(selectEvent);
}
else if ("itemUnselect".equals(eventName)) {
Object unselectedItemValue = ComponentUtils.getConvertedValue(context, this, params.get(getClientId(context) + "_itemUnselect"));
UnselectEvent unselectEvent = new UnselectEvent(this, ajaxBehaviorEvent.getBehavior(), unselectedItemValue);
unselectEvent.setPhaseId(ajaxBehaviorEvent.getPhaseId());
super.queueEvent(unselectEvent);
}
else if ("moreTextSelect".equals(eventName) || "emptyMessageSelect".equals(eventName) || "clear".equals(eventName)) {
ajaxBehaviorEvent.setPhaseId(event.getPhaseId());
super.queueEvent(ajaxBehaviorEvent);
}
else {
//e.g. blur, focus, change
super.queueEvent(event);
}
}
else {
//e.g. valueChange, autoCompleteEvent
super.queueEvent(event);
}
}
@Override
public void broadcast(javax.faces.event.FacesEvent event) throws javax.faces.event.AbortProcessingException {
super.broadcast(event);
if (!(event instanceof org.primefaces.event.AutoCompleteEvent)) {
return;
}
String query = ((org.primefaces.event.AutoCompleteEvent) event).getQuery();
LazyDataModel lazyModel = getLazyModel();
if (lazyModel != null) {
String field = getLazyField();
if (LangUtils.isEmpty(field)) {
throw new FacesException("lazyField is required with lazyModel");
}
Map searchFilter = new HashMap<>();
searchFilter.put(field,
FilterMeta.builder()
.field(field)
.filterValue(query)
.matchMode(MatchMode.CONTAINS)
.build());
Map sortBy = new HashMap<>();
sortBy.put(field,
SortMeta.builder()
.field(field)
.order(SortOrder.ASCENDING)
.build());
suggestions = lazyModel.load(0, getMaxResults(), sortBy, searchFilter);
suggestionsCount = lazyModel.count(searchFilter);
}
else {
FacesContext facesContext = getFacesContext();
MethodExpression me = getCompleteMethod();
if (me != null) {
suggestions = me.invoke(facesContext.getELContext(), new Object[]{query});
if (suggestions == null) {
suggestions = isServerQueryMode() ? Collections.emptyList() : Collections.emptyMap();
}
facesContext.renderResponse();
}
}
}
protected boolean hasMoreSuggestions() {
int count = suggestionsCount != null ? suggestionsCount : ((List) getSuggestions()).size();
return count > getMaxResults();
}
public List getColums() {
List columns = new ArrayList<>();
for (int i = 0; i < getChildCount(); i++) {
UIComponent child = getChildren().get(i);
if (child instanceof Column) {
columns.add((Column) child);
}
}
return columns;
}
public Object getSuggestions() {
return suggestions;
}
public boolean isServerQueryMode() {
return "server".equals(getQueryMode());
}
public boolean isClientQueryMode() {
return "client".equals(getQueryMode());
}
public boolean isHybridQueryMode() {
return "hybrid".equals(getQueryMode());
}
@Override
public String getInputClientId() {
return getClientId(getFacesContext()) + "_input";
}
@Override
public String getValidatableInputClientId() {
return getInputClientId();
}
@Override
public String getLabelledBy() {
return (String) getStateHelper().get("labelledby");
}
@Override
public void setLabelledBy(String labelledBy) {
getStateHelper().put("labelledby", labelledBy);
}
@Override
public Object saveState(FacesContext context) {
// reset component for MyFaces view pooling
suggestions = null;
suggestionsCount = null;
return super.saveState(context);
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy