summaryrefslogtreecommitdiff
path: root/src/editor/simple/cloud.js
blob: f19b6ee375ea68ab728e817b24a9261be4cfc2e1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148

function with_dir(cont) {
    var dir=local.get("dir","");
    if(dir) cont(dir);
    else ajax_http_get("upload.cgi?dir",
		       function(dir) {
			   local.put("dir",dir);
			   cont(dir);
		       });
}

function remove_cloud_grammar(g) {
    var dir=local.get("dir")
    if(dir && g.unique_name) {
	var path=dir+"/"+g.unique_name+".json"
	ajax_http_get("upload.cgi?rm="+encodeURIComponent(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.cgi"+dir},
		      [hidden(g.basename+".gf",show_abstract(g))])
	for(var i in g.concretes)
	    form.appendChild(hidden(g.basename+g.concretes[i].langcode+".gf",
				    show_concrete(g.basename)(g.concretes[i])));
	editor.appendChild(form);
	form.submit();
	form.parentNode.removeChild(form);
    }

    with_dir(upload2);
}

// Upload the grammar to store it in the cloud
function upload_json(cont) {
    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="",sep="";
	//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+=sep+encodeURIComponent(g.unique_name+".json")+"="+
		    encodeURIComponent(JSON.stringify(g))
		sep="&"
	    }
	}
	//debug("Upload to "+prefix);
	ajax_http_post("upload.cgi"+dir,form,upload3,cont)
    }

    with_dir(upload2);
}

function download_json() {
    var dir=local.get("dir");
    var index=grammar_index();
    var downloading=0;

    function get_list(ok,err) {
	ajax_http_get("upload.cgi?ls="+dir,ok,err);
    }

    function get_file(file,ok,err) {
	downloading++;
	ajax_http_get("upload.cgi?download="+encodeURIComponent(dir+"/"+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(newdir,cont) {
    with_dir(function(olddir) {
	ajax_http_get("upload.cgi?rmdir="+olddir+"&newdir="+newdir,cont)
    })
}

/* -------------------------------------------------------------------------- */

// Send a command to the GF shell
function gfshell(cmd,cont) {
    alert("gfshell(...) not implmemented!!!")
}

// Check the syntax of an expression
function check_exp(s,cont) {
    function check(gf_message) {
	//debug("cc "+s+" = "+gf_message);
	cont(/parse error/.test(gf_message) ? "parse error" : null);
    }
    if(navigator.onLine)
	ajax_http_get("upload.cgi?cc="+encodeURIComponent(s),check)
    else cont(null)
}