1 | import os |
---|
2 | |
---|
3 | from genshi.template import TemplateLoader |
---|
4 | from genshi.builder import * |
---|
5 | from genshi import HTML |
---|
6 | |
---|
7 | |
---|
8 | class FormRenderer(object): |
---|
9 | """ |
---|
10 | A set of rendering methods for HTML forms. |
---|
11 | """ |
---|
12 | |
---|
13 | def __init__(self): |
---|
14 | # Instantiate Genshi template loader |
---|
15 | self.templateLoader = TemplateLoader( |
---|
16 | os.path.join(os.getcwd(), 'cows_wps/templates'), |
---|
17 | auto_reload=True) |
---|
18 | |
---|
19 | def htmlify(self, html): return HTML(html) |
---|
20 | |
---|
21 | def renderRadioButton(self, name, is_boolean=True, values=None): |
---|
22 | """ |
---|
23 | Returns HTML of radio button. |
---|
24 | """ |
---|
25 | if is_boolean == True: |
---|
26 | values = ("true", "false") |
---|
27 | |
---|
28 | html = "" |
---|
29 | for value in values: |
---|
30 | html += """ <input type="radio" id="%s" name="%s" value="%s" /> |
---|
31 | <label for="%s">%s</label><br />\n""" |
---|
32 | |
---|
33 | return html |
---|
34 | |
---|
35 | def renderTextInput(self, name, dtype, optional=False, default=None): |
---|
36 | """ |
---|
37 | Returns HTML for text input including onChange validator. |
---|
38 | optional is a boolean. |
---|
39 | |
---|
40 | Populates with default value if not None. |
---|
41 | """ |
---|
42 | if default == None: default = "" |
---|
43 | |
---|
44 | allowed_dtypes = ("float", "int", "string", "datetime") |
---|
45 | js_opt = str(optional).lower() |
---|
46 | |
---|
47 | if dtype not in allowed_dtypes: |
---|
48 | raise Exception("Invalid type sent to FormRenderer.renderTextInput(): %s" % dtype) |
---|
49 | |
---|
50 | validators = {"int": ("/^[0-9]+$/", "The %s input must be an integer." % name), |
---|
51 | "float": ("/^[-+]?([0-9]*\.[0-9]+|[0-9]+)$/", "The %s input must be a decimal number." % name), |
---|
52 | "string": ("/[0-9a-zA-Z]+/", "The %s input must include text characters." % name), |
---|
53 | "datetime": ("/[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}/", "The %s input must be a date/time entry as follows YYYY-MM-DDThh:mm:ss (e.g. 2009-01-01T12:00:00)." % name)} |
---|
54 | |
---|
55 | (regex, msg) = validators[dtype] |
---|
56 | |
---|
57 | html = """<input type="text" name="%s" id="%s" value="%s" onChange="validateTextInput(this.value, %s, '%s', %s);" /><br />\n""" % (name, name, default, regex, msg, js_opt) |
---|
58 | html += """<script type="text/javascript">addValidator('%s', 'text', %s, '%s', %s);</script>\n""" % (name, regex, msg, js_opt) |
---|
59 | return html |
---|
60 | |
---|
61 | def renderSelectList(self, name, values, optional=False, multiple=False): |
---|
62 | """ |
---|
63 | Returns HTML for select list. |
---|
64 | |
---|
65 | If optional is True then add an extra option called "-- Please select --". |
---|
66 | """ |
---|
67 | if optional == True: |
---|
68 | values.insert(0, "-- Please select --") |
---|
69 | |
---|
70 | multiple_string = "" |
---|
71 | if multiple == True: |
---|
72 | multiple_string = 'multiple="multiple"' |
---|
73 | |
---|
74 | html = """<select %s name="%s" id="%s">\n""" % (multiple_string, name, name) |
---|
75 | |
---|
76 | selected = 'selected="selected"' |
---|
77 | for value in values: |
---|
78 | html += """ <option %s value="%s">%s</option>\n""" % (selected, value, value) |
---|
79 | selected = "" |
---|
80 | |
---|
81 | return html |
---|
82 | |
---|
83 | def renderBBox(self, name, extent="90|-180|-90|180"): |
---|
84 | """ |
---|
85 | Renders a bounding box and N/W/S/E/ selectors. |
---|
86 | """ |
---|
87 | if extent == False: extent = "90|-180|-90|180" |
---|
88 | |
---|
89 | (n, w, s, e) = extent.split("|") |
---|
90 | |
---|
91 | html = """ <label for="_north">North:</label> <input type="text" id="_north" name="_north" value="%s" onChange="updateBBox('%s');" /><br> |
---|
92 | <label for="_west">West:</label> <input type="text" id="_west" name="_west" value="%s" onChange="updateBBox('%s');" /><br> |
---|
93 | <label for="_south">South:</label> <input type="text" id="_south" name="_south" value="%s" onChange="updateBBox('%s');" /><br> |
---|
94 | <label for="_east">East:</label> <input type="text" id="_east" name="_east" value="%s" onChange="updateBBox('%s');" /><br> |
---|
95 | <input type="hidden" id="%s" name="%s" value="%s" /> |
---|
96 | """ % (n, name, w, name, s, name, e, name, name, name, extent) |
---|
97 | |
---|
98 | return html |
---|
99 | |
---|
100 | |
---|
101 | def renderBBoxOLD(self, name, extent): |
---|
102 | """ |
---|
103 | Renders a bounding box and N/W/S/E selectors. |
---|
104 | """ |
---|
105 | html = """ <script type="text/javascript" src="/js/ext/jquery-1.3.2/jquery-1.3.2.js"></script> |
---|
106 | <script type="text/javascript" src="http://openlayers.org/api/OpenLayers.js"></script> |
---|
107 | <script type="text/javascript" src="/js/ui/openlayers-x.js"></script> |
---|
108 | <script type="text/javascript" src="/js/ui/boundingBoxMapSelect.js"></script> |
---|
109 | <script type="text/javascript"> |
---|
110 | var layers = new Array(); |
---|
111 | var layer_data = false; |
---|
112 | var bboxSelect = false; |
---|
113 | |
---|
114 | $(document).ready(function() { |
---|
115 | var layer_data = new OpenLayers.Layer.WMS( "World Map", "http://labs.metacarta.com/wms-c/Basic.py?", {layers: 'basic', format: 'image/png' } ); |
---|
116 | layers.push(layer_data); |
---|
117 | var bboxSelect = new BoundingBoxMapSelect('map', 'bounds_control_container', layers, 'bbox_hidden_input', null); |
---|
118 | }); |
---|
119 | </script> |
---|
120 | |
---|
121 | <div id="bounding_container"> |
---|
122 | <div id="bounds_control_container"></div> |
---|
123 | <div id="map_container"> |
---|
124 | <div id='map'></div> |
---|
125 | </div> |
---|
126 | <input type="hidden" name="bbox" id="bbox_hidden_input" value="" /> |
---|
127 | </div> |
---|
128 | """ |
---|
129 | return html |
---|
130 | |
---|
131 | def renderTypeAheadDirList(self, name, base_dir): |
---|
132 | """ |
---|
133 | Renders type-ahead widget for directory listing. |
---|
134 | """ |
---|
135 | html = """ <script type="text/javascript" src="/js/ext/jquery-1.3.2/jquery-1.3.2.js"></script> |
---|
136 | <script type="text/javascript" src="/js/ui/type_ahead_dirs.js"></script> |
---|
137 | <script type="text/javascript"> |
---|
138 | var current_list = false; |
---|
139 | var abs_pos_%s = null; |
---|
140 | |
---|
141 | $(document).ready(function(){ |
---|
142 | initTypeAhead("%s", "type_ahead_for_%s", "%s"); |
---|
143 | abs_pos_%s = type_ahead_div.offset(); |
---|
144 | positionRespDiv(abs_pos_%s); |
---|
145 | }); |
---|
146 | </script> |
---|
147 | |
---|
148 | <p>Press down button to fill with text...</p> |
---|
149 | <div style="z-index: 1;"> |
---|
150 | <input type="text" id="%s" name="%s" value="" style="width: 505px; height: 25px;" onBlur="hideSuggestions();" onFocus="showSuggestions();" /> |
---|
151 | </div> |
---|
152 | <!--<span id="resp_container"> |
---|
153 | <div id="type_ahead_for_%s" style="width: 500px; background-color: yellow; border-style: solid; border-width: 1px; position: relative;"></div> |
---|
154 | </span>--> |
---|
155 | <div id="type_ahead_for_%s" style="width: 500px; background-color: yellow; border-style: solid; border-width: 1px; position: statis;"></div> |
---|
156 | """ % (name, name, name, base_dir, name, name, name, name, name, name) |
---|
157 | return html |
---|