~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-29 02:57:10 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:322
Added doc/setup - a setup guide specific to our configuration. This is the
result of my successfully configuring IVLE on a production server.

Show diffs side-by-side

added added

removed removed

Lines of Context:
69
69
 *  it will automatically be URL-encoded.
70
70
 * \param onclick Optional string. Will be set as the "onclick" attribute
71
71
 *  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.
72
75
 * \return DOM Element object.
73
76
 */
74
 
function dom_make_link_elem(tagname, text, title, href, onclick)
 
77
function dom_make_link_elem(tagname, text, title, href, onclick, dontencode)
75
78
{
76
79
    if (text == null) text = "";
77
80
    if (href == null) href = "";
78
81
    var elem = document.createElement(tagname);
79
82
    var link = document.createElement("a");
80
 
    link.setAttribute("href", encodeURI(href));
 
83
    if (dontencode != true)
 
84
        href = urlencode_path(href);
 
85
    link.setAttribute("href", href);
81
86
    if (title != null)
82
87
        link.setAttribute("title", title);
83
88
    if (onclick != null)
93
98
function dom_make_img(src, width, height, title, alt)
94
99
{
95
100
    var img = document.createElement("img");
96
 
    img.setAttribute("src", src);
 
101
    img.setAttribute("src", urlencode_path(src));
97
102
    if (width != null)
98
103
        img.setAttribute("width", width);
99
104
    if (height != null)
149
154
 * args is never null, though it may be empty.
150
155
 *
151
156
 * All strings are decoded/unescaped. Reserved characters
152
 
 * (; , / ? : @ & = + * $) are not decoded except in args.
 
157
 * (; , / ? : @ & = + * $) are not decoded except in args and path.
153
158
 *
154
159
 * \param url String. A URL. To read from the current browser window, use
155
160
 *  window.location.href.
162
167
    var serverpart;
163
168
    var args;
164
169
 
165
 
    url = decodeURI(url);
166
 
 
167
170
    /* Split scheme from rest */
168
171
    index = url.indexOf("://");
169
172
    if (index < 0)
220
223
            obj.query_string = url.substr(index+1);
221
224
        }
222
225
    }
 
226
    obj.path = decodeURIComponent(obj.path);
223
227
 
224
228
    /* Split query string into arguments */
225
229
    args = {};
307
311
        url += ":" + obj.server_port.toString();
308
312
    if (("path" in obj) && obj.path != null)
309
313
    {
310
 
        var path = obj.path.toString();
 
314
        var path = urlencode_path(obj.path.toString());
311
315
        if (url.length > 0 && path.length > 0 && path[0] != "/")
312
316
            path = "/" + path;
313
317
        url += path;
314
318
    }
315
319
    if (("query_string" in obj) && obj.query_string != null)
316
 
        query_string = obj.query_string.toString();
 
320
        query_string = encodeURI(obj.query_string.toString());
317
321
    else if (("args" in obj) && obj.args != null)
318
322
        query_string = make_query_string(obj.args);
319
323
 
320
324
    if (query_string != null)
321
325
        url += "?" + query_string;
322
326
 
323
 
    return encodeURI(url);
 
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;
324
344
}
325
345
 
326
346
/** Given an argument map, as output in the args parameter of the return of
408
428
    {
409
429
        /* FIXME: Encoding not supported here (should not matter if we
410
430
         * only use ASCII names */
411
 
        data += "--" + boundary + "\n"
 
431
        data += "--" + boundary + "\r\n"
412
432
            + "Content-Disposition: form-data; name=\"" + arg_key
413
 
            + "\"\n\n"
414
 
            + arg_val + "\n";
 
433
            + "\"\r\n\r\n"
 
434
            + arg_val + "\r\n";
415
435
    }
416
436
 
417
437
    for (var arg_key in args)
426
446
            extend_data(arg_key, arg_val);
427
447
    }
428
448
    /* End boundary */
429
 
    data += "--" + boundary + "--\n";
 
449
    data += "--" + boundary + "--\r\n";
430
450
 
431
451
    return data;
432
452
}
451
471
    return path_join(root_dir, path);
452
472
}
453
473
 
 
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
 
454
524
/** Makes an XMLHttpRequest call to the server. Waits (synchronously) for a
455
525
 * response, and returns an XMLHttpRequest object containing the completed
456
526
 * response.
468
538
{
469
539
    if (content_type != "multipart/form-data")
470
540
        content_type = "application/x-www-form-urlencoded";
471
 
    path = make_path(path_join(app, path));
 
541
    path = app_path(app, path);
472
542
    var url;
473
543
    /* A random string, for multipart/form-data
474
544
     * (This is not checked against anywhere else, it is solely defined and