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.io.IOException;
25  
26  import javax.faces.component.UIComponent;
27  import javax.faces.context.FacesContext;
28  import javax.faces.context.ResponseWriter;
29  
30  import org.primefaces.extensions.util.Attrs;
31  import org.primefaces.extensions.util.ExtLangUtils;
32  import org.primefaces.renderkit.CoreRenderer;
33  import org.primefaces.util.HTML;
34  import org.primefaces.util.WidgetBuilder;
35  
36  /**
37   * Renderer for the {@link SlideOut} component.
38   *
39   * @author Melloware mellowaredev@gmail.com
40   * @since 6.1
41   */
42  public class SlideOutRenderer extends CoreRenderer {
43  
44      /**
45       * {@inheritDoc}
46       */
47      @Override
48      public void decode(final FacesContext context, final UIComponent component) {
49          decodeBehaviors(context, component);
50      }
51  
52      /**
53       * {@inheritDoc}
54       */
55      @Override
56      public void encodeEnd(final FacesContext context, final UIComponent component) throws IOException {
57          final SlideOut slideOut = (SlideOut) component;
58          encodeMarkup(context, slideOut);
59          encodeScript(context, slideOut);
60      }
61  
62      /**
63       * {@inheritDoc}
64       */
65      @Override
66      public void encodeChildren(final FacesContext context, final UIComponent component) {
67          // Do nothing
68      }
69  
70      /**
71       * {@inheritDoc}
72       */
73      @Override
74      public boolean getRendersChildren() {
75          return true;
76      }
77  
78      /**
79       * Create the HTML markup for the DOM.
80       */
81      private void encodeMarkup(final FacesContext context, final SlideOut slideOut) throws IOException {
82          final ResponseWriter writer = context.getResponseWriter();
83          final String clientId = slideOut.getClientId(context);
84          final String widgetVar = slideOut.resolveWidgetVar();
85  
86          writer.startElement("div", slideOut);
87          writer.writeAttribute("id", clientId, "id");
88          writer.writeAttribute(HTML.WIDGET_VAR, widgetVar, null);
89  
90          if (slideOut.getPanelStyleClass() != null) {
91              writer.writeAttribute(Attrs.CLASS, slideOut.getPanelStyleClass(), "styleClass");
92          }
93          if (slideOut.getPanelStyle() != null) {
94              writer.writeAttribute(Attrs.STYLE, slideOut.getPanelStyle(), Attrs.STYLE);
95          }
96  
97          // handle
98          encodeHandle(context, slideOut);
99  
100         // content of the panel
101         renderChildren(context, slideOut);
102 
103         writer.endElement("div");
104     }
105 
106     /**
107      * HTML markup for the tab handle.
108      */
109     private void encodeHandle(final FacesContext context, final SlideOut slideOut) throws IOException {
110         final ResponseWriter writer = context.getResponseWriter();
111         final String styleClass = getStyleClassBuilder(context)
112                     .add(SlideOut.HANDLE_CLASS)
113                     .add(slideOut.getHandleStyleClass())
114                     .build();
115 
116         writer.startElement("a", null);
117         writer.writeAttribute("id", getHandleId(context, slideOut), null);
118         if (slideOut.getHandleStyle() != null) {
119             writer.writeAttribute(Attrs.STYLE, slideOut.getHandleStyle(), Attrs.STYLE);
120         }
121         writer.writeAttribute(Attrs.CLASS, styleClass, "styleClass");
122 
123         // icon
124         encodeIcon(context, slideOut);
125 
126         // handle text
127         if (slideOut.getTitle() != null) {
128             writer.writeText(slideOut.getTitle(), "title");
129         }
130 
131         writer.endElement("a");
132     }
133 
134     /**
135      * HTML markup for the tab handle icon if defined.
136      */
137     private void encodeIcon(final FacesContext context, final SlideOut slideOut) throws IOException {
138         if (slideOut.getIcon() == null) {
139             return;
140         }
141 
142         // fontawesome icons are OK but themeroller we need to add styles
143         String icon = slideOut.getIcon().trim();
144         if (icon.startsWith("ui")) {
145             icon = "ui-icon " + icon + " ui-slideouttab-icon";
146         }
147 
148         // <i class="ui-icon fa fa-television"></i>
149         final ResponseWriter writer = context.getResponseWriter();
150         writer.startElement("span", null);
151         writer.writeAttribute(Attrs.CLASS, icon, null);
152         writer.endElement("span");
153         writer.write(" ");
154     }
155 
156     /**
157      * Create the Javascript.
158      */
159     private void encodeScript(final FacesContext context, final SlideOut slideOut) throws IOException {
160         final WidgetBuilder wb = getWidgetBuilder(context);
161         final String handleId = getHandleId(context, slideOut);
162         wb.init("ExtSlideOut", slideOut);
163         wb.attr("tabLocation", slideOut.getLocation());
164         wb.attr("tabHandle", handleId);
165         wb.attr("speed", slideOut.getAnimateSpeed());
166         wb.attr("action", ExtLangUtils.lowerCase(slideOut.getShowOn()));
167         wb.attr("clickScreenToClose", slideOut.isClickScreenToClose());
168         wb.attr("onLoadSlideOut", slideOut.isAutoOpen());
169         wb.attr("positioning", slideOut.isSticky() ? "absolute" : "fixed");
170         wb.attr("offset", slideOut.getOffset());
171         wb.attr("offsetReverse", slideOut.isOffsetReverse());
172         wb.attr("handleOffsetReverse", slideOut.isHandleOffsetReverse());
173         wb.attr("bounceTimes", slideOut.getBounceTimes());
174         wb.attr("bounceDistance", slideOut.getBounceDistance());
175         wb.nativeAttr("clickScreenToCloseFilters", "['.ui-slideouttab-panel', 'button', 'a']");
176 
177         if (slideOut.getHandleOffset() != null) {
178             wb.attr("handleOffset", slideOut.getHandleOffset());
179         }
180 
181         if (slideOut.getOnopen() != null) {
182             wb.callback("onOpen", "function()", slideOut.getOnopen());
183         }
184         if (slideOut.getOnclose() != null) {
185             wb.callback("onClose", "function()", slideOut.getOnclose());
186         }
187         if (slideOut.getOnslide() != null) {
188             wb.callback("onSlide", "function()", slideOut.getOnslide());
189         }
190         if (slideOut.getOnbeforeopen() != null) {
191             wb.callback("onBeforeOpen", "function()", slideOut.getOnbeforeopen());
192         }
193         if (slideOut.getOnbeforeclose() != null) {
194             wb.callback("onBeforeClose", "function()", slideOut.getOnbeforeclose());
195         }
196         if (slideOut.getOnbeforeslide() != null) {
197             wb.callback("onBeforeSlide", "function()", slideOut.getOnbeforeslide());
198         }
199 
200         encodeClientBehaviors(context, slideOut);
201 
202         wb.finish();
203     }
204 
205     private String getHandleId(final FacesContext context, final SlideOut slideOut) {
206         final String clientId = slideOut.getClientId(context);
207         return clientId + "_handle";
208     }
209 
210 }