[web component architecture] extract LayoutableMixIn from components
authorSylvain Thénault <sylvain.thenault@logilab.fr>
Fri, 21 Oct 2011 14:32:37 +0200
changeset 7991 dcc5a4d48122
parent 7990 a673d1d9a738
child 7992 4ff9f25cb06e
[web component architecture] extract LayoutableMixIn from components in the intend to use it in the table refactoring to come.
web/component.py
--- a/web/component.py	Fri Oct 21 14:32:37 2011 +0200
+++ b/web/component.py	Fri Oct 21 14:32:37 2011 +0200
@@ -245,7 +245,7 @@
 
 
 class Layout(Component):
-    __regid__ = 'layout'
+    __regid__ = 'component_layout'
     __abstract__ = True
 
     def init_rendering(self):
@@ -263,7 +263,23 @@
         return True
 
 
-class CtxComponent(AppObject):
+class LayoutableMixIn(object):
+    layout_id = None # to be defined in concret class
+    layout_args = {}
+
+    def layout_render(self, w):
+        getlayout = self._cw.vreg['components'].select
+        layout = getlayout(self.layout_id, self._cw, **self.layout_select_args())
+        layout.render(w)
+
+    def layout_select_args(self):
+        args  = dict(rset=self.cw_rset, row=self.cw_row, col=self.cw_col,
+                     view=self)
+        args.update(self.layout_args)
+        return args
+
+
+class CtxComponent(LayoutableMixIn, AppObject):
     """base class for contextual components. The following contexts are
     predefined:
 
@@ -310,6 +326,7 @@
     context = 'left'
     contextual = False
     title = None
+    layout_id = 'component_layout'
 
     # XXX support kwargs for compat with old boxes which gets the view as
     # argument
@@ -323,19 +340,17 @@
             self.wview = wview
             self.call(**kwargs) # pylint: disable=E1101
             return
-        getlayout = self._cw.vreg['components'].select
-        layout = getlayout('layout', self._cw, **self.layout_select_args())
-        layout.render(w)
+        self.layout_render(w)
 
     def layout_select_args(self):
+        args = super(CtxComponent, self).layout_select_args()
         try:
             # XXX ensure context is given when the component is reloaded through
             # ajax
-            context = self.cw_extra_kwargs['context']
+            args['context'] = self.cw_extra_kwargs['context']
         except KeyError:
-            context = self.cw_propval('context')
-        return dict(rset=self.cw_rset, row=self.cw_row, col=self.cw_col,
-                    view=self, context=context)
+            args['context'] = self.cw_propval('context')
+        return args
 
     def init_rendering(self):
         """init rendering callback: that's the good time to check your component