Added "TextLine list" schema field and widget ZTK-1.1
authorThierry Florac <tflorac@ulthar.net>
Thu, 30 Aug 2012 14:58:30 +0200
branchZTK-1.1
changeset 165 9615e7d1a4ee
parent 164 b09c98b285cc
child 166 6f4668311c9e
Added "TextLine list" schema field and widget
docs/HISTORY.txt
src/ztfy/utils/browser/configure.zcml
src/ztfy/utils/browser/templates/textlinelist_display.pt
src/ztfy/utils/browser/templates/textlinelist_input.pt
src/ztfy/utils/browser/textlinelist.py
src/ztfy/utils/schema.py
--- a/docs/HISTORY.txt	Mon Aug 20 18:54:13 2012 +0200
+++ b/docs/HISTORY.txt	Thu Aug 30 14:58:30 2012 +0200
@@ -4,6 +4,7 @@
 0.3.13
 ------
  - added "ztfy.utils.container" utility module
+ - added "TextLine list" schema field and widget
  - added Python 2.7 compatibility code and timeout parameter to XML-RPC
    protocol helper
 
--- a/src/ztfy/utils/browser/configure.zcml	Mon Aug 20 18:54:13 2012 +0200
+++ b/src/ztfy/utils/browser/configure.zcml	Thu Aug 30 14:58:30 2012 +0200
@@ -35,4 +35,29 @@
 		widget=".color.ColorWidget"
 		layer="z3c.form.interfaces.IFormLayer" />
 
