Author: bree
Date: Wed Feb  1 17:40:31 2006
New Revision: 2259

Added:
   azax/branches/snowsprint/azaxview.py
      - copied, changed from r2258, azax/branches/snowsprint/azaxresponse.py
   azax/branches/snowsprint/browser/
   azax/branches/snowsprint/browser/kukitresponse.pt
Removed:
   azax/branches/snowsprint/azaxresponse.py
Modified:
   azax/branches/snowsprint/__init__.py
   azax/branches/snowsprint/config.py
   azax/branches/snowsprint/demos/azaxdemo/azaxview.py
   azax/branches/snowsprint/demos/azaxdemo/browser/azax_instant_edit.pt
   azax/branches/snowsprint/demos/azaxdemo/browser/azax_three_autoupdate.azax
   azax/branches/snowsprint/interfaces.py
Log:
Refactoring azax response generation

* AzaxResponse is made to the base class of the browser view
* products need to inherit from this view, adjusted this
* lxml is not used any more

Demos:

* fixed three_autoupdate
* title_edit does not work properly

TODOS:

* not checked with IE
* fix tests


Modified: azax/branches/snowsprint/__init__.py
==============================================================================
--- azax/branches/snowsprint/__init__.py        (original)
+++ azax/branches/snowsprint/__init__.py        Wed Feb  1 17:40:31 2006
@@ -1,3 +1,5 @@
 import mimetypes
 
 mimetypes.types_map['.azax'] = 'text/xml' 
+
+from azaxview import AzaxBaseView

Copied: azax/branches/snowsprint/azaxview.py (from r2258, 
azax/branches/snowsprint/azaxresponse.py)
==============================================================================
--- azax/branches/snowsprint/azaxresponse.py    (original)
+++ azax/branches/snowsprint/azaxview.py        Wed Feb  1 17:40:31 2006
@@ -18,108 +18,113 @@
 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 # 02111-1307, USA.
 #
-from StringIO import StringIO
 
 from zope.interface import implements
+#import config
+from zope.app.pagetemplate.viewpagetemplatefile import ViewPageTemplateFile
+from Products.Five import BrowserView
 
-from interfaces import IAzaxResponse
-import config
+class AzaxParam:
+    
+    def __init__(self, name, content=''):
+        self.name = name
+        self.content = content
+
+    def getName(self):
+        return self.name
 
+    def getContent(self):
+        return self.content
+        
 class AzaxCommand:
-    def __init__(self, commands_ob, name, selector):
-        etree = config.etree
-        self.ob = etree.SubElement(commands_ob, 'kukit:command')
-        self.ob.set('selector', selector)
-        self.ob.set('name', name)
-
-    def addData(self, name):
-        etree = config.etree
-        param = etree.SubElement(self.ob, 'kukit:param')
-        param.set('name', name)
+    
+    def __init__(self, name, selector):
+        self.name = name
+        self.selector = selector
+        self.params = []
+
+    def addParam(self, name, content=''):
+        param = AzaxParam(name, content)
+        self.params.append(param)
         return param
 
+    def getName(self):
+        return self.name
+
+    def getSelector(self):
+        return self.selector
 
+    def getParams(self):
+        return self.params
+    
+#HTML_DTD = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 ' \
+#   'Transitional//EN" "file://%s/xhtml1-' \
+#   'transitional.dtd">' % config.DTDS_DIR
+#   
+#HTML_PREFIX = HTML_DTD + '<html><body>%s</body></html>'
 
