70
/** Creates a DOM img element. All parameters are optional except src.
71
* If alt (compulsory in HTML) is omitted, will be set to "".
73
function dom_make_img(src, width, height, title, alt)
75
var img = document.createElement("img");
76
img.setAttribute("src", src);
78
img.setAttribute("width", width);
80
img.setAttribute("height", height);
82
img.setAttribute("title", title);
83
if (alt == null) alt = "";
84
img.setAttribute("alt", alt);
88
/** Given a number of bytes, returns a string representing the file size in a
89
* human-readable format.
90
* eg. nice_filesize(6) -> "6 bytes"
91
* nice_filesize(81275) -> "79.4 kB"
92
* nice_filesize(13498346) -> "12.9 MB"
93
* \param bytes Number of bytes. Must be an integer.
67
/** Converts a list of directories into a path name, with a slash at the end.
68
* \param pathlist List of strings.
96
function nice_filesize(bytes)
71
function pathlist_to_path(pathlist)
98
if (bytes == null) return "";
101
return bytes.toString() + " B";
104
return size.toFixed(1) + " kB";
107
return size.toFixed(1) + " MB";
109
return size.toFixed(1) + " GB";
74
for (var i=0; i<pathlist.length; i++)
76
ret += pathlist[i] + "/";
112
81
/** Given a URL, returns an object containing a number of attributes
238
/** Builds a query_string from an args object. Encodes the arguments.
239
* \param args Args object as described in parse_url.
240
* \return Query string portion of a URL.
242
function make_query_string(args)
244
var query_string = "";
246
for (var arg_key in args)
248
arg_val = args[arg_key];
249
if (arg_val instanceof Array)
250
for (var i=0; i<arg_val.length; i++)
251
query_string += "&" + encodeURIComponent(arg_key) + "=" +
252
encodeURIComponent(arg_val[i]);
254
query_string += "&" + encodeURIComponent(arg_key) + "=" +
255
encodeURIComponent(arg_val);
257
if (query_string == "")
260
/* Drop the first "&" */
261
query_string = query_string.substr(1);
266
205
/** Given an object exactly of the form described for the output of parseurl,
267
* returns a URL string built from those parameters. The URL is properly
206
* returns a URL string built from those parameters.
269
207
* parseurl and buildurl are strict inverses of each other.
270
208
* Note that either query_string or args may be supplied. If both are
271
209
* supplied, query_string is preferred (because it keeps the argument order).
274
212
* \param obj Object as returned by parseurl.
275
213
* \return String, a URL.
277
function build_url(obj)
215
function buildurl(obj)
280
218
var query_string = null;
282
if (("scheme" in obj) && obj.scheme != null)
220
if (!("scheme" in obj) || obj.scheme != null)
283
221
url = obj.scheme.toString() + "://";
284
if (("server_name" in obj) && obj.server_name != null)
222
if (!("server_name" in obj) || obj.server_name != null)
285
223
url += obj.server_name.toString();
286
if (("server_port" in obj) && obj.server_port != null)
224
if (!("server_port" in obj) || obj.server_port != null)
287
225
url += ":" + obj.server_port.toString();
288
if (("path" in obj) && obj.path != null)
226
if (!("path" in obj) || obj.path != null)
290
228
var path = obj.path.toString();
291
if (url.length > 0 && path.length > 0 && path[0] != "/")
229
if (path.length > 0 && path[0] != "/")
292
230
path = "/" + path;
295
if (("query_string" in obj) && obj.query_string != null)
233
if (!("query_string" in obj) || obj.query_string != null)
296
234
query_string = obj.query_string.toString();
297
else if (("args" in obj) && obj.args != null)
298
query_string = make_query_string(obj.args);
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);
300
258
if (query_string != null)
301
259
url += "?" + query_string;
303
return encodeURI(url);
306
264
/** Given an argument map, as output in the args parameter of the return of
346
/** Joins one or more paths together. Accepts 1 or more arguments.
348
function path_join(path1 /*, path2, ... */)
352
for (var i=1; i<arguments.length; i++)
355
if (arg.length == 0) continue;
360
if (path[path.length-1] != '/')
369
/** Builds a multipart_formdata string from an args object. Similar to
370
* make_query_string, but it returns data of type "multipart/form-data"
371
* instead of "application/x-www-form-urlencoded". This is good for
372
* encoding large strings such as text objects from the editor.
373
* Should be written with a Content-Type of
374
* "multipart/form-data, boundary=<boundary>".
375
* All fields are sent with a Content-Type of text/plain.
376
* \param args Args object as described in parse_url.
377
* \param boundary Random "magic" string which DOES NOT appear in any of
378
* the argument values. This should match the "boundary=" value written to
379
* the Content-Type header.
380
* \return String in multipart/form-data format.
382
function make_multipart_formdata(args, boundary)
387
var extend_data = function(arg_key, arg_val)
389
/* FIXME: Encoding not supported here (should not matter if we
390
* only use ASCII names */
391
data += "--" + boundary + "\n"
392
+ "Content-Disposition: form-data; name=\"" + arg_key
397
for (var arg_key in args)
399
arg_val = args[arg_key];
400
if (arg_val instanceof Array)
401
for (var i=0; i<arg_val.length; i++)
403
extend_data(arg_key, arg_val[i]);
406
extend_data(arg_key, arg_val);
409
data += "--" + boundary + "--\n";
414
/** Converts a list of directories into a path name, with a slash at the end.
415
* \param pathlist List of strings.
418
function pathlist_to_path(pathlist)
420
ret = path_join.apply(null, pathlist);
421
if (ret[ret.length-1] != '/')
426
/** Given a path relative to the IVLE root, gives a path relative to
429
function make_path(path)
431
return path_join(root_dir, path);
434
/** Makes an XMLHttpRequest call to the server. Waits (synchronously) for a
435
* response, and returns an XMLHttpRequest object containing the completed
438
* \param app IVLE app to call (such as "fileservice").
439
* \param path URL path to make the request to, within the application.
440
* \param args Argument object, as described in parse_url and friends.
441
* \param method String; "GET" or "POST"
442
* \param content_type String, optional. Only applies if method is "POST".
443
* May be "application/x-www-form-urlencoded" or "multipart/form-data".
444
* Defaults to "application/x-www-form-urlencoded".
445
* \return An XMLHttpRequest object containing the completed response.
447
function ajax_call(app, path, args, method, content_type)
449
if (content_type != "multipart/form-data")
450
content_type = "application/x-www-form-urlencoded";
451
path = make_path(path_join(app, path));
453
/* A random string, for multipart/form-data
454
* (This is not checked against anywhere else, it is solely defined and
455
* used within this function) */
456
var boundary = "48234n334nu7n4n2ynonjn234t683jyh80j";
457
var xhr = new XMLHttpRequest();
460
/* GET sends the args in the URL */
461
url = build_url({"path": path, "args": args});
462
/* open's 3rd argument = false -> SYNCHRONOUS (wait for response)
463
* (No need for a callback function) */
464
xhr.open(method, url, false);
469
/* POST sends the args in application/x-www-form-urlencoded */
470
url = encodeURI(path);
471
xhr.open(method, url, false);
473
if (content_type == "multipart/form-data")
475
xhr.setRequestHeader("Content-Type",
476
"multipart/form-data, boundary=" + boundary);
477
message = make_multipart_formdata(args, boundary);
481
xhr.setRequestHeader("Content-Type", content_type);
482
message = make_query_string(args);