~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:
25
25
/* Expects the following variables to have been declared by JavaScript in
26
26
 * the HTML generated by the server:
27
27
 * - root_dir
28
 
 * - public_host
29
28
 * - username
30
29
 */
31
30
 
70
69
 *  it will automatically be URL-encoded.
71
70
 * \param onclick Optional string. Will be set as the "onclick" attribute
72
71
 *  of the "a" element.
73
 
 * \param dontencode Optional boolean. If true, will not encode the href.
74
 
 *  if including query strings, you must set this to true and use build_url
75
 
 *  to escape the URI correctly.
76
72
 * \return DOM Element object.
77
73
 */
78
 
function dom_make_link_elem(tagname, text, title, href, onclick, dontencode)
 
74
function dom_make_link_elem(tagname, text, title, href, onclick)
79
75
{
80
76
    if (text == null) text = "";
81
77
    if (href == null) href = "";
82
78
    var elem = document.createElement(tagname);
83
79
    var link = document.createElement("a");
84
 
    if (dontencode != true)
85
 
        href = urlencode_path(href);
86
 
    link.setAttribute("href", href);
 
80
    link.setAttribute("href", encodeURI(href));
87
81
    if (title != null)
88
82
        link.setAttribute("title", title);
89
83
    if (onclick != null)
99
93
function dom_make_img(src, width, height, title, alt)
100
94
{
101
95
    var img = document.createElement("img");
102
 
    img.setAttribute("src", urlencode_path(src));
 
96
    img.setAttribute("src", src);
103
97
    if (width != null)
104
98
        img.setAttribute("width", width);
105
99
    if (height != null)
155
149
 * args is never null, though it may be empty.
156
150
 *
157
151
 * All strings are decoded/unescaped. Reserved characters
158
 
 * (; , / ? : @ & = + * $) are not decoded except in args and path.
 
152
 * (; , / ? : @ & = + * $) are not decoded except in args.
159
153
 *
160
154
 * \param url String. A URL. To read from the current browser window, use
161
155
 *  window.location.href.
168
162
    var serverpart;
169
163
    var args;
170
164
 
 
165
    url = decodeURI(url);
 
166
 
171
167
    /* Split scheme from rest */
172
168
    index = url.indexOf("://");
173
169
    if (index < 0)
224
220
            obj.query_string = url.substr(index+1);
225
221
        }
226
222
    }
227
 
    obj.path = decodeURIComponent(obj.path);
228
223
 
229
224
    /* Split query string into arguments */
230
225
    args = {};
279
274
            query_string += "&" + encodeURIComponent(arg_key) + "=" +
280
275
                encodeURIComponent(arg_val);
281
276
    }
282
 
    if (query_string != "")
 
277
    if (query_string == "")
 
278
        query_string = null;
 
279
    else
283
280
        /* Drop the first "&" */
284
281
        query_string = query_string.substr(1);
285
282
 
310
307
        url += ":" + obj.server_port.toString();
311
308
    if (("path" in obj) && obj.path != null)
312
309
    {
313
 
        var path = urlencode_path(obj.path.toString());
314
 
        if (url.length > 0 && path.length > 0 && path.charAt(0) != "/")
 
310
        var path = obj.path.toString();
 
311
        if (url.length > 0 && path.length > 0 && path[0] != "/")
315
312
            path = "/" + path;
316
313
        url += path;
317
314
    }
318
315
    if (("query_string" in obj) && obj.query_string != null)
319
 
        query_string = encodeURI(obj.query_string.toString());
 
316
        query_string = obj.query_string.toString();
320
317
    else if (("args" in obj) && obj.args != null)
321
318
        query_string = make_query_string(obj.args);
322
319
 
323
 
    if (query_string != "" && query_string != null)
 
320
    if (query_string != null)
324
321
        url += "?" + query_string;
325
322
 
326
 
    return url;
327
 
}
328
 
 
329
 
/** URL-encodes a path. This is a special case of URL encoding as all
330
 
 * characters *except* the slash must be encoded.
331
 
 */
