From 68e2c0c0ebaad660103a6ccc2c792b49003bd556 Mon Sep 17 00:00:00 2001 From: hallgren Date: Tue, 10 Apr 2012 18:00:13 +0000 Subject: [PATCH] minibar: internal state type change The current intput is now represented as an array of words instead of as a string. (This is the kind of change is scary to do in a dynamically type language like JavaScript. In a statically typed language like Haskell you can do it with confidence, since you know the compiler can help you catch all mistakes...) --- src/www/minibar/minibar.js | 6 ++-- src/www/minibar/minibar_input.js | 37 ++++++++++++------------- src/www/minibar/minibar_translations.js | 7 ++--- 3 files changed, 24 insertions(+), 26 deletions(-) diff --git a/src/www/minibar/minibar.js b/src/www/minibar/minibar.js index 83a336434..466886865 100644 --- a/src/www/minibar/minibar.js +++ b/src/www/minibar/minibar.js @@ -164,8 +164,8 @@ Minibar.prototype.append_extra_buttons=function(extra,options) { Minibar.prototype.try_google=function() { with(this) { var to=translations.target_lang(); - var s=input.current.input; - if(input.surface.typed) s+=input.surface.typed.value; + var s=gf_unlex(input.current.input); + if(input.surface.typed) s+=" "+input.surface.typed.value; var url="http://translate.google.com/?sl=" +langpart(input.current.from,grammar.name); if(to!="All") url+="&tl="+to; @@ -198,7 +198,7 @@ function prefill_feedback_form() { var form=document.forms.namedItem("feedback"); setField(form,"grammar",gn); setField(form,"from",langpart(state.current.from,gn)); - setField(form,"input",state.current.input); + setField(form,"input",gf_unlex(state.current.input)); setField(form,"to",to); if(to=="All") element("translation_box").innerHTML=" To suggest a better translation to a particular language, select that language in the To: menu before pressing the Feedback button." else setField(form,"translation",trans.single_translation.join(" / ")); diff --git a/src/www/minibar/minibar_input.js b/src/www/minibar/minibar_input.js index 88a410114..281df2011 100644 --- a/src/www/minibar/minibar_input.js +++ b/src/www/minibar/minibar_input.js @@ -39,7 +39,7 @@ function Input(server,translations,opts) { // Input object constructor } /* --- Input client state initialization --- */ - this.current={from: null, input: ""}; + this.current={from: null, input: [] }; this.from_menu.onchange=bind(this.change_language,this); this.startcat_menu.onchange=bind(this.change_startcat,this); @@ -79,7 +79,7 @@ Input.prototype.change_language=function () { this.local.put("from",this.current.from) var old_current=this.local.get("current"); var new_input= old_current && old_current.from==this.current.from - ? old_current.input : "" + ? old_current.input : [] this.clear_all1(); this.add_words(new_input) } @@ -87,7 +87,7 @@ Input.prototype.change_language=function () { Input.prototype.clear_all2=function() { with(this) { - current.input=""; + current.input=[]; local.put("current",current) clear(surface) if(surface.typed) surface.appendChild(surface.typed) @@ -112,9 +112,12 @@ Input.prototype.get_completions=function() { with(this) { //debug("get_completions "); words.innerHTML="..."; - var args={from:current.from,input:current.input,cat:startcat_menu.value} + var s=gf_unlex(current.input)+" "; + var args={from:current.from, input:s, cat:startcat_menu.value} server.complete(args,bind(show_completions,this)); if(options.word_replacements) server.parse(args,bind(get_tree1,this)); + // Making two server calls in parallel! The two continuations can + // be called in any order, make sure they are appropriately independent. } } @@ -233,7 +236,7 @@ Input.prototype.generate_random=function() { var t=this; function show_random(random) { t.clear_all1(); - t.add_words(random[0].text); + t.add_words(gf_lex(random[0].text)); } function lin_random(abs) { @@ -242,15 +245,14 @@ Input.prototype.generate_random=function() { t.server.get_random({cat:t.startcat_menu.value},lin_random); } -Input.prototype.add_words=function(s) { - this.add_words1(s); +Input.prototype.add_words=function(ws) { + this.add_words1(ws); this.get_completions(); } -Input.prototype.add_words1=function(s) { - var ws=s.split(" "); +Input.prototype.add_words1=function(ws) { for(var i=0;i1) { - var ws=gf_lex(current.input) - var w=ws.pop(); - if(w=="") { ws.pop(); ws.push(""); } - current.input=gf_unlex(ws); + else if(current.input.length>0) { + current.input.pop(); local.put("current",current) if(surface.typed) { surface.removeChild(surface.typed.previousSibling); @@ -325,7 +324,7 @@ Input.prototype.get_tree2=function(lin,tree) { var t=this; with(t) { if(lin.length==1 && lin[0].to==current.from - && lin[0].text+" "==current.input + && lin[0].text==gf_unlex(current.input) && (lin[0].brackets)) { var bs=lin[0].brackets; //var tree=show_abstract_tree(bs); @@ -421,7 +420,7 @@ Input.prototype.replace_word=function(brackets,parent,fun,tree) { function replace(lin_output) { if(lin_output.length==1 && lin_output[0].to==t.current.from) { t.clear_all1(); - t.add_words(lin_output[0].text) + t.add_words(gf_lex(lin_output[0].text)) } } function err(text,status,ctype) { diff --git a/src/www/minibar/minibar_translations.js b/src/www/minibar/minibar_translations.js index 1a429a48f..612b5d2ea 100644 --- a/src/www/minibar/minibar_translations.js +++ b/src/www/minibar/minibar_translations.js @@ -51,12 +51,11 @@ Translations.prototype.translateFrom=function(current,startcat) { Translations.prototype.get_translations=function() { with(this) { var c=current; + var args={from:c.from,input:gf_unlex(c.input),cat:startcat} if(options.show_grouped_translations) - server.translategroup({from:c.from,input:c.input,cat:startcat}, - bind(show_groupedtranslations,this)); + server.translategroup(args,bind(show_groupedtranslations,this)); else - server.translate({from:c.from,input:c.input,cat:startcat}, - bind(show_translations,this)); + server.translate(args,bind(show_translations,this)); } }