~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-02-20 21:59:29 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:524
forum: Changed setup to just copy the phpBB directory (probbaly can be made 
neater) and trying to fix some CSS on the forum app frame.

Show diffs side-by-side

added added

removed removed

Lines of Context:
341
341
    return path;
342
342
}
343
343
 
344
 
/** Writes a JSONable object to the cookie under a particular key
345
 
 * (JSON encoded and URL encoded).
346
 
 */
347
 
function write_cookie(key, value)
348
 
{
349
 
    var sendstr = encodeURIComponent(key) + "="
350
 
        + encodeURIComponent(JSON.stringify(value))
351
 
        + "; path=" + urlencode_path(root_dir);
352
 
    /* This actually just assigns to the key, not replacing the whole cookie
353
 
     * as it appears to. */
354
 
    document.cookie = sendstr;
355
 
}
356
 
/** Reads a cookie which has a JSONable object encoded as its value.
357
 
 * Returns the object, parsed from JSON.
358
 
 */
359
 
function read_cookie(key)
360
 
{
361
 
    var cookies = document.cookie.split(";");
362
 
    var checkstart = encodeURIComponent(key) + "=";
363
 
    var checklen = checkstart.length;
364
 
    for (var i=0; i<cookies.length; i++)
365
 
    {
366
 
        var cookie = cookies[i];
367
 
        while (cookie[0] == ' ')
368
 
            cookie = cookie.substr(1);
369
 
        if (cookie.substr(0, checklen) == checkstart)
370
 
        {
371
 
            var valstr = cookie.substr(checklen);
372
 
            valstr = decodeURIComponent(valstr);
373
 
            return JSON.parse(valstr);
374
 
        }
375
 
    }
376
 
}
377
 
 
378
344
/** Given an argument map, as output in the args parameter of the return of
379
345
 * parseurl, gets the first occurence of an argument in the URL string.
380
346
 * If the argument was not found, returns null.
553
519
    arr.splice(j, i-j);
554
520
}
555
521
 
556
 
/** Shallow-clones an object */
557
 
function shallow_clone_object(obj)
558
 
{
559
 
    o = {};
560
 
    for (k in obj)
561
 
        o[k] = obj[k];
562
 
    return o;
563
 
}
564
 
 
565
 
/** Returns a new XMLHttpRequest object, in a somewhat browser-agnostic
566
 
 * fashion.
567
 
 */
568
 
function new_xmlhttprequest()
569
 
{
570
 
    try
571
 
    {
572
 
        /* Real Browsers */
573
 
        return new XMLHttpRequest();
574
 
    }
575
 
    catch (e)
576
 
    {
577
 
        /* Internet Explorer */
578
 
        try
579
 
        {
580
 
            return new ActiveXObject("Msxml2.XMLHTTP");
581
 
        }
582
 
        catch (e)
583
 
        {
584
 
            try
585
 
            {
586
 
                return new ActiveXObject("Microsoft.XMLHTTP");
587
 
            }
588
 
            catch (e)
589
 
            {
590
 
                throw("Your browser does not support AJAX. "
591
 
                    + "IVLE requires a modern browser.");
592
 
            }
593
 
        }
594
 
    }
595
 
}
596
 
 
597
 
/** Makes an asynchronous XMLHttpRequest call to the server.
598
 
 * Sends the XMLHttpRequest object containing the completed response to a
599
 
 * specified callback function.
 
522
/** Makes an XMLHttpRequest call to the server. Waits (synchronously) for a
 
523
 * response, and returns an XMLHttpRequest object containing the completed
 
524
 * response.
600
525
 *
601
 
 * \param callback A callback function. Will be called when the response is
602
 
 *      complete. Passed 1 parameter, an XMLHttpRequest object containing the
603
 
 *      completed response.
604
526
 * \param app IVLE app to call (such as "fileservice").
605
527
 * \param path URL path to make the request to, within the application.
606
528
 * \param args Argument object, as described in parse_url and friends.
608
530
 * \param content_type String, optional. Only applies if method is "POST".
609
531
 *      May be "application/x-www-form-urlencoded" or "multipart/form-data".
610
532
 *      Defaults to "application/x-www-form-urlencoded".
 
533
 * \return An XMLHttpRequest object containing the completed response.
611
534
 */
612
 
function ajax_call(callback, app, path, args, method, content_type)
 
535
function ajax_call(app, path, args, method, content_type)
613
536
{
614
537
    if (content_type != "multipart/form-data")
615
538
        content_type = "application/x-www-form-urlencoded";
619
542
     * (This is not checked against anywhere else, it is solely defined and
620
543
     * used within this function) */
621
544
    var boundary = "48234n334nu7n4n2ynonjn234t683jyh80j";
622
 
    var xhr = new_xmlhttprequest();
623
 
    xhr.onreadystatechange = function()
624
 
        {
625
 
            if (xhr.readyState == 4)
626
 
            {
627
 
                callback(xhr);
628
 
            }
629
 
        }
 
545
    var xhr = new XMLHttpRequest();
630
546
    if (method == "GET")
631
547
    {
632
548
        /* GET sends the args in the URL */
633
549
        url = build_url({"path": path, "args": args});
634
 
        /* open's 3rd argument = true -> asynchronous */
635
 
        xhr.open(method, url, true);
 
550
        /* open's 3rd argument = false -> SYNCHRONOUS (wait for response)
 
551
         * (No need for a callback function) */
 
552
        xhr.open(method, url, false);
636
553
        xhr.send(null);
637
554
    }
638
555
    else
639
556
    {
640
557
        /* POST sends the args in application/x-www-form-urlencoded */
641
558
        url = encodeURI(path);
642
 
        xhr.open(method, url, true);
 
559
        xhr.open(method, url, false);
643
560
        var message;
644
561
        if (content_type == "multipart/form-data")
645
562
        {
655
572
        xhr.setRequestHeader("Content-Length", message.length);
656
573
        xhr.send(message);
657
574
    }
 
575
    return xhr;
658
576
}
659
577