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