1
/* IVLE - Informatics Virtual Learning Environment
2
* Copyright (C) 2007-2008 The University of Melbourne
4
* This program is free software; you can redistribute it and/or modify
5
* it under the terms of the GNU General Public License as published by
6
* the Free Software Foundation; either version 2 of the License, or
7
* (at your option) any later version.
9
* This program is distributed in the hope that it will be useful,
10
* but WITHOUT ANY WARRANTY; without even the implied warranty of
11
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12
* GNU General Public License for more details.
14
* You should have received a copy of the GNU General Public License
15
* along with this program; if not, write to the Free Software
16
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
* Module: JavaScript Utilities
22
* Defines some generic JavaScript utility functions.
25
/** Removes all children of a given DOM element
26
* \param elem A DOM Element. Will be modified.
28
function dom_removechildren(elem)
30
while (elem.lastChild != null)
31
elem.removeChild(elem.lastChild);
34
/** Creates a DOM element with simple text inside it.
35
* \param tagname String. Name of the element's tag (eg. "p").
36
* \param text String. Text to be placed inside the element.
37
* \return DOM Element object.
39
function dom_make_text_elem(tagname, text)
41
var elem = document.createElement(tagname);
42
elem.appendChild(document.createTextNode(text));
46
/** Creates a DOM element with hyperlinked text inside it.
47
* \param tagname String. Name of the element's tag (eg. "p").
48
* \param text String. Text to be placed inside the element.
49
* \param href String. URL the text will link to. This is a raw string,
50
* it will automatically be URL-encoded.
51
* \param onclick Optional string. Will be set as the "onclick" attribute
53
* \return DOM Element object.
55
function dom_make_link_elem(tagname, text, href, onclick)
57
var elem = document.createElement(tagname);
58
var link = document.createElement("a");
59
link.setAttribute("href", encodeURI(href));
61
link.setAttribute("onclick", onclick);
62
link.appendChild(document.createTextNode(text));
63
elem.appendChild(link);
67
/** Converts a list of directories into a path name, with a slash at the end.
68
* \param pathlist List of strings.
71
function pathlist_to_path(pathlist)
74
for (var i=0; i<pathlist.length; i++)
76
ret += pathlist[i] + "/";
81
/** Given a URL, returns an object containing a number of attributes
82
* describing the components of the URL, similar to CGI request variables.
83
* The object has the following attributes:
90
* The first five of these are strings, which comprise the URL as follows:
91
* <scheme> "://" <server_name> ":" <server_port> <path> "?" <query_string>
92
* Any of these strings may be set to null if not found.
94
* "args" is an object whose attributes are the query_string arguments broken
96
* Args values are strings for single values, arrays of strings for values
97
* whose names appear multiple times.
98
* args is never null, though it may be empty.
100
* The args strings are decoded from URL encoding form. Other strings are left
103
* \param url String. A URL. To read from the current browser window, use
104
* window.location.href.
105
* \return The above described object.
107
function parse_url(url)
114
/* Split scheme from rest */
115
index = url.indexOf("://");
120
obj.scheme = url.substr(0, index);
121
url = url.substr(index+3);
124
/* Split server name/port from rest */
125
index = url.indexOf("/");
133
serverpart = url.substr(0, index);
134
url = url.substr(index+1);
137
/* Split server name from port */
138
index = serverpart.indexOf(":");
141
obj.server_name = serverpart;
142
obj.server_port = null;
146
obj.server_name = serverpart.substr(0, index);
147
obj.server_port = serverpart.substr(index+1);
150
/* Split path from query string */
154
obj.query_string = null;
158
index = url.indexOf("?");
162
obj.query_string = null;
166
obj.path = url.substr(0, index);
167
obj.query_string = url.substr(index+1);
171
/* Split query string into arguments */
173
if (obj.query_string != null)
175
var args_strs = obj.query_string.split("&");
177
var arg_key, arg_val;
178
for (var i=0; i<args_strs.length; i++)
180
arg_str = args_strs[i];
181
index = arg_str.indexOf("=");
182
/* Ignore malformed args */
185
arg_key = decodeURI(arg_str.substr(0, index));
186
arg_val = decodeURI(arg_str.substr(index+1));
189
/* Collision - make an array */
190
if (args[arg_key] instanceof Array)
191
args[arg_key][args[arg_key].length] = arg_val;
193
args[arg_key] = [args[arg_key], arg_val];
196
args[arg_key] = arg_val;
205
/** Given an object exactly of the form described for the output of parseurl,
206
* returns a URL string built from those parameters.
207
* parseurl and buildurl are strict inverses of each other.
208
* Note that either query_string or args may be supplied. If both are
209
* supplied, query_string is preferred (because it keeps the argument order).
210
* If you take a url from parseurl, modify args, and pass to buildurl,
211
* you need to set query_string to null to use the new args.
212
* \param obj Object as returned by parseurl.
213
* \return String, a URL.
215
function buildurl(obj)
218
var query_string = null;
220
if (!("scheme" in obj) || obj.scheme != null)
221
url = obj.scheme.toString() + "://";
222
if (!("server_name" in obj) || obj.server_name != null)
223
url += obj.server_name.toString();
224
if (!("server_port" in obj) || obj.server_port != null)
225
url += ":" + obj.server_port.toString();
226
if (!("path" in obj) || obj.path != null)
228
var path = obj.path.toString();
229
if (path.length > 0 && path[0] != "/")
233
if (!("query_string" in obj) || obj.query_string != null)
234
query_string = obj.query_string.toString();
235
else if (!("args" in obj) || obj.args != null)
239
for (var arg_key in obj.args)
241
arg_val = obj.args[arg_key];
242
if (arg_val instanceof Array)
243
for (var i=0; i<arg_val.length; i++)
244
query_string += "&" + encodeURI(arg_key) + "=" +
245
encodeURI(arg_val[i]);
247
query_string += "&" + encodeURI(arg_key) + "=" +
251
if (query_string == "")
254
/* Drop the first "&" */
255
query_string = query_string.substr(1);
258
if (query_string != null)
259
url += "?" + query_string;
264
/** Given an argument map, as output in the args parameter of the return of
265
* parseurl, gets the first occurence of an argument in the URL string.
266
* If the argument was not found, returns null.
267
* If there was a single argument, returns the argument.
268
* If there were multiple arguments, returns the first.
269
* \param args Object mapping arguments to strings or arrays of strings.
270
* \param arg String. Argument name.
273
function arg_getfirst(args, arg)
278
if (r instanceof Array)
284
/** Given an argument map, as output in the args parameter of the return of
285
* parseurl, gets all occurences of an argument in the URL string, as an
287
* If the argument was not found, returns [].
288
* Otherwise, returns all occurences as an array, even if there was only one.
289
* \param args Object mapping arguments to strings or arrays of strings.
290
* \param arg String. Argument name.
291
* \return Array of strings.
293
function arg_getlist(args, arg)
298
if (r instanceof Array)