forked from GitHub/gf-core
pgf_online.js: error callbacks
To enable customized error handling, the methods in the pgf_online objects and the AJAX server call functions in support.js accept an error callback function as an extra argument, in addition to the callback (continuation) for normal results.
This commit is contained in:
@@ -5,6 +5,7 @@ function pgf_online(options) {
|
|||||||
var server = {
|
var server = {
|
||||||
// State variables (private):
|
// State variables (private):
|
||||||
grammars_url: "/grammars/",
|
grammars_url: "/grammars/",
|
||||||
|
other_grammars_urls: [],
|
||||||
grammar_list: null,
|
grammar_list: null,
|
||||||
current_grammar_url: null,
|
current_grammar_url: null,
|
||||||
|
|
||||||
@@ -13,37 +14,62 @@ function pgf_online(options) {
|
|||||||
this.current_grammar_url=this.grammars_url+grammar_url;
|
this.current_grammar_url=this.grammars_url+grammar_url;
|
||||||
if(cont) cont();
|
if(cont) cont();
|
||||||
},
|
},
|
||||||
get_grammarlist: function(cont) {
|
add_grammars_url: function(grammars_url,cont) {
|
||||||
http_get_json(this.grammars_url+"grammars.cgi",cont);
|
this.other_grammars_urls.push(grammars_url);
|
||||||
|
if(cont) cont();
|
||||||
},
|
},
|
||||||
pgf_call: function(cmd,args,cont) {
|
switch_to_other_grammar: function(grammar_url,cont) {
|
||||||
|
this.current_grammar_url=grammar_url;
|
||||||
|
if(cont) cont();
|
||||||
|
},
|
||||||
|
get_grammarlist: function(cont,err) {
|
||||||
|
if(this.grammar_list) cont(this.grammar_list)
|
||||||
|
else http_get_json(this.grammars_url+"grammars.cgi",cont,err);
|
||||||
|
},
|
||||||
|
get_grammarlists: function(cont,err) { // May call cont several times!
|
||||||
|
function pair(dir) {
|
||||||
|
return function(grammar_list){cont(dir,grammar_list)}
|
||||||
|
}
|
||||||
|
function ignore_error(err) { console.log(err) }
|
||||||
|
this.get_grammarlist(pair(this.grammars_url),err)
|
||||||
|
var ds=this.other_grammars_urls;
|
||||||
|
for(var i in ds)
|
||||||
|
http_get_json(ds[i]+"grammars.cgi",pair(ds[i]),ignore_error);
|
||||||
|
},
|
||||||
|
pgf_call: function(cmd,args,cont,err) {
|
||||||
var url=this.current_grammar_url+"?command="+cmd+encodeArgs(args)
|
var url=this.current_grammar_url+"?command="+cmd+encodeArgs(args)
|
||||||
http_get_json(url,cont);
|
http_get_json(url,cont,err);
|
||||||
},
|
},
|
||||||
|
|
||||||
get_languages: function(cont) { this.pgf_call("grammar",{},cont); },
|
get_languages: function(cont,err) {
|
||||||
grammar_info: function(cont) { this.pgf_call("grammar",{},cont); },
|
this.pgf_call("grammar",{},cont,err);
|
||||||
|
},
|
||||||
|
grammar_info: function(cont,err) {
|
||||||
|
this.pgf_call("grammar",{},cont,err);
|
||||||
|
},
|
||||||
|
|
||||||
get_random: function(args,cont) { // cat, limit
|
get_random: function(args,cont,err) { // cat, limit
|
||||||
args.random=Math.random(); // side effect!!
|
args.random=Math.random(); // side effect!!
|
||||||
this.pgf_call("random",args,cont);
|
this.pgf_call("random",args,cont,err);
|
||||||
},
|
},
|
||||||
linearize: function(args,cont) { // tree, to
|
linearize: function(args,cont,err) { // tree, to
|
||||||
this.pgf_call("linearize",args,cont);
|
this.pgf_call("linearize",args,cont,err);
|
||||||
},
|
},
|
||||||
complete: function(args,cont) { // from, input, cat, limit
|
complete: function(args,cont,err) { // from, input, cat, limit
|
||||||
this.pgf_call("complete",args,cont);
|
this.pgf_call("complete",args,cont,err);
|
||||||
},
|
},
|
||||||
parse: function(args,cont) { // from, input, cat
|
parse: function(args,cont,err) { // from, input, cat
|
||||||
this.pgf_call("parse",args,cont);
|
this.pgf_call("parse",args,cont,err);
|
||||||
},
|
},
|
||||||
translate: function(args,cont) { // from, input, cat, to
|
translate: function(args,cont,err) { // from, input, cat, to
|
||||||
this.pgf_call("translate",args,cont);
|
this.pgf_call("translate",args,cont,err);
|
||||||
},
|
},
|
||||||
translategroup: function(args,cont) { // from, input, cat, to
|
translategroup: function(args,cont,err) { // from, input, cat, to
|
||||||
this.pgf_call("translategroup",args,cont);
|
this.pgf_call("translategroup",args,cont,err);
|
||||||
|
},
|
||||||
|
browse: function(id,cont,err) {
|
||||||
|
this.pgf_call("browse",{id:id,format:"json"},cont,err);
|
||||||
}
|
}
|
||||||
|
|
||||||
};
|
};
|
||||||
for(var o in options) server[o]=options[o];
|
for(var o in options) server[o]=options[o];
|
||||||
if(server.grammar_list && server.grammar_list.length>0)
|
if(server.grammar_list && server.grammar_list.length>0)
|
||||||
|
|||||||
@@ -38,7 +38,7 @@ var json = {next:0};
|
|||||||
// Like jsonp, but instead of passing the name of the callback function, you
|
// Like jsonp, but instead of passing the name of the callback function, you
|
||||||
// pass the callback function directly, making it possible to use anonymous
|
// pass the callback function directly, making it possible to use anonymous
|
||||||
// functions.
|
// functions.
|
||||||
function jsonpf(url,callback)
|
function jsonpf(url,callback,errorcallback)
|
||||||
{
|
{
|
||||||
var name="callback"+(json.next++);
|
var name="callback"+(json.next++);
|
||||||
json[name]=function(x) { delete json[name]; callback(x); }
|
json[name]=function(x) { delete json[name]; callback(x); }
|
||||||
@@ -71,10 +71,12 @@ function ajax_http(method,url,body,callback,errorcallback) {
|
|||||||
else alert(errortext)
|
else alert(errortext)
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
var statechange=function() {
|
function statechange() {
|
||||||
if (http.readyState==4 || http.readyState=="complete") {
|
if (http.readyState==4 || http.readyState=="complete") {
|
||||||
if(http.status<300) callback(http.responseText,http.status);
|
if(http.status<300) callback(http.responseText,http.status);
|
||||||
else if(errorcallback) errorcallback(http.responseText,http.status);
|
else if(errorcallback)
|
||||||
|
errorcallback(http.responseText,http.status,
|
||||||
|
http.getResponseHeader("Content-Type"));
|
||||||
else alert("Request for "+url+" failed: "
|
else alert("Request for "+url+" failed: "
|
||||||
+http.status+" "+http.statusText);
|
+http.status+" "+http.statusText);
|
||||||
}
|
}
|
||||||
@@ -96,8 +98,8 @@ function ajax_http_post(url,formdata,callback,errorcallback) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// JSON via AJAX
|
// JSON via AJAX
|
||||||
function ajax_http_get_json(url,cont) {
|
function ajax_http_get_json(url,cont,errorcallback) {
|
||||||
ajax_http_get(url,function(txt) { cont(eval("("+txt+")")); });
|
ajax_http_get(url,function(txt){cont(eval("("+txt+")"));}, errorcallback);
|
||||||
}
|
}
|
||||||
|
|
||||||
function sameOrigin(url) {
|
function sameOrigin(url) {
|
||||||
@@ -107,9 +109,9 @@ function sameOrigin(url) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// Use AJAX when possible, fallback to JSONP
|
// Use AJAX when possible, fallback to JSONP
|
||||||
function http_get_json(url,cont) {
|
function http_get_json(url,cont,errorcallback) {
|
||||||
if(sameOrigin(url)) ajax_http_get_json(url,cont);
|
if(sameOrigin(url)) ajax_http_get_json(url,cont,errorcallback);
|
||||||
else jsonpf(url,cont);
|
else jsonpf(url,cont,errorcallback);
|
||||||
}
|
}
|
||||||
|
|
||||||
/* --- URL construction ----------------------------------------------------- */
|
/* --- URL construction ----------------------------------------------------- */
|
||||||
|
|||||||
Reference in New Issue
Block a user