512
512
return make_path(path_join.apply(null, arguments));
515
/** Same as app_path but creates a properly-escaped site-relative URL.
517
function app_url(app /*,...*/)
519
return urlencode_path(app_path.apply(null, arguments));
522
515
/** Generates an absolute URL to a public application
524
function public_app_url(app /*,...*/)
517
function public_app_path(app /*,...*/)
526
return "http://" + public_host + app_url.apply(null, arguments);
519
return location.protocol + "//" + public_host
520
+ make_path(path_join.apply(null, arguments));
529
523
/** Given a path, gets the "basename" (the last path segment).
544
538
return str.substr(str.length - substring.length) == substring;
541
/** Equivalent to Python's repr.
542
* Gets the JavaScript string representation.
543
* Actually just calls JSON.stringify.
547
return JSON.stringify(str);
547
550
/** Removes all occurences of a value from an array.
549
552
Array.prototype.removeall = function(val)
617
620
return str.join('');
620
/** Makes an XMLHttpRequest call to the server.
623
/** Makes an asynchronous XMLHttpRequest call to the server.
621
624
* Sends the XMLHttpRequest object containing the completed response to a
622
625
* specified callback function.
624
627
* \param callback A callback function. Will be called when the response is
625
628
* complete. Passed 1 parameter, an XMLHttpRequest object containing the
626
* completed response. If callback is null this is a syncronous request
627
* otherwise this is an asynchronous request.
629
* completed response.
628
630
* \param app IVLE app to call (such as "fileservice").
629
631
* \param path URL path to make the request to, within the application.
630
632
* \param args Argument object, as described in parse_url and friends.
631
* \param method String; "GET", "POST", "PUT", or "PATCH"
632
* \param content_type String, optional.
633
* \param method String; "GET" or "POST"
634
* \param content_type String, optional. Only applies if method is "POST".
635
* May be "application/x-www-form-urlencoded" or "multipart/form-data".
633
636
* Defaults to "application/x-www-form-urlencoded".
635
638
function ajax_call(callback, app, path, args, method, content_type)
640
if (content_type != "multipart/form-data")
638
641
content_type = "application/x-www-form-urlencoded";
639
642
path = app_path(app, path);
643
646
* used within this function) */
644
647
var boundary = random_string(20);
645
648
var xhr = new_xmlhttprequest();
646
var asyncronous = callback != null;
649
xhr.onreadystatechange = function()
649
xhr.onreadystatechange = function()
651
if (xhr.readyState == 4)
651
if (xhr.readyState == 4)
657
656
if (method == "GET")
659
658
/* GET sends the args in the URL */
660
659
url = build_url({"path": path, "args": args});
661
660
/* open's 3rd argument = true -> asynchronous */
662
xhr.open(method, url, asyncronous);
661
xhr.open(method, url, true);
667
/* POST & PUT & PATCH sends the args in the request body */
666
/* POST sends the args in application/x-www-form-urlencoded */
668
667
url = encodeURI(path);
669
xhr.open(method, url, asyncronous);
668
xhr.open(method, url, true);
671
670
if (content_type == "multipart/form-data")
674
673
"multipart/form-data; boundary=" + boundary);
675
674
message = make_multipart_formdata(args, boundary);
677
else if (content_type == "application/x-www-form-urlencoded")
679
678
xhr.setRequestHeader("Content-Type", content_type);
680
679
message = make_query_string(args);
682
else if (content_type == "application/json")
684
xhr.setRequestHeader("Content-Type", content_type);
685
message = JSON.stringify(args);
689
xhr.setRequestHeader("Content-Type", content_type);
692
681
xhr.send(message);
694
/* Only return the XHR for syncronous requests */
701
/** Attempts to JSON decodes a response object
702
* If a non-200 response or the JSON decode fails then returns null
704
function decode_response(response)
706
if (response.status == 200)
710
var responseText = response.responseText;
711
return JSON.parse(responseText);