~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-03-05 04:34:27 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:647
phpBB3 - Added svn:ignore on autoconfigged file, config.php.

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
/** 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.
527
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.
528
570
 * \param app IVLE app to call (such as "fileservice").
529
571
 * \param path URL path to make the request to, within the application.
530
572
 * \param args Argument object, as described in parse_url and friends.
532
574
 * \param content_type String, optional. Only applies if method is "POST".
533
575
 *      May be "application/x-www-form-urlencoded" or "multipart/form-data".
534
576
 *      Defaults to "application/x-www-form-urlencoded".
535
 
 * \return An XMLHttpRequest object containing the completed response.
536
577
 */
537
 
function ajax_call(app, path, args, method, content_type)
 
578
function ajax_call(callback, app, path, args, method, content_type)
538
579
{
539
580
    if (content_type != "multipart/form-data")
540
581
        content_type = "application/x-www-form-urlencoded";
544
585
     * (This is not checked against anywhere else, it is solely defined and
545
586
     * used within this function) */
546
587
    var boundary = "48234n334nu7n4n2ynonjn234t683jyh80j";
547
 
    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
        }
548
596
    if (method == "GET")
549
597
    {
550
598
        /* GET sends the args in the URL */
551
599
        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);
 
600
        /* open's 3rd argument = true -> asynchronous */
 
601
        xhr.open(method, url, true);
555
602
        xhr.send(null);
556
603
    }
557
604
    else
558
605
    {
559
606
        /* POST sends the args in application/x-www-form-urlencoded */
560
607
        url = encodeURI(path);
561
 
        xhr.open(method, url, false);
 
608
        xhr.open(method, url, true);
562
609
        var message;
563
610
        if (content_type == "multipart/form-data")
564
611
        {
571
618
            xhr.setRequestHeader("Content-Type", content_type);
572
619
            message = make_query_string(args);
573
620
        }
 
621
        xhr.setRequestHeader("Content-Length", message.length);
574
622
        xhr.send(message);
575
623
    }
576
 
    return xhr;
577
624
}
578
625