pvl/verkko/table.py
changeset 204 72304d66ecd1
parent 183 8fbaaf0564dc
child 206 079bf632442d
--- a/pvl/verkko/table.py	Sun Feb 10 13:18:01 2013 +0200
+++ b/pvl/verkko/table.py	Sun Feb 10 13:18:21 2013 +0200
@@ -81,7 +81,46 @@
 
         return html.td(class_=self.colcss)(input)
 
-    def render_cell (self, item, table, hilight=None) :
+    def cell_html (self, item) :
+        """
+            Render contents for <td>.
+        """
+
+        # XXX: this is sometimes broken, figure out how to index by column
+        value = getattr(item, self.attr)
+
+        if self.rowhtml :
+            return self.rowhtml(item)
+        else :
+            return value
+
+    def cell_css (self, item, hilight=None) :
+        """
+            Return CSS classes for <td>.
+        """
+
+        if self.colcss :
+            yield self.colcss
+
+        if self.rowcss :
+            yield self.rowcss(item)
+
+        if hilight :
+            # lookup attr/value
+            hilight = self.attr in hilight and value in hilight[self.attr]
+
+        if hilight :
+            yield 'hilight'
+
+    def cell_title (self, item) :
+        """
+            Return title= for <td>
+        """
+
+        if self.rowtitle :
+            return self.rowtitle(item)
+
+    def render_cell (self, item, table, filters=None, hilight=None) :
         """
             Render <td> for item in <tbody> in given Table.
 
@@ -91,42 +130,16 @@
         # XXX: this is sometimes broken, figure out how to index by column
         value = getattr(item, self.attr)
 
-        if self.rowhtml :
-            rowhtml = self.rowhtml(item)
-        else :
-            rowhtml = value
-
-        if self.rowcss :
-            rowcss = self.rowcss(item)
-        else :
-            rowcss = None
-
-        if hilight :
-            # lookup attr/value
-            hilight = self.attr in hilight and value in hilight[self.attr]
-
-        if self.rowfilter :
+        if self.rowfilter and filters is not None :
             # filter-link by row-value
             filters = { self.attr: value }
         else :
             filters = None
 
-        # css classes to apply, or None's
-        css = (
-                self.colcss, 
-                rowcss, 
-                'hilight' if hilight else None
-        )
-
-        if self.rowtitle :
-            rowtitle = self.rowtitle(item)
-        else :
-            rowtitle = None
-
-        yield table.render_cell(rowhtml,
-                css         = css,
+        yield table.render_cell(self.cell_html(item),
+                css         = tuple(self.cell_css(item, hilight=hilight)),
                 filters     = filters,
-                rowtitle    = rowtitle,
+                title       = self.cell_title(item),
         )
 
 class Table (object) :
@@ -228,7 +241,7 @@
         for column in self.columns :
             yield column.render_header_filter(filters)
 
-    def render_cell (self, rowhtml, css=(), filters=None, rowtitle=None) :
+    def render_cell (self, rowhtml, css=(), filters=None, title=None) :
         """
             Render a single cell.
 
@@ -245,13 +258,17 @@
 
         css = ' '.join(cls for cls in css if cls)
         
-        return html.td(class_=css, title=rowtitle)(cell)
+        return html.td(class_=css, title=title)(cell)
    
     def render_row (self, item, **opts) :
         """
             Yield columns for row.
         """
-        
+
+        yield html.th(
+            html.a(href=self.itemurl(item))("#")
+        ),
+
         for column in self.columns :
             yield column.render_cell(item, self, **opts)
 
@@ -262,10 +279,6 @@
 
         for i, item in enumerate(rows) :
             yield html.tr(class_=('alternate' if i % 2 else None), id=item.id)(
-                html.th(
-                    html.a(href=self.itemurl(item))("#")
-                ),
-
                 self.render_row(item, **opts)
             )
 
@@ -327,6 +340,7 @@
             ),
             html.tbody(
                 self.render_body(query,
+                    filters = filters,
                     hilight = hilight,
                 )
             ),
@@ -348,6 +362,20 @@
                 table,
             )
 
+    def json (self, item) :
+        """
+            Yield JSON params for given item.
+        """
+
+        yield 'id', item.id
+
+        for column in self.columns :
+            yield column.attr, dict(
+                    html    = unicode(html(column.cell_html(item))),
+                    css     = tuple(column.cell_css(item)),
+                    title   = column.cell_title(item),
+            )
+
 class TableHandler (object) :
     """
         Mixin for handling Table args/rendering.
@@ -362,7 +390,19 @@
     # target Handlers for table links
     TABLE_URL = None
     TABLE_ITEM_URL = None
- 
+
+    def init (self) :
+        """
+            Bind self.table
+        """
+
+        super(TableHandler, self).init()
+
+        self.table = self.TABLE(self.url,
+                table_url   = self.TABLE_URL,
+                item_url    = self.TABLE_ITEM_URL,
+        )
+
     def query (self) :
         """
             Database SELECT query.
@@ -521,10 +561,7 @@
                 hilight     - hilight given { attr: value } cells
         """
 
-        return self.TABLE(self.url,
-                table_url   = self.TABLE_URL,
-                item_url    = self.TABLE_ITEM_URL,
-        ).render(query,
+        return self.table.render(query,
                 filters = filters,
                 sort    = sort,
                 page    = page,