~azzar1/unity/add-show-desktop-key

« back to all changes in this revision

Viewing changes to www/media/common/util.js

  • Committer: dcoles
  • Date: 2008-03-04 11:38:02 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:642
makeuser.py: Needs to import random

Show diffs side-by-side

added added

removed removed

Lines of Context:
519
519
    arr.splice(j, i-j);
520
520
}
521
521
 
522
 
/** Makes an XMLHttpRequest call to the server. Waits (synchronously) for a
523
 
 * response, and returns an XMLHttpRequest object containing the completed
524
 
 * response.
 
522
/** Shallow-clones an object */
 
523
function shallow_clone_object(obj)
 
524
{
 
525
    o = {};
 
526
    for (k in obj)
 
527
        o[k] = obj[k];
 
528
    return o;
 
529
}
 
530
 
 
531
/** Returns a new XMLHttpRequest object, in a somewhat browser-agnostic
 
532
 * fashion.
 
533
 */
 
534
function new_xmlhttprequest()
 
535
{
 
536
    try
 
537
    {
 
538
        /* Real Browsers */
 
539
        return new XMLHttpRequest();
 
540
    }
 
541
    catch (e)
 
542
    {
 
543
        /* Internet Explorer */
 
544
        try
 
545
        {
 
546
            return new ActiveXObject("Msxml2.XMLHTTP");
 
547
        }
 
548
        catch (e)
 
549
        {
 
550
            try
 
551
            {
 
552
                return new ActiveXObject("Microsoft.XMLHTTP");
 
553
            }
 
554
            catch (e)
 
555
            {
 
556
                throw("Your browser does not support AJAX. "
 
557
                    + "IVLE requires a modern browser.");
 
558
            }
 
559
        }
 
560
    }
 
561
}
 
562
 
 
563
/** Makes an asynchronous XMLHttpRequest call to the server.
 
564
 * Sends the XMLHttpRequest object containing the completed response to a
 
565
 * specified callback function.
525
566
 *
 
567
 * \param callback A callback function. Will be called when the response is
 
568
 *      complete. Passed 1 parameter, an XMLHttpRequest object containing the
 
569
 *      completed response.
526
570
 * \param app IVLE app to call (such as "fileservice").
527
571
 * \param path URL path to make the request to, within the application.
528
572
 * \param args Argument object, as described in parse_url and friends.
530
574
 * \param content_type String, optional. Only applies if method is "POST".
531
575
 *      May be "application/x-www-form-urlencoded" or "multipart/form-data".
532
576
 *      Defaults to "application/x-www-form-urlencoded".
533
 
 * \return An XMLHttpRequest object containing the completed response.
534
577
 */
535
 
function ajax_call(app, path, args, method, content_type)
 
578
function ajax_call(callback, app, path, args, method, content_type)
536
579
{
537
580
    if (content_type != "multipart/form-data")
538
581
        content_type = "application/x-www-form-urlencoded";
542
585
     * (This is not checked against anywhere else, it is solely defined and
543
586
     * used within this function) */
544
587
    var boundary = "48234n334nu7n4n2ynonjn234t683jyh80j";
545
 
    var xhr = new XMLHttpRequest();
 
588
    var xhr = new_xmlhttprequest();
 
589
    xhr.onreadystatechange = function()
 
590
        {
 
591
            if (xhr.readyState == 4)
 
592
            {
 
593
                callback(xhr);
 
594
            }
 
595
        }
546
596
    if (method == "GET")
547
597
    {
548
598
        /* GET sends the args in the URL */
549
599
        url = build_url({"path": path, "args": args});
550
 
        /* open's 3rd argument = false -> SYNCHRONOUS (wait for response)
551
 
         * (No need for a callback function) */
552
 
        xhr.open(method, url, false);
 
600
        /* open's 3rd argument = true -> asynchronous */
 
601
        xhr.open(method, url, true);
553
602
        xhr.send(null);
554
603
    }
555
604
    else
556
605
    {
557
606
        /* POST sends the args in application/x-www-form-urlencoded */
558
607
        url = encodeURI(path);
559
 
        xhr.open(method, url, false);
 
608
        xhr.open(method, url, true);
560
609
        var message;
561
610
        if (content_type == "multipart/form-data")
562
611
        {
572
621
        xhr.setRequestHeader("Content-Length", message.length);
573
622
        xhr.send(message);
574
623
    }
575
 
    return xhr;
576
624
}
577
625