1
0
forked from GitHub/gf-core

translator: documents can now be saved in the cloud

some other small fixes
This commit is contained in:
hallgren
2012-06-12 15:36:41 +00:00
parent 0089dcdb78
commit 70b338de20
5 changed files with 97 additions and 31 deletions

View File

@@ -165,7 +165,7 @@ handle state0 cache execute1
case cmd of case cmd of
"make" -> make dir qs "make" -> make dir qs
"upload" -> upload qs "upload" -> upload qs
"ls" -> jsonList "ls" -> jsonList (maybe ".json" id $ lookup "ext" qs)
"rm" -> look "file" rm qs "rm" -> look "file" rm qs
"download" -> look "file" download qs "download" -> look "file" download qs
"link_directories" -> look "newdir" (link_directories dir) qs "link_directories" -> look "newdir" (link_directories dir) qs
@@ -183,8 +183,8 @@ handle state0 cache execute1
do mapM_ (uncurry updateFile) files do mapM_ (uncurry updateFile) files
return (state,resp204) return (state,resp204)
jsonList = jsonList ext =
do jsons <- ls_ext "." ".json" do jsons <- ls_ext "." ext
return (state,json200 jsons) return (state,json200 jsons)
rm path _ | takeExtension path==".json" = rm path _ | takeExtension path==".json" =

View File

