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.blockui;
23  
24  import java.io.IOException;
25  
26  import javax.faces.FacesException;
27  import javax.faces.component.UIComponent;
28  import javax.faces.component.UINamingContainer;
29  import javax.faces.context.FacesContext;
30  import javax.faces.context.ResponseWriter;
31  
32  import org.primefaces.expression.SearchExpressionUtils;
33  import org.primefaces.extensions.util.Attrs;
34  import org.primefaces.renderkit.CoreRenderer;
35  import org.primefaces.util.Constants;
36  import org.primefaces.util.LangUtils;
37  import org.primefaces.util.WidgetBuilder;
38  
39  /**
40   * Renderer for the {@link BlockUI} component.
41   *
42   * @author Oleg Varaksin / last modified by Melloware
43   * @version $Revision$
44   * @since 0.2
45   */
46  public class BlockUIRenderer extends CoreRenderer {
47  
48      @Override
49      public void encodeEnd(final FacesContext fc, final UIComponent component) throws IOException {
50          encodeMarkup(fc, component);
51          encodeScript(fc, component);
52      }
53  
54      protected void encodeMarkup(final FacesContext fc, final UIComponent component) throws IOException {
55          final BlockUI blockUI = (BlockUI) component;
56          if (blockUI.getContent() == null && blockUI.getChildCount() > 0) {
57              final ResponseWriter writer = fc.getResponseWriter();
58              writer.startElement("div", null);
59              writer.writeAttribute("id", blockUI.getClientId(fc) + "_content", null);
60              writer.writeAttribute(Attrs.STYLE, "display: none;", null);
61              renderChildren(fc, component);
62              writer.endElement("div");
63          }
64      }
65  
66      protected void encodeScript(final FacesContext fc, final UIComponent component) throws IOException {
67          final BlockUI blockUI = (BlockUI) component;
68          final String clientId = blockUI.getClientId(fc);
69  
70          // get source
71          String source = blockUI.getSource();
72          if (source == null) {
73              source = blockUI.getParent().getClientId(fc);
74          }
75          else {
76              source = SearchExpressionUtils.resolveClientIdForClientSide(fc, blockUI, source);
77          }
78  
79          if (source == null) {
80              throw new FacesException("Cannot find source for blockUI component '" + clientId + "'.");
81          }
82  
83          // get target
84          String target = blockUI.getTarget();
85          if (target != null) {
86              target = SearchExpressionUtils.resolveClientIdForClientSide(fc, blockUI, target);
87          }
88  
89          // get content
90          String jqContent = null;
91          boolean isContentExtern = false;
92          if (blockUI.getContent() != null) {
93              final UIComponent contentComponent = blockUI.findComponent(blockUI.getContent());
94              if (contentComponent == null) {
95                  throw new FacesException("Cannot find content for blockUI component '" + clientId + "'.");
96              }
97  
98              jqContent = "#" + contentComponent.getClientId(fc);
99              isContentExtern = true;
100         }
101         else if (blockUI.getChildCount() > 0) {
102             jqContent = "#" + clientId + "_content";
103         }
104 
105         // get reg. expression
106         final String eventRegEx;
107         final String events = blockUI.getEvent();
108 
109         if (LangUtils.isBlank(events)) {
110             // no events means all events of the given source are accepted
111             eventRegEx = "/" + Constants.RequestParams.PARTIAL_SOURCE_PARAM + "=" + source + "(.)*$/";
112         }
113         else {
114             eventRegEx = getEventRegEx(events);
115         }
116 
117         // generate script
118         final WidgetBuilder wb = getWidgetBuilder(fc);
119         wb.init("ExtBlockUI", blockUI);
120         wb.attr("source", source);
121         wb.attr("target", target, null);
122         wb.attr("autoShow", blockUI.isAutoShow());
123         wb.attr("focusInput", blockUI.isFocusInput());
124         wb.attr("showOverlay", blockUI.isShowOverlay());
125         wb.attr("centerX", blockUI.isCenterX());
126         wb.attr("centerY", blockUI.isCenterY());
127         wb.attr("fadeIn", blockUI.getFadeIn());
128         wb.attr("fadeOut", blockUI.getFadeOut());
129 
130         wb.nativeAttr("css", blockUI.getCss());
131         wb.nativeAttr("overlayCSS", blockUI.getCssOverlay());
132 
133         wb.attr("timeout", blockUI.getTimeout(), 0);
134 
135         wb.selectorAttr("content", jqContent);
136 
137         wb.attr("contentExtern", isContentExtern);
138         wb.attr("namingContSep", Character.toString(UINamingContainer.getSeparatorChar(fc)));
139         wb.nativeAttr("regEx", eventRegEx);
140 
141         wb.finish();
142     }
143 
144     protected String getEventRegEx(final String events) {
145         final String[] arrEvents = events.split("[\\s,]+");
146         final StringBuilder sb = new StringBuilder("/");
147 
148         for (int i = 0; i < arrEvents.length; i++) {
149             sb.append(Constants.RequestParams.PARTIAL_BEHAVIOR_EVENT_PARAM);
150             sb.append("=");
151             sb.append(arrEvents[i]);
152 
153             if (i + 1 < arrEvents.length) {
154                 sb.append("|");
155             }
156         }
157 
158         sb.append("/");
159         return sb.toString();
160     }
161 
162     @Override
163     public boolean getRendersChildren() {
164         return true;
165     }
166 
167     @Override
168     public void encodeChildren(final FacesContext fc, final UIComponent component) {
169         // nothing to do
170     }
171 }