~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-02-25 02:54:45 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:563
install_proc: Notes on apt-get for epydoc and docutils.

Show diffs side-by-side

added added

removed removed

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