[Checkins] SVN: Sandbox/pcardune/z3c.formextjs/src/z3c/formextjs/ Start working on how to reuse IPageTemplate adapters to generate the

Paul Carduner paulcarduner at gmail.com
Sun Apr 20 22:09:48 EDT 2008


Log message for revision 85521:
  
  Start working on how to reuse IPageTemplate adapters to generate the
  right javascript for different fields.  Now have TextArea widgets working.
  

Changed:
  U   Sandbox/pcardune/z3c.formextjs/src/z3c/formextjs/browser/__init__.py
  U   Sandbox/pcardune/z3c.formextjs/src/z3c/formextjs/browser/configure.zcml
  U   Sandbox/pcardune/z3c.formextjs/src/z3c/formextjs/browser/form-widgets-viewlet.pyt
  U   Sandbox/pcardune/z3c.formextjs/src/z3c/formextjs/browser/text_input.pt
  U   Sandbox/pcardune/z3c.formextjs/src/z3c/formextjs/div-form.pt

-=-
Modified: Sandbox/pcardune/z3c.formextjs/src/z3c/formextjs/browser/__init__.py
===================================================================
--- Sandbox/pcardune/z3c.formextjs/src/z3c/formextjs/browser/__init__.py	2008-04-21 00:46:06 UTC (rev 85520)
+++ Sandbox/pcardune/z3c.formextjs/src/z3c/formextjs/browser/__init__.py	2008-04-21 02:09:47 UTC (rev 85521)
@@ -1,44 +1,34 @@
-#extjs widgets
-import os.path, sys
+##############################################################################
+#
+# 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.
+#
+##############################################################################
+"""ExtJS Form Widgets
+
+$Id$
+"""
 from zope.viewlet.interfaces import IViewletManager
 from zope.viewlet.viewlet import ViewletBase
 from zope.viewlet.manager import WeightOrderedViewletManager
 
+from z3c.formextjs.browser.pytemplate import PythonTemplateFile
 
-def package_home(gdict):
-    filename = gdict["__file__"]
-    return os.path.dirname(filename)
-
-class PythonTemplateFile(object):
-
-    def __init__(self, filename, _prefix=None):
-        path = self.get_path_from_prefix(_prefix)
-        self.filename = os.path.join(path, filename)
-        if not os.path.isfile(self.filename):
-            raise ValueError("No such file", self.filename)
-
-    def get_path_from_prefix(self, _prefix):
-        if isinstance(_prefix, str):
-            path = _prefix
-        else:
-            if _prefix is None:
-                _prefix = sys._getframe(2).f_globals
-            path = package_home(_prefix)
-        return path
-
-    def __call__(self, context, **kwargs):
-        inFile = open(self.filename, 'rb')
-        scope = {'r':'', 'context':context}
-        scope.update(kwargs)
-        exec inFile in scope
-        return scope['r']
-
-
 def escape(s):
     s = str(s).replace('\n','\\n').replace("'","\\'")
     return s
 
+def jsSafe(name):
+    return str(hash(name)).replace('-','_')
 
+
 class IJSFormViewletManager(IViewletManager):
     """js form viewlet manager for rendering form generating javascript."""
 
@@ -78,6 +68,26 @@
         return self.template(self, **globals())
 
 
+class InputWidgetTemplate(object):
+    """provider of IPageTemplate for extjs widgets."""
+
+    template = PythonTemplateFile('form-widget.pyt')
+
+    def __init__(self, context, request, form, field, widget):
+        self.context = context
+        self.request = request
+        self.form = form
+        self.field = field
+        self.widget = widget
+
+    def __call__(self, widget):
+        scope = globals()
+        return self.template(widget, **scope)
+
+class TextAreaInputWidget(InputWidgetTemplate):
+
+    template = PythonTemplateFile('text-area-widget.pyt')
+
 ## class ExtJSButtonsViewlet(FormAwareViewlet):
 ##     """Viewlet that add JS hooks for displaying buttons."""
 

Modified: Sandbox/pcardune/z3c.formextjs/src/z3c/formextjs/browser/configure.zcml
===================================================================
--- Sandbox/pcardune/z3c.formextjs/src/z3c/formextjs/browser/configure.zcml	2008-04-21 00:46:06 UTC (rev 85520)
+++ Sandbox/pcardune/z3c.formextjs/src/z3c/formextjs/browser/configure.zcml	2008-04-21 02:09:47 UTC (rev 85521)
@@ -1,10 +1,11 @@
 <configure
     xmlns="http://namespaces.zope.org/browser"
     xmlns:z3c="http://namespaces.zope.org/z3c"
