~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 06:52:24 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:183
browser: Added top level of response handling. Now determines handler type and
calls the appropriate handler. (Handlers just alert at the moment).

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
 
    if (text == null) text = "";
50
41
    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);
 
42
    elem.appendChild(document.createTextNode(text));
61
43
    return elem;
62
44
}
63
45
 
64
46
/** Creates a DOM element with hyperlinked text inside it.
65
47
 * \param tagname String. Name of the element's tag (eg. "p").
66
48
 * \param text String. Text to be placed inside the element.
67
 
 * \param title String, optional. Sets a tooltip for the link.
68
49
 * \param href String. URL the text will link to. This is a raw string,
69
50
 *  it will automatically be URL-encoded.
70
51
 * \param onclick Optional string. Will be set as the "onclick" attribute
71
52
 *  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
53
 * \return DOM Element object.
76
54
 */
77
 
function dom_make_link_elem(tagname, text, title, href, onclick, dontencode)
 
55
function dom_make_link_elem(tagname, text, href, onclick)
78
56
{
79
 
    if (text == null) text = "";
80
 
    if (href == null) href = "";
81
57
    var elem = document.createElement(tagname);
82
58
    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);
 
59
    link.setAttribute("href", encodeURI(href));
88
60
    if (onclick != null)
89
61
        link.setAttribute("onclick", onclick);
90
62
    link.appendChild(document.createTextNode(text));
92
64
    return elem;
93
65
}
94
66
 
95
 
/** Creates a DOM img element. All parameters are optional except src.
96
 
 * If alt (compulsory in HTML) is omitted, will be set to "".
97
 
 */
98
 
function dom_make_img(src, width, height, title, alt)
99
 
{
100
 
    var img = document.createElement("img");
101
 
    img.setAttribute("src", urlencode_path(src));
102
 
    if (width != null)
103
 
        img.setAttribute("width", width);
104
 
    if (height != null)
105
 
        img.setAttribute("height", height);
106
 
    if (title != null)
107
 
        img.setAttribute("title", title);
108
 
    if (alt == null) alt = "";
109
 
    img.setAttribute("alt", alt);
110
 
    return img;
111
 
}
112
 
 
113
67
/** Given a number of bytes, returns a string representing the file size in a
114
68
 * human-readable format.
115
69
 * eg. nice_filesize(6) -> "6 bytes"
120
74
 */
121
75
function nice_filesize(bytes)
122
76
{
123
 
    if (bytes == null) return "";
124
77
    var size;
125
78
    if (bytes < 1024)
126
 
        return bytes.toString() + " B";
 
79
        return bytes.toString() + " bytes";
127
80
    size = bytes / 1024;
128
81
    if (size < 1024)
129
82
        return size.toFixed(1) + " kB";
154
107
 * args is never null, though it may be empty.
155
108
 *
156
109
 * All strings are decoded/unescaped. Reserved characters
157
 
 * (; , / ? : @ & = + * $) are not decoded except in args and path.
 
110
 * (; , / ? : @ & = + * $) are not decoded except in args.
158
111
 *
159
112
 * \param url String. A URL. To read from the current browser window, use
160
113
 *  window.location.href.
167
120
    var serverpart;
168
121
    var args;
169
122
 
 
123
    url = decodeURI(url);
 
124
 
170
125
    /* Split scheme from rest */
171
126
    index = url.indexOf("://");
172
127
    if (index < 0)
187
142
    else
188
143
    {
189
144
        serverpart = url.substr(0, index);
190
 
        url = url.substr(index);
 
145
        url = url.substr(index+1);
191
146
    }
192
147
 
193
148
    /* Split server name from port */
223
178
            obj.query_string = url.substr(index+1);
224
179
        }
225
180
    }
226
 
    obj.path = decodeURIComponent(obj.path);
227
181
 
228
182
    /* Split query string into arguments */
229
183
    args = {};
311
265
        url += ":" + obj.server_port.toString();
312
266
    if (("path" in obj) && obj.path != null)
313
267
    {
314
 
        var path = urlencode_path(obj.path.toString());
 
268
        var path = obj.path.toString();
315
269
        if (url.length > 0 && path.length > 0 && path[0] != "/")
316
270
            path = "/" + path;
317
271
        url += path;
318
272
    }
319
273
    if (("query_string" in obj) && obj.query_string != null)
320
 
        query_string = encodeURI(obj.query_string.toString());
 
274
        query_string = obj.query_string.toString();
321
275
    else if (("args" in obj) && obj.args != null)
322
276
        query_string = make_query_string(obj.args);
323
277
 
324
278
    if (query_string != null)
