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.waypoint;
23  
24  import java.util.*;
25  
26  import javax.faces.application.ResourceDependency;
27  import javax.faces.component.UIComponentBase;
28  import javax.faces.component.behavior.ClientBehaviorHolder;
29  import javax.faces.context.FacesContext;
30  import javax.faces.event.AjaxBehaviorEvent;
31  import javax.faces.event.FacesEvent;
32  
33  import org.primefaces.component.api.Widget;
34  import org.primefaces.extensions.event.WaypointEvent;
35  import org.primefaces.util.Constants;
36  
37  /**
38   * <code>Waypoint</code> component.
39   *
40   * @author Oleg Varaksin / last modified by Melloware
41   * @since 0.6
42   */
43  @ResourceDependency(library = "primefaces", name = "jquery/jquery.js")
44  @ResourceDependency(library = "primefaces", name = "jquery/jquery-plugins.js")
45  @ResourceDependency(library = "primefaces", name = "core.js")
46  @ResourceDependency(library = "primefaces-extensions", name = "primefaces-extensions.js")
47  @ResourceDependency(library = "primefaces-extensions", name = "waypoint/waypoint.js")
48  public class Waypoint extends UIComponentBase implements Widget, ClientBehaviorHolder {
49  
50      public static final String COMPONENT_TYPE = "org.primefaces.extensions.component.Waypoint";
51      public static final String COMPONENT_FAMILY = "org.primefaces.extensions.component";
52      private static final String DEFAULT_RENDERER = "org.primefaces.extensions.component.WaypointRenderer";
53  
54      private static final Collection<String> EVENT_NAMES = Collections
55                  .unmodifiableCollection(Arrays.asList(WaypointEvent.NAME));
56  
57      @SuppressWarnings("java:S115")
58      protected enum PropertyKeys {
59          // @formatter:off
60        widgetVar,
61        forValue("for"),
62        forContext,
63        enabled,
64        horizontal,
65        offset,
66        continuous,
67        triggerOnce;
68        // @formatter:on
69  
70          private final String toString;
71  
72          PropertyKeys(String toString) {
73              this.toString = toString;
74          }
75  
76          PropertyKeys() {
77              toString = null;
78          }
79  
80          @Override
81          public String toString() {
82              return ((toString != null) ? toString : super.toString());
83          }
84      }
85  
86      public Waypoint() {
87          setRendererType(DEFAULT_RENDERER);
88      }
89  
90      /**
91       * {@inheritDoc}
92       */
93      @Override
94      public String getFamily() {
95          return COMPONENT_FAMILY;
96      }
97  
98      /**
99       * {@inheritDoc}
100      */
101     @Override
102     public Collection<String> getEventNames() {
103         return EVENT_NAMES;
104     }
105 
106     /**
107      * {@inheritDoc}
108      */
109     @Override
110     public String getDefaultEventName() {
111         return WaypointEvent.NAME;
112     }
113 
114     /**
115      * {@inheritDoc}
116      */
117     @Override
118     public void processDecodes(final FacesContext fc) {
119         if (isSelfRequest(fc)) {
120             decode(fc);
121         }
122         else {
123             super.processDecodes(fc);
124         }
125     }
126 
127     /**
128      * {@inheritDoc}
129      */
130     @Override
131     public void processValidators(final FacesContext fc) {
132         if (!isSelfRequest(fc)) {
133             super.processValidators(fc);
134         }
135     }
136 
137     /**
138      * {@inheritDoc}
139      */
140     @Override
141     public void processUpdates(final FacesContext fc) {
142         if (!isSelfRequest(fc)) {
143             super.processUpdates(fc);
144         }
145     }
146 
147     /**
148      * {@inheritDoc}
149      */
150     @Override
151     public void queueEvent(final FacesEvent event) {
152         final FacesContext fc = FacesContext.getCurrentInstance();
153 
154         if (isSelfRequest(fc)) {
155             final Map<String, String> params = fc.getExternalContext().getRequestParameterMap();
156             final String eventName = params.get(Constants.RequestParams.PARTIAL_BEHAVIOR_EVENT_PARAM);
157             final AjaxBehaviorEvent behaviorEvent = (AjaxBehaviorEvent) event;
158 
159             if (WaypointEvent.NAME.equals(eventName)) {
160                 final String direction = params.get(getClientId(fc) + "_direction");
161                 final String waypointId = params.get(getClientId(fc) + "_waypointId");
162 
163                 final WaypointEvent waypointEvent = new WaypointEvent(this, behaviorEvent.getBehavior(),
164                             direction != null ? WaypointEvent.Direction.valueOf(direction.toUpperCase(
165                                         Locale.ENGLISH))
166                                         : null,
167                             waypointId);
168                 waypointEvent.setPhaseId(behaviorEvent.getPhaseId());
169                 super.queueEvent(waypointEvent);
170 
171                 return;
172             }
173         }
174 
175         super.queueEvent(event);
176     }
177 
178     public String getWidgetVar() {
179         return (String) getStateHelper().eval(PropertyKeys.widgetVar, null);
180     }
181 
182     public void setWidgetVar(final String widgetVar) {
183         getStateHelper().put(PropertyKeys.widgetVar, widgetVar);
184     }
185 
186     public String getFor() {
187         return (String) getStateHelper().eval(PropertyKeys.forValue, null);
188     }
189 
190     public void setFor(final String forValue) {
191         getStateHelper().put(PropertyKeys.forValue, forValue);
192     }
193 
194     public String getForContext() {
195         return (String) getStateHelper().eval(PropertyKeys.forContext, null);
196     }
197 
198     public void setForContext(final String forContext) {
199         getStateHelper().put(PropertyKeys.forContext, forContext);
200     }
201 
202     public boolean isEnabled() {
203         return (Boolean) getStateHelper().eval(PropertyKeys.enabled, true);
204     }
205 
206     public void setEnabled(final boolean enabled) {
207         getStateHelper().put(PropertyKeys.enabled, enabled);
208     }
209 
210     public boolean isHorizontal() {
211         return (Boolean) getStateHelper().eval(PropertyKeys.horizontal, false);
212     }
213 
214     public void setHorizontal(final boolean horizontal) {
215         getStateHelper().put(PropertyKeys.horizontal, horizontal);
216     }
217 
218     public String getOffset() {
219         return (String) getStateHelper().eval(PropertyKeys.offset, null);
220     }
221 
222     public void setOffset(final String offset) {
223         getStateHelper().put(PropertyKeys.offset, offset);
224     }
225 
226     public boolean isContinuous() {
227         return (Boolean) getStateHelper().eval(PropertyKeys.continuous, true);
228     }
229 
230     public void setContinuous(final boolean continuous) {
231         getStateHelper().put(PropertyKeys.continuous, continuous);
232     }
233 
234     public boolean isTriggerOnce() {
235         return (Boolean) getStateHelper().eval(PropertyKeys.triggerOnce, false);
236     }
237 
238     public void setTriggerOnce(final boolean triggerOnce) {
239         getStateHelper().put(PropertyKeys.triggerOnce, triggerOnce);
240     }
241 
242     private boolean isSelfRequest(final FacesContext fc) {
243         return getClientId(fc)
244                     .equals(fc.getExternalContext().getRequestParameterMap()
245                                 .get(Constants.RequestParams.PARTIAL_SOURCE_PARAM));
246     }
247 
248 }