Added "Color" field and widget ZTK-1.1
authorThierry Florac <tflorac@ulthar.net>
Fri, 06 Apr 2012 19:09:59 +0200
branchZTK-1.1
changeset 140 d5e916191cf4
parent 139 6478aac3b2bd
child 141 69278ff56498
Added "Color" field and widget
setup.py
ztfy.utils.egg-info/PKG-INFO
ztfy.utils.egg-info/SOURCES.txt
ztfy/utils/browser/color.py
ztfy/utils/browser/configure.zcml
ztfy/utils/browser/templates/color_display.pt
ztfy/utils/browser/templates/color_input.pt
ztfy/utils/schema.py
--- a/setup.py	Mon Apr 02 09:08:09 2012 +0200
+++ b/setup.py	Fri Apr 06 19:09:59 2012 +0200
@@ -85,6 +85,7 @@
           'zope.session',
           'zope.tales',
           'zopyx.txng3.core',
+          'ztfy.jqueryui >= 0.5.6',
       ],
       entry_points="""
       # -*- Entry points: -*-
--- a/ztfy.utils.egg-info/PKG-INFO	Mon Apr 02 09:08:09 2012 +0200
+++ b/ztfy.utils.egg-info/PKG-INFO	Fri Apr 06 19:09:59 2012 +0200
@@ -1,6 +1,6 @@
 Metadata-Version: 1.0
 Name: ztfy.utils
-Version: 0.3.9
+Version: 0.3.10
 Summary: ZTFY utility functions and classes for Zope3
 Home-page: http://www.ztfy.org
 Author: Thierry Florac
@@ -41,6 +41,10 @@
         Changelog
         =========
         
+        0.3.10
+        ------
+         - added StringLine schema field
+        
         0.3.9
         -----
          - added HTTP client based on httplib2, handling authentication and proxies
--- a/ztfy.utils.egg-info/SOURCES.txt	Mon Apr 02 09:08:09 2012 +0200
+++ b/ztfy.utils.egg-info/SOURCES.txt	Fri Apr 06 19:09:59 2012 +0200
@@ -27,7 +27,9 @@
 ztfy/utils/interfaces.py
 ztfy/utils/overrides.zcml
 ztfy/utils/profilehooks.py
+ztfy/utils/property.py
 ztfy/utils/request.py
+ztfy/utils/schema.py
 ztfy/utils/security.py
 ztfy/utils/session.py
 ztfy/utils/site.py
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ztfy/utils/browser/color.py	Fri Apr 06 19:09:59 2012 +0200
@@ -0,0 +1,53 @@
+### -*- 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 ITextWidget, IFieldWidget, IFormLayer
+
+# import local interfaces
+from ztfy.utils.schema import IColorField
+
+# import Zope3 packages
+from z3c.form.browser.text import TextWidget
+from z3c.form.widget import FieldWidget
+from zope.component import adapter
+from zope.interface import implementer, implementsOnly
+
+# import local packages
+from ztfy.jqueryui.browser import jquery_colorpicker
+
+
+class IColorWidget(ITextWidget):
+    """Color widget interface"""
+
+
+class ColorWidget(TextWidget):
+    """Color widget"""
+
+    implementsOnly(IColorWidget)
+
+    def update(self):
+        TextWidget.update(self)
+        jquery_colorpicker.need()
+
+
+@adapter(IColorField, IFormLayer)
+@implementer(IFieldWidget)
+def ColorFieldWidgetFactory(field, request):
+    """IColorField widget factory"""
+    return FieldWidget(field, ColorWidget(request))
--- a/ztfy/utils/browser/configure.zcml	Mon Apr 02 09:08:09 2012 +0200
+++ b/ztfy/utils/browser/configure.zcml	Fri Apr 06 19:09:59 2012 +0200
@@ -1,5 +1,6 @@
 <configure
 	xmlns="http://namespaces.zope.org/zope"
+	xmlns:z3c="http://namespaces.zope.org/z3c"
 	i18n_domain="ztfy.i18n">
 
 	<!-- Encoding selection widget -->
@@ -12,4 +13,26 @@
 			permission="zope.Public" />
 	</class>
 
+	<!-- Color selection widget -->
+	<adapter
+		factory=".color.ColorFieldWidgetFactory" />
+
+	<class class=".color.ColorWidget">
+		<require
+			interface=".color.IColorWidget"
+			permission="zope.Public" />
+	</class>
+
+	<z3c:widgetTemplate
+		mode="input"
+		template="templates/color_input.pt"
+		widget=".color.ColorWidget"
+		layer="z3c.form.interfaces.IFormLayer" />
+
+	<z3c:widgetTemplate
+		mode="display"
+		template="templates/color_display.pt"
+		widget=".color.ColorWidget"
+		layer="z3c.form.interfaces.IFormLayer" />
+
 </configure>
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ztfy/utils/browser/templates/color_display.pt	Fri Apr 06 19:09:59 2012 +0200
@@ -0,0 +1,14 @@
+<div class="colorSelector"
+	 tal:attributes="id string:${view/id}_selector">
+	<input type="hidden"
+		   tal:attributes="id view/id;
+						   name view/name;
+						   lang view/lang;
+						   value view/value;" />
+	<div></div>
+	<script type="text/javascript" tal:content="string:
+		$$(document).ready(function() {
+			$$('DIV[id=${view/id}_selector] div').css('backgroundColor', '#${view/value}');
+		});
+	"></script>
+</div>
\ No newline at end of file
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/ztfy/utils/browser/templates/color_input.pt	Fri Apr 06 19:09:59 2012 +0200
@@ -0,0 +1,32 @@
+<div class="colorSelector"
+	 tal:attributes="id string:${view/id}_selector">
+	<input type="hidden"
+		   tal:attributes="id view/id;
+						   name view/name;
+						   lang view/lang;
+						   value view/value;" />
+	<div></div>
+	<script type="text/javascript" tal:content="string:
+		$$(document).ready(function() {
+			$$('DIV[id=${view/id}_selector] div').css('backgroundColor', '#${view/value}');
+			$$('DIV[id=${view/id}_selector]').ColorPicker({
+				color: '#${view/value}',
+				onShow: function (colpkr) {
+					$$(colpkr).fadeIn(500);
+					return false;
+				},
+				onHide: function (colpkr) {
+					$$(colpkr).fadeOut(500);
+					return false;
+				},
+				onChange: function (hsb, hex, rgb) {
+					$$('DIV[id=${view/id}_selector] div').css('backgroundColor', '#' + hex);
+					$$('INPUT[id=${view/id}]').val(hex);
+				},
+				onSubmit: function() {
+					$$('DIV[id=${view/id}_selector]').ColorPickerHide();
+				}
+			});
+		});
+	"></script>
+</div>
\ No newline at end of file
--- a/ztfy/utils/schema.py	Mon Apr 02 09:08:09 2012 +0200
+++ b/ztfy/utils/schema.py	Fri Apr 06 19:09:59 2012 +0200
@@ -15,16 +15,22 @@
 
 
 # import standard packages
+import string
 
 # import Zope3 interfaces
+from zope.schema.interfaces import ITextLine
 
 # import local interfaces
 
 # import Zope3 packages
+from zope.interface import implements
 from zope.schema import TextLine
+from zope.schema._bootstrapfields import InvalidValue
 
 # import local packages
 
+from ztfy.utils import _
+
 
 class StringLine(TextLine):
     """String line field"""
@@ -33,3 +39,24 @@
 
     def fromUnicode(self, value):
         return str(value)
+
+
+class IColorField(ITextLine):
+    """Marker interface for color fields"""
+
+
+class ColorField(TextLine):
+    """Color field"""
+
+    implements(IColorField)
+
+    def __init__(self, *args, **kw):
+        super(ColorField, self).__init__(max_length=6, *args, **kw)
+
+    def _validate(self, value):
+        if len(value) not in (3, 6):
+            raise InvalidValue, _("Color length must be 3 or 6 characters")
+        for v in value:
+            if v not in string.hexdigits:
+                raise InvalidValue, _("Color value must contain only valid color codes (numbers or letters between 'A' end 'F')")
+        super(ColorField, self)._validate(value)