0e297d7d995cfff2156fc8284003ad2b244e871a
[samba.git] / swat / scripting / common.js
1 /*
2         js functions and code common to all pages
3 */
4
5 /* define some global variables for this request */
6 global.page = new Object();
7
8 /* fill in some defaults */
9 global.page.title = "Samba Web Administration Tool";
10
11 /* to cope with browsers that don't support cookies we append the sessionid
12    to the URI */
13 global.SESSIONURI = "";
14 if (request['COOKIE_SUPPORT'] != "True") {
15         global.SESSIONURI="?SwatSessionId=" + request['SESSION_ID'];
16 }
17
18 /*
19   possibly adjust a local URI to have the session id appended
20   used for browsers that don't support cookies
21 */
22 function session_uri(uri) {
23         return uri + global.SESSIONURI;
24 }
25
26 /*
27   like printf, but to the web page
28 */
29 function writef()
30 {
31         write(vsprintf(arguments));
32 }
33
34 /*
35   like writef with a <br>
36 */
37 function writefln()
38 {
39         write(vsprintf(arguments));
40         write("<br/>\n");
41 }
42
43
44 /* if the browser was too dumb to set the HOST header, then
45    set it now */
46 if (headers['HOST'] == undefined) {
47         headers['HOST'] = server['SERVER_HOST'] + ":" + server['SERVER_PORT'];
48 }
49
50 /*
51   show the page header. page types include "plain" and "column" 
52 */
53 function page_header(pagetype, title) {
54         global.page.pagetype = pagetype;
55         global.page.title = title;
56         include("/scripting/header_" + pagetype + ".esp");
57 }
58
59 /*
60   show the page footer, getting the page type from page.pagetype
61   set in page_header()
62 */
63 function page_footer() {
64         include("/scripting/footer_" + global.page.pagetype + ".esp");
65 }
66
67
68 /*
69   check if a uri is one of the 'always allowed' pages, even when not logged in
70   This allows the login page to use the same style sheets and images
71 */
72 function always_allowed(uri) {
73         var allowed = new Array("/images/favicon.ico", 
74                                 "/images/linkpad.png",
75                                 "/images/logo.png",
76                                 "/style/main.css",
77                                 "/style/common.css");
78         for (i in allowed) {
79                 if (allowed[i] == uri) {
80                         return true;
81                 }
82         }
83         return false;
84 }
85
86 /*
87   create a menu object with the defaults filled in, ready for display_menu()
88  */
89 function MenuObj(name, num_elements)
90 {
91         var o = new Object();
92         o.name = name;
93         o.class = "menu";
94         o.style = "simple";
95         o.orientation = "vertical"
96         o.element = new Array(num_elements);
97         for (i in o.element) {
98                 o.element[i] = new Object();
99         }
100         return o;
101 }
102
103 /*
104   display a menu object. Currently only the "simple", "vertical" menu style
105   is supported
106 */
107 function display_menu(m) {
108         assert(m.style == "simple" && m.orientation == "vertical");
109         write('<div class="' + m.class + '">\n');
110         write("<i>" + m.name + "</i><br /><ul>\n");
111         for (i = 0; i < m.element.length; i++) {
112                 var e = m.element[i];
113                 write("<li><a href=\"" + e.link + "\">" + e.label + "</a></li>\n");
114         }
115         write("</ul></div>\n");
116 }
117
118 function simple_menu() {
119         var i, m = MenuObj(arguments[0], (arguments.length-1)/2);
120         for (i=0;i<m.element.length;i++) {
121                 var ndx = i*2;
122                 m.element[i].label = arguments[ndx+1];
123                 m.element[i].link = arguments[ndx+2];
124         }
125         display_menu(m);
126 }
127
128 /*
129   display a table element
130 */
131 function table_element(i, o) {
132         write("<tr><td>" + i + "</td><td>");
133         if (typeof(o[i]) == "object") {
134                 var j, first;
135                 first = true;
136                 for (j in o[i]) {
137                         if (first == false) {
138                                 write("<br />");
139                         }
140                         write(o[i][j]);
141                         first = false;
142                 }
143         } else {
144                 write(o[i]);
145         }
146         write("</td></tr>\n");
147 }
148
149 /*
150   display a ejs object as a table. The header is optional
151 */
152 function simple_table(v) {
153         if (v.length == 0) {
154                 return;
155         }
156         write("<table class=\"data\">\n");
157         var r;
158         for (r in v) {
159                 table_element(r, v);
160         }
161         write("</table>\n");
162 }
163
164 /*
165   display an array of objects, with the header for each element from the given 
166   attribute
167 */
168 function multi_table(array, header) {
169         var i, n;
170         write("<table class=\"data\">\n");
171         for (i=0;i<array.length;i++) {
172                 var r, v = array[i];
173                 write('<tr><th colspan="2">' + v[header] + "</th></tr>\n");
174                 for (r in v) {
175                         if (r != header) {
176                             table_element(r, v);
177                         }
178                 }
179         }
180         write("</table>\n");
181 }
182
183 /*
184   create a Form object with the defaults filled in, ready for display_form()
185  */
186 function FormObj(name, num_elements, num_submits)
187 {
188         var f = new Object();
189         f.name = name;
190         f.element = new Array(num_elements);
191         f.submit =  new Array(num_submits);
192         f.action = session_uri(request.REQUEST_URI);
193         f.class = "defaultform";
194         for (i in f.element) {
195                 f.element[i] = new Object();
196                 f.element[i].type = "text";
197                 f.element[i].value = "";
198         }
199         return f;
200 }
201
202 /*
203   display a simple form from a ejs Form object
204   caller should fill in
205     f.name          = form name
206     f.action        = action to be taken on submit (optional, defaults to current page)
207     f.class         = css class (optional, defaults to 'form')
208     f.submit        = an array of submit labels
209     f.element[i].label = element label
210     f.element[i].name  = element name (defaults to label)
211     f.element[i].type  = element type (defaults to text)
212     f.element[i].value = current value (optional, defaults to "")
213  */
214 function display_form(f) {
215         var i, size = 20;
216         write('<form name="' + f.name +
217               '" method="post" action="' + f.action + 
218               '" class="' + f.class + '">\n');
219         if (f.element.length > 0) {
220                 write("<table>\n");
221         }
222         for (i in f.element) {
223                 var e = f.element[i];
224                 if (e.name == undefined) {
225                         e.name = e.label;
226                 }
227                 if (e.value == undefined) {
228                         e.value = "";
229                 }
230                 if (strlen(e.value) > size) {
231                         size = strlen(e.value) + 4;
232                 }
233         }
234         for (i in f.element) {
235                 var e = f.element[i];
236                 write("<tr>");
237                 write("<td>" + e.label + "</td>");
238                 if (e.type == "select") {
239                         write('<td><select name="' + e.name + '">\n');
240                         for (s in e.list) {
241                                 if (e.value == e.list[s]) {
242                                         write('<option selected=selected>' + e.list[s] + '</option>\n');
243                                 } else {
244                                         write('<option>' + e.list[s] + '</option>\n');
245                                 }
246                         }
247                         write('</select></td>\n');
248                 } else {
249                         var sizestr = "";
250                         if (e.type == "text" || e.type == "password") {
251                                 sizestr = sprintf('size="%d"', size);
252                         }
253                         writef('<td><input name="%s" type="%s" value="%s" %s /></td>\n',
254                                e.name, e.type, e.value, sizestr);
255                 }
256                 write("</tr>");
257         }
258         if (f.element.length > 0) {
259                 write("</table>\n");
260         }
261         for (i in f.submit) {
262                 write('<input name="submit" type="submit" value="' + f.submit[i] + '" />\n');
263         }
264         write("</form>\n");
265 }
266