332
 
function urlencode_path(path)
333
 
{
334
 
    /* Split up the path, URLEncode each segment with encodeURIComponent,
335
 
     * and rejoin.
336
 
     */
337
 
    var split = path.split('/');
338
 
    for (var i=0; i<split.length; i++)
339
 
        split[i] = encodeURIComponent(split[i]);
340
 
    path = path_join.apply(null, split);
341
 
    if (split[0] == "" && split.length > 1) path = "/" + path;
342
 
    return path;
343
 
}
344
 
 
345
 
/** Writes a JSONable object to the cookie under a particular key
346
 
 * (JSON encoded and URL encoded).
347
 
 */
348
 
function write_cookie(key, value)
349
 
{
350
 
    var sendstr = encodeURIComponent(key) + "="
351
 
        + encodeURIComponent(JSON.stringify(value))
352
 
        + "; path=" + urlencode_path(root_dir);
353
 
    /* This actually just assigns to the key, not replacing the whole cookie
354
 
     * as it appears to. */
355
 
    document.cookie = sendstr;
356
 
}
357
 
/** Reads a cookie which has a JSONable object encoded as its value.
358
 
 * Returns the object, parsed from JSON.
359
 
 */
360
 
function read_cookie(key)
361
 
{
362
 
    var cookies = document.cookie.split(";");
363
 
    var checkstart = encodeURIComponent(key) + "=";
364
 
    var checklen = checkstart.length;
365
 
    for (var i=0; i<cookies.length; i++)
366
 
    {
367
 
        var cookie = cookies[i];
368
 
        while (cookie[0] == ' ')
369
 
            cookie = cookie.substr(1);
370
 
        if (cookie.substr(0, checklen) == checkstart)
371
 
        {
372
 
            var valstr = cookie.substr(checklen);
373
 
            valstr = decodeURIComponent(valstr);
374
 
            return JSON.parse(valstr);
375
 
        }
376
 
    }
 
323
    return encodeURI(url);
377
324
}
378
325
 
379
326
/** Given an argument map, as output in the args parameter of the return of
426
373
    {
427
374
        arg = arguments[i];
428
375
        if (arg.length == 0) continue;
429
 
        if (arg.charAt(0) == '/')
 
376
        if (arg[0] == '/')
430
377
            path = arg;
431
378
        else
432
379
        {
433
 
            if (path.length > 0 && path.charAt(path.length-1) != '/')
 
380
            if (path.length > 0 && path[path.length-1] != '/')
434
381
                path += '/';
435
382
            path += arg;
436
383
        }
461
408
    {
462
409
        /* FIXME: Encoding not supported here (should not matter if we
463
410
         * only use ASCII names */
464
 
        data += "--" + boundary + "\r\n"
 
411
        data += "--" + boundary + "\n"
465
412
            + "Content-Disposition: form-data; name=\"" + arg_key
466
 
            + "\"\r\n\r\n"
467
 
            + arg_val + "\r\n";
 
413
            + "\"\n\n"
 
414
            + arg_val + "\n";
468
415
    }
469
416
 
470
417
    for (var arg_key in args)
479
426
            extend_data(arg_key, arg_val);
480
427
    }
481
428
    /* End boundary */
482
 
    data += "--" + boundary + "--\r\n";
 
429
    data += "--" + boundary + "--\n";
483
430
 
484
431
    return data;
485
432
}
491
438
function pathlist_to_path(pathlist)
492
439
{
493
440
    ret = path_join.apply(null, pathlist);
494
 
    if (ret.charAt(ret.length-1) != '/')
 
441
    if (ret[ret.length-1] != '/')
495
442
        ret += '/';
496
443
    return ret;
497
444
}
504
451
    return path_join(root_dir, path);
505
452
}
506
453
 
507
 
/** Shorthand for make_path(path_join(app, ...))
508
 
 * Creates an absolute path for a given path within a given app.
509
 
 */
510
 
