1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package org.apache.myfaces.custom.subform;
20
21 import org.apache.myfaces.shared_tomahawk.renderkit.RendererUtils;
22 import org.apache.myfaces.shared_tomahawk.renderkit.html.HTML;
23 import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRenderer;
24 import org.apache.myfaces.shared_tomahawk.renderkit.html.HtmlRendererUtils;
25 import org.apache.myfaces.shared_tomahawk.renderkit.html.util.FormInfo;
26
27 import javax.faces.component.UIComponent;
28 import javax.faces.context.FacesContext;
29 import javax.faces.context.ResponseWriter;
30
31 import java.io.IOException;
32 import java.util.Map;
33
34
35
36
37
38
39
40
41
42
43
44
45 public class SubFormRenderer extends HtmlRenderer
46 {
47 private static final String SUBMIT_FUNCTION_SUFFIX = "_submit";
48 private static final String HIDDEN_PARAM_NAME = "org.apache.myfaces.custom.subform.submittedId";
49
50
51 public void encodeBegin(FacesContext context, UIComponent component) throws IOException
52 {
53 super.encodeBegin(context, component);
54
55 ResponseWriter writer = context.getResponseWriter();
56
57 HtmlRendererUtils.writePrettyLineSeparator(context);
58 writer.startElement(HTML.SCRIPT_ELEM, null);
59 writer.writeAttribute(org.apache.myfaces.shared_tomahawk.renderkit.html.HTML.SCRIPT_TYPE_ATTR, org.apache.myfaces.shared_tomahawk.renderkit.html.HTML.SCRIPT_TYPE_TEXT_JAVASCRIPT, null);
60
61 FormInfo parentFormInfo = RendererUtils.findNestingForm(component,context);
62 if(parentFormInfo!=null)
63 {
64 writer.writeText(createPartialSubmitJS(component.getId(), parentFormInfo.getFormName()), null);
65 }
66
67 writer.endElement(org.apache.myfaces.shared_tomahawk.renderkit.html.HTML.SCRIPT_ELEM);
68 HtmlRendererUtils.writePrettyLineSeparator(context);
69 }
70
71
72 public void decode(FacesContext context, UIComponent component)
73 {
74 super.decode(context, component);
75
76 Map paramValuesMap = context.getExternalContext().getRequestParameterMap();
77 String reqValue = (String) paramValuesMap.get(HIDDEN_PARAM_NAME);
78 if (reqValue != null && component.getId().equals(reqValue))
79 {
80 ((SubForm) component).setSubmitted(true);
81 }
82 }
83
84
85 protected String createPartialSubmitJS(String subFormId, String parentFormClientId)
86 {
87 StringBuffer script = new StringBuffer();
88 script.append("function ");
89 script.append(subFormId).append(SUBMIT_FUNCTION_SUFFIX + "()");
90 script.append(" {\n");
91 script.append("var form = document.forms['").append(parentFormClientId).append("'];\n");
92 script.append("var el = document.createElement(\"input\");\n");
93 script.append("el.type = \"hidden\";\n");
94 script.append("el.name = \"" + HIDDEN_PARAM_NAME + "\";\n");
95 script.append("el.value = \"").append(subFormId).append("\";\n");
96 script.append("form.appendChild(el);\n");
97 script.append("form.submit();\n");
98 script.append("}\n");
99
100 return script.toString();
101 }
102
103 }