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)
41
49
if (text == null) text = "";
42
50
var elem = document.createElement(tagname);
43
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);
47
64
/** Creates a DOM element with hyperlinked text inside it.
48
65
* \param tagname String. Name of the element's tag (eg. "p").
49
66
* \param text String. Text to be placed inside the element.
67
* \param title String, optional. Sets a tooltip for the link.
50
68
* \param href String. URL the text will link to. This is a raw string,
51
69
* it will automatically be URL-encoded.
52
70
* \param onclick Optional string. Will be set as the "onclick" attribute
53
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.
54
75
* \return DOM Element object.
56
function dom_make_link_elem(tagname, text, href, onclick)
77
function dom_make_link_elem(tagname, text, title, href, onclick, dontencode)
58
79
if (text == null) text = "";
59
80
if (href == null) href = "";
60
81
var elem = document.createElement(tagname);
61
82
var link = document.createElement("a");
62
link.setAttribute("href", encodeURI(href));
83
if (dontencode != true)
84
href = urlencode_path(href);
85
link.setAttribute("href", href);
87
link.setAttribute("title", title);
63
88
if (onclick != null)
64
89
link.setAttribute("onclick", onclick);
65
90
link.appendChild(document.createTextNode(text));
73
98
function dom_make_img(src, width, height, title, alt)
75
100
var img = document.createElement("img");
76
img.setAttribute("src", src);
101
img.setAttribute("src", urlencode_path(src));
77
102
if (width != null)
78
103
img.setAttribute("width", width);
79
104
if (height != null)
129
154
* args is never null, though it may be empty.
131
156
* All strings are decoded/unescaped. Reserved characters
132
* (; , / ? : @ & = + * $) are not decoded except in args.
157
* (; , / ? : @ & = + * $) are not decoded except in args and path.
134
159
* \param url String. A URL. To read from the current browser window, use
135
160
* window.location.href.
287
309
url += ":" + obj.server_port.toString();
288
310
if (("path" in obj) && obj.path != null)
290
var path = obj.path.toString();
312
var path = urlencode_path(obj.path.toString());
291
313
if (url.length > 0 && path.length > 0 && path[0] != "/")
292
314
path = "/" + path;
295
317
if (("query_string" in obj) && obj.query_string != null)
296
query_string = obj.query_string.toString();
318
query_string = encodeURI(obj.query_string.toString());
297
319
else if (("args" in obj) && obj.args != null)
298
320
query_string = make_query_string(obj.args);
300
if (query_string != null)
322
if (query_string != "" && query_string != null)
301
323
url += "?" + query_string;
303
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;
306
344
/** Given an argument map, as output in the args parameter of the return of
431
469
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
472
/** Shorthand for make_path(path_join(app, ...))
473
* Creates an absolute path for a given path within a given app.
475
function app_path(app /*,...*/)
477
return make_path(path_join.apply(null, arguments));
480
/** Given a path, gets the "basename" (the last path segment).
482
function path_basename(path)
484
segments = path.split("/");
485
if (segments[segments.length-1].length == 0)
486
return segments[segments.length-2];
488
return segments[segments.length-1];
491
/** Given a string str, determines whether it ends with substr */
492
function endswith(str, substring)
494
if (str.length < substring.length) return false;
495
return str.substr(str.length - substring.length) == substring;
498
/** Equivalent to Python's repr.
499
* Gets the JavaScript string representation.
500
* Actually just calls JSON.stringify.
504
return JSON.stringify(str);
507
/** Removes all occurences of a value from an array.
509
Array.prototype.removeall = function(val)
514
for (i=0; i<arr.length; i++)
517
if (arr[i] != val) j++;
522
/** Shallow-clones an object */
523
function shallow_clone_object(obj)
531
/** Returns a new XMLHttpRequest object, in a somewhat browser-agnostic
534
function new_xmlhttprequest()
539
return new XMLHttpRequest();
543
/* Internet Explorer */
546
return new ActiveXObject("Msxml2.XMLHTTP");
552
return new ActiveXObject("Microsoft.XMLHTTP");
556
throw("Your browser does not support AJAX. "
557
+ "IVLE requires a modern browser.");
563
/** Makes an asynchronous XMLHttpRequest call to the server.
564
* Sends the XMLHttpRequest object containing the completed response to a
565
* specified callback function.
567
* \param callback A callback function. Will be called when the response is
568
* complete. Passed 1 parameter, an XMLHttpRequest object containing the
569
* completed response.
438
570
* \param app IVLE app to call (such as "fileservice").
439
571
* \param path URL path to make the request to, within the application.
440
572
* \param args Argument object, as described in parse_url and friends.
442
574
* \param content_type String, optional. Only applies if method is "POST".
443
575
* May be "application/x-www-form-urlencoded" or "multipart/form-data".
444
576
* 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)
578
function ajax_call(callback, app, path, args, method, content_type)
449
580
if (content_type != "multipart/form-data")
450
581
content_type = "application/x-www-form-urlencoded";
451
path = make_path(path_join(app, path));
582
path = app_path(app, path);
453
584
/* A random string, for multipart/form-data
454
585
* (This is not checked against anywhere else, it is solely defined and
455
586
* used within this function) */
456
587
var boundary = "48234n334nu7n4n2ynonjn234t683jyh80j";
457
var xhr = new XMLHttpRequest();
588
var xhr = new_xmlhttprequest();
589
xhr.onreadystatechange = function()
591
if (xhr.readyState == 4)
458
596
if (method == "GET")
460
598
/* GET sends the args in the URL */
461
599
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);
600
/* open's 3rd argument = true -> asynchronous */
601
xhr.open(method, url, true);
469
606
/* POST sends the args in application/x-www-form-urlencoded */
470
607
url = encodeURI(path);
471
xhr.open(method, url, false);
608
xhr.open(method, url, true);
473
610
if (content_type == "multipart/form-data")