22
22
* Defines some generic JavaScript utility functions.
25
/* Expects the following variables to have been declared by JavaScript in
26
* the HTML generated by the server:
25
31
/** Removes all children of a given DOM element
26
32
* \param elem A DOM Element. Will be modified.
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.
39
function dom_make_text_elem(tagname, text)
47
function dom_make_text_elem(tagname, text, title)
49
if (text == null) text = "";
41
50
var elem = document.createElement(tagname);
42
elem.appendChild(document.createTextNode(text));
53
textnode = document.createTextNode(text);
56
textnode = document.createElement("span");
57
textnode.setAttribute("title", title);
58
textnode.appendChild(document.createTextNode(text));
60
elem.appendChild(textnode);
46
64
/** Creates a DOM element with hyperlinked text inside it.
47
65
* \param tagname String. Name of the element's tag (eg. "p").
48
66
* \param text String. Text to be placed inside the element.
67
* \param title String, optional. Sets a tooltip for the link.
49
68
* \param href String. URL the text will link to. This is a raw string,
50
69
* it will automatically be URL-encoded.
51
70
* \param onclick Optional string. Will be set as the "onclick" attribute
52
71
* of the "a" element.
53
72
* \return DOM Element object.
55
function dom_make_link_elem(tagname, text, href, onclick)
74
function dom_make_link_elem(tagname, text, title, href, onclick)
76
if (text == null) text = "";
77
if (href == null) href = "";
57
78
var elem = document.createElement(tagname);
58
79
var link = document.createElement("a");
59
80
link.setAttribute("href", encodeURI(href));
82
link.setAttribute("title", title);
60
83
if (onclick != null)
61
84
link.setAttribute("onclick", onclick);
62
85
link.appendChild(document.createTextNode(text));
90
/** Creates a DOM img element. All parameters are optional except src.
91
* If alt (compulsory in HTML) is omitted, will be set to "".
93
function dom_make_img(src, width, height, title, alt)
95
var img = document.createElement("img");
96
img.setAttribute("src", src);
98
img.setAttribute("width", width);
100
img.setAttribute("height", height);
102
img.setAttribute("title", title);
103
if (alt == null) alt = "";
104
img.setAttribute("alt", alt);
67
108
/** Given a number of bytes, returns a string representing the file size in a
68
109
* human-readable format.
69
110
* eg. nice_filesize(6) -> "6 bytes"
409
451
return path_join(root_dir, path);
454
/** Given a path, gets the "basename" (the last path segment).
456
function path_basename(path)
458
segments = path.split("/");
459
if (segments[segments.length-1].length == 0)
460
return segments[segments.length-2];
462
return segments[segments.length-1];
465
/** Given a string str, determines whether it ends with substr */
466
function endswith(str, substring)
468
if (str.length < substring.length) return false;
469
return str.substr(str.length - substring.length) == substring;
472
/** Equivalent to Python's repr.
473
* Gets the JavaScript string representation.
474
* Actually just calls JSON.stringify.
478
return JSON.stringify(str);
412
481
/** Makes an XMLHttpRequest call to the server. Waits (synchronously) for a
413
482
* response, and returns an XMLHttpRequest object containing the completed