~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-07-21 05:25:48 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:920
userdb: Added to table "offering" fields max_students_per_group and
    max_groups_per_student.

Show diffs side-by-side

added added

removed removed

Lines of Context:
538
538
    return str.substr(str.length - substring.length) == substring;
539
539
}
540
540
 
 
541
/** Equivalent to Python's repr.
 
542
 * Gets the JavaScript string representation.
 
543
 * Actually just calls JSON.stringify.
 
544
 */
 
545
function repr(str)
 
546
{
 
547
    return JSON.stringify(str);
 
548
}
 
549
 
541
550
/** Removes all occurences of a value from an array.
542
551
 */
543
552
Array.prototype.removeall = function(val)
611
620
    return str.join('');
612
621
}
613
622
 
614
 
/** Makes an XMLHttpRequest call to the server.
 
623
/** Makes an asynchronous XMLHttpRequest call to the server.
615
624
 * Sends the XMLHttpRequest object containing the completed response to a
616
625
 * specified callback function.
617
626
 *
618
627
 * \param callback A callback function. Will be called when the response is
619
628
 *      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.
 
629
 *      completed response.
622
630
 * \param app IVLE app to call (such as "fileservice").
623
631
 * \param path URL path to make the request to, within the application.
624
632
 * \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.
 
633
 * \param method String; "GET" or "POST"
 
634
 * \param content_type String, optional. Only applies if method is "POST".
 
635
 *      May be "application/x-www-form-urlencoded" or "multipart/form-data".
627
636
 *      Defaults to "application/x-www-form-urlencoded".
628
637
 */
629
638
function ajax_call(callback, app, path, args, method, content_type)
630
639
{
631
 
    if (!content_type)
 
640
    if (content_type != "multipart/form-data")
632
641
        content_type = "application/x-www-form-urlencoded";
633
642
    path = app_path(app, path);
634
643
    var url;
637
646
     * used within this function) */
638
647
    var boundary = random_string(20);
639
648
    var xhr = new_xmlhttprequest();
640
 
    var asyncronous = callback != null;
641
 
    if (asyncronous)
642
 
    {
643
 
        xhr.onreadystatechange = function()
 
649
    xhr.onreadystatechange = function()
 
650
        {
 
651
            if (xhr.readyState == 4)
644
652
            {
645
 
                if (xhr.readyState == 4)
646
 
                {
647
 
                    callback(xhr);
648
 
                }
 
653
                callback(xhr);
649
654
            }
650
 
    }
 
655
        }
651
656
    if (method == "GET")
652
657
    {
653
658
        /* GET sends the args in the URL */
654
659
        url = build_url({"path": path, "args": args});
655
660
        /* open's 3rd argument = true -> asynchronous */
656
 
        xhr.open(method, url, asyncronous);
 
661
        xhr.open(method, url, true);
657
662
        xhr.send(null);
658
663
    }
659
664
    else
660
665
    {
661
 
        /* POST & PUT & PATCH sends the args in the request body */
 
666
        /* POST sends the args in application/x-www-form-urlencoded */
662
667
        url = encodeURI(path);
663
 
        xhr.open(method, url, asyncronous);
 
668
        xhr.open(method, url, true);
664
669
        var message;
665
670
        if (content_type == "multipart/form-data")
666
671
        {
668
673
                "multipart/form-data; boundary=" + boundary);
669
674
            message = make_multipart_formdata(args, boundary);
670
675
        }
671
 
        else if (content_type == "application/x-www-form-urlencoded")
 
676
        else
672
677
        {
673
678
            xhr.setRequestHeader("Content-Type", content_type);
674
679
            message = make_query_string(args);
675
680
        }
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
681
        xhr.send(message);
687
682
    }
688
 
    /* Only return the XHR for syncronous requests */
689
 
    if (!asyncronous)
690
 
    { 
691
 
        return xhr;
692
 
    }
693
683
}
694
684
 
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
 
}