~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-12 11:48:06 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:192
dispatch.html: Only prints tabs if logged in.
    Changed "Welcome" section to include a "Help" link. This links to the
    most relevant help file to the current page (or if there is none, links
    back to the main Help page).

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
 * Defines some generic JavaScript utility functions.
23
23
 */
24
24
 
25
 
/* Expects the following variables to have been declared by JavaScript in
26
 
 * the HTML generated by the server:
27
 
 * - root_dir
28
 
 * - username
29
 
 */
30
 
 
31
25
/** Removes all children of a given DOM element
32
26
 * \param elem A DOM Element. Will be modified.
33
27
 */
40
34
/** Creates a DOM element with simple text inside it.
41
35
 * \param tagname String. Name of the element's tag (eg. "p").
42
36
 * \param text String. Text to be placed inside the element.
43
 
 * \param title String, optional. Tooltip for the text.
44
 
 *  (Note, title creates a span element around the text).
45
37
 * \return DOM Element object.
46
38
 */
47
 
function dom_make_text_elem(tagname, text, title)
 
39
function dom_make_text_elem(tagname, text)
48
40
{
49
41
    if (text == null) text = "";
50
42
    var elem = document.createElement(tagname);
51
 
    var textnode;
52
 
    if (title == null)
53
 
        textnode = document.createTextNode(text);
54
 
    else
55
 
    {
56
 
        textnode = document.createElement("span");
57
 
        textnode.setAttribute("title", title);
58
 
        textnode.appendChild(document.createTextNode(text));
59
 
    }
60
 
    elem.appendChild(textnode);
 
43
    elem.appendChild(document.createTextNode(text));
61
44
    return elem;
62
45
}
63
46
 
64
47
/** Creates a DOM element with hyperlinked text inside it.
65
48
 * \param tagname String. Name of the element's tag (eg. "p").
66
49
 * \param text String. Text to be placed inside the element.
67
 
 * \param title String, optional. Sets a tooltip for the link.
68
50
 * \param href String. URL the text will link to. This is a raw string,
69
51
 *  it will automatically be URL-encoded.
70
52
 * \param onclick Optional string. Will be set as the "onclick" attribute
71
53
 *  of the "a" element.
72
 
 * \param dontencode Optional boolean. If true, will not encode the href.
73
 
 *  if including query strings, you must set this to true and use build_url
74
 
 *  to escape the URI correctly.
75
54
 * \return DOM Element object.
76
55
 */
77
 
function dom_make_link_elem(tagname, text, title, href, onclick, dontencode)
 
56
function dom_make_link_elem(tagname, text, href, onclick)
78
57
{
79
58
    if (text == null) text = "";
80
59
    if (href == null) href = "";
81
60
    var elem = document.createElement(tagname);
82
61
    var link = document.createElement("a");
83
 
    if (dontencode != true)
84
 
        href = urlencode_path(href);
85
 
    link.setAttribute("href", href);
86
 
    if (title != null)
87
 
        link.setAttribute("title", title);
 
62
    link.setAttribute("href", encodeURI(href));
88
63
    if (onclick != null)
89
64
        link.setAttribute("onclick", onclick);
90
65
    link.appendChild(document.createTextNode(text));
98
73
function dom_make_img(src, width, height, title, alt)
99
74
{
100
75
    var img = document.createElement("img");
101
 
    img.setAttribute("src", urlencode_path(src));
 
76
    img.setAttribute("src", src);
102
77
    if (width != null)
103
78
        img.setAttribute("width", width);
104
79
    if (height != null)
154
129
 * args is never null, though it may be empty.
155
130
 *
156
131
 * All strings are decoded/unescaped. Reserved characters
157
 
 * (; , / ? : @ & = + * $) are not decoded except in args and path.
 
132
 * (; , / ? : @ & = + * $) are not decoded except in args.
158
133
 *
159
134
 * \param url String. A URL. To read from the current browser window, use
160
135
 *  window.location.href.
167
142
    var serverpart;
168
143
    var args;
169
144
 
 
145
    url = decodeURI(url);
 
146
 
170
147
    /* Split scheme from rest */
171
148
    index = url.indexOf("://");
172
149
    if (index < 0)
223
200
            obj.query_string = url.substr(index+1);
224
201
        }
225
202
    }
226
 
    obj.path = decodeURIComponent(obj.path);
227
203
 
228
204
    /* Split query string into arguments */
229
205
    args = {};
278
254
            query_string += "&" + encodeURIComponent(arg_key) + "=" +
279
255
                encodeURIComponent(arg_val);
280
256
    }
281
 
    if (query_string != "")
 
257
    if (query_string == "")
 
258
        query_string = null;
 
259
    else
282
260
        /* Drop the first "&" */
283
261
        query_string = query_string.substr(1);
284
262
 
309
287
        url += ":" + obj.server_port.toString();
310
288
    if (("path" in obj) && obj.path != null)
311
289
    {
312
 
        var path = urlencode_path(obj.path.toString());
 
290
        var path = obj.path.toString();
313
291
        if (url.length > 0 && path.length > 0 && path[0] != "/")
314
292
            path = "/" + path;
315
293
        url += path;
316
294
    }
317
295
    if (("query_string" in obj) && obj.query_string != null)
