diff options
| author | hallgren <hallgren@chalmers.se> | 2011-10-10 19:49:17 +0000 |
|---|---|---|
| committer | hallgren <hallgren@chalmers.se> | 2011-10-10 19:49:17 +0000 |
| commit | ef079ff93964f8a7b0048352f834f36377e1900f (patch) | |
| tree | 005a9dbc1a3978c320619092191ccfd24ea36424 | |
| parent | b13884469559a71c01857ad852df8b210d5c7172 (diff) | |
gfse: make it workw ith "gf -server" cloud services
| -rw-r--r-- | src/editor/simple/cloud.js | 6 | ||||
| -rw-r--r-- | src/editor/simple/cloud2.js | 105 | ||||
| -rw-r--r-- | src/editor/simple/editor.js | 2 | ||||
| -rw-r--r-- | src/editor/simple/example_based.js | 3 |
4 files changed, 104 insertions, 12 deletions
diff --git a/src/editor/simple/cloud.js b/src/editor/simple/cloud.js index 44b7d8157..f19b6ee37 100644 --- a/src/editor/simple/cloud.js +++ b/src/editor/simple/cloud.js @@ -123,8 +123,10 @@ function download_json() { get_list(download_files); } -function link_directories(olddir,newdir,cont) { - ajax_http_get("upload.cgi?rmdir="+olddir+"&newdir="+newdir,cont) +function link_directories(newdir,cont) { + with_dir(function(olddir) { + ajax_http_get("upload.cgi?rmdir="+olddir+"&newdir="+newdir,cont) + }) } /* -------------------------------------------------------------------------- */ diff --git a/src/editor/simple/cloud2.js b/src/editor/simple/cloud2.js index c57922438..38210a6f6 100644 --- a/src/editor/simple/cloud2.js +++ b/src/editor/simple/cloud2.js @@ -10,14 +10,19 @@ function with_dir(cont) { } function remove_cloud_grammar(g) { - alert("remove_cloud_grammar(g) not implemented yet!!!") + var dir=local.get("dir") + if(dir && g.unique_name) { + var path=g.unique_name+".json" + gfcloud("rm",{file:path},debug); + } } // Upload the grammar to the server and check it for errors function upload(g) { function upload2(dir) { - var form=node("form",{method:"post",action:"/upload"}, - [hidden("dir",dir),hidden(g.basename,show_abstract(g))]) + var form=node("form",{method:"post",action:"/cloud"}, + [hidden("dir",dir),hidden("command","upload"), + hidden(g.basename,show_abstract(g))]) var files = [g.basename+".gf"] for(var i in g.concretes) { var cname=g.basename+g.concretes[i].langcode; @@ -39,19 +44,105 @@ function upload(g) { // Upload the grammar to store it in the cloud function upload_json(cont) { - alert("upload_json() not implemented yet!!!") + function upload3(resptext,status) { + local.put("json_uploaded",Date.now()); + //debug("Upload complete") + if(cont) cont(); + else { + var sharing=element("sharing"); + if(sharing) sharing.innerHTML=resptext; + } + } + function upload2(dir) { + var prefix=dir.substr(10)+"-" // skip "/tmp/gfse." + //debug("New form data"); + //var form=new FormData(); // !!! Doesn't work on Android 2.2! + var form={dir:dir}; + //debug("Preparing form data"); + for(var i=0;i<local.count;i++) { + var g=local.get(i,null); + if(g) { + if(!g.unique_name) { + g.unique_name=prefix+i; + save_grammar(g) + } + //form.append(g.unique_name+".json",JSON.stringify(g)); + form[encodeURIComponent(g.unique_name+".json")]=JSON.stringify(g) + } + } + //debug("Upload to "+prefix); + ajax_http_post("/cloud","command=upload"+encodeArgs(form),upload3,cont) + } + + with_dir(upload2); } function download_json() { - alert("download_json() not implemented yet!!!") + var dir=local.get("dir"); + var index=grammar_index(); + var downloading=0; + + function get_list(ok,err) { gfcloud("ls",{},ok,err) } + + function get_file(file,ok,err) { + downloading++; + gfcloud("download",{file:encodeURIComponent(file)},ok,err); + } + + function file_failed(errormsg,status) { + debug(errormsg) + downloading--; + } + function file_downloaded(grammar) { + downloading--; + var newg=JSON.parse(grammar); + debug("Downloaded "+newg.unique_name) + var i=index[newg.unique_name]; + if(i!=undefined) merge_grammar(i,newg) + else { + debug("New") + newg.index=null; + save_grammar(newg); + } + if(downloading==0) done() + } + + function done() { + setTimeout(function(){location.href="."},2000); + } + + function download_files(ls) { + local.put("current",0); + if(ls) { + //debug("Downloading "+ls); + var files=ls.split(" "); + cleanup_deleted(files); + for(var i in files) get_file(files[i],file_downloaded,file_failed); + } + else { + debug("No grammars in the cloud") + done() + } + } + + get_list(download_files); } -function link_directories(olddir,newdir,cont) { - alert("link_directories(...) not implemented yet!!!") +function link_directories(newdir,cont) { + gfcloud("link_directories",{newdir:newdir},cont) } /* -------------------------------------------------------------------------- */ +// Request GF cloud service +function gfcloud(cmd,args,cont,err) { + with_dir(function(dir) { + var enc=encodeURIComponent; + var url="/cloud?dir="+enc(dir)+"&command="+enc(cmd)+encodeArgs(args) + ajax_http_get(url,cont,err) + }) +} + // Send a command to the GF shell function gfshell(cmd,cont) { with_dir(function(dir) { diff --git a/src/editor/simple/editor.js b/src/editor/simple/editor.js index 8560f561f..859f33a04 100644 --- a/src/editor/simple/editor.js +++ b/src/editor/simple/editor.js @@ -1070,7 +1070,7 @@ function download_from_cloud() { function download2(olddir) { //debug("Starting grammar sharing in the cloud") - if(newdir!=olddir) link_directories(olddir,newdir,download3) + if(newdir!=olddir) link_directories(newdir,download3) else download4() } function download3() { diff --git a/src/editor/simple/example_based.js b/src/editor/simple/example_based.js index 1fb13740a..5922b60ea 100644 --- a/src/editor/simple/example_based.js +++ b/src/editor/simple/example_based.js @@ -32,8 +32,7 @@ function exb_state(g,ci) { function exb_call(g,ci,command,args,cont) { var url=window.exb_url || "exb/exb.fcgi"; - var q="" - for(var arg in args) q+="&"+arg+"="+encodeURIComponent(args[arg]); + var q=encodeArgs(args); var cmd="?command="+command+"&state="+encodeURIComponent(exb_state(g,ci))+q; http_get_json(url+cmd,cont) } |
