Source code: com/aendvari/tethys/tag/html/CopyFormTag.java
1 /*
2 * CopyFormTag.java
3 *
4 * Copyright (c) 2001, 2002 Aendvari, Ltd. All Rights Reserved.
5 *
6 * See the file LICENSE for terms of use.
7 *
8 */
9
10 package com.aendvari.tethys.tag.html;
11
12 import java.io.IOException;
13
14 import javax.servlet.http.*;
15 import javax.servlet.jsp.*;
16 import javax.servlet.jsp.tagext.*;
17
18 import com.aendvari.common.model.*;
19
20 import com.aendvari.tethys.tag.*;
21 import com.aendvari.tethys.context.*;
22
23
24 /**
25 * <p>Implementation class for the "copyForm" tag.</p>
26 *
27 * <p>The following changes need to be made to the form that this tag
28 * is being used within:</p>
29 *
30 * <ul>
31 * <li>
32 * You must add the javascript "satyrCopyForm()" to the onSubmit
33 * event of the form. If you have another function there already, add this on
34 * first, then any other calls you want/require.
35 * </li>
36 * <li>
37 * If copying more than one form (using this tag more than once within a form)
38 * then you must add a "satyrCopyForm()" function for each copied form.
39 * </li>
40 * </ul>
41 *
42 * <code>
43 * function satyrCopyForm(targetFormObj, sourceFormName)
44 * </code>
45 * <ul>
46 * <li>targetFormObj - simply pass in "this" for the form this tag is within.</li>
47 * <li>sourceFormName - the name of the form to copy data from.</li>
48 * </ul>
49 *
50 * @author Scott Milne
51 *
52 */
53
54 public class CopyFormTag extends TagSupport
55 {
56 /* Variables */
57
58 /** The name of the source form to copy from. */
59 protected String sourceForm;
60
61
62 /* Constructor */
63
64 public CopyFormTag()
65 {
66 super();
67
68 sourceForm = "";
69 }
70
71 public String getSourceForm() { return sourceForm; }
72 public void setSourceForm( String name ) { sourceForm = name; }
73
74 public int doEndTag() throws JspTagException
75 {
76 try
77 {
78 // check to see if this tag is within a <html:form>. If it is, add the "satyrCopyForm()" call to the onsubmit
79 FormTag formTag = (FormTag)findAncestorWithClass(this, FormTag.class);
80 if( formTag != null )
81 {
82 formTag.addCopyFormEntry(sourceForm);
83 }
84
85
86 // create the JS (if required) and HTML output
87 String ls_output = "";
88
89 String defined = (String)pageContext.getAttribute( TagConstants.Fields.SatyrCopyFormFieldName );
90 if ( defined == null )
91 {
92 // add the JS to the output
93 ls_output += createCopyFormJavaScript();
94 ls_output += "\n\n";
95
96 // set the value into the page scope to let other copyform values
97 // know that the JS has already been added to this page
98 pageContext.setAttribute(TagConstants.Fields.SatyrCopyFormFieldName, "true", PageContext.PAGE_SCOPE);
99 }
100
101 // add the hidden form
102 ls_output += "<input type=\"hidden\" name=\"" + TagConstants.Fields.SatyrCopyFormFieldName + ":" + sourceForm + "\" >\n";
103
104 // output whatever "string" we want to produce back to the user
105 pageContext.getOut().write(ls_output.trim());
106 }
107 catch (IOException e)
108 {
109 throw new JspTagException("CopyFormTag Error:" + e.toString());
110 }
111
112 // Have the JSP Container continue the JSP page as normal
113 return EVAL_PAGE;
114 }
115
116 private String createCopyFormJavaScript()
117 {
118 String javascript = "<script language=\"JavaScript\">\n";
119 javascript += "function satyrCopyForm(targetForm, sourceFormName)\n";
120 javascript += "{\n";
121 javascript += " var sourceForm = document.forms[sourceFormName];\n";
122 javascript += "\n";
123 javascript += " var formDataField = targetForm[\"satyr_copyform:\"+sourceFormName];\n";
124 javascript += " var formData = \"\";\n";
125 javascript += "\n";
126 javascript += " if( sourceForm )\n";
127 javascript += " {\n";
128 javascript += " var counter = 1;\n";
129 javascript += "\n";
130 javascript += " for( var i=0; i<sourceForm.elements.length; i++ )\n";
131 javascript += " {\n";
132 javascript += " var wasAdded = false;\n";
133 javascript += " var formField = sourceForm.elements[i];\n";
134 javascript += "\n";
135 javascript += " // text, textarea, password, and hidden\n";
136 javascript += " if( formField.type == \"text\" || formField.type == \"textarea\" ||\n";
137 javascript += " formField.type == \"password\" || formField.type == \"hidden\" )\n";
138 javascript += " {\n";
139 javascript += " formData += escape(formField.name);\n";
140 javascript += " formData += \"=\";\n";
141 javascript += " formData += escape(formField.value);\n";
142 javascript += " wasAdded = true;\n";
143 javascript += " }\n";
144 javascript += "\n";
145 javascript += " // radio\n";
146 javascript += " if( formField.type == \"radio\" )\n";
147 javascript += " {\n";
148 javascript += " if( formField.checked == true )\n";
149 javascript += " {\n";
150 javascript += " formData += escape(formField.name);\n";
151 javascript += " formData += \"=\";\n";
152 javascript += " formData += escape(formField.value);\n";
153 javascript += " wasAdded = true;\n";
154 javascript += " }\n";
155 javascript += " }\n";
156 javascript += "\n";
157 javascript += " // checkbox\n";
158 javascript += " if( formField.type == \"checkbox\" )\n";
159 javascript += " {\n";
160 javascript += " if( formField.checked == true )\n";
161 javascript += " {\n";
162 javascript += " formData += escape(formField.name);\n";
163 javascript += " formData += \"=\";\n";
164 javascript += " formData += escape(formField.value);\n";
165 javascript += " wasAdded = true;\n";
166 javascript += " }\n";
167 javascript += " }\n";
168 javascript += "\n";
169 javascript += " // select\n";
170 javascript += " if( formField.type == \"select\" ||\n";
171 javascript += " formField.type == \"select-one\" ||\n";
172 javascript += " formField.type == \"select-multiple\" )\n";
173 javascript += " {\n";
174 javascript += " // for each element in the select list\n";
175 javascript += " var j;\n";
176 javascript += " var selCounter = 1;\n";
177 javascript += "\n";
178 javascript += " for( j=0; j<formField.options.length; j++ )\n";
179 javascript += " {\n";
180 javascript += " if( formField.options[j].selected == true && formField.name != \"\" )\n";
181 javascript += " {\n";
182 javascript += " formData += escape(formField.name);\n";
183 javascript += " formData += \"=\";\n";
184 javascript += " formData += escape(formField.options[j].value);\n";
185 javascript += " formData += \";\";\n";
186 javascript += "\n";
187 javascript += " }\n";
188 javascript += "\n";
189 javascript += " selCounter++;\n";
190 javascript += " }\n";
191 javascript += " }\n";
192 javascript += "\n";
193 javascript += " if( counter < sourceForm.length && wasAdded )\n";
194 javascript += " formData += \";\";\n";
195 javascript += "\n";
196 javascript += " wasAdded = false;\n";
197 javascript += " counter++;\n";
198 javascript += " }\n";
199 javascript += " }\n";
200 javascript += "\n";
201 javascript += " // place the form data into the hidden field\n";
202 javascript += " formDataField.value = formData;\n";
203 javascript += "}\n";
204 javascript += "</script>\n";
205
206 return javascript;
207 }
208 }
209