[Zope3-checkins] SVN: Zope3/trunk/src/zope/formlib/ Fix #691: make formlib helptext displaying more robust; toggle widget div and deal with complex widgets by searching for form element name prefixes

Martijn Pieters mj at zopatista.com
Thu Aug 24 10:20:56 EDT 2006


Log message for revision 69763:
  Fix #691: make formlib helptext displaying more robust; toggle widget div and deal with complex widgets by searching for form element name prefixes

Changed:
  U   Zope3/trunk/src/zope/formlib/pageform.pt
  U   Zope3/trunk/src/zope/formlib/subpageform.pt

-=-
Modified: Zope3/trunk/src/zope/formlib/pageform.pt
===================================================================
--- Zope3/trunk/src/zope/formlib/pageform.pt	2006-08-24 13:52:30 UTC (rev 69762)
+++ Zope3/trunk/src/zope/formlib/pageform.pt	2006-08-24 14:20:56 UTC (rev 69763)
@@ -17,18 +17,9 @@
 
 function toggleFormFieldHelp(ob,state) {
   // ob is the label element
-  var field = ob.form[ob.htmlFor];
+  var field = findWidgetDiv(ob);
   if (field) {
-    var viz = state && 'hidden' || 'visible';
-    if (field.length == null) {
-      field.style.visibility = viz;
-    }
-    else {
-      for (var i = 0; i < field.length; ++i) {
-        var e = field.item(i);
-        e.style.visibility = viz;
-      }
-    }
+    field.style.visibility = state && 'hidden' || 'visible';
     var help = document.getElementById("field-help-for-" + ob.htmlFor);
     if (help) {
       help.style.visibility = state && 'visible' || 'hidden';
@@ -36,6 +27,31 @@
   }
 }
 
+function findWidgetDiv(label) {
+  var element = findFormField(label);
+  while (element) {
+    element = element.parentNode;
+    if (element.tagName == 'DIV' && element.getAttribute('class') == 'widget')
+      return element;
+  }
+}
+
+function findFormField(label) {
+  var name = label.htmlFor;
+  var field = label.form[name];
+  // Multiple fields with the same name, such as radiobuttons
+  if (field) {
+    if (field.length)
+      field = field[0];
+    return field;
+  }
+  // No field with the exact name; find one that starts with the name
+  for (var i = 0; field = label.form[i++];) {
+    if (field.name.substr(0, name.length) == name)
+      return field;
+  }
+} 
+
 //-->
 </script>
 

Modified: Zope3/trunk/src/zope/formlib/subpageform.pt
===================================================================
--- Zope3/trunk/src/zope/formlib/subpageform.pt	2006-08-24 13:52:30 UTC (rev 69762)
+++ Zope3/trunk/src/zope/formlib/subpageform.pt	2006-08-24 14:20:56 UTC (rev 69763)
@@ -5,25 +5,41 @@
 
 function toggleFormFieldHelp(ob,state) {
   // ob is the label element
-  var field = ob.form[ob.htmlFor];
+  var field = findWidgetDiv(ob);
   if (field) {
-    var viz = state && 'hidden' || 'visible';
-    if (field.length == null) {
-      field.style.visibility = viz;
-    }
-    else {
-      for (var i = 0; i < field.length; ++i) {
-        var e = field.item(i);
-        e.style.visibility = viz;
-      }
-    }
-    var help = document.getElementById("field-help-for-" + field.name);
+    field.style.visibility = state && 'hidden' || 'visible';
+    var help = document.getElementById("field-help-for-" + ob.htmlFor);
     if (help) {
       help.style.visibility = state && 'visible' || 'hidden';
     }
   }
 }
 
+function findWidgetDiv(label) {
+  var element = findFormField(label);
+  while (element) {
+    element = element.parentNode;
+    if (element.tagName == 'DIV' && element.getAttribute('class') == 'widget')
+      return element;
+  }
+}
+
+function findFormField(label) {
+  var name = label.htmlFor;
+  var field = label.form[name];
+  // Multiple fields with the same name, such as radiobuttons
+  if (field) {
+    if (field.length)
+      field = field[0];
+    return field;
+  }
+  // No field with the exact name; find one that starts with the name
+  for (var i = 0; field = label.form[i++];) {
+    if (field.name.substr(0, name.length) == name)
+      return field;
+  }
+} 
+
 //-->
 </script>
 



More information about the Zope3-Checkins mailing list