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.slideout;
23  
24  import java.util.Arrays;
25  import java.util.Collection;
26  import java.util.Collections;
27  import java.util.Map;
28  
29  import javax.faces.application.ResourceDependency;
30  import javax.faces.component.UIComponentBase;
31  import javax.faces.component.behavior.ClientBehaviorHolder;
32  import javax.faces.context.FacesContext;
33  import javax.faces.event.AjaxBehaviorEvent;
34  import javax.faces.event.FacesEvent;
35  
36  import org.primefaces.component.api.Widget;
37  import org.primefaces.extensions.event.CloseEvent;
38  import org.primefaces.extensions.event.OpenEvent;
39  import org.primefaces.util.Constants;
40  
41  /**
42   * <code>SlideOut</code> component.
43   *
44   * @author Melloware info@melloware.com
45   * @since 6.1
46   */
47  @ResourceDependency(library = "primefaces", name = "components.css")
48  @ResourceDependency(library = "primefaces", name = "jquery/jquery.js")
49  @ResourceDependency(library = "primefaces", name = "jquery/jquery-plugins.js")
50  @ResourceDependency(library = "primefaces", name = "core.js")
51  @ResourceDependency(library = "primefaces-extensions", name = "slideout/slideout.css")
52  @ResourceDependency(library = "primefaces-extensions", name = "slideout/slideout.js")
53  public class SlideOut extends UIComponentBase implements ClientBehaviorHolder, Widget {
54  
55      public static final String HANDLE_CLASS = "ui-slideout-handle ui-slideouttab-handle-rounded";
56  
57      public static final String COMPONENT_TYPE = "org.primefaces.extensions.component.SlideOut";
58      public static final String COMPONENT_FAMILY = "org.primefaces.extensions.component";
59      private static final String DEFAULT_RENDERER = "org.primefaces.extensions.component.SlideOutRenderer";
60  
61      private static final Collection<String> EVENT_NAMES = Collections
62                  .unmodifiableCollection(Arrays.asList(OpenEvent.NAME, CloseEvent.NAME));
63  
64      @SuppressWarnings("java:S115")
65      protected enum PropertyKeys {
66  
67          // @formatter:off
68        widgetVar,
69        panelStyle,
70        panelStyleClass,
71        handleStyle,
72        handleStyleClass,
73        title,
74        icon,
75        showOn, // click or hover
76        location, // left, right, top or bottom
77        offset, // panel distance from edge
78        offsetReverse,  // if true, panel is aligned with right or bottom of window
79        handleOffset, // panel distance from edge
80        handleOffsetReverse,  // if true, panel is aligned with right or bottom of window
81        sticky, // fixed or absolute
82        clickScreenToClose,
83        autoOpen, // slide out after DOM load
84        animateSpeed,
85        bounceDistance, // how far bounce event will move everything
86        bounceTimes, // how many bounces when 'bounce' is called
87        onopen, // handler called after opening
88        onclose, // handler called after closing
89        onslide, // handler called after opening or closing
90        onbeforeopen, // handler called before opening, return false to cancel
91        onbeforeclose, // handler called before closing, return false to cancel
92        onbeforeslide // handler called before opening or closing, return false to cancel
93        // @formatter:on
94      }
95  
96      /**
97       * Default constructor
98       */
99      public SlideOut() {
100         setRendererType(DEFAULT_RENDERER);
101     }
102 
103     /**
104      * {@inheritDoc}
105      */
106     @Override
107     public String getFamily() {
108         return COMPONENT_FAMILY;
109     }
110 
111     /**
112      * {@inheritDoc}
113      */
114     @Override
115     public Collection<String> getEventNames() {
116         return EVENT_NAMES;
117     }
118 
119     /**
120      * {@inheritDoc}
121      */
122     @Override
123     public String getDefaultEventName() {
124         return OpenEvent.NAME;
125     }
126 
127     /**
128      * {@inheritDoc}
129      */
130     @Override
131     public void processDecodes(final FacesContext fc) {
132         if (isSelfRequest(fc)) {
133             decode(fc);
134         }
135         else {
136             super.processDecodes(fc);
137         }
138     }
139 
140     /**
141      * {@inheritDoc}
142      */
143     @Override
144     public void processValidators(final FacesContext fc) {
145         if (!isSelfRequest(fc)) {
146             super.processValidators(fc);
147         }
148     }
149 
150     /**
151      * {@inheritDoc}
152      */
153     @Override
154     public void processUpdates(final FacesContext fc) {
155         if (!isSelfRequest(fc)) {
156             super.processUpdates(fc);
157         }
158     }
159 
160     /**
161      * {@inheritDoc}
162      */
163     @Override
164     public void queueEvent(final FacesEvent event) {
165         final FacesContext fc = FacesContext.getCurrentInstance();
166 
167         if (isSelfRequest(fc) && event instanceof AjaxBehaviorEvent) {
168             final Map<String, String> params = fc.getExternalContext().getRequestParameterMap();
169             final String eventName = params.get(Constants.RequestParams.PARTIAL_BEHAVIOR_EVENT_PARAM);
170             final AjaxBehaviorEvent behaviorEvent = (AjaxBehaviorEvent) event;
171 
172             if (OpenEvent.NAME.equals(eventName)) {
173                 final OpenEvent openEvent = new OpenEvent(this, behaviorEvent.getBehavior());
174                 openEvent.setPhaseId(event.getPhaseId());
175                 super.queueEvent(openEvent);
176 
177                 return;
178             }
179             else if (CloseEvent.NAME.equals(eventName)) {
180                 final CloseEvent closeEvent = new CloseEvent(this, behaviorEvent.getBehavior());
181                 closeEvent.setPhaseId(event.getPhaseId());
182                 super.queueEvent(closeEvent);
183 
184                 return;
185             }
186         }
187 
188         super.queueEvent(event);
189     }
190 
191     public String getWidgetVar() {
192         return (String) getStateHelper().eval(PropertyKeys.widgetVar, null);
193     }
194 
195     public void setWidgetVar(final String _widgetVar) {
196         getStateHelper().put(PropertyKeys.widgetVar, _widgetVar);
197     }
198 
199     public String getPanelStyle() {
200         return (String) getStateHelper().eval(PropertyKeys.panelStyle, null);
201     }
202 
203     public void setPanelStyle(final String _panelStyle) {
204         getStateHelper().put(PropertyKeys.panelStyle, _panelStyle);
205     }
206 
207     public String getPanelStyleClass() {
208         return (String) getStateHelper().eval(PropertyKeys.panelStyleClass, null);
209     }
210 
211     public void setPanelStyleClass(final String _panelStyleClass) {
212         getStateHelper().put(PropertyKeys.panelStyleClass, _panelStyleClass);
213     }
214 
215     public String getHandleStyle() {
216         return (String) getStateHelper().eval(PropertyKeys.handleStyle, null);
217     }
218 
219     public void setHandleStyle(final String _handleStyle) {
220         getStateHelper().put(PropertyKeys.handleStyle, _handleStyle);
221     }
222 
223     public String getHandleStyleClass() {
224         return (String) getStateHelper().eval(PropertyKeys.handleStyleClass, null);
225     }
226 
227     public void setHandleStyleClass(final String _handleStyleClass) {
228         getStateHelper().put(PropertyKeys.handleStyleClass, _handleStyleClass);
229     }
230 
231     public String getTitle() {
232         return (String) getStateHelper().eval(PropertyKeys.title, null);
233     }
234 
235     public void setTitle(final String _title) {
236         getStateHelper().put(PropertyKeys.title, _title);
237     }
238 
239     public String getIcon() {
240         return (String) getStateHelper().eval(PropertyKeys.icon, null);
241     }
242 
243     public void setIcon(final String _icon) {
244         getStateHelper().put(PropertyKeys.icon, _icon);
245     }
246 
247     public String getShowOn() {
248         return (String) getStateHelper().eval(PropertyKeys.showOn, "click");
249     }
250 
251     public void setShowOn(final String _showOn) {
252         getStateHelper().put(PropertyKeys.showOn, _showOn);
253     }
254 
255     public String getLocation() {
256         return (String) getStateHelper().eval(PropertyKeys.location, "right");
257     }
258 
259     public void setLocation(final String _location) {
260         getStateHelper().put(PropertyKeys.location, _location);
261     }
262 
263     public String getOffset() {
264         return (String) getStateHelper().eval(PropertyKeys.offset, "200px");
265     }
266 
267     public void setOffset(final String _offset) {
268         getStateHelper().put(PropertyKeys.offset, _offset);
269     }
270 
271     public void setOffsetReverse(final boolean _offsetReverse) {
272         getStateHelper().put(PropertyKeys.offsetReverse, _offsetReverse);
273     }
274 
275     public boolean isOffsetReverse() {
276         return (Boolean) getStateHelper().eval(PropertyKeys.offsetReverse, false);
277     }
278 
279     public String getHandleOffset() {
280         return (String) getStateHelper().eval(PropertyKeys.handleOffset, null);
281     }
282 
283     public void setHandleOffset(final String _handleOffset) {
284         getStateHelper().put(PropertyKeys.handleOffset, _handleOffset);
285     }
286 
287     public void setHandleOffsetReverse(final boolean _handleOffsetReverse) {
288         getStateHelper().put(PropertyKeys.handleOffsetReverse, _handleOffsetReverse);
289     }
290 
291     public boolean isHandleOffsetReverse() {
292         return (Boolean) getStateHelper().eval(PropertyKeys.handleOffsetReverse, false);
293     }
294 
295     public void setSticky(final boolean _sticky) {
296         getStateHelper().put(PropertyKeys.sticky, _sticky);
297     }
298 
299     public boolean isSticky() {
300         return (Boolean) getStateHelper().eval(PropertyKeys.sticky, false);
301     }
302 
303     public void setClickScreenToClose(final boolean _clickScreenToClose) {
304         getStateHelper().put(PropertyKeys.clickScreenToClose, _clickScreenToClose);
305     }
306 
307     public boolean isClickScreenToClose() {
308         return (Boolean) getStateHelper().eval(PropertyKeys.clickScreenToClose, true);
309     }
310 
311     public void setAutoOpen(final boolean _autoOpen) {
312         getStateHelper().put(PropertyKeys.autoOpen, _autoOpen);
313     }
314 
315     public boolean isAutoOpen() {
316         return (Boolean) getStateHelper().eval(PropertyKeys.autoOpen, false);
317     }
318 
319     public int getAnimateSpeed() {
320         return (Integer) getStateHelper().eval(PropertyKeys.animateSpeed, 300);
321     }
322 
323     public void setAnimateSpeed(final int _animateSpeed) {
324         getStateHelper().put(PropertyKeys.animateSpeed, _animateSpeed);
325     }
326 
327     public String getBounceDistance() {
328         return (String) getStateHelper().eval(PropertyKeys.bounceDistance, "50px");
329     }
330 
331     public void setBounceDistance(final String _offset) {
332         getStateHelper().put(PropertyKeys.bounceDistance, _offset);
333     }
334 
335     public int getBounceTimes() {
336         return (Integer) getStateHelper().eval(PropertyKeys.bounceTimes, 4);
337     }
338 
339     public void setBounceTimes(final int _bounceTimes) {
340         getStateHelper().put(PropertyKeys.bounceTimes, _bounceTimes);
341     }
342 
343     public String getOnopen() {
344         return (String) getStateHelper().eval(PropertyKeys.onopen, null);
345     }
346 
347     public void setOnopen(final String _onOpen) {
348         getStateHelper().put(PropertyKeys.onopen, _onOpen);
349     }
350 
351     public String getOnclose() {
352         return (String) getStateHelper().eval(PropertyKeys.onclose, null);
353     }
354 
355     public void setOnclose(final String _onClose) {
356         getStateHelper().put(PropertyKeys.onclose, _onClose);
357     }
358 
359     public String getOnslide() {
360         return (String) getStateHelper().eval(PropertyKeys.onslide, null);
361     }
362 
363     public void setOnslide(final String _onslide) {
364         getStateHelper().put(PropertyKeys.onslide, _onslide);
365     }
366 
367     public String getOnbeforeopen() {
368         return (String) getStateHelper().eval(PropertyKeys.onbeforeopen, null);
369     }
370 
371     public void setOnbeforeopen(final String _onBeforeOpen) {
372         getStateHelper().put(PropertyKeys.onbeforeopen, _onBeforeOpen);
373     }
374 
375     public String getOnbeforeclose() {
376         return (String) getStateHelper().eval(PropertyKeys.onbeforeclose, null);
377     }
378 
379     public void setOnbeforeclose(final String _onbeforeClose) {
380         getStateHelper().put(PropertyKeys.onbeforeclose, _onbeforeClose);
381     }
382 
383     public String getOnbeforeslide() {
384         return (String) getStateHelper().eval(PropertyKeys.onbeforeslide, null);
385     }
386 
387     public void setOnbeforeslide(final String _onbeforeslide) {
388         getStateHelper().put(PropertyKeys.onbeforeslide, _onbeforeslide);
389     }
390 
391     private boolean isSelfRequest(final FacesContext context) {
392         return getClientId(context)
393                     .equals(context.getExternalContext().getRequestParameterMap().get(
394                                 Constants.RequestParams.PARTIAL_SOURCE_PARAM));
395     }
396 
397 }