View Javadoc
1   /*
2    * Copyright (c) 2011-2024 PrimeFaces Extensions
3    *
4    *  Permission is hereby granted, free of charge, to any person obtaining a copy
5    *  of this software and associated documentation files (the "Software"), to deal
6    *  in the Software without restriction, including without limitation the rights
7    *  to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8    *  copies of the Software, and to permit persons to whom the Software is
9    *  furnished to do so, subject to the following conditions:
10   *
11   *  The above copyright notice and this permission notice shall be included in
12   *  all copies or substantial portions of the Software.
13   *
14   *  THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15   *  IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16   *  FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17   *  AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18   *  LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19   *  OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20   *  THE SOFTWARE.
21   */
22  package org.primefaces.extensions.component.fuzzysearch;
23  
24  import java.util.Collection;
25  import java.util.Map;
26  
27  import javax.faces.application.ResourceDependency;
28  import javax.faces.context.FacesContext;
29  import javax.faces.event.AjaxBehaviorEvent;
30  import javax.faces.event.BehaviorEvent;
31  import javax.faces.event.FacesEvent;
32  
33  import org.primefaces.event.SelectEvent;
34  import org.primefaces.util.ComponentUtils;
35  import org.primefaces.util.Constants;
36  import org.primefaces.util.MapBuilder;
37  
38  /**
39   * <code>FuzzySearch</code> component.
40   *
41   * @author https://github.com/aripddev
42   * @since 8.0.1
43   */
44  @ResourceDependency(library = "primefaces", name = "components.css")
45  @ResourceDependency(library = "primefaces", name = "jquery/jquery.js")
46  @ResourceDependency(library = "primefaces", name = "jquery/jquery-plugins.js")
47  @ResourceDependency(library = "primefaces", name = "core.js")
48  @ResourceDependency(library = "primefaces", name = "components.js")
49  @ResourceDependency(library = "primefaces-extensions", name = "fuzzysearch/fuzzysearch.js")
50  public class FuzzySearch extends FuzzySearchBase {
51  
52      public static final String COMPONENT_TYPE = "org.primefaces.extensions.component.FuzzySearch";
53  
54      public static final String STYLE_CLASS = "ui-fuzzysearch ui-widget ui-corner-all";
55      public static final String ITEM_CLASS = "ui-fuzzysearch-item";
56  
57      private static final String DEFAULT_EVENT = "change";
58  
59      private static final Map<String, Class<? extends BehaviorEvent>> BEHAVIOR_EVENT_MAPPING = MapBuilder.<String, Class<? extends BehaviorEvent>> builder()
60                  .put(DEFAULT_EVENT, null)
61                  .build();
62  
63      private static final Collection<String> EVENT_NAMES = BEHAVIOR_EVENT_MAPPING.keySet();
64  
65      @Override
66      public Map<String, Class<? extends BehaviorEvent>> getBehaviorEventMapping() {
67          return BEHAVIOR_EVENT_MAPPING;
68      }
69  
70      @Override
71      public Collection<String> getEventNames() {
72          return EVENT_NAMES;
73      }
74  
75      @Override
76      public String getDefaultEventName() {
77          return DEFAULT_EVENT;
78      }
79  
80      @Override
81      public void queueEvent(final FacesEvent event) {
82          if (event instanceof AjaxBehaviorEvent) {
83              final FacesContext context = getFacesContext();
84              final AjaxBehaviorEvent behaviorEvent = (AjaxBehaviorEvent) event;
85              final Map<String, String> params = context.getExternalContext().getRequestParameterMap();
86              final String eventName = params.get(Constants.RequestParams.PARTIAL_BEHAVIOR_EVENT_PARAM);
87  
88              if (DEFAULT_EVENT.equals(eventName)) {
89                  final Object item = ComponentUtils.getConvertedValue(context, this, params.get(getClientId(context) + "_change"));
90                  final SelectEvent selectEvent = new SelectEvent(this, behaviorEvent.getBehavior(), item);
91                  selectEvent.setPhaseId(event.getPhaseId());
92                  super.queueEvent(selectEvent);
93              }
94              else {
95                  super.queueEvent(event);
96              }
97          }
98          else {
99              super.queueEvent(event);
100         }
101     }
102 
103 }