~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to www/media/common/util.js

  • Committer: mattgiuca
  • Date: 2008-01-11 00:49:06 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:172
util: Added buildurl function.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* IVLE - Informatics Virtual Learning Environment
 
2
 * Copyright (C) 2007-2008 The University of Melbourne
 
3
 *
 
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.
 
8
 *
 
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.
 
13
 *
 
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
 
17
 *
 
18
 * Module: JavaScript Utilities
 
19
 * Author: Matt Giuca
 
20
 * Date: 11/1/2008
 
21
 *
 
22
 * Defines some generic JavaScript utility functions.
 
23
 */
 
24
 
 
25
/** Removes all children of a given DOM element
 
26
 * \param elem A DOM Element. Will be modified.
 
27
 */
 
28
function dom_removechildren(elem)
 
29
{
 
30
    while (elem.lastChild != null)
 
31
        elem.removeChild(elem.lastChild);
 
32
}
 
33
 
 
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.
 
38
 */
 
39
function dom_make_text_elem(tagname, text)
 
40
{
 
41
    var elem = document.createElement(tagname);
 
42
    elem.appendChild(document.createTextNode(text));
 
43
    return elem;
 
44
}
 
45
 
 
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
 
52
 *  of the "a" element.
 
53
 * \return DOM Element object.
 
54
 */
 
55
function dom_make_link_elem(tagname, text, href, onclick)
 
56
{
 
57
    var elem = document.createElement(tagname);
 
58
    var link = document.createElement("a");
 
59
    link.setAttribute("href", encodeURI(href));
 
60
    if (onclick != null)
 
61
        link.setAttribute("onclick", onclick);
 
62
    link.appendChild(document.createTextNode(text));
 
63
    elem.appendChild(link);
 
64
    return elem;
 
65
}
 
66
 
 
67
/** Converts a list of directories into a path name, with a slash at the end.
 
68
 * \param pathlist List of strings.
 
69
 * \return String.
 
70
 */
 
71
function pathlist_to_path(pathlist)
 
72
{
 
73
    ret = "";
 
74
    for (var i=0; i<pathlist.length; i++)
 
75
    {
 
76
        ret += pathlist[i] + "/";
 
77
    }
 
78
    return ret;
 
79
}
 
80
 
 
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:
 
84
 * - scheme
 
85
 * - server_name
 
86
 * - server_port
 
87
 * - path
 
88
 * - query_string
 
89
 * - args
 
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.
 
93
 *
 
94
 * "args" is an object whose attributes are the query_string arguments broken
 
95
 * up.
 
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.
 
99
 *
 
100
 * The args strings are decoded from URL encoding form. Other strings are left
 
101
 * in raw URL form.
 
102
 *
 
103
 * \param url String. A URL. To read from the current browser window, use
 
104
 *  window.location.href.
 
105
 * \return The above described object.
 
106
 */
 
107
function parse_url(url)
 
108
{
 
109
    var obj = {};
 
110
    var index;
 
111
    var serverpart;
 
112
    var args;
 
113
 
 
114
    /* Split scheme from rest */
 
115
    index = url.indexOf("://");
 
116
    if (index < 0)
 
117
        obj.scheme = null;
 
118
    else
 
119
    {
 
120
        obj.scheme = url.substr(0, index);
 
121
        url = url.substr(index+3);
 
122
    }
 
123
 
 
124
    /* Split server name/port from rest */
 
125
    index = url.indexOf("/");
 
126
    if (index < 0)
 
127
    {
 
128
        serverpart = url;
 
129
        url = null;
 
130
    }
 
131
    else
 
132
    {
 
133
        serverpart = url.substr(0, index);
 
134
        url = url.substr(index+1);
 
135
    }
 
136
 
 
137
    /* Split server name from port */
 
138
    index = serverpart.indexOf(":");
 
139
    if (index < 0)
 
140
    {
 
141
        obj.server_name = serverpart;
 
142
        obj.server_port = null;
 
143
    }
 
144
    else
 
145
    {
 
146
        obj.server_name = serverpart.substr(0, index);
 
147
        obj.server_port = serverpart.substr(index+1);
 
148
    }
 
149
 
 
150
    /* Split path from query string */
 
151
    if (url == null)
 
152
    {
 
153
        obj.path = null;
 
154
        obj.query_string = null;
 
155
    }
 
156
    else
 
157
    {
 
158
        index = url.indexOf("?");
 
159
        if (index < 0)
 
160
        {
 
161
            obj.path = url;
 
162
            obj.query_string = null;
 
163
        }
 
164
        else
 
165
        {
 
166
            obj.path = url.substr(0, index);
 
167
            obj.query_string = url.substr(index+1);
 
168
        }
 
169
    }
 
170
 
 
171
    /* Split query string into arguments */
 
172
    args = {};
 
173
    if (obj.query_string != null)
 
174
    {
 
175
        var args_strs = obj.query_string.split("&");
 
176
        var arg_str;
 
177
        var arg_key, arg_val;
 
178
        for (var i=0; i<args_strs.length; i++)
 
179
        {
 
180
            arg_str = args_strs[i];
 
181
            index = arg_str.indexOf("=");
 
182
            /* Ignore malformed args */
 
183
            if (index >= 0)
 
184
            {
 
185
                arg_key = decodeURI(arg_str.substr(0, index));
 
186
                arg_val = decodeURI(arg_str.substr(index+1));
 
187
                if (arg_key in args)
 
188
                {
 
189
                    /* Collision - make an array */
 
190
                    if (args[arg_key] instanceof Array)
 
191
                        args[arg_key][args[arg_key].length] = arg_val;
 
192
                    else
 
193
                        args[arg_key] = [args[arg_key], arg_val];
 
194
                }
 
195
                else
 
196
                    args[arg_key] = arg_val;
 
197
            }
 
198
        }
 
199
    }
 
200
    obj.args = args;
 
201
 
 
202
    return obj;
 
203
}
 
204
 
 
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.
 
214
 */
 
215
function buildurl(obj)
 
216
{
 
217
    var url = "";
 
218
    var query_string = null;
 
219
 
 
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)
 
227
    {
 
228
        var path = obj.path.toString();
 
229
        if (path.length > 0 && path[0] != "/")
 
230
            path = "/" + path;
 
231
        url += path;
 
232
    }
 
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)
 
236
    {
 
237
        query_string = "";
 
238
        var arg_val;
 
239
        for (var arg_key in obj.args)
 
240
        {
 
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]);
 
246
            else
 
247
                query_string += "&" + encodeURI(arg_key) + "=" +
 
248
                    encodeURI(arg_val);
 
249
   
 
250
        }
 
251
        if (query_string == "")
 
252
            query_string = null;
 
253
        else
 
254
            /* Drop the first "&" */
 
255
            query_string = query_string.substr(1);
 
256
    }
 
257
 
 
258
    if (query_string != null)
 
259
        url += "?" + query_string;
 
260
 
 
261
    return url;
 
262
}
 
263
 
 
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.
 
271
 * \return String.
 
272
 */
 
273
function arg_getfirst(args, arg)
 
274
{
 
275
    if (!(arg in args))
 
276
        return null;
 
277
    var r = args[arg];
 
278
    if (r instanceof Array)
 
279
        return r[0];
 
280
    else
 
281
        return r;
 
282
}
 
283
 
 
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
 
286
 * array.
 
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.
 
292
 */
 
293
function arg_getlist(args, arg)
 
294
{
 
295
    if (!(arg in args))
 
296
        return [];
 
297
    var r = args[arg];
 
298
    if (r instanceof Array)
 
299
        return r;
 
300
    else
 
301
        return [r];
 
302
}