~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-01-11 04:01:16 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:176
util: Rewrote pathlist_to_path in terms of path_join (more robust and
    shorter).
        Added function ajax_call. Able to make GET and POST (urlencoded) Ajax
        requests and return a response object.

Show diffs side-by-side

added added

removed removed

Lines of Context:
64
64
    return elem;
65
65
}
66
66
 
67
 
/** Converts a list of directories into a path name, with a slash at the end.
68
 
 * \param pathlist List of strings.
69
 
 * \return String.
70
 
 */
71
 
function pathlist_to_path(pathlist)
72
 
{
73
 
    ret = "";
74
 
    for (var i=0; i<pathlist.length; i++)
75
 
    {
76
 
        ret += pathlist[i] + "/";
77
 
    }
78
 
    return ret;
79
 
}
80
 
 
81
67
/** Given a URL, returns an object containing a number of attributes
82
68
 * describing the components of the URL, similar to CGI request variables.
83
69
 * The object has the following attributes:
229
215
    if (("path" in obj) && obj.path != null)
230
216
    {
231
217
        var path = obj.path.toString();
232
 
        if (path.length > 0 && path[0] != "/")
 
218
        if (url.length > 0 && path.length > 0 && path[0] != "/")
233
219
            path = "/" + path;
234
220
        url += path;
235
221
    }
325
311
    return path;
326
312
}
327
313
 
 
314
/** Converts a list of directories into a path name, with a slash at the end.
 
315
 * \param pathlist List of strings.
 
316
 * \return String.
 
317
 */
 
318
function pathlist_to_path(pathlist)
 
319
{
 
320
    ret = path_join.apply(null, pathlist);
 
321
    if (ret[ret.length-1] != '/')
 
322
        ret += '/';
 
323
    return ret;
 
324
}
 
325
 
328
326
/** Given a path relative to the IVLE root, gives a path relative to
329
327
 * the site root.
330
328
 */
333
331
    return path_join(root_dir, path);
334
332
}
335
333
 
 
334
/** Makes an XMLHttpRequest call to the server. Waits (synchronously) for a
 
335
 * response, and returns an XMLHttpRequest object containing the completed
 
336
 * response.
 
337
 *
 
338
 * \param app IVLE app to call (such as "fileservice").
 
339
 * \param path URL path to make the request to, within the application.
 
340
 * \param args Argument object, as described in parse_url and friends.
 
341
 * \param method String; "GET" or "POST"
 
342
 * \return An XMLHttpRequest object containing the completed response.
 
343
 */
 
344
function ajax_call(app, path, args, method)
 
345
{
 
346
    path = make_path(path_join(app, path));
 
347
    var url;
 
348
    var xhr = new XMLHttpRequest();
 
349
    if (method == "GET")
 
350
    {
 
351
        /* GET sends the args in the URL */
 
352
        url = build_url({"path": path, "args": args});
 
353
        /* open's 3rd argument = false -> SYNCHRONOUS (wait for response)
 
354
         * (No need for a callback function) */
 
355
        xhr.open(method, url, false);
 
356
        xhr.send("");
 
357
    }
 
358
    else
 
359
    {
 
360
        /* POST sends the args in application/x-www-form-urlencoded */
 
361
        url = encodeURI(path);
 
362
        xhr.open(method, url, false);
 
363
        xhr.setRequestHeader("Content-Type",
 
364
            "application/x-www-form-urlencoded");
 
365
        var message = build_url({"args": args}).substr(1); /* Remove "?" */
 
366
        xhr.send(message);
 
367
    }
 
368
    return xhr;
 
369
}