~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-07-03 04:20:54 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:803
Setup: Modularised setup.py so it is now no longer over 1000 lines. This should 
allow us to get in there and tidy up each module much easier. Also removed 
updatejails since this functionality seems to be duplicated with remakeuser.py 
and remakealluser.py scripts.

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.
638
646
     * used within this function) */
639
647
    var boundary = random_string(20);
640
648
    var xhr = new_xmlhttprequest();
641
 
    var asyncronous = callback != null;
642
 
    if (asyncronous)
643
 
    {
644
 
        xhr.onreadystatechange = function()
 
649
    xhr.onreadystatechange = function()
 
650
        {
 
651
            if (xhr.readyState == 4)
645
652
            {
646
 
                if (xhr.readyState == 4)
647
 
                {
648
 
                    callback(xhr);
649
 
                }
 
653
                callback(xhr);
650
654
            }
651
 
    }
 
655
        }
652
656
    if (method == "GET")
653
657
    {
654
658
        /* GET sends the args in the URL */
655
659
        url = build_url({"path": path, "args": args});
656
660
        /* open's 3rd argument = true -> asynchronous */
657
 
        xhr.open(method, url, asyncronous);
 
661
        xhr.open(method, url, true);
658
662
        xhr.send(null);
659
663
    }
660
664
    else
661
665
    {
662
666
        /* POST sends the args in application/x-www-form-urlencoded */
663
667
        url = encodeURI(path);
664
 
        xhr.open(method, url, asyncronous);
 
668
        xhr.open(method, url, true);
665
669
        var message;
666
670
        if (content_type == "multipart/form-data")
667
671
        {
676
680
        }
677
681
        xhr.send(message);
678
682
    }
679
 
    /* Only return the XHR for syncronous requests */
680
 
    if (!asyncronous)
681
 
    { 
682
 
        return xhr;
683
 
    }
684
683
}
685
684
 
686
 
/** Attempts to JSON decodes a response object
687
 
 * If a non-200 response or the JSON decode fails then returns null
688
 
 */
689
 
function decode_response(response)
690
 
{
691
 
    if (response.status == 200)
692
 
    {
693
 
        try
694
 
        {
695
 
            var responseText = response.responseText;
696
 
            return JSON.parse(responseText);
697
 
        }
698
 
        catch (e)
699
 
        {
700
 
            // Pass
701
 
        }
702
 
     }
703
 
    
704
 
     return null;
705
 
}