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.
72
* \param dontencode Optional boolean. If true, will not encode the href.
73
* if including query strings, you must set this to true and use build_url
74
* to escape the URI correctly.
53
75
* \return DOM Element object.
55
function dom_make_link_elem(tagname, text, href, onclick)
77
function dom_make_link_elem(tagname, text, title, href, onclick, dontencode)
79
if (text == null) text = "";
80
if (href == null) href = "";
57
81
var elem = document.createElement(tagname);
58
82
var link = document.createElement("a");
59
link.setAttribute("href", encodeURI(href));
83
if (dontencode != true)
84
href = urlencode_path(href);
85
link.setAttribute("href", href);
87
link.setAttribute("title", title);
60
88
if (onclick != null)
61
89
link.setAttribute("onclick", onclick);
62
90
link.appendChild(document.createTextNode(text));
67
/** Converts a list of directories into a path name, with a slash at the end.
68
* \param pathlist List of strings.
95
/** Creates a DOM img element. All parameters are optional except src.
96
* If alt (compulsory in HTML) is omitted, will be set to "".
98
function dom_make_img(src, width, height, title, alt)
100
var img = document.createElement("img");
101
img.setAttribute("src", urlencode_path(src));
103
img.setAttribute("width", width);
105
img.setAttribute("height", height);
107
img.setAttribute("title", title);
108
if (alt == null) alt = "";
109
img.setAttribute("alt", alt);
113
/** Given a number of bytes, returns a string representing the file size in a
114
* human-readable format.
115
* eg. nice_filesize(6) -> "6 bytes"
116
* nice_filesize(81275) -> "79.4 kB"
117
* nice_filesize(13498346) -> "12.9 MB"
118
* \param bytes Number of bytes. Must be an integer.
71
function pathlist_to_path(pathlist)
121
function nice_filesize(bytes)
74
for (var i=0; i<pathlist.length; i++)
76
ret += pathlist[i] + "/";
123
if (bytes == null) return "";
126
return bytes.toString() + " B";
129
return size.toFixed(1) + " kB";
132
return size.toFixed(1) + " MB";
134
return size.toFixed(1) + " GB";
81
137
/** Given a URL, returns an object containing a number of attributes
262
/** Builds a query_string from an args object. Encodes the arguments.
263
* \param args Args object as described in parse_url.
264
* \return Query string portion of a URL.
266
function make_query_string(args)
268
var query_string = "";
270
for (var arg_key in args)
272
arg_val = args[arg_key];
273
if (arg_val instanceof Array)
274
for (var i=0; i<arg_val.length; i++)
275
query_string += "&" + encodeURIComponent(arg_key) + "=" +
276
encodeURIComponent(arg_val[i]);
278
query_string += "&" + encodeURIComponent(arg_key) + "=" +
279
encodeURIComponent(arg_val);
281
if (query_string == "")
284
/* Drop the first "&" */
285
query_string = query_string.substr(1);
205
290
/** Given an object exactly of the form described for the output of parseurl,
206
* returns a URL string built from those parameters.
291
* returns a URL string built from those parameters. The URL is properly
207
293
* parseurl and buildurl are strict inverses of each other.
208
294
* Note that either query_string or args may be supplied. If both are
209
295
* supplied, query_string is preferred (because it keeps the argument order).
212
298
* \param obj Object as returned by parseurl.
213
299
* \return String, a URL.
215
function buildurl(obj)
301
function build_url(obj)
218
304
var query_string = null;
220
if (!("scheme" in obj) || obj.scheme != null)
306
if (("scheme" in obj) && obj.scheme != null)
221
307
url = obj.scheme.toString() + "://";
222
if (!("server_name" in obj) || obj.server_name != null)
308
if (("server_name" in obj) && obj.server_name != null)
223
309
url += obj.server_name.toString();
224
if (!("server_port" in obj) || obj.server_port != null)
310
if (("server_port" in obj) && obj.server_port != null)
225
311
url += ":" + obj.server_port.toString();
226
if (!("path" in obj) || obj.path != null)
312
if (("path" in obj) && obj.path != null)
228
var path = obj.path.toString();
229
if (path.length > 0 && path[0] != "/")
314
var path = urlencode_path(obj.path.toString());
315
if (url.length > 0 && path.length > 0 && path[0] != "/")
230
316
path = "/" + path;
233
if (!("query_string" in obj) || obj.query_string != null)
234
query_string = obj.query_string.toString();
235
else if (!("args" in obj) || obj.args != null)
239
for (var arg_key in obj.args)
241
arg_val = obj.args[arg_key];
242
if (arg_val instanceof Array)
243
for (var i=0; i<arg_val.length; i++)
244
query_string += "&" + encodeURI(arg_key) + "=" +
245
encodeURI(arg_val[i]);
247
query_string += "&" + encodeURI(arg_key) + "=" +
251
if (query_string == "")
254
/* Drop the first "&" */
255
query_string = query_string.substr(1);
319
if (("query_string" in obj) && obj.query_string != null)
320
query_string = encodeURI(obj.query_string.toString());
321
else if (("args" in obj) && obj.args != null)
322
query_string = make_query_string(obj.args);
258
324
if (query_string != null)
259
325
url += "?" + query_string;
386
/** Joins one or more paths together. Accepts 1 or more arguments.
388
function path_join(path1 /*, path2, ... */)
392
for (var i=0; i<arguments.length; i++)
395
if (arg.length == 0) continue;
400
if (path.length > 0 && path[path.length-1] != '/')
409
/** Builds a multipart_formdata string from an args object. Similar to
410
* make_query_string, but it returns data of type "multipart/form-data"
411
* instead of "application/x-www-form-urlencoded". This is good for
412
* encoding large strings such as text objects from the editor.
413
* Should be written with a Content-Type of
414
* "multipart/form-data, boundary=<boundary>".
415
* All fields are sent with a Content-Type of text/plain.
416
* \param args Args object as described in parse_url.
417
* \param boundary Random "magic" string which DOES NOT appear in any of
418
* the argument values. This should match the "boundary=" value written to
419
* the Content-Type header.
420
* \return String in multipart/form-data format.
422
function make_multipart_formdata(args, boundary)
427
var extend_data = function(arg_key, arg_val)
429
/* FIXME: Encoding not supported here (should not matter if we
430
* only use ASCII names */
431
data += "--" + boundary + "\n"
432
+ "Content-Disposition: form-data; name=\"" + arg_key
437
for (var arg_key in args)
439
arg_val = args[arg_key];
440
if (arg_val instanceof Array)
441
for (var i=0; i<arg_val.length; i++)
443
extend_data(arg_key, arg_val[i]);
446
extend_data(arg_key, arg_val);
449
data += "--" + boundary + "--\n";
454
/** Converts a list of directories into a path name, with a slash at the end.
455
* \param pathlist List of strings.
458
function pathlist_to_path(pathlist)
460
ret = path_join.apply(null, pathlist);
461
if (ret[ret.length-1] != '/')
466
/** Given a path relative to the IVLE root, gives a path relative to
469
function make_path(path)
471
return path_join(root_dir, path);
474
/** Shorthand for make_path(path_join(app, ...))
475
* Creates an absolute path for a given path within a given app.
477
function app_path(app /*,...*/)
479
return make_path(path_join.apply(null, arguments));
482
/** Given a path, gets the "basename" (the last path segment).
484
function path_basename(path)
486
segments = path.split("/");
487
if (segments[segments.length-1].length == 0)
488
return segments[segments.length-2];
490
return segments[segments.length-1];
493
/** Given a string str, determines whether it ends with substr */
494
function endswith(str, substring)
496
if (str.length < substring.length) return false;
497
return str.substr(str.length - substring.length) == substring;
500
/** Equivalent to Python's repr.
501
* Gets the JavaScript string representation.
502
* Actually just calls JSON.stringify.
506
return JSON.stringify(str);
509
/** Removes all occurences of a value from an array.
511
Array.prototype.removeall = function(val)
516
for (i=0; i<arr.length; i++)
519
if (arr[i] != val) j++;
524
/** Makes an XMLHttpRequest call to the server. Waits (synchronously) for a
525
* response, and returns an XMLHttpRequest object containing the completed
528
* \param app IVLE app to call (such as "fileservice").
529
* \param path URL path to make the request to, within the application.
530
* \param args Argument object, as described in parse_url and friends.
531
* \param method String; "GET" or "POST"
532
* \param content_type String, optional. Only applies if method is "POST".
533
* May be "application/x-www-form-urlencoded" or "multipart/form-data".
534
* Defaults to "application/x-www-form-urlencoded".
535
* \return An XMLHttpRequest object containing the completed response.
537
function ajax_call(app, path, args, method, content_type)
539
if (content_type != "multipart/form-data")
540
content_type = "application/x-www-form-urlencoded";
541
path = app_path(app, path);
543
/* A random string, for multipart/form-data
544
* (This is not checked against anywhere else, it is solely defined and
545
* used within this function) */
546
var boundary = "48234n334nu7n4n2ynonjn234t683jyh80j";
547
var xhr = new XMLHttpRequest();
550
/* GET sends the args in the URL */
551
url = build_url({"path": path, "args": args});
552
/* open's 3rd argument = false -> SYNCHRONOUS (wait for response)
553
* (No need for a callback function) */
554
xhr.open(method, url, false);
559
/* POST sends the args in application/x-www-form-urlencoded */
560
url = encodeURI(path);
561
xhr.open(method, url, false);
563
if (content_type == "multipart/form-data")
565
xhr.setRequestHeader("Content-Type",
566
"multipart/form-data, boundary=" + boundary);
567
message = make_multipart_formdata(args, boundary);
571
xhr.setRequestHeader("Content-Type", content_type);
572
message = make_query_string(args);