[Checkins] SVN: Sandbox/pcardune/z3cFormJS/trunk/src/z3c/formjs/ more work on actual implementation. still not finished.

Paul Carduner paulcarduner at gmail.com
Wed Jun 6 13:56:45 EDT 2007


Log message for revision 76441:
  more work on actual implementation.  still not finished.

Changed:
  U   Sandbox/pcardune/z3cFormJS/trunk/src/z3c/formjs/README.txt
  A   Sandbox/pcardune/z3cFormJS/trunk/src/z3c/formjs/browser/
  A   Sandbox/pcardune/z3cFormJS/trunk/src/z3c/formjs/browser/__init__.py
  A   Sandbox/pcardune/z3cFormJS/trunk/src/z3c/formjs/browser/button_input.pt
  U   Sandbox/pcardune/z3cFormJS/trunk/src/z3c/formjs/interfaces.py
  A   Sandbox/pcardune/z3cFormJS/trunk/src/z3c/formjs/jsbutton.py
  U   Sandbox/pcardune/z3cFormJS/trunk/src/z3c/formjs/testing.py
  U   Sandbox/pcardune/z3cFormJS/trunk/src/z3c/formjs/tests.py

-=-
Modified: Sandbox/pcardune/z3cFormJS/trunk/src/z3c/formjs/README.txt
===================================================================
--- Sandbox/pcardune/z3cFormJS/trunk/src/z3c/formjs/README.txt	2007-06-06 17:19:40 UTC (rev 76440)
+++ Sandbox/pcardune/z3cFormJS/trunk/src/z3c/formjs/README.txt	2007-06-06 17:56:44 UTC (rev 76441)
@@ -150,7 +150,7 @@
 Next we want to display our button actions. To be able to do this, we have to
 register a template for the button widget:
 
-  >>> from z3c.formjs import testing as jstesting,
+  >>> from z3c.formjs import testing as jstesting
   >>> from z3c.form import widget
   >>> templatePath = jstesting.getPath('button_input.pt')
   >>> factory = widget.WidgetTemplateFactory(templatePath, 'text/html')
@@ -166,13 +166,13 @@
 
   >>> print actions['apply'].render()
   <input type="button" id="form.buttons.apply"
-         name="form.buttons.apply" class="submitWidget"
+         name="form.buttons.apply" class="buttonWidget"
          value="Apply"
          onClick="alert("You Clicked the Apply Button!");"/>
 
   >>> print actions['cancel'].render()
   <input type="button" id="form.buttons.apply"
-         name="form.buttons.apply" class="submitWidget"
+         name="form.buttons.apply" class="buttonWidget"
          value="Apply"
          onDblClick="alert("You Double Clicked the Cancel Button!");"/>
 

Added: Sandbox/pcardune/z3cFormJS/trunk/src/z3c/formjs/browser/__init__.py
===================================================================


Property changes on: Sandbox/pcardune/z3cFormJS/trunk/src/z3c/formjs/browser/__init__.py
___________________________________________________________________
Name: svn:eol-style
   + native

Added: Sandbox/pcardune/z3cFormJS/trunk/src/z3c/formjs/browser/button_input.pt
===================================================================
--- Sandbox/pcardune/z3cFormJS/trunk/src/z3c/formjs/browser/button_input.pt	                        (rev 0)
+++ Sandbox/pcardune/z3cFormJS/trunk/src/z3c/formjs/browser/button_input.pt	2007-06-06 17:56:44 UTC (rev 76441)
@@ -0,0 +1,11 @@
+<input type="button" id="" name="" class="" value=""
+       accesskey=""
+       tal:attributes="id view/id;
+                       name view/name;
+                       class view/css;
+                       value view/value;
+                       accesskey view/accesskey;
+                       " />
+<script type="text/javascript"
+	tal:content="structure view/eventHandler">
+</script>
\ No newline at end of file


Property changes on: Sandbox/pcardune/z3cFormJS/trunk/src/z3c/formjs/browser/button_input.pt
___________________________________________________________________
Name: svn:eol-style
   + native

Modified: Sandbox/pcardune/z3cFormJS/trunk/src/z3c/formjs/interfaces.py
===================================================================
--- Sandbox/pcardune/z3cFormJS/trunk/src/z3c/formjs/interfaces.py	2007-06-06 17:19:40 UTC (rev 76440)
+++ Sandbox/pcardune/z3cFormJS/trunk/src/z3c/formjs/interfaces.py	2007-06-06 17:56:44 UTC (rev 76441)
@@ -21,6 +21,7 @@
 from zope.interface import Interface
 from zope import schema
 
