~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-14 01:13:50 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:217
Console: Python code generates a minimal document with a DIV and links to
    JavaScript.
    Added console.js. This spawns the server (calling consoleservice) and sets
    up the iframe. Console now working.
Added md5.js to media/common.

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
 
25
31
/** Removes all children of a given DOM element
26
32
 * \param elem A DOM Element. Will be modified.
27
33
 */
34
40
/** Creates a DOM element with simple text inside it.
35
41
 * \param tagname String. Name of the element's tag (eg. "p").
36
42
 * \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).
37
45
 * \return DOM Element object.
38
46
 */
39
 
function dom_make_text_elem(tagname, text)
 
47
function dom_make_text_elem(tagname, text, title)
40
48
{
 
49
    if (text == null) text = "";
41
50
    var elem = document.createElement(tagname);
42
 
    elem.appendChild(document.createTextNode(text));
 
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
61
    return elem;
44
62
}
45
63
 
46
64
/** Creates a DOM element with hyperlinked text inside it.
47
65
 * \param tagname String. Name of the element's tag (eg. "p").
48
66
 * \param text String. Text to be placed inside the element.
 
67
 * \param title String, optional. Sets a tooltip for the link.
49
68
 * \param href String. URL the text will link to. This is a raw string,
50
69
 *  it will automatically be URL-encoded.
51
70
 * \param onclick Optional string. Will be set as the "onclick" attribute
52
71
 *  of the "a" element.
53
72
 * \return DOM Element object.
54
73
 */
55
 
function dom_make_link_elem(tagname, text, href, onclick)
 
74
function dom_make_link_elem(tagname, text, title, href, onclick)
56
75
{
 
76
    if (text == null) text = "";
 
77
    if (href == null) href = "";
57
78
    var elem = document.createElement(tagname);
58
79
    var link = document.createElement("a");
59
80
    link.setAttribute("href", encodeURI(href));
 
81
    if (title != null)
 
82
        link.setAttribute("title", title);
60
83
    if (onclick != null)
61
84
        link.setAttribute("onclick", onclick);
62
85
    link.appendChild(document.createTextNode(text));
64
87
    return elem;
65
88
}
66
89
 
 
90
/** Creates a DOM img element. All parameters are optional except src.
 
91
 * If alt (compulsory in HTML) is omitted, will be set to "".
 
92
 */
 
93
function dom_make_img(src, width, height, title, alt)
 
94
{
 
95
    var img = document.createElement("img");
 
96
    img.setAttribute("src", src);
 
97
    if (width != null)
 
98
        img.setAttribute("width", width);
 
99
    if (height != null)
 
100
        img.setAttribute("height", height);
 
101
    if (title != null)
 
102
        img.setAttribute("title", title);
 
103
    if (alt == null) alt = "";
 
104
    img.setAttribute("alt", alt);
 
105
    return img;
 
106
}
 
107
 
67
108
/** Given a number of bytes, returns a string representing the file size in a
68
109
 * human-readable format.
69
110
 * eg. nice_filesize(6) -> "6 bytes"
74
115
 */
75
116
function nice_filesize(bytes)
76
117
{
 
118
    if (bytes == null) return "";
77
119
    var size;
78
120
    if (bytes < 1024)
79
 
        return bytes.toString() + " bytes";
 
121
        return bytes.toString() + " B";
80
122
    size = bytes / 1024;
81
123
    if (size < 1024)
82
124
        return size.toFixed(1) + " kB";
142
184
    else
143
185
    {
144
186
        serverpart = url.substr(0, index);
145
 
        url = url.substr(index+1);
 
187
        url = url.substr(index);
146
188
    }
147
189
 
148
190
    /* Split server name from port */
326
368
function path_join(path1 /*, path2, ... */)
327
369
{
328
370
    var arg;
329
 
    path = path1;
330
 
    for (var i=1; i<arguments.length; i++)
 
371
    var path = "";
 
372
    for (var i=0; i<arguments.length; i++)
331
373
    {
332
374
        arg = arguments[i];
333
375
        if (arg.length == 0) continue;
335
377
            path = arg;
336
378
        else
337
379
        {
338
 
            if (path[path.length-1] != '/')
 
380
            if (path.length > 0 && path[path.length-1] != '/')
339
381
                path += '/';
340
382
            path += arg;
341
383
        }
409
451
    return path_join(root_dir, path);
410
452
}
411
453
 
 
454
/** Given a path, gets the "basename" (the last path segment).
 
455
 */
 
456
function path_basename(path)
 
457
{
 
458
    segments = path.split("/");
 
459
    if (segments[segments.length-1].length == 0)
 
460
        return segments[segments.length-2];
 
461
    else
 
462
        return segments[segments.length-1];
 
463
}
 
464
 
 
465
/** Given a string str, determines whether it ends with substr */
 
466
function endswith(str, substring)
 
467
{
 
468
    if (str.length < substring.length) return false;
 
469
    return str.substr(str.length - substring.length) == substring;
 
470
}
 
471
 
 
472
/** Equivalent to Python's repr.
 
473
 * Gets the JavaScript string representation.
 
474
 * Actually just calls JSON.stringify.
 
475
 */
 
476
function repr(str)
 
477
{
 
478
    return JSON.stringify(str);
 
479
}
 
480
 
412
481
/** Makes an XMLHttpRequest call to the server. Waits (synchronously) for a
413
482
 * response, and returns an XMLHttpRequest object containing the completed
414
483
 * response.
440
509
        /* open's 3rd argument = false -> SYNCHRONOUS (wait for response)
441
510
         * (No need for a callback function) */
442
511
        xhr.open(method, url, false);
443
 
        xhr.send("");
 
512
        xhr.send(null);
444
513
    }
445
514
    else
446
515
    {