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

« back to all changes in this revision

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

  • Committer: chadnickbok
  • Date: 2009-02-02 04:00:25 UTC
  • Revision ID: svn-v4:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:1189
Adding the changes from my genshi branch into trunk.

Most apps now use the Genshi templating engine, in preparation
for future changes to dispatch

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
 
 
550
541
/** Removes all occurences of a value from an array.
551
542
 */
552
543
Array.prototype.removeall = function(val)
620
611
    return str.join('');
621
612
}
622
613
 
623
 
/** Makes an asynchronous XMLHttpRequest call to the server.
 
614
/** Makes an XMLHttpRequest call to the server.
624
615
 * Sends the XMLHttpRequest object containing the completed response to a
625
616
 * specified callback function.
626
617
 *
627
618
 * \param callback A callback function. Will be called when the response is
628
619
 *      complete. Passed 1 parameter, an XMLHttpRequest object containing the
629
 
 *      completed response.
 
620
 *      completed response. If callback is null this is a syncronous request 
 
621
 *      otherwise this is an asynchronous request.
630
622
 * \param app IVLE app to call (such as "fileservice").
631
623
 * \param path URL path to make the request to, within the application.
632
624
 * \param args Argument object, as described in parse_url and friends.
646
638
     * used within this function) */
647
639
    var boundary = random_string(20);
648
640
    var xhr = new_xmlhttprequest();
649
 
    xhr.onreadystatechange = function()
650
 
        {
651
 
            if (xhr.readyState == 4)
 
641
    var asyncronous = callback != null;
 
642
    if (asyncronous)
 
643
    {
 
644
        xhr.onreadystatechange = function()
652
645
            {
653
 
                callback(xhr);
 
646
                if (xhr.readyState == 4)
 
647
                {
 
648
                    callback(xhr);
 
649
                }
654
650
            }
655
 
        }
 
651
    }
656
652
    if (method == "GET")
657
653
    {
658
654
        /* GET sends the args in the URL */
659
655
        url = build_url({"path": path, "args": args});
660
656
        /* open's 3rd argument = true -> asynchronous */
661
 
        xhr.open(method, url, true);
 
657
        xhr.open(method, url, asyncronous);
662
658
        xhr.send(null);
663
659
    }
664
660
    else
665
661
    {
666
662
        /* POST sends the args in application/x-www-form-urlencoded */
667
663
        url = encodeURI(path);
668
 
        xhr.open(method, url, true);
 
664
        xhr.open(method, url, asyncronous);
669
665
        var message;
670
666
        if (content_type == "multipart/form-data")
671
667
        {
680
676
        }
681
677
        xhr.send(message);
682
678
    }
 
679
    /* Only return the XHR for syncronous requests */
 
680
    if (!asyncronous)
 
681
    { 
 
682
        return xhr;
 
683
    }
683
684
}
684
685
 
 
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
}