forked from GitHub/gf-core
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...)
This commit is contained in:
@@ -164,8 +164,8 @@ Minibar.prototype.append_extra_buttons=function(extra,options) {
|
|||||||
Minibar.prototype.try_google=function() {
|
Minibar.prototype.try_google=function() {
|
||||||
with(this) {
|
with(this) {
|
||||||
var to=translations.target_lang();
|
var to=translations.target_lang();
|
||||||
var s=input.current.input;
|
var s=gf_unlex(input.current.input);
|
||||||
if(input.surface.typed) s+=input.surface.typed.value;
|
if(input.surface.typed) s+=" "+input.surface.typed.value;
|
||||||
var url="http://translate.google.com/?sl="
|
var url="http://translate.google.com/?sl="
|
||||||
+langpart(input.current.from,grammar.name);
|
+langpart(input.current.from,grammar.name);
|
||||||
if(to!="All") url+="&tl="+to;
|
if(to!="All") url+="&tl="+to;
|
||||||
@@ -198,7 +198,7 @@ function prefill_feedback_form() {
|
|||||||
var form=document.forms.namedItem("feedback");
|
var form=document.forms.namedItem("feedback");
|
||||||
setField(form,"grammar",gn);
|
setField(form,"grammar",gn);
|
||||||
setField(form,"from",langpart(state.current.from,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);
|
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."
|
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(" / "));
|
else setField(form,"translation",trans.single_translation.join(" / "));
|
||||||
|
|||||||
@@ -39,7 +39,7 @@ function Input(server,translations,opts) { // Input object constructor
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* --- Input client state initialization --- */
|
/* --- Input client state initialization --- */
|
||||||
this.current={from: null, input: ""};
|
this.current={from: null, input: [] };
|
||||||
|
|
||||||
this.from_menu.onchange=bind(this.change_language,this);
|
this.from_menu.onchange=bind(this.change_language,this);
|
||||||
this.startcat_menu.onchange=bind(this.change_startcat,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)
|
this.local.put("from",this.current.from)
|
||||||
var old_current=this.local.get("current");
|
var old_current=this.local.get("current");
|
||||||
var new_input= old_current && old_current.from==this.current.from
|
var new_input= old_current && old_current.from==this.current.from
|
||||||
? old_current.input : ""
|
? old_current.input : []
|
||||||
this.clear_all1();
|
this.clear_all1();
|
||||||
this.add_words(new_input)
|
this.add_words(new_input)
|
||||||
}
|
}
|
||||||
@@ -87,7 +87,7 @@ Input.prototype.change_language=function () {
|
|||||||
|
|
||||||
Input.prototype.clear_all2=function() {
|
Input.prototype.clear_all2=function() {
|
||||||
with(this) {
|
with(this) {
|
||||||
current.input="";
|
current.input=[];
|
||||||
local.put("current",current)
|
local.put("current",current)
|
||||||
clear(surface)
|
clear(surface)
|
||||||
if(surface.typed) surface.appendChild(surface.typed)
|
if(surface.typed) surface.appendChild(surface.typed)
|
||||||
@@ -112,9 +112,12 @@ Input.prototype.get_completions=function() {
|
|||||||
with(this) {
|
with(this) {
|
||||||
//debug("get_completions ");
|
//debug("get_completions ");
|
||||||
words.innerHTML="...";
|
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));
|
server.complete(args,bind(show_completions,this));
|
||||||
if(options.word_replacements) server.parse(args,bind(get_tree1,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;
|
var t=this;
|
||||||
function show_random(random) {
|
function show_random(random) {
|
||||||
t.clear_all1();
|
t.clear_all1();
|
||||||
t.add_words(random[0].text);
|
t.add_words(gf_lex(random[0].text));
|
||||||
}
|
}
|
||||||
|
|
||||||
function lin_random(abs) {
|
function lin_random(abs) {
|
||||||
@@ -242,15 +245,14 @@ Input.prototype.generate_random=function() {
|
|||||||
t.server.get_random({cat:t.startcat_menu.value},lin_random);
|
t.server.get_random({cat:t.startcat_menu.value},lin_random);
|
||||||
}
|
}
|
||||||
|
|
||||||
Input.prototype.add_words=function(s) {
|
Input.prototype.add_words=function(ws) {
|
||||||
this.add_words1(s);
|
this.add_words1(ws);
|
||||||
this.get_completions();
|
this.get_completions();
|
||||||
}
|
}
|
||||||
|
|
||||||
Input.prototype.add_words1=function(s) {
|
Input.prototype.add_words1=function(ws) {
|
||||||
var ws=s.split(" ");
|
|
||||||
for(var i=0;i<ws.length;i++)
|
for(var i=0;i<ws.length;i++)
|
||||||
if(ws[i]) this.add_word1(ws[i]+" ");
|
if(ws[i]) this.add_word1(ws[i]);
|
||||||
}
|
}
|
||||||
|
|
||||||
Input.prototype.word=function(s) {
|
Input.prototype.word=function(s) {
|
||||||
@@ -264,7 +266,7 @@ Input.prototype.word=function(s) {
|
|||||||
|
|
||||||
Input.prototype.add_word=function(s) {
|
Input.prototype.add_word=function(s) {
|
||||||
with(this) {
|
with(this) {
|
||||||
add_word1(s+" ");
|
add_word1(s);
|
||||||
if(surface.typed) {
|
if(surface.typed) {
|
||||||
var s2;
|
var s2;
|
||||||
if(hasPrefix(s2=surface.typed.value,s)) {
|
if(hasPrefix(s2=surface.typed.value,s)) {
|
||||||
@@ -280,7 +282,7 @@ Input.prototype.add_word=function(s) {
|
|||||||
|
|
||||||
Input.prototype.add_word1=function(s) {
|
Input.prototype.add_word1=function(s) {
|
||||||
with(this) {
|
with(this) {
|
||||||
current.input+=s;
|
current.input.push(s);
|
||||||
local.put("current",current)
|
local.put("current",current)
|
||||||
var w=span_class("word",text(s));
|
var w=span_class("word",text(s));
|
||||||
if(surface.typed) surface.insertBefore(w,surface.typed);
|
if(surface.typed) surface.insertBefore(w,surface.typed);
|
||||||
@@ -292,11 +294,8 @@ Input.prototype.delete_last=function() {
|
|||||||
with(this) {
|
with(this) {
|
||||||
if(surface.typed && surface.typed.value!="")
|
if(surface.typed && surface.typed.value!="")
|
||||||
surface.typed.value="";
|
surface.typed.value="";
|
||||||
else if(current.input.length>1) {
|
else if(current.input.length>0) {
|
||||||
var ws=gf_lex(current.input)
|
current.input.pop();
|
||||||
var w=ws.pop();
|
|
||||||
if(w=="") { ws.pop(); ws.push(""); }
|
|
||||||
current.input=gf_unlex(ws);
|
|
||||||
local.put("current",current)
|
local.put("current",current)
|
||||||
if(surface.typed) {
|
if(surface.typed) {
|
||||||
surface.removeChild(surface.typed.previousSibling);
|
surface.removeChild(surface.typed.previousSibling);
|
||||||
@@ -325,7 +324,7 @@ Input.prototype.get_tree2=function(lin,tree) {
|
|||||||
var t=this;
|
var t=this;
|
||||||
with(t) {
|
with(t) {
|
||||||
if(lin.length==1 && lin[0].to==current.from
|
if(lin.length==1 && lin[0].to==current.from
|
||||||
&& lin[0].text+" "==current.input
|
&& lin[0].text==gf_unlex(current.input)
|
||||||
&& (lin[0].brackets)) {
|
&& (lin[0].brackets)) {
|
||||||
var bs=lin[0].brackets;
|
var bs=lin[0].brackets;
|
||||||
//var tree=show_abstract_tree(bs);
|
//var tree=show_abstract_tree(bs);
|
||||||
@@ -421,7 +420,7 @@ Input.prototype.replace_word=function(brackets,parent,fun,tree) {
|
|||||||
function replace(lin_output) {
|
function replace(lin_output) {
|
||||||
if(lin_output.length==1 && lin_output[0].to==t.current.from) {
|
if(lin_output.length==1 && lin_output[0].to==t.current.from) {
|
||||||
t.clear_all1();
|
t.clear_all1();
|
||||||
t.add_words(lin_output[0].text)
|
t.add_words(gf_lex(lin_output[0].text))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
function err(text,status,ctype) {
|
function err(text,status,ctype) {
|
||||||
|
|||||||
@@ -51,12 +51,11 @@ Translations.prototype.translateFrom=function(current,startcat) {
|
|||||||
Translations.prototype.get_translations=function() {
|
Translations.prototype.get_translations=function() {
|
||||||
with(this) {
|
with(this) {
|
||||||
var c=current;
|
var c=current;
|
||||||
|
var args={from:c.from,input:gf_unlex(c.input),cat:startcat}
|
||||||
if(options.show_grouped_translations)
|
if(options.show_grouped_translations)
|
||||||
server.translategroup({from:c.from,input:c.input,cat:startcat},
|
server.translategroup(args,bind(show_groupedtranslations,this));
|
||||||
bind(show_groupedtranslations,this));
|
|
||||||
else
|
else
|
||||||
server.translate({from:c.from,input:c.input,cat:startcat},
|
server.translate(args,bind(show_translations,this));
|
||||||
bind(show_translations,this));
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user