@@ -216,7 +216,7 @@ function parse_oper(s) {
} }
/* --- Print as plain text (normal GF source syntax) ------------------------- */ /* --- Print as plain text (normal GF source syntax) ------------------------ */
function show_type(t) { function show_type(t) {
var s=""; var s="";

View File

@@ -33,8 +33,8 @@ grammar to use from a menu of available grammars. Through menu options,
the user also sets the source and target language for the document. the user also sets the source and target language for the document.
<p> <p>
The tool handles a set of documents. Documents can be named, saved (locally), The tool handles a set of documents. Documents can be named, saved,
closed and reopened later. closed and reopened later. Documents can be saved locally or in the cloud.
<h2>TODO</h2> <h2>TODO</h2>
<ul> <ul>
@@ -43,7 +43,7 @@ closed and reopened later.
<li>GF's lexer/unlexer is used to allow for more natural looking text, but <li>GF's lexer/unlexer is used to allow for more natural looking text, but
the unlexer does the wrong thing if the first word of a sentence is supposed the unlexer does the wrong thing if the first word of a sentence is supposed
to be capitalized, e.g. "I am ready." and "Spanish wine is good." to be capitalized, e.g. "I am ready." and "Spanish wine is good."
<li>Cloud service. <li>Document sharing in the cloud.
<li>Interface to other translation services. <li>Interface to other translation services.
<li>Interface to the grammar editor for grammar extension. <li>Interface to the grammar editor for grammar extension.
<li>More browser compatibility testing (Chrome, Firefox &amp; Safari tested <li>More browser compatibility testing (Chrome, Firefox &amp; Safari tested
@@ -55,7 +55,7 @@ closed and reopened later.
<hr> <hr>
<div class=modtime><small> <div class=modtime><small>
<!-- hhmts start --> Last modified: Mon Jun 11 17:25:39 CEST 2012 <!-- hhmts end --> <!-- hhmts start --> Last modified: Tue Jun 12 17:32:46 CEST 2012 <!-- hhmts end -->
</small></div> </small></div>
<address> <address>
<a href="http://www.cse.chalmers.se/~hallgren/">TH</a> <a href="http://www.cse.chalmers.se/~hallgren/">TH</a>

View File

@@ -58,6 +58,7 @@
<dt><label><input name=method value=Manual type=radio onchange="translator.change(this)">Manual</label> <dt><label><input name=method value=Manual type=radio onchange="translator.change(this)">Manual</label>
</dl> </dl>
</table> </table>
<dt><label><input name=cloud type=checkbox onchange="translator.change(this)"> Save in the cloud</label>
</dl> </dl>
</table> </table>
</form> </form>
@@ -70,7 +71,7 @@
</div> </div>
<hr> <hr>
<div class=modtime><small>HMTL <div class=modtime><small>HMTL
<!-- hhmts start --> Last modified: Sat May 26 13:23:57 CEST 2012 <!-- hhmts end --> <!-- hhmts start --> Last modified: Tue Jun 12 16:08:01 CEST 2012 <!-- hhmts end -->
</small></div> </small></div>
<a href="about.html">About</a> <a href="about.html">About</a>

View File

@@ -39,6 +39,7 @@ Translator.prototype.redraw=function() {
update_radiobutton("source",o.from) update_radiobutton("source",o.from)
update_radiobutton("target",o.to) update_radiobutton("target",o.to)
update_radiobutton("view",o.view || "segmentbysegment") update_radiobutton("view",o.view || "segmentbysegment")
update_checkbox("cloud",o.cloud || false)
if(o.method!="Manual") { if(o.method!="Manual") {
function cont2(gr_info) { function cont2(gr_info) {
t.grammar_info=gr_info t.grammar_info=gr_info
@@ -148,11 +149,18 @@ Translator.prototype.change=function(el) {
t.redraw() t.redraw()
} }
} }
function update_checkbox(field) {
if(el.checked!=o[field]) {
o[field]=el.checked
t.redraw()
}
}
switch(el.name) { switch(el.name) {
case "method": update("method"); break; case "method": update("method"); break;
case "source": update("from"); break; case "source": update("from"); break;
case "target": update("to"); break; case "target": update("to"); break;
case "view": update("view"); break; case "view" : update("view"); break;
case "cloud" : update_checkbox("cloud"); break;
} }
} }
@@ -167,17 +175,32 @@ Translator.prototype.new=function(el) {
Translator.prototype.browse=function(el) { Translator.prototype.browse=function(el) {
hide_menu(el); hide_menu(el);
var t=this var t=this
function browse() { function ls(files,op) {
var files=t.local.ls("/")
var ul=empty_class("ul","files") var ul=empty_class("ul","files")
for(var i in files) { for(var i in files) {
var name=files[i] var name=files[i]
var link=a(jsurl("translator.open('"+name+"')"),[text(name)]) var link=a(jsurl(op+"('"+name+"')"),[text(name)])
ul.appendChild(li(link)) ul.appendChild(li(link))
} }
return ul
}
function browse() {
clear(t.view) clear(t.view)
t.view.appendChild(wrap("h2",text("Your translator documents"))) t.view.appendChild(wrap("h2",text("Your translator documents")))
t.view.appendChild(ul) var files=t.local.ls("/")
if(files.length>0) {
t.view.appendChild(wrap("h3",text("Local documents")))
t.view.appendChild(ls(files,"translator.open"))
}
function lscloud(result) {
var filenames=JSON.parse(result)
var files=map(strip_cloudext,filenames)
if(files.length>0) {
t.view.appendChild(wrap("h3",text("Documents in the cloud")))
t.view.appendChild(ls(files,"translator.open_from_cloud"))
}
}
if(navigator.onLine) gfcloud("ls",{ext:cloudext},lscloud)
t.current="/" t.current="/"
t.local.put("current","/") t.local.put("current","/")
} }
@@ -189,33 +212,52 @@ Translator.prototype.open=function(name) {
if(name) { if(name) {
var path="/"+name var path="/"+name
var document=t.local.get(path); var document=t.local.get(path);
if(document) { if(document) t.load(name,document)
t.current=name;
t.local.put("current",name)
t.document=document;
t.redraw();
}
else alert("No such document: "+name) else alert("No such document: "+name)
} }
} }
Translator.prototype.load=function(name,document) {
var t=this
t.current=name;
t.local.put("current",name)
t.document=document;
t.redraw();
}
Translator.prototype.open_from_cloud=function(name) {
var t=this
var filename=name+cloudext
function ok(result) {
var document=JSON.parse(result)
if(document) t.load(name,document)
}
gfcloud("download",{file:encodeURIComponent(filename)},ok);
}
Translator.prototype.save=function(el) { Translator.prototype.save=function(el) {
var t=this
hide_menu(el); hide_menu(el);
if(this.current!="/") { if(t.current!="/") {
if(this.current) this.local.put("/"+this.current,this.document) if(t.current) {
else this.saveAs() if(t.document.options.cloud)
save_in_cloud(t.current+cloudext,t.document)
else
t.local.put("/"+t.current,t.document)
}
else t.saveAs()
} }
} }
Translator.prototype.saveAs=function(el) { Translator.prototype.saveAs=function(el) {
var t=this
hide_menu(el); hide_menu(el);
if(this.current!="/") { if(t.current!="/") {
var name=prompt("File name?") var name=prompt("File name?")
if(name) { if(name) {
this.current=this.document.name=name; t.current=t.document.name=name;
this.local.put("current",name) t.local.put("current",name)
this.save(); t.save();
this.redraw(); t.redraw();
} }
} }
} }
@@ -264,7 +306,7 @@ Translator.prototype.add_segment=function(el) {
e.onsubmit=done e.onsubmit=done
inp.focus(); inp.focus();
} }
setTimeout(imp,100) if(t.current!="/") setTimeout(imp,100)
} }
Translator.prototype.import=function(el) { Translator.prototype.import=function(el) {
@@ -306,7 +348,7 @@ Translator.prototype.import=function(el) {
e.onsubmit=done e.onsubmit=done
inp.focus(); inp.focus();
} }
setTimeout(imp,100) if(t.current!="/") setTimeout(imp,100)
} }
Translator.prototype.remove=function(el) { Translator.prototype.remove=function(el) {
@@ -397,11 +439,13 @@ function hide_menu(el) {
/* --- Documents ------------------------------------------------------------ */ /* --- Documents ------------------------------------------------------------ */
/* /*
type Document = { name:String, options: Options, segments:[Segment] } type Document = { name:String, options:DocOptions, segments:[Segment] }
type Segment = { source:String, target:String, options:Options } type Segment = { source:String, target:String, options:Options }
type DocOptions = Options & { view:View }
type Options = {from: Lang, to: Lang, method:Method} type Options = {from: Lang, to: Lang, method:Method}
type Lang = String // Eng, Swe, Ita, etc type Lang = String // Eng, Swe, Ita, etc
type Method = "Manual" | GrammarName type Method = "Manual" | GrammarName
type View = "segmentbysegment" | "paralleltexts"
type GrammarName = String // e.g. "Foods.pgf" type GrammarName = String // e.g. "Foods.pgf"
*/ */
@@ -595,6 +639,23 @@ function split_punct(text,punct) {
return segs return segs
} }
/* --- Cloud Support -------------------------------------------------------- */
var cloudext=".gfstdoc"
function strip_cloudext(s) { return s.substr(0,s.length-cloudext.length) }
function save_in_cloud(filename,document) {
function done() { }
function save(dir) {
var form={dir:dir}
form[filename]=JSON.stringify(document);
ajax_http_post("/cloud","command=upload"+encodeArgs(form),done)
}
with_dir(save)
}
/* --- DOM Support ---------------------------------------------------------- */ /* --- DOM Support ---------------------------------------------------------- */
function a(url,linked) { return node("a",{href:url},linked); } function a(url,linked) { return node("a",{href:url},linked); }
@@ -615,6 +676,10 @@ function update_radiobutton(name,value) {
for(var i in bs) if(bs[i].value==value) bs[i].checked=true for(var i in bs) if(bs[i].value==value) bs[i].checked=true
} }
function update_checkbox(name,checked) {
document.forms.options[name].checked=checked
}
function submit(label) { function submit(label) {
return node("input",{type:"submit",value:label||"OK"}) return node("input",{type:"submit",value:label||"OK"})
} }