[Zope3-checkins] CVS: Zope3/src/zope/app/browser/form - widget.py:1.29.4.6

Fred L. Drake, Jr. fred@zope.com
Tue, 6 May 2003 12:17:20 -0400


Update of /cvs-repository/Zope3/src/zope/app/browser/form
In directory cvs.zope.org:/tmp/cvs-serv28666

Modified Files:
      Tag: schema-vocabulary-branch
	widget.py 
Log Message:
- added a display widget for multi-selection vocabulary fields
- added hidden() support for single- and multi-select edit fields for
  vocabularies


=== Zope3/src/zope/app/browser/form/widget.py 1.29.4.5 => 1.29.4.6 ===
--- Zope3/src/zope/app/browser/form/widget.py:1.29.4.5	Mon May  5 16:11:32 2003
+++ Zope3/src/zope/app/browser/form/widget.py	Tue May  6 12:17:19 2003
@@ -25,6 +25,7 @@
 from zope.app.interfaces.form import MissingInputError
 from zope.app.datetimeutils import parseDatetimetz
 from zope.app.datetimeutils import DateTimeError
+from zope.app.i18n import ZopeMessageIDFactory as _
 from zope.schema.interfaces import ValidationError
 from zope.component import getService, getView
 
@@ -335,8 +336,7 @@
     default = ""
     width = 60
     height = 15
-    extra=""
-    #style="width:100%"
+    extra = ""
     style = ''
 
     def _convert(self, value):
@@ -725,6 +725,9 @@
 class VocabularyWidgetBase(BrowserWidget):
     """Convenience base class for vocabulary-based widgets."""
 
+    propertyNames = BrowserWidget.propertyNames + ["extra"]
+
+    extra = ""
     type = "vocabulary"
 
     def __init__(self, context, request):
@@ -771,8 +774,7 @@
 
 class VocabularyEditWidgetBase(VocabularyWidgetBase):
     propertyNames = (VocabularyWidgetBase.propertyNames
-                     + ['extra', 'size', 'tag'])
-    extra = ''
+                     + ['size', 'tag'])
     size = 5
     tag = 'select'
 
@@ -824,8 +826,7 @@
     to be very large.
     """
     __implements__ = SingleItemsWidget.__implements__
-    propertyNames = (VocabularyEditWidgetBase.propertyNames
-                     + ['firstItem'])
+    propertyNames = VocabularyEditWidgetBase.propertyNames + ['firstItem']
     firstItem = False
 
     def render(self, value):
@@ -855,6 +856,54 @@
 
         return VocabularyEditWidgetBase.renderItemsWithValues(self, values)
 
+    def hidden(self):
+        return renderElement('input',
+                             type = 'hidden',
+                             name = self.name,
+                             id = self.name,
+                             value = self._showData())
+
+
+class VocabularyMultiDisplayWidget(VocabularyWidgetBase):
+
+    propertyNames = (VocabularyWidgetBase.propertyNames
+                     + ['itemTag', 'tag'])
+
+    itemTag = 'li'
+    tag = 'ol'
+
+    def render(self, value):
+        if value == self._missing:
+            return renderElement('span',
+                                 type = self.getValue('type'),
+                                 name = self.name,
+                                 id = self.name,
+                                 cssClass = self.getValue('cssClass'),
+                                 contents = _("(no values)"),
+                                 extra = self.getValue('extra'))
+        else:
+            rendered_items = self.renderItems(value)
+            return renderElement(self.getValue('tag'),
+                                 type = self.getValue('type'),
+                                 name = self.name,
+                                 id = self.name,
+                                 cssClass = self.getValue('cssClass'),
+                                 contents = "\n".join(rendered_items),
+                                 extra = self.getValue('extra'))
+
+    def renderItems(self, value):
+        L = []
+        vocabulary = self.context
+        cssClass = self.getValue('cssClass') or ''
+        if cssClass:
+            cssClass += "-item"
+        tag = self.getValue('itemTag')
+        for v in value:
+            term = vocabulary.getTerm(v)
+            L.append(renderElement(tag,
+                                   cssClass = cssClass,
+                                   contents = self.textForValue(term)))
+        return L
 
 class VocabularyMultiEditWidget(VocabularyEditWidgetBase):
     """Vocabulary-backed widget supporting multiple selections."""
@@ -880,6 +929,17 @@
                              contents = "\n".join(rendered_items),
                              extra = self.getValue('extra'))
 
+    def hidden(self):
+        L = []
+        for v in self._showData():
+            s = renderElement('input',
+                              type = 'hidden',
+                              name = self.name + ':list',
+                              value = v)
+            assert s[-1] == '>'
+            L.append(s[:-1])
+            L.append('\n>')
+        return ''.join(L)
 
 # XXX Note, some HTML quoting is needed in renderTag and renderElement.