function app_path(app /*,...*/)
511
 
{
512
 
    return make_path(path_join.apply(null, arguments));
513
 
}
514
 
 
515
 
/** Generates an absolute URL to a public application
516
 
 */
517
 
function public_app_path(app /*,...*/)
518
 
{
519
 
    return location.protocol + "//" + public_host
520
 
        + make_path(path_join.apply(null, arguments));
521
 
}
522
 
 
523
454
/** Given a path, gets the "basename" (the last path segment).
524
455
 */
525
456
function path_basename(path)
538
469
    return str.substr(str.length - substring.length) == substring;
539
470
}
540
471
 
541
 
/** Removes all occurences of a value from an array.
542
 
 */
543
 
Array.prototype.removeall = function(val)
544
 
{
545
 
    var i, j;
546
 
    var arr = this;
547
 
    j = 0;
548
 
    for (i=0; i<arr.length; i++)
549
 
    {
550
 
        arr[j] = arr[i];
551
 
        if (arr[i] != val) j++;
552
 
    }
553
 
    arr.splice(j, i-j);
554
 
}
555
 
 
556
 
/** Shallow-clones an object */
557
 
function shallow_clone_object(obj)
558
 
{
559
 
    o = {};
560
 
    for (k in obj)
561
 
        o[k] = obj[k];
562
 
    return o;
563
 
}
564
 
 
565
 
/** Returns a new XMLHttpRequest object, in a somewhat browser-agnostic
566
 
 * fashion.
567
 
 */
568
 
function new_xmlhttprequest()
569
 
{
570
 
    try
571
 
    {
572
 
        /* Real Browsers */
573
 
        return new XMLHttpRequest();
574
 
    }
575
 
    catch (e)
576
 
    {
577
 
        /* Internet Explorer */
578
 
        try
579
 
        {
580
 
            return new ActiveXObject("Msxml2.XMLHTTP");
581
 
        }
582
 
        catch (e)
583
 
        {
584
 
            try
585
 
            {
586
 
                return new ActiveXObject("Microsoft.XMLHTTP");
587
 
            }
588
 
            catch (e)
589
 
            {
590
 
                throw("Your browser does not support AJAX. "
591
 
                    + "IVLE requires a modern browser.");
592
 
            }
593
 
        }
594
 
    }
595
 
}
596
 
 
597
 
/** Creates a random string of length length,
598
 
 * consisting of alphanumeric characters.
599
 
 */
600
 
var rand_chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXTZ"
601
 
               + "abcdefghiklmnopqrstuvwxyz";
602
 
function random_string(length)
603
 
{
604
 
    var str = Array(length);
605
 
    var v;
606
 
    for (var i=0; i<length; i++)
607
 
    {
608
 
        v = Math.floor(Math.random() * rand_chars.length);
609
 
        str[i] = rand_chars.charAt(v);
610
 
    }
611
 
    return str.join('');
612
 
}
613
 
 
614
 
/** Makes an XMLHttpRequest call to the server.
615
 
 * Sends the XMLHttpRequest object containing the completed response to a
616
 
 * specified callback function.
 
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
 
 
481
/** Makes an XMLHttpRequest call to the server. Waits (synchronously) for a
 
482
 * response, and returns an XMLHttpRequest object containing the completed
 
483
 * response.
617
484
 *
618
 
 * \param callback A callback function. Will be called when the response is
619
 
 *      complete. Passed 1 parameter, an XMLHttpRequest object containing the
620
 
 *      completed response. If callback is null this is a syncronous request 
621
 
 *      otherwise this is an asynchronous request.
622
485
 * \param app IVLE app to call (such as "fileservice").
623
486
 * \param path URL path to make the request to, within the application.
624
487
 * \param args Argument object, as described in parse_url and friends.
625
 
 * \param method String; "GET", "POST", "PUT", or "PATCH"
626
 
 * \param content_type String, optional.
 
488
 * \param method String; "GET" or "POST"
 
489
 * \param content_type String, optional. Only applies if method is "POST".
 
490
 *      May be "application/x-www-form-urlencoded" or "multipart/form-data".
627
491
 *      Defaults to "application/x-www-form-urlencoded".
 
492
 * \return An XMLHttpRequest object containing the completed response.
628
493
 */
