Added color selector widget
authorThierry Florac <thierry.florac@onf.fr>
Wed, 15 Apr 2015 15:00:56 +0200
changeset 10 674e6e01ea54
parent 9 c41a835fc348
child 11 ffebc76eaaa3
Added color selector widget
src/pyams_form/interfaces/form.py
src/pyams_form/widget/__init__.py
src/pyams_form/widget/templates/color-input.pt
--- a/src/pyams_form/interfaces/form.py	Fri Mar 20 17:29:54 2015 +0100
+++ b/src/pyams_form/interfaces/form.py	Wed Apr 15 15:00:56 2015 +0200
@@ -405,6 +405,10 @@
     """Time widget interface"""
 
 
+class IColorWidget(ITextWidget):
+    """Color widget interface"""
+
+
 #
 # Form events
 #
--- a/src/pyams_form/widget/__init__.py	Fri Mar 20 17:29:54 2015 +0100
+++ b/src/pyams_form/widget/__init__.py	Wed Apr 15 15:00:56 2015 +0200
@@ -19,10 +19,12 @@
 import venusian
 
 # import interfaces
-from pyams_form.interfaces.form import IFormLayer, IResetWidget, ICloseWidget, IDateWidget, IDatetimeWidget, ITimeWidget
+from pyams_form.interfaces.form import IFormLayer, IResetWidget, ICloseWidget, IDateWidget, IDatetimeWidget, ITimeWidget, \
+    IColorWidget
 from pyams_form.schema import IResetButton, ICloseButton
+from pyams_utils.schema import IColorField
 from pyramid.interfaces import IRequest
-from z3c.form.interfaces import INPUT_MODE, IFieldWidget, IButtonAction, IWidgetLayoutTemplate
+from z3c.form.interfaces import INPUT_MODE, IFieldWidget, IButtonAction, IWidgetLayoutTemplate, IDataConverter
 from zope.pagetemplate.interfaces import IPageTemplate
 from zope.schema.interfaces import IDate, IDatetime, ITime
 
@@ -33,6 +35,7 @@
 from z3c.form.browser.submit import SubmitWidget
 from z3c.form.browser.text import TextWidget
 from z3c.form.button import ButtonAction
+from z3c.form.converter import BaseDataConverter
 from z3c.form.widget import FieldWidget, WidgetTemplateFactory, WidgetLayoutFactory
 from zope.interface import implementer_only, directlyProvides, Interface
 
@@ -242,3 +245,34 @@
 @adapter_config(context=(ITime, IFormLayer), provides=IFieldWidget)
 def TimeFieldWidget(field, request):
     return FieldWidget(field, TimeWidget(request))
+
+
+#
+# Color widget
+#
+
+@adapter_config(context=(IColorField, IColorWidget), provides=IDataConverter)
+class ColorDataConverter(BaseDataConverter):
+    """Color field data converter"""
+
+    def toWidgetValue(self, value):
+        value = super(ColorDataConverter, self).toWidgetValue(value)
+        if value:
+            value = '#' + value
+        return value
+
+    def toFieldValue(self, value):
+        if value and value.startswith('#'):
+            value = value[1:]
+        return super(ColorDataConverter, self).toFieldValue(value)
+
+
+@widgettemplate_config(mode='input', template='templates/color-input.pt', layer=IFormLayer)
+@implementer_only(IColorWidget)
+class ColorWidget(TextWidget):
+    """Color widget"""
+
+
+@adapter_config(context=(IColorField, IFormLayer), provides=IFieldWidget)
+def ColorFieldWidget(field, request):
+    return FieldWidget(field, ColorWidget(request))
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/src/pyams_form/widget/templates/color-input.pt	Wed Apr 15 15:00:56 2015 +0200
@@ -0,0 +1,35 @@
+<tal:var i18n:domain="pyams_form">
+	<input id="" name="" class="" title="" lang="" disabled=""
+		   readonly="" alt="" tabindex="" accesskey="" size="" maxlength=""
+		   style="" value="" type="text"
+		   tal:attributes="id view/id;
+							name view/name;
+							class string:${view/klass} colorpicker;
+							style view/style;
+							title view/title;
+							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;
+							value view/value;
+							disabled view/disabled;
+							tabindex view/tabindex;
+							onfocus view/onfocus;
+							onblur view/onblur;
+							onchange view/onchange;
+							readonly view/readonly;
+							alt view/alt;
+							accesskey view/accesskey;
+							onselect view/onselect;
+							size view/size;
+							maxlength view/maxlength;
+							placeholder view/placeholder;
+							autocapitalize view/autocapitalize;" />
+</tal:var>