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));
67
/** Converts a list of directories into a path name, with a slash at the end.
68
* \param pathlist List of strings.
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);
108
/** Given a number of bytes, returns a string representing the file size in a
109
* human-readable format.
110
* eg. nice_filesize(6) -> "6 bytes"
111
* nice_filesize(81275) -> "79.4 kB"
112
* nice_filesize(13498346) -> "12.9 MB"
113
* \param bytes Number of bytes. Must be an integer.
71
function pathlist_to_path(pathlist)
116
function nice_filesize(bytes)
74
for (var i=0; i<pathlist.length; i++)
76
ret += pathlist[i] + "/";
118
if (bytes == null) return "";
121
return bytes.toString() + " B";
124
return size.toFixed(1) + " kB";
127
return size.toFixed(1) + " MB";
129
return size.toFixed(1) + " GB";
81
132
/** Given a URL, returns an object containing a number of attributes
258
/** Builds a query_string from an args object. Encodes the arguments.
259
* \param args Args object as described in parse_url.
260
* \return Query string portion of a URL.
262
function make_query_string(args)
264
var query_string = "";
266
for (var arg_key in args)
268
arg_val = args[arg_key];
269
if (arg_val instanceof Array)
270
for (var i=0; i<arg_val.length; i++)
271
query_string += "&" + encodeURIComponent(arg_key) + "=" +
272
encodeURIComponent(arg_val[i]);
274
query_string += "&" + encodeURIComponent(arg_key) + "=" +
275
encodeURIComponent(arg_val);
277
if (query_string == "")
280
/* Drop the first "&" */
281
query_string = query_string.substr(1);
205
286
/** Given an object exactly of the form described for the output of parseurl,
206
* returns a URL string built from those parameters.
287
* returns a URL string built from those parameters. The URL is properly
207
289
* parseurl and buildurl are strict inverses of each other.
208
290
* Note that either query_string or args may be supplied. If both are
209
291
* supplied, query_string is preferred (because it keeps the argument order).
212
294
* \param obj Object as returned by parseurl.
213
295
* \return String, a URL.
215
function buildurl(obj)
297
function build_url(obj)
218
300
var query_string = null;
220
if (!("scheme" in obj) || obj.scheme != null)
302
if (("scheme" in obj) && obj.scheme != null)
221
303
url = obj.scheme.toString() + "://";
222
if (!("server_name" in obj) || obj.server_name != null)
304
if (("server_name" in obj) && obj.server_name != null)
223
305
url += obj.server_name.toString();
224
if (!("server_port" in obj) || obj.server_port != null)
306
if (("server_port" in obj) && obj.server_port != null)
225
307
url += ":" + obj.server_port.toString();
226
if (!("path" in obj) || obj.path != null)
308
if (("path" in obj) && obj.path != null)
228
310
var path = obj.path.toString();
229
if (path.length > 0 && path[0] != "/")
311
if (url.length > 0 && path.length > 0 && path[0] != "/")
230
312
path = "/" + path;
233
if (!("query_string" in obj) || obj.query_string != null)
315
if (("query_string" in obj) && obj.query_string != null)
234
316
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);
317
else if (("args" in obj) && obj.args != null)
318
query_string = make_query_string(obj.args);
258
320
if (query_string != null)
259
321
url += "?" + query_string;
323
return encodeURI(url);
264
326
/** Given an argument map, as output in the args parameter of the return of
366
/** Joins one or more paths together. Accepts 1 or more arguments.
368
function path_join(path1 /*, path2, ... */)
372
for (var i=0; i<arguments.length; i++)
375
if (arg.length == 0) continue;
380
if (path.length > 0 && path[path.length-1] != '/')
389
/** Builds a multipart_formdata string from an args object. Similar to
390
* make_query_string, but it returns data of type "multipart/form-data"
391
* instead of "application/x-www-form-urlencoded". This is good for
392
* encoding large strings such as text objects from the editor.
393
* Should be written with a Content-Type of
394
* "multipart/form-data, boundary=<boundary>".
395
* All fields are sent with a Content-Type of text/plain.
396
* \param args Args object as described in parse_url.
397
* \param boundary Random "magic" string which DOES NOT appear in any of
398
* the argument values. This should match the "boundary=" value written to
399
* the Content-Type header.
400
* \return String in multipart/form-data format.
402
function make_multipart_formdata(args, boundary)
407
var extend_data = function(arg_key, arg_val)
409
/* FIXME: Encoding not supported here (should not matter if we
410
* only use ASCII names */
411
data += "--" + boundary + "\n"
412
+ "Content-Disposition: form-data; name=\"" + arg_key
417
for (var arg_key in args)
419
arg_val = args[arg_key];
420
if (arg_val instanceof Array)
421
for (var i=0; i<arg_val.length; i++)
423
extend_data(arg_key, arg_val[i]);
426
extend_data(arg_key, arg_val);
429
data += "--" + boundary + "--\n";
434
/** Converts a list of directories into a path name, with a slash at the end.
435
* \param pathlist List of strings.
438
function pathlist_to_path(pathlist)
440
ret = path_join.apply(null, pathlist);
441
if (ret[ret.length-1] != '/')
446
/** Given a path relative to the IVLE root, gives a path relative to
449
function make_path(path)
451
return path_join(root_dir, path);
454
/** Makes an XMLHttpRequest call to the server. Waits (synchronously) for a
455
* response, and returns an XMLHttpRequest object containing the completed
458
* \param app IVLE app to call (such as "fileservice").
459
* \param path URL path to make the request to, within the application.
460
* \param args Argument object, as described in parse_url and friends.
461
* \param method String; "GET" or "POST"
462
* \param content_type String, optional. Only applies if method is "POST".
463
* May be "application/x-www-form-urlencoded" or "multipart/form-data".
464
* Defaults to "application/x-www-form-urlencoded".
465
* \return An XMLHttpRequest object containing the completed response.
467
function ajax_call(app, path, args, method, content_type)
469
if (content_type != "multipart/form-data")
470
content_type = "application/x-www-form-urlencoded";
471
path = make_path(path_join(app, path));
473
/* A random string, for multipart/form-data
474
* (This is not checked against anywhere else, it is solely defined and
475
* used within this function) */
476
var boundary = "48234n334nu7n4n2ynonjn234t683jyh80j";
477
var xhr = new XMLHttpRequest();
480
/* GET sends the args in the URL */
481
url = build_url({"path": path, "args": args});
482
/* open's 3rd argument = false -> SYNCHRONOUS (wait for response)
483
* (No need for a callback function) */
484
xhr.open(method, url, false);
489
/* POST sends the args in application/x-www-form-urlencoded */
490
url = encodeURI(path);
491
xhr.open(method, url, false);
493
if (content_type == "multipart/form-data")
495
xhr.setRequestHeader("Content-Type",
496
"multipart/form-data, boundary=" + boundary);
497
message = make_multipart_formdata(args, boundary);
501
xhr.setRequestHeader("Content-Type", content_type);
502
message = make_query_string(args);