629
 
function ajax_call(callback, app, path, args, method, content_type)
 
494
function ajax_call(app, path, args, method, content_type)
630
495
{
631
 
    if (!content_type)
 
496
    if (content_type != "multipart/form-data")
632
497
        content_type = "application/x-www-form-urlencoded";
633
 
    path = app_path(app, path);
 
498
    path = make_path(path_join(app, path));
634
499
    var url;
635
500
    /* A random string, for multipart/form-data
636
501
     * (This is not checked against anywhere else, it is solely defined and
637
502
     * used within this function) */
638
 
    var boundary = random_string(20);
639
 
    var xhr = new_xmlhttprequest();
640
 
    var asyncronous = callback != null;
641
 
    if (asyncronous)
642
 
    {
643
 
        xhr.onreadystatechange = function()
644
 
            {
645
 
                if (xhr.readyState == 4)
646
 
                {
647
 
                    callback(xhr);
648
 
                }
649
 
            }
650
 
    }
 
503
    var boundary = "48234n334nu7n4n2ynonjn234t683jyh80j";
 
504
    var xhr = new XMLHttpRequest();
651
505
    if (method == "GET")
652
506
    {
653
507
        /* GET sends the args in the URL */
654
508
        url = build_url({"path": path, "args": args});
655
 
        /* open's 3rd argument = true -> asynchronous */
656
 
        xhr.open(method, url, asyncronous);
 
509
        /* open's 3rd argument = false -> SYNCHRONOUS (wait for response)
 
510
         * (No need for a callback function) */
 
511
        xhr.open(method, url, false);
657
512
        xhr.send(null);
658
513
    }
659
514
    else
660
515
    {
661
 
        /* POST & PUT & PATCH sends the args in the request body */
 
516
        /* POST sends the args in application/x-www-form-urlencoded */
662
517
        url = encodeURI(path);
663
 
        xhr.open(method, url, asyncronous);
 
518
        xhr.open(method, url, false);
664
519
        var message;
665
520
        if (content_type == "multipart/form-data")
666
521
        {
667
522
            xhr.setRequestHeader("Content-Type",
668
 
                "multipart/form-data; boundary=" + boundary);
 
523
                "multipart/form-data, boundary=" + boundary);
669
524
            message = make_multipart_formdata(args, boundary);
670
525
        }
671
 
        else if (content_type == "application/x-www-form-urlencoded")
 
526
        else
672
527
        {
673
528
            xhr.setRequestHeader("Content-Type", content_type);
674
529
            message = make_query_string(args);
675
530
        }
676
 
        else if (content_type == "application/json")
677
 
        {
678
 
            xhr.setRequestHeader("Content-Type", content_type);
679
 
            message = JSON.stringify(args);
680
 
        }
681
 
        else
682
 
        {
683
 
            xhr.setRequestHeader("Content-Type", content_type);
684
 
            message = args;
685
 
        }
686
531
        xhr.send(message);
687
532
    }
688
 
    /* Only return the XHR for syncronous requests */
689
 
    if (!asyncronous)
690
 
    { 
691
 
        return xhr;
692
 
    }
 
533
    return xhr;
693
534
}
694
535
 
695
 
/** Attempts to JSON decodes a response object
696
 
 * If a non-200 response or the JSON decode fails then returns null
697
 
 */
698
 
function decode_response(response)
699
 
{
700
 
    if (response.status == 200)
701
 
    {
702
 
        try
703
 
        {
704
 
            var responseText = response.responseText;
705
 
            return JSON.parse(responseText);
706
 
        }
707
 
        catch (e)
708
 
        {
709
 
            // Pass
710
 
        }
711
 
     }
712
 
    
713
 
     return null;
714
 
}