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));
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);
67
113
/** Given a number of bytes, returns a string representing the file size in a
68
114
* human-readable format.
69
115
* eg. nice_filesize(6) -> "6 bytes"
107
154
* args is never null, though it may be empty.
109
156
* All strings are decoded/unescaped. Reserved characters
110
* (; , / ? : @ & = + * $) are not decoded except in args.
157
* (; , / ? : @ & = + * $) are not decoded except in args and path.
112
159
* \param url String. A URL. To read from the current browser window, use
113
160
* window.location.href.
265
309
url += ":" + obj.server_port.toString();
266
310
if (("path" in obj) && obj.path != null)
268
var path = obj.path.toString();
312
var path = urlencode_path(obj.path.toString());
269
313
if (url.length > 0 && path.length > 0 && path[0] != "/")
270
314
path = "/" + path;
273
317
if (("query_string" in obj) && obj.query_string != null)
274
query_string = obj.query_string.toString();
318
query_string = encodeURI(obj.query_string.toString());
275
319
else if (("args" in obj) && obj.args != null)
276
320
query_string = make_query_string(obj.args);
278
if (query_string != null)
322
if (query_string != "" && query_string != null)
279
323
url += "?" + query_string;
281
return encodeURI(url);
328
/** URL-encodes a path. This is a special case of URL encoding as all
329
* characters *except* the slash must be encoded.
331
function urlencode_path(path)
333
/* Split up the path, URLEncode each segment with encodeURIComponent,
336
var split = path.split('/');
337
for (var i=0; i<split.length; i++)
338
split[i] = encodeURIComponent(split[i]);
339
path = path_join.apply(null, split);
340
if (split[0] == "" && split.length > 1) path = "/" + path;
344
/** Writes a JSONable object to the cookie under a particular key
345
* (JSON encoded and URL encoded).
347
function write_cookie(key, value)
349
var sendstr = encodeURIComponent(key) + "="
350
+ encodeURIComponent(JSON.stringify(value))
351
+ "; path=" + urlencode_path(root_dir);
352
/* This actually just assigns to the key, not replacing the whole cookie
353
* as it appears to. */
354
document.cookie = sendstr;
356
/** Reads a cookie which has a JSONable object encoded as its value.
357
* Returns the object, parsed from JSON.
359
function read_cookie(key)
361
var cookies = document.cookie.split(";");
362
var checkstart = encodeURIComponent(key) + "=";
363
var checklen = checkstart.length;
364
for (var i=0; i<cookies.length; i++)
366
var cookie = cookies[i];
367
while (cookie[0] == ' ')
368
cookie = cookie.substr(1);
369
if (cookie.substr(0, checklen) == checkstart)
371
var valstr = cookie.substr(checklen);
372
valstr = decodeURIComponent(valstr);
373
return JSON.parse(valstr);
284
378
/** Given an argument map, as output in the args parameter of the return of
409
503
return path_join(root_dir, path);
412
/** Makes an XMLHttpRequest call to the server. Waits (synchronously) for a
413
* response, and returns an XMLHttpRequest object containing the completed
506
/** Shorthand for make_path(path_join(app, ...))
507
* Creates an absolute path for a given path within a given app.
509
function app_path(app /*,...*/)
511
return make_path(path_join.apply(null, arguments));
514
/** Given a path, gets the "basename" (the last path segment).
516
function path_basename(path)
518
segments = path.split("/");
519
if (segments[segments.length-1].length == 0)
520
return segments[segments.length-2];
522
return segments[segments.length-1];
525
/** Given a string str, determines whether it ends with substr */
526
function endswith(str, substring)
528
if (str.length < substring.length) return false;
529
return str.substr(str.length - substring.length) == substring;
532
/** Equivalent to Python's repr.
533
* Gets the JavaScript string representation.
534
* Actually just calls JSON.stringify.
538
return JSON.stringify(str);
541
/** Removes all occurences of a value from an array.
543
Array.prototype.removeall = function(val)
548
for (i=0; i<arr.length; i++)
551
if (arr[i] != val) j++;
556
/** Shallow-clones an object */
557
function shallow_clone_object(obj)
565
/** Returns a new XMLHttpRequest object, in a somewhat browser-agnostic
568
function new_xmlhttprequest()
573
return new XMLHttpRequest();
577
/* Internet Explorer */
580
return new ActiveXObject("Msxml2.XMLHTTP");
586
return new ActiveXObject("Microsoft.XMLHTTP");
590
throw("Your browser does not support AJAX. "
591
+ "IVLE requires a modern browser.");
597
/** Makes an asynchronous XMLHttpRequest call to the server.
598
* Sends the XMLHttpRequest object containing the completed response to a
599
* specified callback function.
601
* \param callback A callback function. Will be called when the response is
602
* complete. Passed 1 parameter, an XMLHttpRequest object containing the
603
* completed response.
416
604
* \param app IVLE app to call (such as "fileservice").
417
605
* \param path URL path to make the request to, within the application.
418
606
* \param args Argument object, as described in parse_url and friends.
420
608
* \param content_type String, optional. Only applies if method is "POST".
421
609
* May be "application/x-www-form-urlencoded" or "multipart/form-data".
422
610
* Defaults to "application/x-www-form-urlencoded".
423
* \return An XMLHttpRequest object containing the completed response.
425
function ajax_call(app, path, args, method, content_type)
612
function ajax_call(callback, app, path, args, method, content_type)
427
614
if (content_type != "multipart/form-data")
428
615
content_type = "application/x-www-form-urlencoded";
429
path = make_path(path_join(app, path));
616
path = app_path(app, path);
431
618
/* A random string, for multipart/form-data
432
619
* (This is not checked against anywhere else, it is solely defined and
433
620
* used within this function) */
434
621
var boundary = "48234n334nu7n4n2ynonjn234t683jyh80j";
435
var xhr = new XMLHttpRequest();
622
var xhr = new_xmlhttprequest();
623
xhr.onreadystatechange = function()
625
if (xhr.readyState == 4)
436
630
if (method == "GET")
438
632
/* GET sends the args in the URL */
439
633
url = build_url({"path": path, "args": args});
440
/* open's 3rd argument = false -> SYNCHRONOUS (wait for response)
441
* (No need for a callback function) */
442
xhr.open(method, url, false);
634
/* open's 3rd argument = true -> asynchronous */
635
xhr.open(method, url, true);
447
640
/* POST sends the args in application/x-www-form-urlencoded */
448
641
url = encodeURI(path);
449
xhr.open(method, url, false);
642
xhr.open(method, url, true);
451
644
if (content_type == "multipart/form-data")