-HTML_DTD = '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 ' \
-   'Transitional//EN" "file://%s/xhtml1-' \
-   'transitional.dtd">' % config.DTDS_DIR
-   
-HTML_PREFIX = HTML_DTD + '<html><body>%s</body></html>'
+class AzaxBaseView(BrowserView):
+    """ Base azax view
 
-class AzaxResponse(object):
-    """ azax command Response
+    this allows setting up the content of the response, and then
+    generate it out.
     """
-    implements(IAzaxResponse)
-
-    custom_options = config.etree.getDefaultParseOptions() + 4
+    
+    render = ViewPageTemplateFile('browser/kukitresponse.pt', 
content_type='text/xml')
 
-    def _setEtreeParseOptions(self):
-        etree = config.etree
-        etree.setGlobalParseOptions(self.custom_options)
-
-    def __init__(self, response=None):
-        etree = config.etree
-        self._setEtreeParseOptions()
-        self._html = etree.fromstring(HTML_PREFIX % '')
-        self._body = self._html[0]
-        self._html.set('xmlns:kukit', 'http://www.kukit.org/commands/1.0')
-        self._response = response
+    def __init__(self, context, request):
+        BrowserView.__init__(self, context, request)
+        self.commands = []
 
     def addCommand(self, name, selector):
-        return AzaxCommand(self._body, name, selector)
-        
-    def __str__(self):
-        """ renders the xml """
-        etree = config.etree
-        result = etree.tostring(self._html)
-        return result
-
-    def __call__(self):
-        """ set response content type, if given """
-        if self._response is not None:
-            self._response.setHeader('Content-Type', 'text/xml')
-        return str(self)
-
-    def wrapInHtmlNamespace(self, new_value, data):
-        etree = config.etree
-        new_value = HTML_PREFIX % new_value
-        self._setEtreeParseOptions()
-        valuetree = etree.fromstring(new_value)
-        body = valuetree[0]
-        data.text = body.text
-        for elem in body.getchildren():
-            elem.set("xmlns", "http://www.w3.org/1999/xhtml";)
-            data.append(elem)
+        command = AzaxCommand(name, selector)
+        self.commands.append(command)
+        return command
+
+    def getCommands(self):
+        return self.commands
+
+#    def wrapInHtmlNamespace(self, new_value, data):
+#        etree = config.etree
+#        new_value = HTML_PREFIX % new_value
+#        self._setEtreeParseOptions()
+#        valuetree = etree.fromstring(new_value)
+#        body = valuetree[0]
+#        data.text = body.text
+#        for elem in body.getchildren():
+#            elem.set("xmlns", "http://www.w3.org/1999/xhtml";)
+#            data.append(elem)
+    
+    def _validateXml(self, value):
+        pass
     
     # commands
     def setHtmlAsChild(self, selector, new_value):
         """ see interfaces.py """
+        self._validateXml(new_value)
         command = self.addCommand('setHtmlAsChild', selector)
-        data = command.addData('html')
-        self.wrapInHtmlNamespace(new_value, data)        
+        data = command.addParam('html', new_value)
     
     def addAfter(self, selector, new_value):
         """ see interfaces.py """
+        self._validateXml(new_value)
         command = self.addCommand('addAfter', selector)
-        data = command.addData('html')
-        self.wrapInHtmlNamespace(new_value, data)        
+        data = command.addParam('html', new_value)
 
     def clearChildren(self, selector):
         """ see interfaces.py """
         command = self.addCommand('clearChildren', selector)
-        data = command.addData('none')
+        data = command.addParam('none')
 
     def copyChildrenFrom(self, selector, id):
         """ see interfaces.py """
         command = self.addCommand('copyChildrenFrom', selector)
-        data = command.addData('html_id')
-        data.text = id
+        data = command.addParam('html_id', id)
 
     def copyChildrenTo(self, selector, id):
         """ see interfaces.py """
         command = self.addCommand('copyChildrenTo', selector)
-        data = command.addData('html_id')
-        data.text = id
+        data = command.addParam('html_id', id)
 
     def moveChildrenTo(self, selector, id):
         """ see interfaces.py """
@@ -128,5 +133,4 @@
 
     def executeCode(self, selector, code):
         command = self.addCommand('executeCode', selector)
-        data = command.addData('code')
-        data.text = code 
+        data = command.addParam('code', code)

Added: azax/branches/snowsprint/browser/kukitresponse.pt
==============================================================================
--- (empty file)
+++ azax/branches/snowsprint/browser/kukitresponse.pt   Wed Feb  1 17:40:31 2006
@@ -0,0 +1,17 @@
+<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"file:///var/lib/zope2.8/instance/kukit/Products/azax/dtds/xhtml1-transitional.dtd">
+<html xmlns="http://www.w3.org/1999/xhtml";
+      xmlns:kukit="http://www.kukit.org/commands/1.0";
+      xmlns:tal="http://xml.zope.org/namespaces/tal";
+      xmlns:metal="http://xml.zope.org/namespaces/metal";
+      xmlns:i18n="http://xml.zope.org/namespaces/i18n";><body>
+<kukit:command tal:repeat="command view/getCommands"
+               selector="div#demo" name="setHtmlAsChild"
+               tal:attributes="selector command/getSelector;
+                                           name command/getName">
+       <kukit:param tal:repeat="param command/params" name="html"
+                       tal:attributes="name param/name"
+                       tal:content="structure param/content">
+               <h1 xmlns="http://www.w3.org/1999/xhtml";>it worked</h1>
+       </kukit:param>
+</kukit:command>
+</body></html>

Modified: azax/branches/snowsprint/config.py
==============================================================================
--- azax/branches/snowsprint/config.py  (original)
+++ azax/branches/snowsprint/config.py  Wed Feb  1 17:40:31 2006
@@ -1,16 +1 @@
-# we use lxml for all the XML stuff for speed, but you can also use
-# ElemenTree or maybe even cElementTree
 
-try:
-    from Products.lxml import etree
-except ImportError:
-    try:
-        import cElementTree as etree
-    except ImportError:
-        from elementtree import ElementTree as etree
-
-import Products.azax
-import os.path
-AZAX_DIR = os.path.split(Products.azax.__file__)[0]
-
-DTDS_DIR= '/'.join(AZAX_DIR.split(os.path.sep)+['dtds'])

Modified: azax/branches/snowsprint/demos/azaxdemo/azaxview.py
==============================================================================
--- azax/branches/snowsprint/demos/azaxdemo/azaxview.py (original)
+++ azax/branches/snowsprint/demos/azaxdemo/azaxview.py Wed Feb  1 17:40:31 2006
@@ -18,46 +18,37 @@
 # Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
 # 02111-1307, USA.
 #
-from Products.Five import BrowserView
 
-from Products.azax.azaxresponse import AzaxResponse
+from Products.azax import AzaxBaseView
 from datetime import datetime
 
-class AzaxView(BrowserView):
-
-    def __init__(self, context, request):
-        BrowserView.__init__(self, context, request)
+class AzaxView(AzaxBaseView):
 
     def clearDivContent(self):
         """ clear div content """
-        return_object = AzaxResponse(self.request.response)
-        return_object.clearChildren('div#demo')
-        return return_object()
+        self.clearChildren('div#demo')
+        return self.render()
 
     def copyFromDivContent(self):
         """ copy div content """
-        return_object = AzaxResponse(self.request.response)
-        return_object.copyChildrenFrom('div#copy', 'demo')
-        return return_object()
+        self.copyChildrenFrom('div#copy', 'demo')
+        return self.render()
 
     def copyToDivContent(self):
         """ copy div content """
-        return_object = AzaxResponse(self.request.response)
         return_object.copyChildrenTo('div#copy', 'demo')
-        return return_object()
+        return self.render()
 
     def moveToDivContent(self):
         """ copy div content """
-        return_object = AzaxResponse(self.request.response)
-        return_object.moveChildrenTo('div#copy', 'demo')
-        return return_object()
+        self.moveChildrenTo('div#copy', 'demo')
+        return self.render()
 
     def getDivContent(self):
         """ returns div content """
-        return_object = AzaxResponse(self.request.response)
-        return_object.setHtmlAsChild('div#demo', '<h1>it worked</h1>')
-        return_object.setHtmlAsChild('div#demo', '<h1>it 
worked&nbsp;again</h1>')
-        return return_object()
+        self.setHtmlAsChild('div#demo', '<h1>it worked</h1>')
+        self.setHtmlAsChild('div#demo', '<h1>it worked&nbsp;again</h1>')
+        return self.render()
 
     def getCorrespondingSelect(self, value):
         """ returns select content """
@@ -65,48 +56,39 @@
         mapping['']=[]
         mapping['animals']=['dog', 'cat', 'cow']
         mapping['machines']=['computer', 'car', 'airplane']
-        return_object = AzaxResponse(self.request.response)
         result = ['<option>%s</option>' % item for item in mapping[value]]
-            
-        return_object.setHtmlAsChild('select#second', ' '.join(result))
-        return return_object()
+        self.setHtmlAsChild('select#second', ' '.join(result))
+        return self.render()
 
     def getAutoupdateMarkup(self):
         """ returns the current time """
-        return_object = AzaxResponse(self.request.response)
-        return_object.setHtmlAsChild('div#update-wrapper', '<div 
id="update-area"></div>')
-        return return_object()
+        self.setHtmlAsChild('div#update-wrapper', '<div 
id="update-area"></div>')
+        return self.render()
 
     def getCurrentTime(self):
         """ returns the current time """
-        return_object = AzaxResponse(self.request.response)
-        return_object.setHtmlAsChild('div#update-area', "<p>%s</p>" % 
str(datetime.now()))
-        return return_object()
-        
-    def getInputField(self, value):
+        self.setHtmlAsChild('div#update-area', "<p>%s</p>" % 
str(datetime.now()))
+        return self.render()
+        
+    def getInputField(self, value):
         """ returns the current time """
-        return_object = AzaxResponse(self.request.response)
-        return_object.setHtmlAsChild('div#text', 
-                                                '<input type="text" 
name="value" value="'+value+'" />' \
-                                                '<input type="button" 
value="save" id="save" />'
-                                                )
-        return return_object()
+        self.setHtmlAsChild('div#text', 
+                            '<div><input type="text" name="value" 
value="'+value+'" /></div>' \
+                            '<input type="button" value="save" id="save" />'
+                           )
+        return self.render()
         
     def saveText(self, value):
         """ returns the current time """
-        return_object = AzaxResponse(self.request.response)
-        return_object.setHtmlAsChild('div#text', value+'<input type="hidden" 
name="value" value="'+value+'" />')
-        return return_object()
+        self.setHtmlAsChild('div#text', value+'<input type="hidden" 
name="value" value="'+value+'" />')
+        return self.render()
     
     #we'll need parameter passing first do this in a sane way
     def getSubTree(self, value):
         """ returns the current time """
-        return_object = AzaxResponse(self.request.response)
-        return_object.setHtmlAsChild('div#text', 'works')
-        return return_object()
+        self.setHtmlAsChild('div#text', 'works')
+        return self.render()
         
     def cancelSubmitSave(self, text_save):
-        return_object = AzaxResponse(self.request.response)
-        return_object.setHtmlAsChild('div#async', 'Async saved %s' % text_save)
-        return return_object()
-
+        self.setHtmlAsChild('div#async', 'Async saved %s' % text_save)
+        return self.render()

Modified: azax/branches/snowsprint/demos/azaxdemo/browser/azax_instant_edit.pt
==============================================================================
--- azax/branches/snowsprint/demos/azaxdemo/browser/azax_instant_edit.pt        
(original)
+++ azax/branches/snowsprint/demos/azaxdemo/browser/azax_instant_edit.pt        
Wed Feb  1 17:40:31 2006
@@ -23,11 +23,14 @@
     <p><a href=".">All demos</a></p>
 
 <h3>Instant edit Demo</h3>
+       <div>
    <form name="edit">
         <div id="text">
         click me!
         <input type="hidden" name="value" value="click me!"  />
         </div>
     </form>
+       </div>
+
   </body>
 </html>

Modified: 
azax/branches/snowsprint/demos/azaxdemo/browser/azax_three_autoupdate.azax
==============================================================================
--- azax/branches/snowsprint/demos/azaxdemo/browser/azax_three_autoupdate.azax  
(original)
+++ azax/branches/snowsprint/demos/azaxdemo/browser/azax_three_autoupdate.azax  
Wed Feb  1 17:40:31 2006
@@ -1,7 +1,7 @@
 <?xml version="1.0"?>
 <rules>
   <rule selector="div#update-area">
-    <kukitevent name="timeout">getCurrentTime</kukitevent>
+    <kukitevent name="timeout">getCurrentTime 2000</kukitevent>
   </rule>
   <rule selector="input#start-update">
     <event name="click">getAutoupdateMarkup</event>

Modified: azax/branches/snowsprint/interfaces.py
==============================================================================
--- azax/branches/snowsprint/interfaces.py      (original)
+++ azax/branches/snowsprint/interfaces.py      Wed Feb  1 17:40:31 2006
@@ -19,22 +19,5 @@
 # 02111-1307, USA.
 #
 
-""" IAzaxResponse provide apis to be able to generate
-        the xml response used by the javascript
-"""
 from zope.interface import Interface
 
-class IAzaxResponse(Interface):
-
-    def updateTag(selector, new_value):
-        """ updates a tag on the page """
-
-
-
-
-
-
-
-
-
-
-- 
http://lists.nuxeo.com/mailman/listinfo/z3lab-checkins

Reply via email to