r7134: a number of small changes to make the pages HTML compliant. The
[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
28 /* if the browser was too dumb to set the HOST header, then
29    set it now */
30 if (headers['HOST'] == undefined) {
31         headers['HOST'] = server['SERVER_HOST'] + ":" + server['SERVER_PORT'];
32 }
33
34 /*
35   show the page header. page types include "plain" and "column" 
36 */
37 function page_header(pagetype, title) {
38         global.page.pagetype = pagetype;
39         global.page.title = title;
40         include("/scripting/header_" + pagetype + ".esp");
41 }
42
43 /*
44   show the page footer, getting the page type from page.pagetype
45   set in page_header()
46 */
47 function page_footer() {
48         include("/scripting/footer_" + global.page.pagetype + ".esp");
49 }
50
51
52 /*
53   check if a uri is one of the 'always allowed' pages, even when not logged in
54   This allows the login page to use the same style sheets and images
55 */
56 function always_allowed(uri) {
57         var allowed = new Array("/images/favicon.ico", 
58                                 "/images/linkpad.gif",
59                                 "/images/logo.png",
60                                 "/images/logo.gif",
61                                 "/style/main.css",
62                                 "/style/common.css");
63         for (i in allowed) {
64                 if (allowed[i] == uri) {
65                         return true;
66                 }
67         }
68         return false;
69 }
70
71 /*
72   create a menu object with the defaults filled in, ready for display_menu()
73  */
74 function MenuObj(name, num_elements)
75 {
76         var o = new Object();
77         o.name = name;
78         o.class = "menu";
79         o.style = "simple";
80         o.orientation = "vertical"
81         o.element = new Array(num_elements);
82         for (i in o.element) {
83                 o.element[i] = new Object();
84         }
85         return o;
86 }
87
88 /*
89   display a menu object. Currently only the "simple", "vertical" menu style
90   is supported
91 */
92 function display_menu(m) {
93         assert(m.style == "simple" && m.orientation == "vertical");
94         write('<div class="' + m.class + '">\n');
95         write("<i>" + m.name + "</i><br /><ul>\n");
96         for (i = 0; i < m.element.length; i++) {
97                 var e = m.element[i];
98                 write("<li><a href=\"" + e.link + "\">" + e.label + "</a></li>\n");
99         }
100         write("</ul></div>\n");
101 }
102
103 function simple_menu() {
104         var m = MenuObj(arguments[0], (arguments.length-1)/2);
105         for (i=0;i<m.element.length;i++) {
106                 m.element[i].label = arguments[1+(i*2)];
107                 m.element[i].link = arguments[2+(i*2)];
108         }
109         display_menu(m);
110 }
111
112 /*
113   display a table element
114 */
115 function table_element(i, o) {
116         write("<tr><td>" + i + "</td><td>");
117         if (typeof(o[i]) == "object") {
118                 var first;
119                 first = true;
120                 for (j in o[i]) {
121                         if (first == false) {
122                                 write("<br />");
123                         }
124                         write(o[i][j]);
125                         first = false;
126                 }
127         } else {
128                 write(o[i]);
129         }
130         write("</td></tr>\n");
131 }
132
133 /*
134   return the number of elements in an object
135 */
136 function elcount(o) {
137         var count = 0;
138         for (i in o) {
139                 count++;
140         }
141         return count;
142 }
143
144 /*
145   display a ejs object as a table. The header is optional
146 */
147 function simple_table(v) {
148         if (elcount(v) == 0) {
149                 return;
150         }
151         write("<table class=\"data\">\n");
152         for (r in v) {
153                 table_element(r, v);
154         }
155         write("</table>\n");
156 }
157
158 /*
159   display an array of objects, with the header for each element from the given 
160   attribute
161 */
162 function multi_table(array, header) {
163         if (elcount(v) == 0) {
164                 return;
165         }
166         write("<table class=\"data\">\n");
167         for (i in array) {
168                 var v = array[i];
169                 write('<tr><th colspan="2">' + v[header] + "</th></tr>\n");
170                 for (r in v) {
171                         if (r != header) {
172                             table_element(r, v);
173                         }
174                 }
175         }
176         write("</table>\n");
177 }
178
179 /*
180   create a Form object with the defaults filled in, ready for display_form()
181  */
182 function FormObj(name, num_elements, num_submits)
183 {
184         var f = new Object();
185         f.name = name;
186         f.element = new Array(num_elements);
187         f.submit =  new Array(num_submits);
188         f.action = session_uri(request.REQUEST_URI);
189         f.class = "defaultform";
190         for (i in f.element) {
191                 f.element[i] = new Object();
192                 f.element[i].type = "text";
193                 f.element[i].value = "";
194         }
195         return f;
196 }
197
198 /*
199   display a simple form from a ejs Form object
200   caller should fill in
201     f.name          = form name
202     f.action        = action to be taken on submit (optional, defaults to current page)
203     f.class         = css class (optional, defaults to 'form')
204     f.submit        = an array of submit labels
205     f.element[i].label = element label
206     f.element[i].name  = element name (defaults to label)
207     f.element[i].type  = element type (defaults to text)
208     f.element[i].value = current value (optional, defaults to "")
209  */
210 function display_form(f) {
211         write('<form name="' + f.name +
212               '" method="post" action="' + f.action + 
213               '" class="' + f.class + '">\n');
214         if (f.element.length > 0) {
215                 write("<table>\n");
216         }
217         for (i in f.element) {
218                 var e = f.element[i];
219                 if (e.name == undefined) {
220                         e.name = e.label;
221                 }
222                 if (e.value == undefined) {
223                         e.value = "";
224                 }
225                 write("<tr>");
226                 write("<td>" + e.label + "</td>");
227                 if (e.type == "select") {
228                         write('<td><select name="' + e.name + '">\n');
229                         for (s in e.list) {
230                                 if (e.value == e.list[s]) {
231                                         write('<option selected=selected>' + e.list[s] + '</option>\n');
232                                 } else {
233                                         write('<option>' + e.list[s] + '</option>\n');
234                                 }
235                         }
236                         write('</select></td>\n');
237                 } else {
238                         write('<td><input name="' + e.name + '" type="' + 
239                               e.type + '" value="' + e.value + '" /></td>\n');
240                 }
241                 write("</tr>");
242         }
243         if (f.element.length > 0) {
244                 write("</table>\n");
245         }
246         for (i in f.submit) {
247                 write('<input name="submit" type="submit" value="' + f.submit[i] + '" />\n');
248         }
249         write("</form>\n");
250 }
251