325
279
        url += "?" + query_string;
326
280
 
327
 
    return url;
328
 
}
329
 
 
330
 
/** URL-encodes a path. This is a special case of URL encoding as all
331
 
 * characters *except* the slash must be encoded.
332
 
 */
333
 
function urlencode_path(path)
334
 
{
335
 
    /* Split up the path, URLEncode each segment with encodeURIComponent,
336
 
     * and rejoin.
337
 
     */
338
 
    var split = path.split('/');
339
 
    for (var i=0; i<split.length; i++)
340
 
        split[i] = encodeURIComponent(split[i]);
341
 
    path = path_join.apply(null, split);
342
 
    if (split[0] == "" && split.length > 1) path = "/" + path;
343
 
    return path;
 
281
    return encodeURI(url);
344
282
}
345
283
 
346
284
/** Given an argument map, as output in the args parameter of the return of
388
326
function path_join(path1 /*, path2, ... */)
389
327
{
390
328
    var arg;
391
 
    var path = "";
392
 
    for (var i=0; i<arguments.length; i++)
 
329
    path = path1;
 
330
    for (var i=1; i<arguments.length; i++)
393
331
    {
394
332
        arg = arguments[i];
395
333
        if (arg.length == 0) continue;
397
335
            path = arg;
398
336
        else
399
337
        {
400
 
            if (path.length > 0 && path[path.length-1] != '/')
 
338
            if (path[path.length-1] != '/')
401
339
                path += '/';
402
340
            path += arg;
403
341
        }
428
366
    {
429
367
        /* FIXME: Encoding not supported here (should not matter if we
430
368
         * only use ASCII names */
431
 
        data += "--" + boundary + "\r\n"
 
369
        data += "--" + boundary + "\n"
432
370
            + "Content-Disposition: form-data; name=\"" + arg_key
433
 
            + "\"\r\n\r\n"
434
 
            + arg_val + "\r\n";
 
371
            + "\"\n\n"
 
372
            + arg_val + "\n";
435
373
    }
436
374
 
437
375
    for (var arg_key in args)
446
384
            extend_data(arg_key, arg_val);
447
385
    }
448
386
    /* End boundary */
449
 
    data += "--" + boundary + "--\r\n";
 
387
    data += "--" + boundary + "--\n";
450
388
 
451
389
    return data;
452
390
}
471
409
    return path_join(root_dir, path);
472
410
}
473
411
 
474
 
/** Shorthand for make_path(path_join(app, ...))
475
 
 * Creates an absolute path for a given path within a given app.
476
 
 */
477
 
function app_path(app /*,...*/)
478
 
{
479
 
    return make_path(path_join.apply(null, arguments));
480
 
}
481
 
 
482
 
/** Given a path, gets the "basename" (the last path segment).
483
 
 */
484
 
function path_basename(path)
485
 
{
486
 
    segments = path.split("/");
487
 
    if (segments[segments.length-1].length == 0)
488
 
        return segments[segments.length-2];
489
 
    else
490
 
        return segments[segments.length-1];
491
 
}
492
 
 
493
 
/** Given a string str, determines whether it ends with substr */
494
 
function endswith(str, substring)
495
 
{
496
 
    if (str.length < substring.length) return false;
497
 
    return str.substr(str.length - substring.length) == substring;
498
 
}
499
 
 
500
 
/** Equivalent to Python's repr.
501
 
 * Gets the JavaScript string representation.
502
 
 * Actually just calls JSON.stringify.
503
 
 */
504
 
function repr(str)
505
 
{
506
 
    return JSON.stringify(str);
507
 
}
508
 
 
509
 
/** Removes all occurences of a value from an array.
510
 
 */
511
 
Array.prototype.removeall = function(val)
512
 
{
513
 
    var i, j;
514
 
    var arr = this;
515
 
    j = 0;
516
 
    for (i=0; i<arr.length; i++)
517
 
    {
518
 
        arr[j] = arr[i];
519
 
        if (arr[i] != val) j++;
520
 
    }
521
 
    arr.splice(j, i-j);
522
 
}
523
 
 
524
412
/** Makes an XMLHttpRequest call to the server. Waits (synchronously) for a
525
413
 * response, and returns an XMLHttpRequest object containing the completed
526
414
 * response.
538
426
{
539
427
    if (content_type != "multipart/form-data")
540
428
        content_type = "application/x-www-form-urlencoded";
541
 
    path = app_path(app, path);
 
429
    path = make_path(path_join(app, path));
542
430
    var url;
543
431
    /* A random string, for multipart/form-data
544
432
     * (This is not checked against anywhere else, it is solely defined and