click-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sa...@apache.org
Subject svn commit: r1054228 - in /click/trunk/click/extras/src: META-INF/resources/click/extras-control.js org/apache/click/extras/cayenne/TabbedCayenneForm.java org/apache/click/extras/control/TabbedForm.htm org/apache/click/extras/control/TabbedForm.java
Date Sat, 01 Jan 2011 12:54:59 GMT
Author: sabob
Date: Sat Jan  1 12:54:59 2011
New Revision: 1054228

URL: http://svn.apache.org/viewvc?rev=1054228&view=rev
Log:
fixed TabbedForm and TabbedCyennneForm JavaScript validation. CLK-735

Modified:
    click/trunk/click/extras/src/META-INF/resources/click/extras-control.js
    click/trunk/click/extras/src/org/apache/click/extras/cayenne/TabbedCayenneForm.java
    click/trunk/click/extras/src/org/apache/click/extras/control/TabbedForm.htm
    click/trunk/click/extras/src/org/apache/click/extras/control/TabbedForm.java

Modified: click/trunk/click/extras/src/META-INF/resources/click/extras-control.js
URL: http://svn.apache.org/viewvc/click/trunk/click/extras/src/META-INF/resources/click/extras-control.js?rev=1054228&r1=1054227&r2=1054228&view=diff
==============================================================================
--- click/trunk/click/extras/src/META-INF/resources/click/extras-control.js (original)
+++ click/trunk/click/extras/src/META-INF/resources/click/extras-control.js Sat Jan  1 12:54:59
2011
@@ -306,4 +306,79 @@ Click.getUrlParams = function(url) {
     }
   }
   return params;
-};
\ No newline at end of file
+}
+
+/**
+ * Return the tab sheet number for the element with the given ID.
+ */
+Click.getTabSheetNumber=function(id) {
+    var node=document.getElementById(id);
+    if(!node) return 1;
+    var parent = node.parentNode;
+    while(parent && !Click.isTabSheet(parent)) {
+        parent = parent.parentNode;
+    }
+    if (parent) return parent.getAttribute("id").substr(10);
+    return 1;
+}
+
+/**
+ * Return true if the node is a tab sheet, false otherwise.
+ */
+Click.isTabSheet=function(node) {
+    if(!node) return false;
+    var id=node.getAttribute('id');
+    if(id){
+        if(id.indexOf('tab-sheet-')>=0) return true;
+    }
+    return false;
+}
+
+/**
+ * Validate a TabbedForm.
+ */
+Click.validateTabbedForm=function(msgs, id, align, style) {
+    var errorsHtml = '';
+    var focusFieldId = null;
+
+    for (i = 0; i < msgs.length; i++) {
+        var value = msgs[i];
+        if (value != null) {
+            var index = value.lastIndexOf('|');
+            var fieldMsg = value.substring(0, index);
+            var fieldId = value.substring(index + 1);
+
+            if (focusFieldId == null) {
+                focusFieldId = fieldId;
+            }
+
+            errorsHtml += '<tr class="errors"><td class="errors" align="';
+            errorsHtml += align;
+			if (style != null) {
+            	errorsHtml += '" style="';
+				errorsHtml += style;
+            }
+            errorsHtml += '">';
+            errorsHtml += '<a class="error" href="javascript:onShowTab(Click.getTabSheetNumber(\''
+                + fieldId + '\'));setFocus(\'';
+            errorsHtml += fieldId;
+            errorsHtml += '\');">';
+            errorsHtml += fieldMsg;
+            errorsHtml += '</a>';
+            errorsHtml += '</td></tr>';
+        }
+    }
+
+    if (errorsHtml.length > 0) {
+        errorsHtml = '<table class="errors">' + errorsHtml + '</table>';
+
+        document.getElementById(id + '-errorsDiv').innerHTML = errorsHtml;
+        document.getElementById(id + '-errorsTr').style.display = 'inline';
+
+        setFocus(focusFieldId);
+
+        return false;
+    } else {
+        return true;
+    }
+}