+from z3c.form.interfaces import IButton, IButtonHandler, IManager, IWidget
 
 class IJSEvent(Interface):
     """A marker interface for javascript event objects."""
@@ -45,3 +46,37 @@
         """render javascript to link DOM element with given id to the
         code produced by the given handler.
         """
+
+
+class IJSButton(IButton):
+    """A button that just connects to javascript handlers."""
+
+
+class IButtonWidget(IWidget):
+    """Button widget."""
+
+
+class IJSButtonHandler(IButtonHandler):
+    """A button handler for javascript buttons."""
+
+
+class IJSAction(Interface):
+    """Action"""
+
+    __name__ = schema.TextLine(
+        title=u'Name',
+        description=u'The object name.',
+        required=False,
+        default=None)
+
+    title = schema.TextLine(
+        title=u'Title',
+        description=u'The action title.',
+        required=True)
+
+
+class IJSActions(IManager):
+    """A action manager"""
+
+    def update():
+        """Setup actions."""

Added: Sandbox/pcardune/z3cFormJS/trunk/src/z3c/formjs/jsbutton.py
===================================================================
--- Sandbox/pcardune/z3cFormJS/trunk/src/z3c/formjs/jsbutton.py	                        (rev 0)
+++ Sandbox/pcardune/z3cFormJS/trunk/src/z3c/formjs/jsbutton.py	2007-06-06 17:56:44 UTC (rev 76441)
@@ -0,0 +1,169 @@
+##############################################################################
+#
+# Copyright (c) 2007 Zope Foundation and Contributors.
+# 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.
+#
+##############################################################################
+"""Javascript Form Framework Button Framework.
+
+$Id: $
+"""
+__docformat__ = "reStructuredText"
+
+import sys
+
+import zope.schema
+import zope.interface
+import zope.location
+
+from z3c.form import button, util, action, widget
+from z3c.form.interfaces import (IButton, IFieldWidget, IValue,
+                            IButtonHandlers, IFormLayer, IButtonForm)
+
+import interfaces
+
+
+class ButtonWidget(widget.Widget):
+    """A submit button of a form."""
+    zope.interface.implementsOnly(interfaces.IButtonWidget)
+
+    css = u'buttonWidget'
+    accesskey = None
+
+ at zope.component.adapter(IButton, IFormLayer)
+ at zope.interface.implementer(IFieldWidget)
+def SubmitFieldWidget(field, request):
+    submit = widget.FieldWidget(field, SubmitWidget(request))
+    submit.value = field.title
+    return submit
+
+
+class JSButton(button.Button):
+    """A simple javascript button in a form."""
+    zope.interface.implements(interfaces.IJSButton)
+
+
+class Handlers(button.Handlers):
+    """Event Handlers for Javascript Buttons."""
+
+    def addHandler(self, button, handler):
+        """See z3c.form.interfaces.IButtonHandlers"""
+        # Create a specification for the button
+        buttonSpec = util.getSpecification(button)
+        if isinstance(buttonSpec, util.classTypes):
+            buttonSpec = zope.interface.implementedBy(buttonSpec)
+        # Register the handler
+        self._registry.register(
+            (buttonSpec,), interfaces.IJSButtonHandler, '', handler)
+        self._handlers += ((button, handler),)
+
+    def getHandler(self, button):
+        """See z3c.form.interfaces.IButtonHandlers"""
+        buttonProvided = zope.interface.providedBy(button)
+        return self._registry.lookup1(buttonProvided, interfaces.IJSButtonHandler)
+
+
+class Handler(object):
+    zope.interface.implements(interfaces.IJSButtonHandler)
+
+    def __init__(self, button, func):
+        self.button = button
+        self.func = func
+
+    def __call__(self):
+        return self.func()
+
+    def __repr__(self):
+        return '<%s for %r>' %(self.__class__.__name__, self.button)
+
+
+def handler(button, **kwargs):
+    """A decoratore for defining a javascript event handler."""
+    def createHandler(func):
+        handler = Handler(button, func)
+        frame = sys._getframe(1)
+        f_locals = frame.f_locals
+        jshandlers = f_locals.setdefault('jshandlers', Handlers())
+        jshandlers.addHandler(button, handler)
+        return handler
+    return createHandler
+
+
+class JSButtonAction(action.Action, ButtonWidget, zope.location.Location):
+    zope.interface.implements(IFieldWidget)
+
+    def __init__(self, request, field, name):
+        action.Action.__init__(self, request, field.title, name)
+        ButtonWidget.__init__(self, request)
+        self.field = field
+
+    @property
+    def accesskey(self):
+        return self.field.accessKey
+
+    @property
+    def value(self):
+        return self.title
+
+    @property
+    def id(self):
+        return self.name.replace('.', '-')
+
+    @property
+    def eventHandler(self):
+        actions = self.__parent__
+        handler = actions.form.handlers.getHandler(self.field)
+        if handler is None:
+            return
+        return handler()
+
+
+class JSButtonActions(util.Manager):
+    """JS Button Action Manager class."""
+    zope.interface.implementsOnly(interfaces.IJSActions)
+
+    zope.component.adapts(
+        IButtonForm,
+        zope.interface.Interface,
+        zope.interface.Interface)
+
+    __name__ = __parent__ = None
+
+    def __init__(self, form, request, content):
+        super(JSButtonActions, self).__init__()
+        self.form = form
+        self.request = request
+        self.content = content
+
+    def update(self):
+        """See z3c.form.interfaces.IActions."""
+        # Create a unique prefix
+        prefix = util.expandPrefix(self.form.prefix)
+        prefix += util.expandPrefix(self.form.buttons.prefix)
+        for name, button in self.form.buttons.items():
+            # Only create an action for the button, if the condition is
+            # fulfilled
+            if button.condition is not None and not button.condition(self.form):
+                continue
+            fullName = prefix + name
+            buttonAction = JSButtonAction(self.request, button, fullName)
+            # Look up a potential custom title for the action.
+            title = zope.component.queryMultiAdapter(
+                (self.form, self.request, self.content, button, self),
+                IValue, name='title')
+            if title is not None:
+                buttonAction.title = title.get()
+            self._data_keys.append(name)
+            self._data_values.append(buttonAction)
+            self._data[name] = buttonAction
+            zope.location.locate(buttonAction, self, name)
+
+    def __repr__(self):
+        return '<%s %r>' % (self.__class__.__name__, self.__name__)


