/*---输入对象---------------------------------------------------------*/函数Input(服务器、翻译、opts){//输入对象构造函数var t=这个t.server=服务器;t.translations=翻译;//选项的默认值:t.选项={删除按钮文本:“\9003;”,默认源语言:空,startcat_menu:为真,random_button:true,word_replacements:错误}//应用提供的选项if(opts)for(opts中的var o)t.options[o]=opts[o];//用户界面元素t.main=空(“div”);t.menus=空(“span”);t.buttons=空(“span”);t.surface=div_id(“表面”);t.words=div_id(“单词”);t.from_menu=空(“选择”);t.startcat_menu=空(“选择”)带(t){appendChildren(main,[surface,words]);if(选项.startcat菜单)appendChildren(菜单,[text(“Startcat:”),Startcat_menu])appendChildren(菜单,[text(“From:”),From_menu])appendChildren(按钮,[标题(“删除最后一个单词”,按钮(options.Delete_button_text,bind(Delete_last,t),“H”),按钮(“Clear”,bind(Clear_all,t),“L”);if(选项.random_button)buttons.appendChild(按钮(“Random”,绑定(generate_Random,t),“R”);var o=选项;if(o.initial_grammar&&o.inital&&o.initial.from&&o.enitial.input)t.set_input_for(o.initial_grammar,o.initial)}/*---输入客户端状态初始化---*/t.current={from:空,输入:[]};t.from_menu.onchange=绑定(t.change_language,t);t.startcat_menu.onchange=绑定(t.change_startcat,t);}Input.prototype.change_grammar=函数(语法){var t=这个;grammar.browse={}//用于缓存browse命令的输出t.grammar=语法;t.local=mi_local(t.server.current_grammar_url)更新语言菜单(t.from_menu,语法);设置初始语言(t.options、t.from_menu、grammar、t.local.get(“from”));t.update_startcat_menu(语法)t.更改语言();}Input.prototype.update_startcat_menu=函数(语法){var菜单=this.startcat菜单;menu.innerHTML=“”;var cats=语法类别;for(var cat in cats)menu.appendChild(选项(cats[cat],cats[cat]))var startcat=this.local.get(“startcat”)||grammar.startcat;if(startcat)menu.value=startcat;其他{insertFirst(菜单,选项(“默认”,“”);menu.value=“”;}}Input.prototype.change_startcat=函数(){this.local.put(“startcat”,this.startcatmenu.value)this.clear_all();}Input.prototype.change_language=函数(){this.current.from=this.from_menu.value;this.local.put(“from”,this.current.from)var old_current=this.local.get(“当前”);var new_input=old_current&&old_curren.from==this.current.from? 旧当前输入:[]this.clear_all1();this.addwords(新输入)}Input.prototype.set_Input_for=函数(grammar_url,initial){var-local=mi_local(语法url)local.put(“from”,initial.from)local.put(“当前”,{from:initial.from,input:initial.input})if(initial.startcat)local.put(“startcat”,initial.sartcat)}Input.prototype.clear_all2=函数(){用(这个){电流输入=[];local.put(“当前”,当前)删除表面文字()}}Input.prototype.clear_all1=函数(){用(这个){删除类型输入();清除所有2();translations.clear();}}Input.prototype.clear_all=函数(){this.clear_all1();this.get_completions();}Input.prototype.get_completions=函数(){用(这个){//调试(“get_completions”);words.innerHTML=“…”;var s=gf_unlex(电流输入)+“”;var args={from:current.from,input:s,cat:startcat_menu.value}server.complete(参数,绑定(show_completions,this));if(options.wordreplacements)server.parse(args,bind(gettree1,this));//并行进行两次服务器调用!这两个延续可以//以任何顺序调用,确保它们适当独立。}}Input.prototype.show_completions=函数(complete_output){var self=此;功能开关输入(lin){带(自我){local.put(“当前”,{from:lin.to,input:lin.text.split(“”)})from_menu.value=lin.to;更改语言()}}带(自我){//调试(“show_completions”);var completions=complete_output[0].completions;var emptycnt=添加补全(补全)translations.translateFrom(当前,startcat_menu.value,switch_input);if(surface.typed&&emptycnt==完成长度){if(surface.typed.value==“”)remove_typed_input();}否则添加类型输入();}}Input.prototype.add_completions=函数(补全){用(这个){if(words.timeout)clearTimeout(words超时),words.temeout=null;words.innerHTML=“”;words.completions=补全;words.word=[];var t=surface.typed?surface.typed.value:“”;var emptycnt=0;对于(var i=0;i0) {var w=单词;words.appendChild(w);words.word[i]=w;}否则为空;}filter_completions(t,true);返回空值;}}Input.prototype.filter_completions=函数(t,dim){用(这个){if(words.timeout)clearTimeout(words超时),words.temeout=null;words.filtered=t;//if(dim)调试('filter“'+t+'”');var w=单词.word;words.count=0;var调暗=0;var前缀=“”;//最长公共前缀,用于完成对于(var i=0;i0)words.timeout=setTimeout(function(){filter_completions(t,false)},1000);}}Input.prototype.add_typed_Input=函数(){用(这个){if(!surface.typed){var inp=空(“输入”,“类型”,“文本”);inp.value=“”;inp.setAttribute(“accesskey”,“t”);inp.style.width=“10em”;inp.onkeyup=绑定(complete_typed,this);surface.appendChild(inp);surface.typed=inp;inp.focus();}}}Input.prototype.remove_surface_words=函数(){用(这个){var-typed=表面类型;if(typed)while(typed.previousSibling)surface.removeChild(typed.previousSibling)否则清除(表面)}}Input.prototype.remove_typed_Input=函数(){用(这个){if(表面类型){surface.typed.parentNode.removeChild(surface.typed);surface.typed=空;}}}Input.prototype.complete_typed=函数(事件){用(这个){//元素(“debug”).innerHTML=show_props(event,“event”);var inp=表面类型;//调试(“”+输入值+“”);var s=输入值;var ws=s.split(“”);if(ws.length>1||event.keyCode==13){if(ws[0]!=words.filtered)filter_completions(ws[0,true);if(words.count==1)add_word(words.theword);else if(event.keyCode==13)add_word(ws[0])//用于文字else if(elem(ws[0],words.completions))add_word(ws[0);else if(words.theword.length>ws[0].length)inp.value=单词.theword;}else if(s!=words.filtered)filter_completions(s,true)}}Input.prototype.generate_random=函数(){var t=这个;函数show_random(随机){t.清除所有1();t.add_words(gf_lex(随机[0].text));}函数lin_random(abs){t.server.linealize({tree:abs[0].tree,to:t.current.from},show_random);}t.server.get_random({cat:t.startcat_menu.value},林_随机);}Input.prototype.add_words=函数(ws){this.add_words1(ws);this.get_completions();}Input.prototype.add_words1=函数(ws){对于(var i=0;i0&&s2[0]==“”)s2=s2.substr(1);surface.typed.value=s2;}else surface.typed.value=“”;}获取完成();}}Input.prototype.add_word1=函数{用(这个){电流输入推力;var w=span_class(“单词”,文本);surface.insertBefore(w,surface.typed);}}Input.prototype.delete_last=函数(){用(这个){if(surface.typed&&surface.typed.value!=“”)surface.typed.value=“”;else if(current.input.length>0){current.input.pop();local.put(“当前”,当前)if(表面类型){surface.removeChild(surface.typed.previousSibling);surface.typed.focus();}else surface.removeChild(surfacel.lastChild);translations.clear();获取完成();}}}/*---结构编辑--------------------------------------------------*/Input.prototype.get_tree1=函数(解析){var t=这个;函数proceed(lin){t.gettree2(lin,parse[0].trees[0])}if(parse.length==1&&parse[0].from==t.current.from&&parse[0].trees&&parse[0].trees.length==1)t.server.linealize({to:t.current.from,tree:parse[0].trees[0]},继续);否则t.end_structural_editing();}Input.prototype.get_tree2=函数(lin,tree){var t=这个;带(t){if(lin.length==1&&lin[0].to==current.from&&lin[0].text==gf_unlex(当前输入)&&(林[0].括号){var bs=lin[0].括号;//var树=show_abstract_tree(bs);函数proceed(){t.enable_structural_editing(bs,tree)}服务器线性化({to:current.from,tree:tree},继续,绑定(end_structural_editing,t)}否则end_structural_editing();}}Input.prototype.end_structural_editing=函数(){var t=这个;if(t.surface.structure_editing_enabled){var ws=t.current.input;t.清除所有2()t.add_words1(ws);t.surface.structure_editing_enabled=假;}}Input.prototype.enable_structural_editing=函数(括号,树){var t=这个;带(t){var-typed=表面类型;函数add_brack(括号){函数add_bs(b,父){if(b.token){var fun=parent.fun,cat=parent.cat;函数showrepl(){t.show_replacements(括号、父项、树)}if(有趣&猫){var w=span_class(“文字可编辑”,文本(b.token));w.onclick=展示}其他的var w=span_class(“单词”,文本(b.token));w.title=(乐趣||“_”)+“:”+(猫||“-”)+”“+parent.fid+”:“+parent.indexsurface.insertBefore(w,键入);}else b.childrens.map(函数(c){add_bs(c,b)})}add_bs(括号,空)}删除表面文字()//add_bs(括号);if(Array.isArray(括号))括号地图(add_black)//gf>3.5其他的add_支架(支架)//gf<=3.5t.surface.structure_editing_enabled=真;}}Input.prototype.show_replacements=函数(括号、父项、树){var fun=parent.fun,cat=parent.cat;var t=这个;带(t){函数browse1(fun_info){var fun_type=fun_info.def.split(“:”)[1];函数browse2(cat_info){var extb=空;功能检查替换(rfun){函数browse3(rfun_info){var rfun_type=rfun_info.def.split(“:”)[1];函数replace(){t.replace_word(括号、父项、rfun、树);}函数show_replacement(lin){//控制台.log(lin)t.words.insertBefore(按钮(lin[0].text||rfun,替换),extb);}if(rfun_type==函数类型){var tmpl=fun_template(rfun,rfun_type)if(tmpl)t.server.linealize({to:t.current.from,cat:cat,tree:tmpl},show_replacement)其他的t.words.insertBefore(按钮(rfun,replace),extb)}}t.浏览(rfun,browse3)}var ps=cat_info.producers;清晰(t.words);var extf=t.options.extend_grammar;if(extf){函数update(){console.log(“更新迷你吧”)t.grammar.browse={};//清除缓存t.show_replacements(括号、父项、树)}extb=按钮(“New”+cat+“…”,function(){extf(cat,fun_type,update)})t.words.appendChild(扩展)}如果(ps)for(以ps为单位的var pi)if(ps[pi]!=fun)examine_replacement(ps[pi])}t.浏览(cat,browse2)}t.浏览(有趣,浏览1)}}Input.prototype.replace_word=函数(括号、父项、fun、树){var t=这个;函数继续(树){//parent.fun=乐趣;//var树=show_abstract_tree(括号);tree=修改树(tree,parent.fid,fun)tree=show_tree(tree)//将树的JSON repr转换回字符串函数替换(lin_output){if(lin_output.length==1&&lin_output[0].to==t.current.from){t.清除所有1();t.add_words(gf_lex(lin_output[0].text))}}函数错误(文本、状态、ctype){t.words.innerHTML=ctype.split(“;”)[0]==“text/html”? 文本:“单词替换失败”}t.server.linealize({to:t.current.from,tree:tree},replace,err)}//将抽象语法树的字符串表示转换为JSON:t.server.pgf_call(“absrjson”,{tree:tree},继续)}Input.prototype.browse=函数(id,cont){var t=这个;if(t.grammar.browse[id])cont(t.gammar.brwse[id=)其他{浏览的功能(信息){t.grammar.browse[id]=信息;续(信息);}t.server.browse({id:id},已浏览);}}/*---辅助功能--------------------------------------------------*/函数mi_local(grammar_url){return appLocalStorage(“gf.minibar_input”+语法url+“.”)}函数set_initial_language(选项、菜单、语法、旧源){var user_from=old_from||grammar.userLanguage;if(user_from)menu.value=用户来自;//!!如果语言被删除了怎么办?else if(options.default_source_language){对于(var i=0;i0 ? “(”+s+“)”:s}//在树中找到标有fid的节点,并用fun替换那里的函数函数modify_tree(tree,fid,fun){if(tree.fun){if(tree.fid==fid)tree.fun=funelse if(树.children)tree.childrens.map(函数(t){modify_tree(t,fid,fun)})}回归树;}函数fun_template(fname,ftype){if(window.parse_fun){var-fun=parse_fun(fname+“:”+ftype).okif(有趣){var t=fname;对于(var i=1;i-->