Modified: click/trunk/click/extras/src/org/apache/click/extras/cayenne/TabbedCayenneForm.java
URL: http://svn.apache.org/viewvc/click/trunk/click/extras/src/org/apache/click/extras/cayenne/TabbedCayenneForm.java?rev=1054228&r1=1054227&r2=1054228&view=diff
==============================================================================
--- click/trunk/click/extras/src/org/apache/click/extras/cayenne/TabbedCayenneForm.java (original)
+++ click/trunk/click/extras/src/org/apache/click/extras/cayenne/TabbedCayenneForm.java Sat
Jan  1 12:54:59 2011
@@ -22,12 +22,14 @@ import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.StringTokenizer;
 import org.apache.click.Context;
 
 import org.apache.click.control.Field;
 import org.apache.click.control.FieldSet;
 import org.apache.click.element.CssImport;
 import org.apache.click.element.Element;
+import org.apache.click.element.JsImport;
 import org.apache.click.util.ClickUtils;
 import org.apache.click.util.HtmlStringBuffer;
 
@@ -170,9 +172,10 @@ public class TabbedCayenneForm extends C
     }
 
     /**
-     * Return the TabbedCayenneForm HTML HEAD elements for the following resource:
+     * Return the TabbedCayenneForm HTML HEAD elements for the following resources:
      *
      * <ul>
+     * <li><tt>click/extras-control.js</tt></li>
      * <li><tt>click/extras-control.css</tt></li>
      * </ul>
      *
@@ -192,6 +195,7 @@ public class TabbedCayenneForm extends C
             String versionIndicator = ClickUtils.getResourceVersionIndicator(context);
 
             headElements.add(new CssImport("/click/extras-control.css", versionIndicator));
+            headElements.add(new JsImport("/click/extras-control.js", versionIndicator));
         }
         return headElements;
     }
@@ -335,4 +339,60 @@ public class TabbedCayenneForm extends C
         buffer.append(getContext().renderTemplate(getTemplate(), model));
     }
 
+    @Override
+    protected void renderValidationJavaScript(HtmlStringBuffer buffer, List<Field>
formFields) {
+
+        // Render JavaScript form validation code
+        if (isJavaScriptValidation()) {
+            List<String> functionNames = new ArrayList<String>();
+
+            buffer.append("<script type=\"text/javascript\"><!--\n");
+
+            // Render field validation functions & build list of function names
+            for (Field field : formFields) {
+                String fieldJS = field.getValidationJavaScript();
+                if (fieldJS != null) {
+                    buffer.append(fieldJS);
+
+                    StringTokenizer tokenizer = new StringTokenizer(fieldJS);
+                    tokenizer.nextToken();
+                    functionNames.add(tokenizer.nextToken());
+                }
+            }
+
+            if (!functionNames.isEmpty()) {
+                buffer.append("function on_");
+                buffer.append(getId());
+                buffer.append("_submit() {\n");
+                buffer.append("   var msgs = new Array(");
+                buffer.append(functionNames.size());
+                buffer.append(");\n");
+                for (int i = 0; i < functionNames.size(); i++) {
+                    buffer.append("   msgs[");
+                    buffer.append(i);
+                    buffer.append("] = ");
+                    buffer.append(functionNames.get(i).toString());
+                    buffer.append(";\n");
+                }
+                buffer.append("   return Click.validateTabbedForm(msgs, '");
+                buffer.append(getId());
+                buffer.append("', '");
+                buffer.append(getErrorsAlign());
+                buffer.append("', ");
+                if (getErrorsStyle() == null) {
+                    buffer.append("null");
+                } else {
+                    buffer.append("'" + getErrorsStyle() + "'");
+                }
+                buffer.append(");\n");
+                buffer.append("}\n");
+
+            } else {
+                buffer.append("function on_");
+                buffer.append(getId());
+                buffer.append("_submit() { return true; }\n");
+            }
+            buffer.append("//--></script>\n");
+        }
+    }
 }

Modified: click/trunk/click/extras/src/org/apache/click/extras/control/TabbedForm.htm
URL: http://svn.apache.org/viewvc/click/trunk/click/extras/src/org/apache/click/extras/control/TabbedForm.htm?rev=1054228&r1=1054227&r2=1054228&view=diff
==============================================================================
--- click/trunk/click/extras/src/org/apache/click/extras/control/TabbedForm.htm (original)
+++ click/trunk/click/extras/src/org/apache/click/extras/control/TabbedForm.htm Sat Jan  1
12:54:59 2011
@@ -16,6 +16,34 @@
    specific language governing permissions and limitations
    under the License.*#
 -->
+#* Macro used for rendering field errors *#
+#macro(writeErrors $form)
+	#* Render javascript validation result *#
+	#if ($form.javaScriptValidation)
+	<tr style="display:none" id="${form.id}-errorsTr">
+		<td width='100%'>
+		<div class="errors" id="${form.id}-errorsDiv"></div>
+		</td>
+	</tr>
+	#end
+	
+	#if ($form.error)
+	<tr style="${form.errorsStyle}">
+    	<td class="tf_error"> $form.error </td>
+	</tr>
+	#end
+	
+	#* Render field errors *#
+	#foreach ($errorField in $form.errorFields)
+	<tr>
+    	<td style="${form.errorsStyle}" class="tf_error">
+    		<a class="error" href="#" 
+			  onclick="onShowTab($form.getTabSheetNumber($errorField.name));setFocus('$errorField.id');"
+			  > $errorField.error </a>
+    	</td>
+	</tr>
+	#end	
+#end
 
 <script type="text/javascript">
 function onShowTab(index) {
@@ -44,22 +72,7 @@ $form.startTag()
 <tbody>
 	#* Render form errors at the top position *#
 #if ($form.errorsPosition == "top")
-	#if ($form.error)
-	<tr style="${form.errorsStyle}">
-    	<td class="tf_error"> $form.error </td>
-	</tr>
-	#end
-	
-	#* Render field errors *#
-	#foreach ($errorField in $form.errorFields)
-	<tr>
-    	<td style="${form.errorsStyle}" class="tf_error">
-    		<a class="error" href="#" 
-			  onclick="onShowTab($form.getTabSheetNumber($errorField.name));setFocus('$errorField.id');"
-			  > $errorField.error </a>
-    	</td>
-	</tr>		
-	#end
+	#writeErrors($form)
 #end	
 	
 	<tr><td style="height:0.5em;">&#160;</td></tr>
@@ -110,22 +123,7 @@ $form.startTag()
 
 	#* Render form errors at the middle position *#
 #if ($form.errorsPosition == "middle")
-	#if ($form.error)
-	<tr style="${form.errorsStyle}">
-    	<td class="tf_error"> $form.error </td>
-	</tr>
-	#end
-	
-	#* Render field errors *#
-	#foreach ($errorField in $form.errorFields)
-	<tr>
-    	<td style="${form.errorsStyle}" class="tf_error">
-    		<a class="error" href="#" 
-			  onclick="onShowTab($form.getTabSheetNumber($errorField.name));setFocus('$errorField.id');"
-			  > $errorField.error </a>
-    	</td>
-	</tr>		
-	#end
+	#writeErrors($form)
 #end
 
 	<tr><td style="height:0.5em;">&#160;</td></tr>
@@ -144,22 +142,7 @@ $form.startTag()
 
 	<tr><td style="height:0.5em;">&#160;</td></tr>
 
-	#if ($form.error)
-	<tr style="${form.errorsStyle}">
-    	<td class="tf_error"> $form.error </td>
-	</tr>
-	#end
-	
-	#* Render field errors *#
-	#foreach ($errorField in $form.errorFields)
-	<tr>
-    	<td style="${form.errorsStyle}" class="tf_error">
-    		<a class="error" href="#" 
-			  onclick="onShowTab($form.getTabSheetNumber($errorField.name));setFocus('$errorField.id');"
-			  > $errorField.error </a>
-    	</td>
-	</tr>		
-	#end
+	#writeErrors($form)
 #end	
 </tbody>
 </table>

Modified: click/trunk/click/extras/src/org/apache/click/extras/control/TabbedForm.java
URL: http://svn.apache.org/viewvc/click/trunk/click/extras/src/org/apache/click/extras/control/TabbedForm.java?rev=1054228&r1=1054227&r2=1054228&view=diff
==============================================================================
--- click/trunk/click/extras/src/org/apache/click/extras/control/TabbedForm.java (original)
+++ click/trunk/click/extras/src/org/apache/click/extras/control/TabbedForm.java Sat Jan 
1 12:54:59 2011
@@ -22,6 +22,7 @@ import java.util.ArrayList;
 import java.util.HashMap;
 import java.util.List;
 import java.util.Map;
+import java.util.StringTokenizer;
 import org.apache.click.Context;
 
 import org.apache.click.control.Field;
@@ -29,6 +30,7 @@ import org.apache.click.control.FieldSet
 import org.apache.click.control.Form;
 import org.apache.click.element.CssImport;
 import org.apache.click.element.Element;
+import org.apache.click.element.JsImport;
 import org.apache.click.util.ClickUtils;
 import org.apache.click.util.HtmlStringBuffer;
 
@@ -243,9 +245,10 @@ public class TabbedForm extends Form {
     }
 
     /**
-     * Return the TabbedForm HTML HEAD elements for the following resource:
+     * Return the TabbedForm HTML HEAD elements for the following resources:
      *
      * <ul>
+     * <li><tt>click/extras-control.js</tt></li>
      * <li><tt>click/extras-control.css</tt></li>
      * </ul>
      *
@@ -265,6 +268,7 @@ public class TabbedForm extends Form {
             String versionIndicator = ClickUtils.getResourceVersionIndicator(context);
 
             headElements.add(new CssImport("/click/extras-control.css", versionIndicator));
+            headElements.add(new JsImport("/click/extras-control.js", versionIndicator));
         }
         return headElements;
     }
@@ -406,4 +410,60 @@ public class TabbedForm extends Form {
         return super.toString();
     }
 
+    @Override
+    protected void renderValidationJavaScript(HtmlStringBuffer buffer, List<Field>
formFields) {
+
+        // Render JavaScript form validation code
+        if (isJavaScriptValidation()) {
+            List<String> functionNames = new ArrayList<String>();
+
+            buffer.append("<script type=\"text/javascript\"><!--\n");
+
+            // Render field validation functions & build list of function names
+            for (Field field : formFields) {
+                String fieldJS = field.getValidationJavaScript();
+                if (fieldJS != null) {
+                    buffer.append(fieldJS);
+
+                    StringTokenizer tokenizer = new StringTokenizer(fieldJS);
+                    tokenizer.nextToken();
+                    functionNames.add(tokenizer.nextToken());
+                }
+            }
+
+            if (!functionNames.isEmpty()) {
+                buffer.append("function on_");
+                buffer.append(getId());
+                buffer.append("_submit() {\n");
+                buffer.append("   var msgs = new Array(");
+                buffer.append(functionNames.size());
+                buffer.append(");\n");
+                for (int i = 0; i < functionNames.size(); i++) {
+                    buffer.append("   msgs[");
+                    buffer.append(i);
+                    buffer.append("] = ");
+                    buffer.append(functionNames.get(i).toString());
+                    buffer.append(";\n");
+                }
+                buffer.append("   return Click.validateTabbedForm(msgs, '");
+                buffer.append(getId());
+                buffer.append("', '");
+                buffer.append(getErrorsAlign());
+                buffer.append("', ");
+                if (getErrorsStyle() == null) {
+                    buffer.append("null");
+                } else {
+                    buffer.append("'" + getErrorsStyle() + "'");
+                }
+                buffer.append(");\n");
+                buffer.append("}\n");
+
+            } else {
+                buffer.append("function on_");
+                buffer.append(getId());
+                buffer.append("_submit() { return true; }\n");
+            }
+            buffer.append("//--></script>\n");
+        }
+    }
 }



Mime
View raw message