Commits

Anonymous committed de8e407

Updated grid.py

Comments (0)

Files changed (1)

unfinished/grid.py

 from routes.util import url_for
 from pylons import request
+from webhelpers.html.builder import HTML, literal
 
 class Grid(object):
     
+    def default_column_format(self,i,record,column_name):
+        return HTML.tag('td', record[column_name])
+    
+    def numbered_column_format(self,i,record):
+        return HTML.tag('td', i)
+    
+    def default_record_format(self, i, record, class_name, columns):
+        return HTML.tag('tr', columns,class_=class_name)
+
+    def default_record_format(self, i, record, class_name, columns):
+        return HTML.tag('tr', columns,class_=class_name)
+
+    def default_header_record_format(self, headers):
+        return HTML.tag('tr', headers,class_='header')
+
+    def default_header_ordered_column_format(self, column_number, order, header_label):
+        header_label = HTML(header_label,HTML.tag('span',class_='marker'))
+        class_name = 'column%s ordering' % (column_number)
+        return HTML.tag('td', header_label, class_=class_name)
+
+    def default_header_column_format(self, column_number, header_label):
+        class_name = 'column%s' % (column_number)
+        return HTML.tag('td', header_label, class_=class_name)
+    
+    def default_header_link(self,url,content):
+        return HTML.tag("a", href=url, c=content)
+    
     def __init__(self, itemlist, *args, **kwargs):
-        self.default_column_format = '\n\t<td>%s</td>'
-        self.default_row_format = '\n\t<tr class="%s">%s</tr>'
-        self.custom_row_format = None
-        self.default_header_row_format = '\n<tr class="header">\n%s\n</tr>'
-        self.default_header_ordered_column_format = '<td class="column%s ordering %s">%s<span class="marker"></span></td>'
-        self.default_header_column_format = '<td class="column%s">%s</td>'
-        self.default_header_link = '<a href="%s">%s</a>'
+        self.custom_record_format = None
         
         self.labels = {}
         self.exclude_ordering = ['_numbered']
         else:
             self._start_number = 1
 
-    def __make_header_columns(self):
+    def make_headers(self):
         header_columns = []
         request_copy = request.copy()
         if 'order_by' in request_copy.GET:
                     new_ordering = column + '_dsc'
                 else:
                     new_ordering = column + '_asc'
-                label_text = self.default_header_link % (url_for(order_by=new_ordering, **request_copy.GET), label_text)
+                label_text = self.default_header_link(url_for(order_by=new_ordering, **request_copy.GET), label_text)
                 #lets test if the column we try to order on is this column
                 if self.order_column and column == self.order_column[0:-4] and self.order_column[-3:] == 'asc':
-                    header_columns.append(self.default_header_ordered_column_format % (i+1,'asc', label_text))
+                    header_columns.append(self.default_header_ordered_column_format(i+1,'asc', label_text))
                 elif self.order_column and column == self.order_column[0:-4] and self.order_column[-3:] == 'dsc':
-                    header_columns.append(self.default_header_ordered_column_format % (i+1,'dsc', label_text))
+                    header_columns.append(self.default_header_ordered_column_format(i+1,'dsc', label_text))
                 else:
-                    header_columns.append(self.default_header_column_format % (i+1,label_text))    
+                    header_columns.append(self.default_header_column_format(i+1,label_text))    
             else:
-                header_columns.append(self.default_header_column_format % (i+1,label_text))
-        return ''.join(header_columns)
+                header_columns.append(self.default_header_column_format(i+1,label_text))
+        return HTML(*header_columns)
     
-    def __make_columns(self, i, row):
+    def make_columns(self, i, record):
         columns = []
         if '_numbered' in self.columns:
-            columns.append(self.default_column_format % (i + self._start_number))
+            columns.append(self.numbered_column_format(i + self._start_number, record))
             for column in self.columns[1:]:
                 if column in self.format:
                     pass
-                    columns.append(self.format[column](i, row))
+                    columns.append(self.format[column](i, record))
                 else:
-                    columns.append(self.default_column_format % row[column])        
+                    columns.append(self.default_column_format(i, record, column))     
         else:
             for i,column in enumerate(self.columns):
                 if column in self.format:
-                    pass
-                    columns.append(self.format[column](i, row))
+                    columns.append(self.format[column](i, record))
                 else:
-                    columns.append(self.default_column_format % row[column]) 
-        return ''.join(columns)
+                    columns.append(self.default_column_format(i, record, column)) 
+        return HTML(*columns)
     
-    def render(self):
+    def __html__(self):
         ''' renders the grid '''
-        rows = []
-        #first render headers row
-        rows.append(self.default_header_row_format % self.__make_header_columns())
-        if self.custom_row_format is None:
-            for i, row in enumerate(self.itemlist):
-                if i % 2 == 0:
-                    class_name = 'even'
-                else:
-                    class_name = 'odd'
-                rows.append(self.default_row_format % (class_name,self.__make_columns(i, row)))
-        else:
-            for i, row in enumerate(self.itemlist):
-                if i % 2 == 0:
-                    class_name = 'even'
-                else:
-                    class_name = 'odd'
-                rows.append(self.custom_row_format(class_name,i, row, self.__make_columns(i, row)))
-        return ''.join(rows)
+        records = []
+        #first render headers record
+        records.append(self.default_header_record_format(self.make_headers()))
+        for i, record in enumerate(self.itemlist):
+            if i % 2 == 0:
+                class_name = 'even'
+            else:
+                class_name = 'odd'
+            if self.custom_record_format is None:
+                records.append(self.default_record_format(i, record, class_name,self.make_columns(i, record)))
+            else:
+                records.append(self.custom_record_format(i, record, class_name, self.make_columns(i, record)))
+
+        return HTML(*records)
+    
+    __str__ = __html__
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.