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.tooltip;
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.expression.SearchExpressionUtils;
31  import org.primefaces.renderkit.CoreRenderer;
32  import org.primefaces.util.ComponentUtils;
33  import org.primefaces.util.Constants;
34  import org.primefaces.util.EscapeUtils;
35  import org.primefaces.util.FastStringWriter;
36  import org.primefaces.util.LangUtils;
37  import org.primefaces.util.WidgetBuilder;
38  
39  /**
40   * Renderer for the {@link Tooltip} component.
41   *
42   * @author Oleg Varaksin / last modified by Melloware
43   * @since 0.2
44   */
45  public class TooltipRenderer extends CoreRenderer {
46  
47      @Override
48      public void encodeEnd(final FacesContext context, final UIComponent component) throws IOException {
49          final Tooltip tooltip = (Tooltip) component;
50          final String header = tooltip.getHeader();
51          final String styleClass = tooltip.getStyleClass();
52          final boolean global = tooltip.isGlobal();
53          final boolean shared = tooltip.isShared();
54          final boolean autoShow = tooltip.isAutoShow();
55          final boolean mouseTracking = tooltip.isMouseTracking();
56          String target = null;
57  
58          if (!global || tooltip.getFor() != null) {
59              target = SearchExpressionUtils.resolveClientIdsForClientSide(context, component, tooltip.getFor());
60          }
61  
62          final ResponseWriter writer = context.getResponseWriter();
63          String text = null;
64          if (tooltip.getChildCount() > 0) {
65              final FastStringWriter fsw = new FastStringWriter();
66              final ResponseWriter clonedWriter = writer.cloneWithWriter(fsw);
67              context.setResponseWriter(clonedWriter);
68              renderChildren(context, tooltip);
69              context.setResponseWriter(writer);
70              text = fsw.toString();
71          }
72          else {
73              final String valueToRender = ComponentUtils.getValueToRender(context, tooltip);
74              if (valueToRender != null) {
75                  text = valueToRender;
76              }
77          }
78  
79          final WidgetBuilder wb = getWidgetBuilder(context);
80          wb.init("ExtTooltip", tooltip);
81          wb.attr("global", global);
82          wb.attr("shared", shared);
83          wb.attr("autoShow", autoShow);
84          if (target == null) {
85              wb.nativeAttr("forTarget", null);
86          }
87          else {
88              wb.attr("forTarget", target);
89          }
90  
91          // content
92          wb.append(",content: {");
93  
94          final boolean hasText = !global && LangUtils.isNotBlank(text);
95          if (hasText) {
96              wb.append("text: \"" + EscapeUtils.forJavaScript(text) + "\"");
97          }
98  
99          if (LangUtils.isNotBlank(header)) {
100             String headerValue = Constants.EMPTY_STRING;
101             if (hasText) {
102                 headerValue = ",";
103             }
104             headerValue = headerValue + "title: \"" + EscapeUtils.forJavaScript(header) + "\"";
105             wb.append(headerValue);
106         }
107         wb.append("}");
108 
109         // style (if no class is set it will default to ThemeRoller widget=true)
110         final boolean isStyled = LangUtils.isNotBlank(styleClass);
111         wb.append(",style: {");
112         wb.append("widget:" + !isStyled);
113         if (isStyled) {
114             wb.append(",classes:'" + styleClass + "'");
115         }
116         wb.append("}");
117 
118         // events
119         if (mouseTracking) {
120             wb.append(",hide:{fixed:true}");
121         }
122         else if (shared && !global) {
123             wb.append(",show:{target:PrimeFaces.expressions.SearchExpressionFacade.resolveComponentsAsSelector('"
124                         + target + "')" + ",delay:"
125                         + tooltip.getShowDelay() + ",effect:function(){$(this)." + tooltip.getShowEffect() + "("
126                         + tooltip.getShowEffectLength() + ");}}");
127             wb.append(",hide:{target:PrimeFaces.expressions.SearchExpressionFacade.resolveComponentsAsSelector('"
128                         + target + "')" + ",delay:"
129                         + tooltip.getHideDelay() + ",fixed:" + tooltip.isFixed() + ",effect:function(){$(this)."
130                         + tooltip.getHideEffect() + "(" + tooltip.getHideEffectLength() + ");}}");
131         }
132         else if (autoShow) {
133             wb.append(",show:{when:false,ready:true}");
134             wb.append(",hide:false");
135         }
136         else {
137             wb.append(",show:{event:'" + tooltip.getShowEvent() + "',delay:" + tooltip.getShowDelay()
138                         + ",effect:function(){$(this)." + tooltip.getShowEffect() + "(" + tooltip.getShowEffectLength()
139                         + ");}}");
140             wb.append(",hide:{event:'" + tooltip.getHideEvent() + "',delay:" + tooltip.getHideDelay() + ",fixed:"
141                         + tooltip.isFixed() + ",effect:function(){$(this)." + tooltip.getHideEffect() + "("
142                         + tooltip.getHideEffectLength() + ");}}");
143         }
144 
145         // position
146         wb.append(",position: {");
147         wb.append("at:'" + tooltip.getAtPosition() + "'");
148         wb.append(",my:'" + tooltip.getMyPosition() + "'");
149         wb.append(",adjust:{x:" + tooltip.getAdjustX() + ",y:" + tooltip.getAdjustY() + "}");
150         wb.append(",viewport:$(window)");
151         if (mouseTracking) {
152             wb.append(",target:'mouse'");
153         }
154         else if (shared && !global) {
155             wb.append(",target:'event'");
156             wb.append(",effect:false");
157         }
158         wb.append("}");
159 
160         wb.finish();
161     }
162 
163     @Override
164     public void encodeChildren(final FacesContext context, final UIComponent component) {
165         // do nothing
166     }
167 
168     @Override
169     public boolean getRendersChildren() {
170         return true;
171     }
172 }