Setting tag plewww-4.3-67
[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_dict = locals()
78         result_dict.update(self.__dict__)
79         result = """
80 <tr class='pagesize_area'><td class='pagesize_area' colspan='%(width)s'>
81 <form class='pagesize' action='satisfy_xhtml_validator'><fieldset>
82    <input class='pagesize_input' type='text' id="%(pagesize_text_id)s" value='%(pagesize)s'
83       onkeyup='plekit_pagesize_set("%(table_id)s","%(pagesize_text_id)s", %(pagesize)s);' 
84       size='3' maxlength='3' /> 
85   <label class='pagesize_label'> items/page </label>   
86   <img class='reset' src="/planetlab/icons/clear.png" alt="reset visible size"
87       onmousedown='plekit_pagesize_reset("%(table_id)s","%(pagesize_text_id)s",%(pagesize_def)s);' />
88 </fieldset></form></td></tr>
89 """ % result_dict
90         return result
91
92     ##########      
93     def search_area_html (self):
94         width = len(self.headers)
95         search_text_id = self.table_id + "_search"
96         search_reset_id = self.table_id + "_search_reset"
97         search_and_id = self.table_id + "_search_and"
98         result_dict = locals()
99         result_dict.update(self.__dict__)
100         result = """
101 <tr class='search_area'><td class='search_area' colspan='%(width)s'>
102 <div class='search'><fieldset>
103    <label class='search_label'> Search </label> 
104    <input class='search_input' type='text' id='%(search_text_id)s'
105       onkeyup='plekit_table_filter("%(table_id)s","%(search_text_id)s","%(search_and_id)s");'
106       size='%(search_width)s' maxlength='256' />
107    <label>and</label>
108    <input id='%(search_and_id)s' class='search_and' 
109       type='checkbox' checked='checked' 
110       onchange='plekit_table_filter("%(table_id)s","%(search_text_id)s","%(search_and_id)s");' />
111    <img class='reset' src="/planetlab/icons/clear.png" alt="reset search"
112       onmousedown='plekit_table_filter_reset("%(table_id)s","%(search_text_id)s","%(search_and_id)s");' />
113 </fieldset></div></td></tr>
114 """ % result_dict
115         return result
116
117     ##########
118     ## start a <tfoot> section
119     def tfoot_start (self): print self.tfoot_start_html()
120     def tfoot_start_html (self):
121         self.has_tfoot=true
122         return "</tbody><tfoot>"
123
124     ##########
125     def bottom_html (self):
126         result=""
127         if self.has_tfoot:
128             result += "</tfoot>"
129         else:
130             result += "</tbody>"
131             result += "</table>\n"
132         return result
133
134     ##########
135     default_notes =  [
136         "Enter &amp; or | in the search area to switch between <span class='bold'>AND</span> and <span class='bold'>OR</span> search modes",
137         "Hold down the shift key to select multiple columns to sort" ]
138
139     def notes_area_html (self):
140         if (self.notes):
141             notes=self.notes
142         else:
143             notes=[]
144         notes = notes + self.default_notes
145         result = ""
146         result += "<p class='table_note'> <span class='table_note_title'>Notes</span>\n"
147         for note in notes:
148             result += "<br/>%s\n" % note
149         result += "</p>"
150         return result
151
152     ##########
153     def row_start (self, id=None, classpart=None):
154         print "<tr"
155         if id: print " id='%s'" % id
156         if classpart: print ' class="%s"' % classpart
157         print ">\n"
158         
159     def row_end (self):
160         print "</tr>\n"
161
162     ##########
163     ## supported options:
164     ## (*) only-if : if set and false, then print 'n/a' instead of (presumably void) $text
165     ## (*) classpart
166     ## (*) columns
167     ## (*) hfill
168     ## (*) align
169     def cell (self, text,only_if=True, classpart=None, columns=None, hfill=None, align=None): 
170         print self.cell_html (text, only_if, classpart, columns, hfill, align)
171     def cell_html (self, text, only_if=True, classpart=None, columns=None, hfill=None, align=None):
172         if not only_if:
173             text="n/a"
174         html=""
175         html += "<td"
176         if classpart: html += " class='%s'" % classpart
177         if columns: html += " colspan='%d'" % columns
178         elif hfill: html += " colspan='%d'" % self.columns()
179         if align: html += " style='text-align:%s'" % align
180         html += ">%s</td>" % text
181         return html