mirror of
https://github.com/GrammaticalFramework/gf-core.git
synced 2026-04-09 04:59:31 -06:00
minibar: use AJAX instead of JSONP when possible
Using AJAX instead of JSONP can give better caching in the browser, since the URL doesn't need to contain a (dynamically generated) callback function name. But because of the same origin policy in JavaScript, AJAX can only be used when the HTML file is served from the same server as the PGF. The new function http_get_json in support.js uses AJAX if this is the case, and falls back to JSONP otherwise.
This commit is contained in:
@@ -30,41 +30,37 @@ var server = {
|
||||
switch_grammar: function(grammar_name) {
|
||||
this.current_grammar_url=options.grammars_url+grammar_name;
|
||||
},
|
||||
|
||||
get_grammarlist: function(cont) {
|
||||
jsonpf(options.grammars_url+"grammars.cgi",cont);
|
||||
http_get_json(options.grammars_url+"grammars.cgi",cont);
|
||||
},
|
||||
get_languages: function(cont) {
|
||||
jsonpf(this.current_grammar_url,cont);
|
||||
http_get_json(this.current_grammar_url,cont);
|
||||
},
|
||||
pgf_call: function(cmd,args,cont) {
|
||||
var url=this.current_grammar_url+"?command="+cmd;
|
||||
for(var arg in args) url+="&"+arg+"="+encodeURIComponent(args[arg]);
|
||||
http_get_json(url,cont);
|
||||
},
|
||||
|
||||
get_random: function(cont) {
|
||||
jsonpf(this.current_grammar_url+"?command=random&random="+Math.random(),cont);
|
||||
//jsonpf(this.current_grammar_url+"?command=random&random="+Math.random(),cont);
|
||||
this.pgf_call("random",{random:Math.random()},cont);
|
||||
},
|
||||
linearize: function(tree,to,cont) {
|
||||
jsonpf(this.current_grammar_url+"?command=linearize&tree="
|
||||
+encodeURIComponent(tree)+"&to="+to,cont)
|
||||
},
|
||||
complete: function(from,input,cont) {
|
||||
jsonpf(this.current_grammar_url
|
||||
+"?command=complete"
|
||||
+"&from="+encodeURIComponent(from)
|
||||
+"&input="+encodeURIComponent(input),
|
||||
cont);
|
||||
|
||||
this.pgf_call("complete",{from:from,input:input},cont);
|
||||
},
|
||||
parse: function(from,input,cont) {
|
||||
this.pgf_call("parse",{from:from,input:input},cont);
|
||||
},
|
||||
translate: function(from,input,cont) {
|
||||
jsonpf(this.current_grammar_url
|
||||
+"?command=translate"
|
||||
+"&from="+encodeURIComponent(from)
|
||||
+"&input="+encodeURIComponent(input),
|
||||
cont)
|
||||
this.pgf_call("translate",{from:from,input:input},cont);
|
||||
},
|
||||
translategroup: function(from,input,cont) {
|
||||
jsonpf(this.current_grammar_url
|
||||
+"?command=translategroup"
|
||||
+"&from="+encodeURIComponent(from)
|
||||
+"&input="+encodeURIComponent(input),
|
||||
cont)
|
||||
this.pgf_call("translategroup",{from:from,input:input},cont);
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
@@ -37,6 +37,55 @@ function jsonpf(url,callback)
|
||||
jsonp(url,"json."+name);
|
||||
}
|
||||
|
||||
/* --- AJAX ----------------------------------------------------------------- */
|
||||
|
||||
function GetXmlHttpObject(handler)
|
||||
{
|
||||
var objXMLHttp=null
|
||||
if (window.XMLHttpRequest)
|
||||
{
|
||||
objXMLHttp=new XMLHttpRequest()
|
||||
}
|
||||
else if (window.ActiveXObject)
|
||||
{
|
||||
objXMLHttp=new ActiveXObject("Microsoft.XMLHTTP")
|
||||
}
|
||||
return objXMLHttp
|
||||
}
|
||||
|
||||
function ajax_http_get(url,callback) {
|
||||
var http=GetXmlHttpObject()
|
||||
if (http==null) {
|
||||
alert ("Browser does not support HTTP Request")
|
||||
return
|
||||
}
|
||||
var statechange=function() {
|
||||
if (http.readyState==4 || http.readyState=="complete")
|
||||
callback(http.responseText)
|
||||
}
|
||||
http.onreadystatechange=statechange
|
||||
http.open("GET",url,true)
|
||||
http.send(null)
|
||||
//dump("http get "+url+"\n")
|
||||
return http
|
||||
}
|
||||
|
||||
// JSON via AJAX
|
||||
function ajax_http_get_json(url,cont) {
|
||||
ajax_http_get(url,function(txt) { cont(eval("("+txt+")")); });
|
||||
}
|
||||
|
||||
function sameOrigin(url) {
|
||||
return hasPrefix(url,location.protocol+"//"+location.host+"/");
|
||||
}
|
||||
|
||||
// Use AJAX when possible, fallback to JSONP
|
||||
function http_get_json(url,cont) {
|
||||
if(sameOrigin(url)) ajax_http_get_json(url,cont);
|
||||
else jsonpf(url,cont);
|
||||
}
|
||||
|
||||
|
||||
/* --- HTML construction ---------------------------------------------------- */
|
||||
function text(s) { return document.createTextNode(s); }
|
||||
|
||||
|
||||
Reference in New Issue
Block a user