1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
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
41
42
43
44
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
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
84 String target = blockUI.getTarget();
85 if (target != null) {
86 target = SearchExpressionUtils.resolveClientIdForClientSide(fc, blockUI, target);
87 }
88
89
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
106 final String eventRegEx;
107 final String events = blockUI.getEvent();
108
109 if (LangUtils.isBlank(events)) {
110
111 eventRegEx = "/" + Constants.RequestParams.PARTIAL_SOURCE_PARAM + "=" + source + "(.)*$/";
112 }
113 else {
114 eventRegEx = getEventRegEx(events);
115 }
116
117
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
170 }
171 }