-    i18n_domain="z3c.formjs">
+    xmlns:zope="http://namespaces.zope.org/zope"
+    i18n_domain="z3c.formextjs">
 
   <viewletManager
-      name="z3c.formjs.ext.JSFormViewletManager"
+      name="z3c.formextjs.JSFormViewletManager"
       provides=".IJSFormViewletManager"
       class=".ExtJSFormViewletManager"
       layer="z3c.formextjs.interfaces.IExtJSFormLayer"
@@ -31,11 +32,26 @@
       weight="2"
       />
 
-  <z3c:widgetTemplate
-      mode="input"
-      widget="z3c.form.interfaces.ITextWidget"
-      layer="z3c.formextjs.interfaces.IExtJSFormLayer"
-      template="text_input.pt"
+  <zope:adapter
+      for="zope.interface.Interface
+	   z3c.formextjs.interfaces.IExtJSFormLayer
+	   zope.interface.Interface
+	   zope.interface.Interface
+	   z3c.form.interfaces.ITextWidget"
+      provides="zope.pagetemplate.interfaces.IPageTemplate"
+      name="input"
+      factory=".InputWidgetTemplate"
       />
 
+  <zope:adapter
+      for="zope.interface.Interface
+	   z3c.formextjs.interfaces.IExtJSFormLayer
+	   zope.interface.Interface
+	   zope.interface.Interface
+	   z3c.form.interfaces.ITextAreaWidget"
+      provides="zope.pagetemplate.interfaces.IPageTemplate"
+      name="input"
+      factory=".TextAreaInputWidget"
+      />
+
 </configure>

Modified: Sandbox/pcardune/z3c.formextjs/src/z3c/formextjs/browser/form-widgets-viewlet.pyt
===================================================================
--- Sandbox/pcardune/z3c.formextjs/src/z3c/formextjs/browser/form-widgets-viewlet.pyt	2008-04-21 00:46:06 UTC (rev 85520)
+++ Sandbox/pcardune/z3c.formextjs/src/z3c/formextjs/browser/form-widgets-viewlet.pyt	2008-04-21 02:09:47 UTC (rev 85521)
@@ -2,20 +2,25 @@
 
 formVar = 'form%s' % context.hash(context.form.id)
 for widget in context.form.widgets.values():
-    w = """
-    var ${widgetVar} = ${formVar}.add(new Ext.form.TextField({
-      fieldLabel: '${widgetLabel}',
-      value: '${widgetValue}',
-      width: '300px',
-      }));
-    """
+    w = widget.render()
+    if '<' in w:
+        r += InputWidgetTemplate(widget.context, widget.request, widget.form, widget.field, widget)(widget)
+    else:
+        r += w
+    ## w = """
+    ## var ${widgetVar} = ${formVar}.add(new Ext.form.TextField({
+    ##   fieldLabel: '${widgetLabel}',
+    ##   value: '${widgetValue}',
+    ##   width: '300px',
+    ##   }));
+    ## """
 
