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.dialog;
21
22 import java.io.IOException;
23 import java.util.ArrayList;
24 import java.util.Arrays;
25 import java.util.List;
26 import java.util.StringTokenizer;
27
28 import javax.faces.component.UIComponent;
29 import javax.faces.context.FacesContext;
30 import javax.faces.context.ResponseWriter;
31
32 import org.apache.myfaces.custom.dojo.DojoUtils;
33 import org.apache.myfaces.renderkit.html.util.AddResource;
34 import org.apache.myfaces.renderkit.html.util.AddResourceFactory;
35 import org.apache.myfaces.shared_tomahawk.renderkit.JSFAttr;
36 import org.apache.myfaces.shared_tomahawk.renderkit.RendererUtils;
37 import org.apache.myfaces.shared_tomahawk.renderkit.html.HTML;
38 import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRenderer;
39 import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRendererUtils;
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69 public class ModalDialogRenderer extends HtmlRenderer
70 {
71 public static final String RENDERER_TYPE = "org.apache.myfaces.ModalDialog";
72
73 public static final String DIV_ID_PREFIX = "_div";
74
75
76
77
78
79
80 public void encodeBegin(FacesContext context, UIComponent component)
81 throws IOException
82 {
83 String javascriptLocation = (String) component.getAttributes().get(JSFAttr.JAVASCRIPT_LOCATION);
84 DojoUtils.addMainInclude(context, component, javascriptLocation,
85 DojoUtils.getDjConfigInstance(context));
86 DojoUtils.addRequire(context, component, "dojo.widget.Dialog");
87
88 writeModalDialogBegin(context, (ModalDialog) component, context.getResponseWriter());
89 }
90
91
92
93
94
95
96 public void encodeEnd(FacesContext context, UIComponent component) throws IOException
97 {
98 ModalDialog dlg = (ModalDialog) component;
99
100 StringBuffer buf = new StringBuffer();
101
102 buf.append("</div>");
103
104 writeDialogLoader(context, dlg, buf);
105
106 context.getResponseWriter().write(buf.toString());
107
108 if (dlg.getViewId() != null)
109 {
110
111
112 RendererUtils.renderChildren(context, component);
113 HtmlRendererUtils.writePrettyLineSeparator(context);
114 }
115 }
116
117 private void appendHiderIds(StringBuffer buf, ModalDialog dlg)
118 {
119 List hiders = new ArrayList();
120
121 if (dlg.getHiderIds() != null)
122 {
123 hiders.addAll(Arrays.asList(dlg.getHiderIds().split(",")));
124 }
125
126 if (isRenderCloseButton(dlg) && dlg.getDialogTitle() != null)
127 {
128 hiders.add(dlg.getDialogVar() + "Closer");
129 }
130
131 for (int i = 0; i < hiders.size(); i++)
132 {
133 String varName = "btn" + i;
134 buf
135 .append("var ")
136 .append(varName)
137 .append(" = document.getElementById(\"")
138 .append(((String) hiders.get(i)).trim())
139 .append("\");")
140
141 .append(dlg.getDialogVar())
142 .append(".setCloseControl(")
143 .append(varName)
144 .append(");");
145 }
146 }
147
148 private void appendDialogAttributes(StringBuffer buf, ModalDialog dlg)
149 {
150 if(dlg.getDialogAttr() == null)
151 {
152 return;
153 }
154
155 StringTokenizer it = new StringTokenizer(dlg.getDialogAttr(), " ");
156 while(it.hasMoreElements())
157 {
158 String[] pair = it.nextToken().split("=");
159 String attribute = pair[0];
160 String value = pair[1].replaceAll("'", "");
161 try
162 {
163
164 new Double(value);
165 }
166 catch(NumberFormatException e)
167 {
168
169 value = new StringBuffer("\"").append(value).append("\"").toString();
170 }
171 buf
172 .append(", ")
173 .append(attribute)
174 .append(":")
175 .append(value);
176 }
177 }
178
179
180
181
182
183
184
185 private void writeModalDialogBegin(FacesContext context, ModalDialog dlg, ResponseWriter writer)
186 throws IOException
187 {
188 StringBuffer buf = new StringBuffer();
189
190 String dlgId = getDialogWrapperId(dlg);
191 buf.append("<div id=\"").append(dlgId).append("\"");
192 if(dlg.getStyle() != null)
193 {
194 buf.append(" style=\"").append(dlg.getStyle()).append("\"");
195 }
196 if(dlg.getStyleClass() != null)
197 {
198 buf.append(" class=\"").append(dlg.getStyleClass()).append("\"");
199 }
200 buf.append(">");
201
202 writer.write(buf.toString());
203 }
204
205
206
207
208
209
210 private String getDialogWrapperId(ModalDialog dlg)
211 {
212
213
214
215 String dlgId = dlg.getDialogId() != null ?
216 new StringBuffer(dlg.getDialogId()).append(DIV_ID_PREFIX).toString() :
217 dlg.getId();
218 return dlgId;
219 }
220
221
222
223
224
225
226
227 private String writeDialogLoader(FacesContext context, ModalDialog dlg, StringBuffer buf)
228 {
229 String dlgWrapperId = getDialogWrapperId(dlg);
230 String dialogVar = dlg.getDialogVar();
231 buf.append("<script type=\"text/javascript\">");
232
233
234
235
236 buf.append("var ").append(dialogVar).append(";");
237
238
239 buf .append("function " + dialogVar + "_loader(e) {")
240 .append(dialogVar)
241 .append(" = dojo.widget.createWidget(\"dialog\", {id:")
242 .append("\"")
243 .append(dlg.getDialogId())
244 .append("\"");
245
246 appendDialogAttributes(buf, dlg);
247
248 buf.append("}, dojo.byId(\"").append(dlgWrapperId).append("\"));");
249
250 appendHiderIds(buf, dlg);
251
252 String viewId = dlg.getViewId();
253 String contentURL = dlg.getContentURL();
254 if (viewId != null)
255 {
256 StringBuffer sbUrl = new StringBuffer();
257 sbUrl.append(context.getExternalContext().getRequestContextPath());
258 sbUrl.append("/");
259 sbUrl.append(viewId);
260 String encodedUrl = context.getExternalContext().encodeActionURL(sbUrl.toString());
261 appendShowHideView(context, buf, dialogVar, encodedUrl);
262 }
263 else if (contentURL != null)
264 {
265 String encodedUrl = context.getExternalContext().encodeActionURL(contentURL);
266 appendShowHideView(context, buf, dialogVar, encodedUrl);
267 }
268
269 buf.append("}");
270
271
272
273
274
275
276
277
278
279
280
281 buf.append("dojo.addOnLoad(function() {" + dialogVar + "_loader();});");
282
283 buf.append("</script>");
284 return dlgWrapperId;
285 }
286
287
288
289
290
291
292
293
294
295
296 private void appendShowHideView(
297 FacesContext context,
298 StringBuffer buf,
299 String dialogVar,
300 String url)
301 {
302
303 buf .append(dialogVar)
304 .append(".oldOnShow=")
305 .append(dialogVar)
306 .append(".onShow;");
307
308
309
310
311
312
313
314
315
316
317
318 buf .append(dialogVar)
319 .append(".onShow = function() {")
320 .append("this.oldOnShow();")
321 .append("var content = document.getElementById(\"modalDialogContent")
322 .append(dialogVar)
323 .append("\"); ")
324 .append("window._myfaces_currentModal=")
325 .append(dialogVar)
326 .append("; ")
327 .append(dialogVar)
328 .append("._myfaces_ok=false; ")
329 .append("content.contentWindow.location.replace('")
330 .append(url)
331 .append("'); ")
332 .append("}; ");
333
334
335 buf .append(dialogVar)
336 .append(".oldOnHide=")
337 .append(dialogVar)
338 .append(".onHide;");
339
340
341
342
343 buf .append(dialogVar)
344 .append(".onHide = function() {")
345 .append("this.oldOnHide();")
346 .append("window._myfaces_currentModal=null;")
347 .append("var content = document.getElementById(\"modalDialogContent")
348 .append(dialogVar)
349 .append("\"); ")
350 .append("content.contentWindow.location.replace('javascript:false;'); ")
351 .append("}; ");
352 }
353
354 public boolean getRendersChildren()
355 {
356 return true;
357 }
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378 public void encodeChildren(FacesContext facesContext, UIComponent uiComponent) throws IOException
379 {
380 ModalDialog dlg = (ModalDialog) uiComponent;
381 ResponseWriter writer = facesContext.getResponseWriter();
382
383 UIComponent titleFacet = dlg.getFacet("titleBar");
384 if (titleFacet != null)
385 {
386 RendererUtils.renderChild(facesContext, titleFacet);
387 }
388 else if (dlg.getDialogTitle() != null)
389 {
390 AddResourceFactory.getInstance(facesContext).addStyleSheet(facesContext, AddResource.HEADER_BEGIN, ModalDialog.class, "modalDialog.css");
391
392 writer.startElement(HTML.TABLE_ELEM, dlg);
393 writer.writeAttribute(HTML.CLASS_ATTR, "modalDialogDecoration " + getStyleName(dlg, "Decoration") , null);
394 writer.writeAttribute(HTML.CELLPADDING_ATTR, "2", null);
395 writer.writeAttribute(HTML.CELLSPACING_ATTR, "0", null);
396
397 writer.startElement(HTML.TR_ELEM, dlg);
398 writer.writeAttribute(HTML.CLASS_ATTR, "modalDialogTitle " + getStyleName(dlg, "Title"), null);
399
400 writer.startElement(HTML.TD_ELEM, dlg);
401 writer.writeAttribute(HTML.CLASS_ATTR, "modalDialogTitleLeft " + getStyleName(dlg, "TitleLeft"), null);
402 writer.writeText(dlg.getDialogTitle(), null);
403 writer.endElement(HTML.TD_ELEM);
404
405 writer.startElement(HTML.TD_ELEM, dlg);
406 writer.writeAttribute(HTML.CLASS_ATTR, "modalDialogTitleRight " + getStyleName(dlg, "TitleRight"), null);
407 if (isRenderCloseButton(dlg))
408 {
409 String imageUri = AddResourceFactory.getInstance(facesContext).getResourceUri(facesContext, ModalDialog.class, "close.gif");
410 writer.startElement(HTML.IMG_ELEM, dlg);
411 writer.writeAttribute(HTML.ID_ATTR, dlg.getDialogVar() + "Closer", null);
412 writer.writeAttribute(HTML.SRC_ATTR, imageUri, null);
413 writer.writeAttribute(HTML.CLASS_ATTR, "modalDialogCloser " + getStyleName(dlg, "Closer"), null);
414 writer.endElement(HTML.IMG_ELEM);
415 }
416 writer.endElement(HTML.TD_ELEM);
417
418 writer.endElement(HTML.TR_ELEM);
419 writer.endElement(HTML.TABLE_ELEM);
420 }
421
422 if ((dlg.getViewId() != null) || (dlg.getContentURL() != null))
423 {
424 renderDialogViewFrame(facesContext, dlg);
425
426
427
428 }
429 else
430 {
431 RendererUtils.renderChildren(facesContext, uiComponent);
432 HtmlRendererUtils.writePrettyLineSeparator(facesContext);
433 }
434 }
435
436 protected boolean isRenderCloseButton(ModalDialog dlg)
437 {
438 return !Boolean.FALSE.equals(dlg.getCloseButton());
439 }
440
441 private String getStyleName(ModalDialog dlg, String suffix)
442 {
443 if (dlg.getStyleClass() != null)
444 {
445 return dlg.getStyleClass() + suffix;
446 }
447
448 return "";
449 }
450
451
452
453
454 private void renderDialogViewFrame(FacesContext facesContext, ModalDialog dlg) throws IOException
455 {
456 ResponseWriter writer = facesContext.getResponseWriter();
457
458 writer.startElement(HTML.IFRAME_ELEM, dlg);
459 writer.writeAttribute(HTML.ID_ATTR, "modalDialogContent" + dlg.getDialogVar(), null);
460 writer.writeAttribute(HTML.CLASS_ATTR, "modalDialogContent " + getStyleName(dlg, "Content"), null);
461 writer.writeAttribute(HTML.SCROLLING_ATTR, "auto", null);
462 writer.writeAttribute(HTML.FRAMEBORDER_ATTR, "0", null);
463 writer.endElement(HTML.IFRAME_ELEM);
464 }
465 }