small fixes
[plewww.git] / plekit / python / table.py
1 # $Id$
2
3 class plekit_table:
4
5     def __init__ (self, table_id, headers, column_sort, 
6                   caption=None,
7                   search_area=True, pagesize_area=True, notes_area=True,
8                   search_width=40, pagesize=25, pagesize_def=999,
9                   max_pages=10,
10                   notes=None):
11         self.table_id=table_id
12         self.headers=headers
13         self.column_sort =column_sort 
14         self.caption=caption
15         self.search_area=search_area
16         self.pagesize_area=pagesize_area
17         self.notes_area=notes_area
18         self.search_width =search_width 
19         self.pagesize =pagesize 
20         self.pagesize_def=pagesize_def
21         self.max_pages=max_pages
22         self.notes=notes
23         self.has_tfoot=False
24
25     def columns (self):
26         return len(self.headers)
27
28     def start (self):
29         paginator = self.table_id + "_paginator"
30         classname = "paginationcallback-%s" % paginator
31         classname += " max-pages-%d" % self.max_pages
32         classname += " paginate-%d" % self.pagesize
33         # instantiate paginator callback
34         table_id = self.table_id
35         print """
36 <script type="text/javascript"> 
37 function %(paginator)s (opts) { plekit_table_paginator (opts,"%(table_id)s)"; }
38 </script>
39 <br/>
40 <table id="%(table_id)s" cellpadding="0" cellspacing="0" border="0" 
41 class="plekit_table sortable-onload-self.sort_column rowstyle-alt colstyle-alt no-arrow %(classname)s">
42 <thead>
43 """ % locals()
44
45         if self.pagesize_area:
46             print self.pagesize_area_html ()
47         if self.search_area:
48             print self.search_area_html ()
49
50         if (self.caption):
51             print "<caption> %s </caption>" % self.caption
52         print "<tr>"
53         for (label,type) in self.headers.iteritems():
54             if type == "none":
55                 classpart=""
56             elif type == "string" or type == "int" or type == "float":
57                 classpart="sortable"
58             elif type.find ("date-") == 0:
59                 classpart="sortable-" + type
60             else:
61                 classpart="sortable-sort" + type
62             print '<th class="%(classpart)s plekit_table">%(label)s</th>'%locals()
63
64         print """</tr></thead><tbody>\n"""
65
66     ##########
67     ## unlike the php version there is no support for last-minute options, pass them all to the constructor
68     def end (self):
69         print self.bottom_html()
70         if (self.notes_area):
71             print self.notes_area_html()
72                     
73     ##########
74     def pagesize_area_html (self):
75         width=len(self.headers)
76         pagesize_text_id = self.table_id + "_pagesize"
77         result = """
78 <tr class='pagesize_area'><td class='pagesize_area' colspan='%(width)s'>
79 <form class='pagesize' action='satisfy_xhtml_validator'><fieldset>
80    <input class='pagesize_input' type='text' id="%(pagesize_text_id)s" value='self.pagesize'
81       onkeyup='plekit_pagesize_set("self.table_id","%(pagesize_text_id)s", self.pagesize);' 
82       size='3' maxlength='3' /> 
83   <label class='pagesize_label'> items/page </label>   
84   <img class='reset' src="/planetlab/icons/clear.png" alt="reset visible size"
85       onmousedown='plekit_pagesize_reset("self.table_id","%(pagesize_text_id)s",self.pagesize_def);' />
86 </fieldset></form></td></tr>
87 """ % locals()
88         return result
89
90     ##########      
91     def search_area_html (self):
92         width = len(self.headers)
93         search_text_id = self.table_id + "_search"
94         search_reset_id = self.table_id + "_search_reset"
95         search_and_id = self.table_id + "_search_and"
96         result = """
97 <tr class='search_area'><td class='search_area' colspan='%(width)s'>
98 <div class='search'><fieldset>
99    <label class='search_label'> Search </label> 
100    <input class='search_input' type='text' id='%(search_text_id)s'
101       onkeyup='plekit_table_filter("self.table_id","%(search_text_id)s","%(search_and_id)s");'
102       size='self.search_width' maxlength='256' />
103    <label>and</label>
104    <input id='%(search_and_id)s' class='search_and' 
105       type='checkbox' checked='checked' 
106       onchange='plekit_table_filter("self.table_id","%(search_text_id)s","%(search_and_id)s");' />
107    <img class='reset' src="/planetlab/icons/clear.png" alt="reset search"
108       onmousedown='plekit_table_filter_reset("self.table_id","%(search_text_id)s","%(search_and_id)s");' />
109 </fieldset></div></td></tr>
110 """ % locals()
111         return result
112
113     ##########
114     ## start a <tfoot> section
115     def tfoot_start (self): print self.tfoot_start_html()
116     def tfoot_start_html (self):
117         self.has_tfoot=true
118         return "</tbody><tfoot>"
119
120     ##########
121     def bottom_html (self):
122         result=""
123         if self.has_tfoot:
124             result += "</tfoot>"
125         else:
126             result += "</tbody>"
127             result += "</table>\n"
128         return result
129
130     ##########
131     default_notes =  [
132         "Enter &amp; or | in the search area to switch between <span class='bold'>AND</span> and <span class='bold'>OR</span> search modes",
133         "Hold down the shift key to select multiple columns to sort" ]
134
135     def notes_area_html (self):
136         if (self.notes):
137             notes=self.notes
138         else:
139             notes=[]
140         notes = notes + self.default_notes
141         result = ""
142         result += "<p class='table_note'> <span class='table_note_title'>Notes</span>\n"
143         for note in notes:
144             result += "<br/>%s\n" % note
145         result += "</p>"
146         return result
147
148     ##########
149     def row_start (self, id=None, classpart=None):
150         print "<tr"
151         if id: print " id='%s'" % id
152         if classpart: print ' class="%s"' % classpart
153         print ">\n"
154         
155     def row_end (self):
156         print "</tr>\n"
157
158     ##########
159     ## supported options:
160     ## (*) only-if : if set and false, then print 'n/a' instead of (presumably void) $text
161     ## (*) classpart
162     ## (*) columns
163     ## (*) hfill
164     ## (*) align
165     def cell (self, text,only_if=True, classpart=None, columns=None, hfill=None, align=None): 
166         print self.cell_html (text, only_if, classpart, columns, hfill, align)
167     def cell_html (self, text, only_if=True, classpart=None, columns=None, hfill=None, align=None):
168         if not only_if:
169             text="n/a"
170         html=""
171         html += "<td"
172         if classpart: html += " class='%s'" % classpart
173         if columns: html += " colspan='%d'" % columns
174         elif hfill: html += " colspan='%d'" % self.columns()
175         if align: html += " style='text-align:%s'" % align
176         html += ">%s</td>" % text
177         return html