-    r += Template(w).substitute(dict(
-        widgetVar = 'widget%s' % context.hash(widget.id),
-        formVar = formVar,
-        widgetLabel = widget.label,
-        widgetValue = escape(widget.value),
-        ))
+    ## r += Template(w).substitute(dict(
+    ##     widgetVar = 'widget%s' % context.hash(widget.id),
+    ##     formVar = formVar,
+    ##     widgetLabel = widget.label,
+    ##     widgetValue = escape(widget.value),
+    ##     ))
 
 r += """
 %(formVar)s.doLayout();

Modified: Sandbox/pcardune/z3c.formextjs/src/z3c/formextjs/browser/text_input.pt
===================================================================
--- Sandbox/pcardune/z3c.formextjs/src/z3c/formextjs/browser/text_input.pt	2008-04-21 00:46:06 UTC (rev 85520)
+++ Sandbox/pcardune/z3c.formextjs/src/z3c/formextjs/browser/text_input.pt	2008-04-21 02:09:47 UTC (rev 85521)
@@ -1,4 +1,3 @@
-<tal:var define="unused resource_library:ext-all" />
 <input type="text" id="" name="" class="" title="" lang="" disabled=""
        readonly="" alt="" tabindex="" accesskey="" size="" maxlength=""
        tal:attributes="id view/id;

Modified: Sandbox/pcardune/z3c.formextjs/src/z3c/formextjs/div-form.pt
===================================================================
--- Sandbox/pcardune/z3c.formextjs/src/z3c/formextjs/div-form.pt	2008-04-21 00:46:06 UTC (rev 85520)
+++ Sandbox/pcardune/z3c.formextjs/src/z3c/formextjs/div-form.pt	2008-04-21 02:09:47 UTC (rev 85521)
@@ -1,116 +1,4 @@
 <h1>EXTJS SKIN</h1>
 <div id="test"></div>
 <div tal:attributes="id view/id"></div>
-<div tal:replace="structure provider:z3c.formjs.ext.JSFormViewletManager"></div>
-
-<form tal:condition="nothing" action="." method="post" enctype="multipart/form-data" class="blahblahblah"
-      metal:define-macro="form"
-      tal:attributes="method view/method;
-                      enctype view/enctype;
-                      acceptCharset view/acceptCharset;
-                      accept view/accept;
-                      action view/action;
-                      name view/name;
-                      id view/id">
-  <metal:block define-macro="subform">
-  <div class="viewspace" metal:define-slot="viewspace">
-    <metal:block define-slot="label">
-      <h1 metal:define-macro="label"
-          tal:condition="view/label|nothing"
-          tal:content="view/label">
-        Form Label
-      </h1>
-    </metal:block>
-    <metal:block define-slot="info">
-      <div class="required-info"
-           metal:define-macro="required-info">
-         <span class="required">*</span>
-         &ndash; required
-      </div>
-    </metal:block>
-    <metal:block define-slot="header">
-      <div class="status"
-           tal:condition="view/status"
-           metal:define-macro="header">
-        <div class="summary"
-             i18n:translate=""
-             tal:content="view/status">
-          Form status summary
-        </div>
-        <ul class="errors"
-            tal:condition="view/widgets/errors"
-            metal:define-macro="errors">
-          <li tal:repeat="error view/widgets/errors">
-            <tal:block condition="error/widget">
-              <span tal:replace="error/widget/label" />:
-            </tal:block>
-            <span tal:replace="structure error/render">Error Type</span>
-          </li>
-        </ul>
-      </div>
-    </metal:block>
-    <div metal:define-slot="extra-info" tal:replace="nothing">
-    </div>
-    <div metal:define-slot="main">
-      <metal:block define-macro="widget-rows">
-        <tal:block repeat="widget view/widgets/values">
-          <div id="" class="row"
-               tal:attributes="id string:${widget/id}-row"
-               tal:condition="python:widget.mode != 'hidden'">
-            <metal:block define-macro="widget-row">
-              <div class="label">
-                <label tal:attributes="for widget/id">
-                  <span i18n:translate=""
-                      tal:content="widget/label">label</span>
-                  <span class="required"
-                        tal:condition="widget/required">*</span>
-                </label>
-              </div>
-              <div class="widget" tal:content="structure widget/render">
-                <input type="text" size="24" value="" />
-              </div>
-              <div class="error"
-                   tal:condition="widget/error">
-                <span tal:replace="structure widget/error/render">error</span>
-              </div>
-            </metal:block>
-          </div>
-          <input type="hidden" value=""
-                 tal:condition="python:widget.mode == 'hidden'"
-                 tal:replace="structure widget/render" />
-        </tal:block>
-      </metal:block>
-      <metal:block define-macro="groups">
-        <fieldset tal:condition="view/groups|nothing"
-                    tal:repeat="view view/groups">
-          <legend tal:condition="view/label"
-                  tal:content="view/label">Label</legend>
-          <metal:block define-slot="group-header">
-            <div class="status"
-                 tal:condition="view/widgets/errors">
-              <div metal:use-macro="template/macros/errors" />
-            </div>
-          </metal:block>
-          <metal:block define-slot="group-rows">
-            <div metal:use-macro="template/macros/widget-rows" />
-          </metal:block>
-        </fieldset>
-      </metal:block>
-    </div>
-    <metal:block define-slot="above-buttons">
-    </metal:block>
-  </div>
-  <metal:block define-slot="buttons">
-  <div metal:define-macro="buttons">
-    <div class="buttons"
-          metal:define-slot="bottom-buttons">
-      <input tal:repeat="action view/actions/values"
-           tal:replace="structure action/render"
-           />
-    </div>
-  </div>
-  </metal:block>
-  <metal:block define-slot="bottom">
-  </metal:block>
-  </metal:block>
-</form>
+<div tal:replace="structure provider:z3c.formextjs.JSFormViewletManager"></div>



More information about the Checkins mailing list