r7132: - start a convention of making object constructors end in Obj, so we
[kai/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   display a ejs object as a table. The header is optional
134 */
135 function simple_table(v) {
136         write("<table class=\"data\">\n");
137         for (r in v) {
138                 table_element(r, v);
139         }
140         write("</table>\n");
141 }
142
143 /*
144   display an array of objects, with the header for each element from the given 
145   attribute
146 */
147 function multi_table(array, header) {
148         write("<table class=\"data\">\n");
149         for (i in array) {
150                 var v = array[i];
151                 write("<tr><th colspan=2>" + v[header] + "</th></tr>\n");
152                 for (r in v) {
153                         if (r != header) {
154                             table_element(r, v);
155                         }
156                 }
157         }
158         write("</table>\n");
159 }
160
161 /*
162   create a Form object with the defaults filled in, ready for display_form()
163  */
164 function FormObj(name, num_elements, num_submits)
165 {
166         var f = new Object();
167         f.name = name;
168         f.element = new Array(num_elements);
169         f.submit =  new Array(num_submits);
170         f.action = session_uri(request.REQUEST_URI);
171         f.class = "form";
172         for (i in f.element) {
173                 f.element[i] = new Object();
174                 f.element[i].type = "text";
175                 f.element[i].value = "";
176         }
177         return f;
178 }
179
180 /*
181   display a simple form from a ejs Form object
182   caller should fill in
183     f.name          = form name
184     f.action        = action to be taken on submit (optional, defaults to current page)
185     f.class         = css class (optional, defaults to 'form')
186     f.submit        = an array of submit labels
187     f.element[i].label = element label
188     f.element[i].name  = element name (defaults to label)
189     f.element[i].type  = element type (defaults to text)
190     f.element[i].value = current value (optional, defaults to "")
191  */
192 function display_form(f) {
193         write('<form name="' + f.name +
194               '" method="post" action="' + f.action + 
195               '" class="' + f.class + '">\n');
196         write("<table>\n");
197         for (i in f.element) {
198                 var e = f.element[i];
199                 if (e.name == undefined) {
200                         e.name = e.label;
201                 }
202                 if (e.value == undefined) {
203                         e.value = '""';
204                 }
205                 write("<tr>");
206                 write("<td>" + e.label + "</td>");
207                 if (e.type == "select") {
208                         write('<td><select name="' + e.name + '">\n');
209                         for (s in e.list) {
210                                 if (e.value == e.list[s]) {
211                                         write('<option selected=selected>' + e.list[s] + '</option>\n');
212                                 } else {
213                                         write('<option>' + e.list[s] + '</option>\n');
214                                 }
215                         }
216                         write('</select></td>\n');
217                 } else {
218                         write('<td><input name="' + e.name + '" type="' + 
219                               e.type + '" value="' + e.value + '"></td>\n');
220                 }
221         }
222         write("</table>\n");
223         for (i in f.submit) {
224                 write('<input name="submit" type="submit" value="' + f.submit[i] + '">\n');
225         }
226         write("</form>\n");
227 }
228