Property changes on: Sandbox/pcardune/z3cFormJS/trunk/src/z3c/formjs/jsbutton.py
___________________________________________________________________
Name: svn:eol-style
   + native

Modified: Sandbox/pcardune/z3cFormJS/trunk/src/z3c/formjs/testing.py
===================================================================
--- Sandbox/pcardune/z3cFormJS/trunk/src/z3c/formjs/testing.py	2007-06-06 17:19:40 UTC (rev 76440)
+++ Sandbox/pcardune/z3cFormJS/trunk/src/z3c/formjs/testing.py	2007-06-06 17:56:44 UTC (rev 76441)
@@ -16,10 +16,26 @@
 $Id: $
 """
 __docformat__ = 'restructuredtext'
+
+import os.path
+
 import zope.interface
 from zope.publisher.browser import TestRequest
+from zope.app.testing import setup
 
 import jquery.layer
+from z3c.formjs import jsbutton
 
+import browser
+
 class TestRequest(TestRequest):
     zope.interface.implements(jquery.layer.IJQueryJavaScriptBrowserLayer)
+
+def getPath(filename):
+    return os.path.join(os.path.dirname(browser.__file__), filename)
+
+def setUp(test):
+    test.globs = {'root': setup.placefulSetUp(True)}
+
+def tearDown(test):
+    setup.placefulTearDown()

Modified: Sandbox/pcardune/z3cFormJS/trunk/src/z3c/formjs/tests.py
===================================================================
--- Sandbox/pcardune/z3cFormJS/trunk/src/z3c/formjs/tests.py	2007-06-06 17:19:40 UTC (rev 76440)
+++ Sandbox/pcardune/z3cFormJS/trunk/src/z3c/formjs/tests.py	2007-06-06 17:56:44 UTC (rev 76441)
@@ -1,9 +1,13 @@
 import unittest
 import zope.testing.doctest
 
+import testing
+
 def test_suite():
     return unittest.TestSuite((
-        zope.testing.doctest.DocFileSuite('README.txt',
+        zope.testing.doctest.DocFileSuite(
+            'README.txt',
+            setUp=testing.setUp, tearDown=testing.tearDown,
             optionflags=zope.testing.doctest.NORMALIZE_WHITESPACE |
                         zope.testing.doctest.ELLIPSIS),
         ))



More information about the Checkins mailing list