View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
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   * @JSFRenderer
37   *   renderKitId = "HTML_BASIC" 
38   *   family = "org.apache.myfaces.SubForm"
39   *   type = "org.apache.myfaces.SubForm"
40   * @since 1.1.7
41   * @author Gerald Muellan
42   *         Date: 19.01.2006
43   *         Time: 14:01:35
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 }