318
 
        query_string = encodeURI(obj.query_string.toString());
 
296
        query_string = obj.query_string.toString();
319
297
    else if (("args" in obj) && obj.args != null)
320
298
        query_string = make_query_string(obj.args);
321
299
 
322
 
    if (query_string != "" && query_string != null)
 
300
    if (query_string != null)
323
301
        url += "?" + query_string;
324
302
 
325
 
    return url;
326
 
}
327
 
 
328
 
/** URL-encodes a path. This is a special case of URL encoding as all
329
 
 * characters *except* the slash must be encoded.
330
 
 */
331
 
function urlencode_path(path)
332
 
{
333
 
    /* Split up the path, URLEncode each segment with encodeURIComponent,
334
 
     * and rejoin.
335
 
     */
336
 
    var split = path.split('/');
337
 
    for (var i=0; i<split.length; i++)
338
 
        split[i] = encodeURIComponent(split[i]);
339
 
    path = path_join.apply(null, split);
340
 
    if (split[0] == "" && split.length > 1) path = "/" + path;
341
 
    return path;
 
303
    return encodeURI(url);
342
304
}
343
305
 
344
306
/** Given an argument map, as output in the args parameter of the return of
386
348
function path_join(path1 /*, path2, ... */)
387
349
{
388
350
    var arg;
389
 
    var path = "";
390
 
    for (var i=0; i<arguments.length; i++)
 
351
    path = path1;
 
352
    for (var i=1; i<arguments.length; i++)
391
353
    {
392
354
        arg = arguments[i];
393
355
        if (arg.length == 0) continue;
395
357
            path = arg;
396
358
        else
397
359
        {
398
 
            if (path.length > 0 && path[path.length-1] != '/')
 
360
            if (path[path.length-1] != '/')
399
361
                path += '/';
400
362
            path += arg;
401
363
        }
426
388
    {
427
389
        /* FIXME: Encoding not supported here (should not matter if we
428
390
         * only use ASCII names */
429
 
        data += "--" + boundary + "\r\n"
 
391
        data += "--" + boundary + "\n"
430
392
            + "Content-Disposition: form-data; name=\"" + arg_key
431
 
            + "\"\r\n\r\n"
432
 
            + arg_val + "\r\n";
 
393
            + "\"\n\n"
 
394
            + arg_val + "\n";
433
395
    }
434
396
 
435
397
    for (var arg_key in args)
444
406
            extend_data(arg_key, arg_val);
445
407
    }
446
408
    /* End boundary */
447
 
    data += "--" + boundary + "--\r\n";
 
409
    data += "--" + boundary + "--\n";
448
410
 
449
411
    return data;
450
412
}
469
431
    return path_join(root_dir, path);
470
432
}
471
433
 
472
 
/** Shorthand for make_path(path_join(app, ...))
473
 
 * Creates an absolute path for a given path within a given app.
474
 
 */
475
 
function app_path(app /*,...*/)
476
 
{
477
 
    return make_path(path_join.apply(null, arguments));
478
 
}
479
 
 
480
 
/** Given a path, gets the "basename" (the last path segment).
481
 
 */
482
 
function path_basename(path)
483
 
{
484
 
    segments = path.split("/");
485
 
    if (segments[segments.length-1].length == 0)
486
 
        return segments[segments.length-2];
487
 
    else
488
 
        return segments[segments.length-1];
489
 
}
490
 
 
491
 
/** Given a string str, determines whether it ends with substr */
492
 
function endswith(str, substring)
493
 
{
494
 
    if (str.length < substring.length) return false;
495
 
    return str.substr(str.length - substring.length) == substring;
496
 
}
497
 
 
498
 
/** Equivalent to Python's repr.
499
 
 * Gets the JavaScript string representation.
500
 
 * Actually just calls JSON.stringify.
501
 
 */
502
 
function repr(str)
503
 
{
504
 
    return JSON.stringify(str);
505
 
}
506
 
 
507
 
/** Removes all occurences of a value from an array.
508
 
 */
509
 
Array.prototype.removeall = function(val)
510
 
{
511
 
    var i, j;
512
 
    var arr = this;
513
 
    j = 0;
514
 
    for (i=0; i<arr.length; i++)
515
 
    {
516
 
        arr[j] = arr[i];
517
 
        if (arr[i] != val) j++;
518
 
    }
519
 
    arr.splice(j, i-j);
520
 
}
521
 
 
522
434
/** Makes an XMLHttpRequest call to the server. Waits (synchronously) for a
523
435
 * response, and returns an XMLHttpRequest object containing the completed
524
436
 * response.
536
448
{
537
449
    if (content_type != "multipart/form-data")
538
450
        content_type = "application/x-www-form-urlencoded";
539
 
    path = app_path(app, path);
 
451
    path = make_path(path_join(app, path));
540
452
    var url;
541
453
    /* A random string, for multipart/form-data
542
454
     * (This is not checked against anywhere else, it is solely defined and
569
481
            xhr.setRequestHeader("Content-Type", content_type);
570
482
            message = make_query_string(args);
571
483
        }
572
 
        xhr.setRequestHeader("Content-Length", message.length);
573
484
        xhr.send(message);
574
485
    }
575
486
    return xhr;