~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-12 15:32:54 UTC
  • Revision ID: svn-v3-trunk0:2b9c9e99-6f39-0410-b283-7f802c844ae2:trunk:200
fileservice.listing: Now returns a nicer date format for mtime_nice.
    Applies an intuitive algorithm to make the date even shorter,
    (eg. "today" or "3 days ago") and returns this as mtime_short.
util.js: Fixed path_join if the first path is empty. (Was incorrectly adding
    a leading slash).
    Added optional "title" arguments to dom_make_text_elem and
    dom_make_link_elem.
browser.js: Displays mtime_short in the file table instead of mtime_nice
    (it was always too long). Displays the full date as a tooltip.
    Auto-navigates to the user's home directory if accessed from the root.
    (This may be a temporary measure, but it was giving a mean error before).
    Various code makes use of util's new optional "title" arguments.
dispatch.html: Writes a new JavaScript variable, "username", so JavaScript
    code knows which user is logged in.
    Cleaned up JS variables repr (previously they did bad escapes in some
    cases).

Show diffs side-by-side

added added

removed removed

Lines of Context:
22
22
 * Defines some generic JavaScript utility functions.
23
23
 */
24
24
 
 
25
/* Expects the following variables to have been declared by JavaScript in
 
26
 * the HTML generated by the server:
 
27
 * - root_dir
 
28
 * - username
 
29
 */
 
30
 
25
31
/** Removes all children of a given DOM element
26
32
 * \param elem A DOM Element. Will be modified.
27
33
 */
34
40
/** Creates a DOM element with simple text inside it.
35
41
 * \param tagname String. Name of the element's tag (eg. "p").
36
42
 * \param text String. Text to be placed inside the element.
 
43
 * \param title String, optional. Tooltip for the text.
 
44
 *  (Note, title creates a span element around the text).
37
45
 * \return DOM Element object.
38
46
 */
39
 
function dom_make_text_elem(tagname, text)
 
47
function dom_make_text_elem(tagname, text, title)
40
48
{
41
49
    if (text == null) text = "";
42
50
    var elem = document.createElement(tagname);
43
 
    elem.appendChild(document.createTextNode(text));
 
51
    var textnode;
 
52
    if (title == null)
 
53
        textnode = document.createTextNode(text);
 
54
    else
 
55
    {
 
56
        textnode = document.createElement("span");
 
57
        textnode.setAttribute("title", title);
 
58
        textnode.appendChild(document.createTextNode(text));
 
59
    }
 
60
    elem.appendChild(textnode);
44
61
    return elem;
45
62
}
46
63
 
47
64
/** Creates a DOM element with hyperlinked text inside it.
48
65
 * \param tagname String. Name of the element's tag (eg. "p").
49
66
 * \param text String. Text to be placed inside the element.
 
67
 * \param title String, optional. Sets a tooltip for the link.
50
68
 * \param href String. URL the text will link to. This is a raw string,
51
69
 *  it will automatically be URL-encoded.
52
70
 * \param onclick Optional string. Will be set as the "onclick" attribute
53
71
 *  of the "a" element.
54
72
 * \return DOM Element object.
55
73
 */
56
 
function dom_make_link_elem(tagname, text, href, onclick)
 
74
function dom_make_link_elem(tagname, text, title, href, onclick)
57
75
{
58
76
    if (text == null) text = "";
59
77
    if (href == null) href = "";
60
78
    var elem = document.createElement(tagname);
61
79
    var link = document.createElement("a");
62
80
    link.setAttribute("href", encodeURI(href));
 
81
    if (title != null)
 
82
        link.setAttribute("title", title);
63
83
    if (onclick != null)
64
84
        link.setAttribute("onclick", onclick);
65
85
    link.appendChild(document.createTextNode(text));
348
368
function path_join(path1 /*, path2, ... */)
349
369
{
350
370
    var arg;
351
 
    path = path1;
352
 
    for (var i=1; i<arguments.length; i++)
 
371
    var path = "";
 
372
    for (var i=0; i<arguments.length; i++)
353
373
    {
354
374
        arg = arguments[i];
355
375
        if (arg.length == 0) continue;
357
377
            path = arg;
358
378
        else
359
379
        {
360
 
            if (path[path.length-1] != '/')
 
380
            if (path.length > 0 && path[path.length-1] != '/')
361
381
                path += '/';
362
382
            path += arg;
363
383
        }