+	<!-- TextLine list widget -->
+	<adapter
+		factory=".textlinelist.TextLineListDataConverter" />
+
+	<adapter
+		factory=".textlinelist.TextLineListFieldWidgetFactory" />
+
+	<class class=".textlinelist.TextLineListWidget">
+		<require
+			interface=".textlinelist.ITextLineListWidget"
+			permission="zope.Public" />
+	</class>
+
+	<z3c:widgetTemplate
+		mode="input"
+		template="templates/textlinelist_input.pt"
+		widget=".textlinelist.TextLineListWidget"
+		layer="z3c.form.interfaces.IFormLayer" />
+
+	<z3c:widgetTemplate
+		mode="display"
+		template="templates/textlinelist_display.pt"
+		widget=".textlinelist.TextLineListWidget"
+		layer="z3c.form.interfaces.IFormLayer" />
+
 </configure>
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ztfy/utils/browser/templates/textlinelist_display.pt	Thu Aug 30 14:58:30 2012 +0200
@@ -0,0 +1,34 @@
+<input type="text"
+	tal:attributes="id view/id;
+					name view/name;
+					class view/klass;
+					style view/style;
+					title view/title;
+					value view/value;
+					lang view/lang;
+					onclick view/onclick;
+					ondblclick view/ondblclick;
+					onmousedown view/onmousedown;
+					onmouseup view/onmouseup;
+					onmouseover view/onmouseover;
+					onmousemove view/onmousemove;
+					onmouseout view/onmouseout;
+					onkeypress view/onkeypress;
+					onkeydown view/onkeydown;
+					onkeyup view/onkeyup;
+					disabled view/disabled;
+					tabindex view/tabindex;
+					onfocus view/onfocus;
+					onblur view/onblur;
+					onchange view/onchange;
+					readonly view/readonly;
+					accesskey view/accesskey;
+					onselect view/onselect;" />
+<script type="text/javascript" tal:content="string:
+	$$().ready(function() {
+		$$('INPUT[id=${view/id}]').multiselect({
+			separator: '|',
+			readonly: true
+		});
+	});">
+</script>
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ztfy/utils/browser/templates/textlinelist_input.pt	Thu Aug 30 14:58:30 2012 +0200
@@ -0,0 +1,39 @@
+<input type="text"
+	tal:attributes="id view/id;
+					name view/name;
+					class view/klass;
+					style view/style;
+					title view/title;
+					value view/value;
+					lang view/lang;
+					onclick view/onclick;
+					ondblclick view/ondblclick;
+					onmousedown view/onmousedown;
+					onmouseup view/onmouseup;
+					onmouseover view/onmouseover;
+					onmousemove view/onmousemove;
+					onmouseout view/onmouseout;
+					onkeypress view/onkeypress;
+					onkeydown view/onkeydown;
+					onkeyup view/onkeyup;
+					disabled view/disabled;
+					tabindex view/tabindex;
+					onfocus view/onfocus;
+					onblur view/onblur;
+					onchange view/onchange;
+					readonly view/readonly;
+					accesskey view/accesskey;
+					onselect view/onselect;" />
+<input name="field-empty-marker" type="hidden" value="1"
+	   tal:attributes="name string:${view/name}-empty-marker" />
+<script type="text/javascript" tal:content="string:
+	$$().ready(function() {
+		$$('INPUT[id=${view/id}]').multiselect({
+			separator: '|',
+			max_complete_results: 0,
+			enable_new_options: true,
+			complex_search: false,
+			input_class: 'directory'
+		});
+	});">
+</script>
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/ztfy/utils/browser/textlinelist.py	Thu Aug 30 14:58:30 2012 +0200
@@ -0,0 +1,69 @@
+### -*- coding: utf-8 -*- ####################################################
+##############################################################################
+#
+# Copyright (c) 2012 Thierry Florac <tflorac AT ulthar.net>
+# All Rights Reserved.
+#
+# This software is subject to the provisions of the Zope Public License,
+# Version 2.1 (ZPL).  A copy of the ZPL should accompany this distribution.
+# THIS SOFTWARE IS PROVIDED "AS IS" AND ANY AND ALL EXPRESS OR IMPLIED
+# WARRANTIES ARE DISCLAIMED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
+# WARRANTIES OF TITLE, MERCHANTABILITY, AGAINST INFRINGEMENT, AND FITNESS
+# FOR A PARTICULAR PURPOSE.
+#
+##############################################################################
+
+
+# import standard packages
+
+# import Zope3 interfaces
+from z3c.form.interfaces import IWidget, IFieldWidget, ITextWidget, IFormLayer
+
+# import local interfaces
+from ztfy.utils.schema import ITextLineListField
+
+# import Zope3 packages
+from z3c.form.browser.text import TextWidget
+from z3c.form.converter import SequenceDataConverter
+from z3c.form.widget import FieldWidget
+from zope.component import adapter, adapts
+from zope.interface import implementer, implementsOnly
+
+# import local packages
+from ztfy.jqueryui import jquery_multiselect
+
+
+class ITextLineListWidget(ITextWidget):
+    """TextLineList widget interface"""
+
+
+class TextLineListDataConverter(SequenceDataConverter):
+    """TextLineList field data converter"""
+
+    adapts(ITextLineListField, IWidget)
+
+    def toWidgetValue(self, value):
+        if value is self.field.missing_value:
+            return []
+        return '|'.join(value)
+
+    def toFieldValue(self, value):
+        if not value:
+            return self.field.missing_value
+        return value.split('|')
+
+
+class TextLineListWidget(TextWidget):
+    """TextLineList field widget"""
+
+    implementsOnly(ITextLineListWidget)
+
+    def render(self):
+        jquery_multiselect.need()
+        return super(TextLineListWidget, self).render()
+
+
+@adapter(ITextLineListField, IFormLayer)
+@implementer(IFieldWidget)
+def TextLineListFieldWidgetFactory(field, request):
+    return FieldWidget(field, TextLineListWidget(request))
--- a/src/ztfy/utils/schema.py	Mon Aug 20 18:54:13 2012 +0200
+++ b/src/ztfy/utils/schema.py	Thu Aug 30 14:58:30 2012 +0200
@@ -20,7 +20,7 @@
 
 # import Zope3 interfaces
 from z3c.form.interfaces import IWidget
-from zope.schema.interfaces import ITextLine, IDecimal
+from zope.schema.interfaces import ITextLine, IDecimal, IList
 
 # import local interfaces
 
@@ -28,7 +28,7 @@
 from z3c.form.converter import BaseDataConverter, FormatterValidationError
 from zope.component import adapts
 from zope.interface import implements
-from zope.schema import TextLine, Decimal
+from zope.schema import TextLine, Decimal, List
 from zope.schema._bootstrapfields import InvalidValue
 
 # import local packages
@@ -108,3 +108,20 @@
             return decimal.Decimal(value)
         except decimal.InvalidOperation:
             raise FormatterValidationError(self.errorMessage, value)
+
+
+#
+# TextLine list field
+#
+
+class ITextLineListField(IList):
+    """Marker interface for textline list field"""
+
+
+class TextLineListField(List):
+    """TextLine list field"""
+
+    implements(ITextLineListField)
+
+    def __init__(self, value_type=None, unique=False, **kw):
+        super(TextLineListField, self).__init__(value_type=TextLine(), unique=True, **kw)