Alessandro Molina avatar Alessandro Molina committed fc2e9f2 Draft

Compatibility with both ToscaWidget1 and ToscaWidgets2

Comments (0)

Files changed (3)

 from setuptools import setup, find_packages
 import os
 
-version = '0.1.3'
+version = '0.2.0'
 
 here = os.path.abspath(os.path.dirname(__file__))
 try:
       zip_safe=False,
       install_requires=[
         "TurboGears2 >= 2.0b7",
-        "tw.jquery"
       ],
       entry_points="""
       # -*- Entry points: -*-

tgext/ajaxforms/ajaxform.py

 import inspect, time
-from tw.api import JSLink, Link
-from tw.forms import Form
-from tw.jquery import jquery_js
-from tg import request, TGController
+import tg
 from tg.decorators import Decoration, expose
 from webhelpers.html import HTML
 
 except:
     import simplejson as json
 
-jquery_form_js = JSLink(modname=__name__, filename='statics/jquery.form.js')
-spinner_icon = Link(modname=__name__, filename='statics/spinner.gif')
+from tw_compat import TW1, TW2, is_tw2_form, inject_widget_resources, form_class_name
 
 def ajaxloaded(decorated):
     def display(w, value={}, **kw):
-        w.register_resources()
-        form_id = w.__class__.__name__
+        inject_widget_resources(w)
+
+        form_id = form_class_name(w)
         return HTML(HTML.div('', id=form_id), 
                     HTML.script(HTML.literal('''jQuery(document).ready(function() {{
 jQuery('#{0}').load('{1}', {2});
 }});'''.format(form_id, w.ajaxurl, json.dumps(value)))))
 
     def on_demand(w):
-        w.register_resources()
-        form_id = w.__class__.__name__
+        inject_widget_resources(w)
+
+        form_id = form_class_name(w)
         return HTML(HTML.script(HTML.literal('''\
 function {0}_on_demand(where, params) {{
     jQuery(where).load('{1}', params);
         raise RuntimeError('Form you are trying to decorate with ajaxloaded seems to be already an ajaxloaded form')
 
     decorated._ajaxform_display = decorated.display
-    decorated.javascript.extend([jquery_js, jquery_form_js])
-    decorated.display = display
+
+    if is_tw2_form(decorated):
+        decorated.resources.extend([TW2.jquery_js, TW2.jquery_form_js])
+        decorated.display = classmethod(display)
+    else:
+        decorated.javascript.extend([TW1.jquery_js, TW1.jquery_form_js])
+        decorated.display = display
+
     decorated.on_demand = on_demand
     return decorated
 
 class formexpose(object):
     def __init__(self, arg, template='tgext.ajaxforms.templates.ajaxform'):
-        if inspect.isclass(arg) and issubclass(arg, Form):
+        if inspect.isclass(arg):
             self.form = arg()
-        elif isinstance(arg, Form):
+        else:
             self.form = arg
         self.template = template
 
     def decorate_func(self, decorated):
         def before_render(remainder, params, output):
             output['ajaxform'] = self.form
-            output['ajaxform_id'] = "%s_loaded_%s" % (self.form.__class__.__name__, int(time.time()*1000))
+            output['ajaxform_id'] = "%s_loaded_%s" % (form_class_name(self.form), int(time.time()*1000))
             output['ajaxform_action'] = self.form.action
-            output['ajaxform_spinner'] = spinner_icon
+
+            if is_tw2_form(self.form):
+                output['ajaxform_spinner'] = TW2.spinner_icon.req()
+                output['ajaxform_spinner'].prepare()
+            else:
+                output['ajaxform_spinner'] = TW1.spinner_icon
+
             if not output.has_key('value'):
                 output['value'] = {}
 

tgext/ajaxforms/tw_compat.py

+try:
+    from tw.api import JSLink as Tw1JSLink, Link as Tw1Link
+    from tw.jquery import jquery_js as tw1_jquery_js
+    class TW1:
+        JSLink = Tw1JSLink
+        Link = Tw1Link
+
+        jquery_js = tw1_jquery_js
+        jquery_form_js = Tw1JSLink(modname=__name__, filename='statics/jquery.form.js')
+        spinner_icon = Tw1Link(modname=__name__, filename='statics/spinner.gif')
+except ImportError:
+    class TW1:
+        pass
+
+try:
+    from tw2.core.resources import JSLink as Tw2JSLink, Link as Tw2Link
+    from tw2.jquery import jquery_js as tw2_jquery_js
+    class TW2:
+        JSLink = Tw2JSLink
+        Link = Tw2Link
+
+        jquery_js = tw2_jquery_js
+        jquery_form_js = Tw2JSLink(modname=__name__, filename='statics/jquery.form.js')
+        spinner_icon = Tw2Link(modname=__name__, filename='statics/spinner.gif')
+except ImportError:
+    class TW2:
+        pass
+
+def is_tw2_form(w):
+    return hasattr(w, 'req')
+
+def inject_widget_resources(w):
+    if is_tw2_form(w):
+        resources = [r.req() for r in w.resources]
+        for r in resources:
+            r.prepare()
+
+        for c in w.children_deep():
+            #Some widgets modify the resources during prepare
+            c = c.req()
+            c.prepare()
+
+            resources = [r.req() for r in c.resources]
+            for r in resources:
+                r.prepare()
+    else:
+        w.register_resources()
+
+def form_class_name(w):
+    if is_tw2_form(w):
+        return w.__name__
+    else:
+        return w.__class__.__name__
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.