1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package org.apache.myfaces.custom.ajax.util;
21
22 import java.io.IOException;
23 import java.io.PrintWriter;
24 import java.util.Iterator;
25 import java.util.Map;
26
27 import javax.faces.application.FacesMessage;
28 import javax.faces.application.ViewHandler;
29 import javax.faces.component.UIComponent;
30 import javax.faces.component.UIOutput;
31 import javax.faces.context.FacesContext;
32 import javax.faces.context.ResponseWriter;
33 import javax.servlet.http.HttpServletResponse;
34
35 import org.apache.commons.logging.Log;
36 import org.apache.commons.logging.LogFactory;
37 import org.apache.myfaces.component.html.ext.HtmlMessages;
38 import org.apache.myfaces.custom.ajax.AjaxCallbacks;
39 import org.apache.myfaces.custom.inputAjax.HtmlInputTextAjax;
40 import org.apache.myfaces.custom.prototype.PrototypeResourceLoader;
41 import org.apache.myfaces.custom.util.ComponentUtils;
42 import org.apache.myfaces.renderkit.html.util.AddResource;
43 import org.apache.myfaces.renderkit.html.util.AddResourceFactory;
44 import org.apache.myfaces.shared_tomahawk.renderkit.JSFAttr;
45 import org.apache.myfaces.shared_tomahawk.renderkit.html.HTML;
46 import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlMessageRendererBase;
47
48
49
50
51
52 public final class AjaxRendererUtils
53 {
54 public static final String STYLECLASS_LOADER = "myFacesInputSuggestAjax";
55
56
57
58
59 private AjaxRendererUtils()
60 {
61
62 }
63
64 private static final Log log = LogFactory.getLog(AjaxRendererUtils.class);
65 public static final String JAVASCRIPT_ENCODED = "org.apache.myfaces.custom.inputAjax.JAVASCRIPT_ENCODED";
66 public static final String JS_MYFACES_NAMESPACE = "_MyFaces_inputAjax_";
67
68 public static void addPrototypeScript(FacesContext context, UIComponent component, AddResource addResource)
69 {
70
71
72 String javascriptLocation = (String) component.getAttributes().get(JSFAttr.JAVASCRIPT_LOCATION);
73 if (javascriptLocation != null)
74 {
75 addResource.addJavaScriptAtPosition(context, AddResource.HEADER_BEGIN, javascriptLocation + "/prototype.js");
76 addResource.addJavaScriptAtPosition(context, AddResource.HEADER_BEGIN, javascriptLocation + "/effects.js");
77 }
78 else
79 {
80 addResource.addJavaScriptAtPosition(context, AddResource.HEADER_BEGIN, PrototypeResourceLoader.class, "prototype.js");
81 addResource.addJavaScriptAtPosition(context, AddResource.HEADER_BEGIN, PrototypeResourceLoader.class, "effects.js");
82 }
83 addResource.addStyleSheet(context, AddResource.HEADER_BEGIN, AjaxRendererUtils.class, "myFaces_Ajax.css");
84 }
85
86 public static void writeAjaxScript(FacesContext context, ResponseWriter out, AjaxCallbacks component)
87 throws IOException
88 {
89 writeAjaxScript(context, out, component, null);
90 }
91
92
93
94
95
96
97
98
99
100
101 public static void writeAjaxScript(FacesContext context, ResponseWriter out, AjaxCallbacks component, String extraParams) throws IOException
102 {
103 UIComponent uiComponent = (UIComponent) component;
104 String clientId = uiComponent.getClientId(context);
105 String viewId = context.getViewRoot().getViewId();
106 ViewHandler viewHandler = context.getApplication().getViewHandler();
107 String ajaxURL = viewHandler.getActionURL(context, viewId);
108
109 String ajaxMessagesId = null;
110
111 if (uiComponent instanceof HtmlInputTextAjax)
112 {
113
114 UIComponent ajaxMessages = (UIComponent) ComponentUtils
115 .findFirstMessagesComponent(context, context.getViewRoot());
116
117 if (ajaxMessages != null && ((HtmlMessages) ajaxMessages).getForceSpan())
118 {
119 ajaxMessagesId = ajaxMessages.getClientId(context);
120 }
121 }
122
123 String jsNameSpace =
124 JS_MYFACES_NAMESPACE;
125 String AJAX_RESPONSE_MAP = JS_MYFACES_NAMESPACE + "ajaxResponseMap";
126
127
128
129
130 if (!context.getExternalContext().getRequestMap().containsKey(JAVASCRIPT_ENCODED))
131 {
132 out.startElement(HTML.SCRIPT_ELEM, null);
133 out.writeAttribute(HTML.TYPE_ATTR, "text/javascript", null);
134
135 out.writeText("var " + jsNameSpace + "ajaxUrl = '" + ajaxURL + "';\n", null);
136 out.writeText("var " + jsNameSpace + "globalErrorsId = '" + ajaxMessagesId + "';\n", null);
137
138 out.writeText("var " + AJAX_RESPONSE_MAP + " = new Object();\n", null);
139
140 out.endElement(HTML.SCRIPT_ELEM);
141
142 AddResource addResource = AddResourceFactory.getInstance(context);
143 addResource.addJavaScriptAtPosition(context, AddResource.HEADER_BEGIN,
144 AjaxRendererUtils.class, "inputAjax.js");
145
146
147 context.getExternalContext().getRequestMap().put(JAVASCRIPT_ENCODED, Boolean.TRUE);
148 }
149
150
151 out.startElement(HTML.SCRIPT_ELEM, null);
152 out.writeAttribute(HTML.TYPE_ATTR, "text/javascript", null);
153 out.writeText(AJAX_RESPONSE_MAP + "['" + clientId + "'] = new Object();\n", null);
154 if (component.getOnSuccess() != null)
155 out.writeText(AJAX_RESPONSE_MAP + "['" + clientId + "']['onSuccessFunction'] = " + component.getOnSuccess() + ";\n", null);
156 if (component.getOnFailure() != null)
157 out.writeText(AJAX_RESPONSE_MAP + "['" + clientId + "']['onFailureFunction'] = " + component.getOnFailure() + ";\n", null);
158 if (component.getOnStart() != null)
159 out.writeText(AJAX_RESPONSE_MAP + "['" + clientId + "']['onStartFunction'] = " + component.getOnStart() + ";\n", null);
160 out.endElement(HTML.SCRIPT_ELEM);
161
162 }
163
164
165 public static void encodeAjax(FacesContext context, UIComponent component)
166 throws IOException
167 {
168 encodeAjax(context, component, null);
169 }
170
171
172
173
174
175
176
177
178
179
180 public static void encodeAjax(FacesContext context, UIComponent component, Map extraReturnAttributes) throws IOException
181 {
182 String clientId = component.getClientId(context);
183 Object responseOb = context.getExternalContext().getResponse();
184 if (responseOb instanceof HttpServletResponse)
185 {
186 HttpServletResponse response = (HttpServletResponse) responseOb;
187
188 StringBuffer buff = new StringBuffer();
189
190
191 boolean hasErrorMessages = false;
192 Iterator iter = context.getMessages(clientId);
193 while (iter.hasNext())
194 {
195 FacesMessage msg = (FacesMessage) iter.next();
196 String style = "";
197 String styleClass = "";
198
199 String msgForId = clientId + "_msgFor";
200
201 UIComponent msgComponent = context.getViewRoot().findComponent(msgForId);
202 String msgId = null;
203 if (msgComponent != null)
204 {
205
206
207
208 String[] styleAndClass = HtmlMessageRendererBase.getStyleAndStyleClass(msgComponent, msg.getSeverity());
209 style = styleAndClass[0];
210 styleClass = styleAndClass[1];
211 msgId = msgComponent.getClientId(context);
212
213 }
214 else
215 {
216
217 }
218 buff.append("<error elname=\"").append(clientId)
219 .append("\" severity=\"").append(msg.getSeverity().toString());
220 if (styleClass != null) buff.append("\" styleClass=\"").append(styleClass);
221 if (style != null) buff.append("\" style=\"").append(style);
222 buff.append("\" summary=\"").append(msg.getSummary())
223 .append("\" ");
224 if(msgId != null) buff.append(" msgId=\"").append(msgId).append("\"");
225 buff.append(">\n");
226 String detail = msg.getDetail();
227 if (detail != null)
228 {
229 buff.append("<detail>");
230 buff.append(msg.getDetail());
231 buff.append("</detail>\n");
232 }
233 buff.append("</error>\n");
234 hasErrorMessages = true;
235 }
236 if (!hasErrorMessages)
237 {
238
239 buff.append("<elementUpdated elname=\"").append(clientId).append("\"");
240 if(component instanceof UIOutput){
241 UIOutput uiOutput = (UIOutput) component;
242
243
244 buff.append(" elvalue=\"").append(uiOutput.getValue()).append("\"");
245
246
247 }
248
249 if (extraReturnAttributes != null)
250 {
251 Iterator iter2 = extraReturnAttributes.keySet().iterator();
252 while (iter2.hasNext())
253 {
254 String key = (String) iter2.next();
255 buff.append(" ").append(key).append("=\"").append(extraReturnAttributes.get(key).toString()).append("\"");
256 }
257 }
258 buff.append(" />");
259 buff.append("\n");
260 }
261
262 String output = buff.toString();
263 log.debug(output);
264 PrintWriter out = response.getWriter();
265 out.print(output);
266
267 }
268 }
269
270
271
272
273
274
275 public static void writeLoadingImage(FacesContext context, UIComponent comp) throws IOException
276 {
277 ResponseWriter writer = context.getResponseWriter();
278
279 writer.startElement(HTML.SPAN_ELEM, null);
280 writer.writeAttribute(HTML.ID_ATTR, comp.getClientId(context) + "_loaderImg", null);
281 writer.writeAttribute(HTML.CLASS_ATTR, AjaxRendererUtils.STYLECLASS_LOADER, null);
282
283
284
285 writer.write("<spacer type=\"block\" width=\"15\" height=\"15\"/>");
286 writer.endElement(HTML.SPAN_ELEM);
287
288 }
289 }