term.js 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500
  1. var arrTerm = new Array();
  2. var arrTerm2 = new Array();
  3. var arrMyTerm = new Array();
  4. var arrAllMean = new Array();
  5. var arrTermAllPali = new Array();
  6. var arrWordIdTermId = new Array();
  7. var strTermTanslationTmp="[%mean%](%pali% %mean2% %mymean%)";
  8. var strTermTanslationTmp2="[%mean%]";
  9. var termCounter=new Array();
  10. var noteCounter=0; //正文内注释计数器
  11. function note(noteId,strDef="unkow"){
  12. document.write("haha");
  13. }
  14. function note_replace(strIn){
  15. /*
  16. */
  17. var output = strIn.replace("/*","<script>");
  18. output = output.replace("*/","</script>");
  19. return(output);
  20. }
  21. //将存储状态的字符串转换为预显示字符串
  22. //设置状态为 0:未处理的原始状态
  23. function term_std_str_to_tran(strIn){
  24. return(strIn.replace(/\[\[/g,"<term status='0'>").replace(/\]\]/g,"</term>"));
  25. }
  26. function term_std_str_to_edit(strIn){
  27. var arrText = strIn.split("/");
  28. for(var i in arrText){
  29. //头尾是*
  30. if(arrText[i].substring(0,1)=="*" && arrText[i].substring(arrText[i].length-1)=="*"){
  31. var arrOneTermWord=arrText[i].split("@");
  32. if(arrOneTermWord.length==2){
  33. arrText[i]="*"+arrOneTermWord[1];
  34. }
  35. }
  36. }
  37. return(arrText.join("/"));
  38. }
  39. function term_tran_edit_replace(strIn){
  40. var strEdit=strIn;
  41. for(var x=0;x<arrTerm2.length;x++){
  42. var strReplace="strEdit=strEdit.replace(/"+arrTerm2[x].meaning+"/g,\"/*"+arrTerm2[x].meaning+"*/\")";
  43. eval(strReplace);
  44. }
  45. //strEdit=strEdit.replace(/\/*\/*/g,"\/*");
  46. //strEdit=strEdit.replace(/\*\/\*\//g,"*\/");
  47. return(strEdit);
  48. }
  49. function term_edit_to_std_str(strIn){
  50. var arrText = strIn.split("/");
  51. for(var i in arrText){
  52. //头尾是*
  53. if(arrText[i].substring(0,1)=="*" && arrText[i].substring(arrText[i].length-1)=="*"){
  54. var wordMeaning=arrText[i].substring(1,arrText[i].length-1);
  55. arrText[i]="*"+term_get_std_str(wordMeaning)+"*";
  56. }
  57. }
  58. return(arrText.join("/"));
  59. }
  60. function term_get_std_str(strMean){
  61. for(var x=0;x<arrTerm2.length;x++){
  62. if(arrTerm2[x].meaning==strMean){
  63. return(arrTerm2[x].guid+"@"+strMean);
  64. }
  65. }
  66. return("unkow@"+strMean);
  67. }
  68. function term_get_my_std_str(strMean){
  69. for(var x in arrMyTerm){
  70. if(arrMyTerm[x].meaning==strMean){
  71. return(arrMyTerm[x].guid+"@"+strMean);
  72. }
  73. }
  74. return("unkow@"+strMean);
  75. }
  76. function note_lookup(word,showto){
  77. $("#"+showto).load("term.php?op=search&word="+word,function(responseTxt,statusTxt,xhr){
  78. if(statusTxt=="success"){
  79. $(".term_note").each(function(index,element){
  80. $(this).html(note_init($(this).html()));
  81. $(this).attr("status",1);
  82. note_refresh_new();
  83. });
  84. }
  85. else if(statusTxt=="error"){
  86. console.error("Error: "+xhr.status+": "+xhr.statusText);
  87. }
  88. });
  89. }
  90. function term_apply(guid){
  91. if(g_eCurrWord){
  92. setNodeText(g_eCurrWord,"note","=term("+guid+")");
  93. term_array_updata();
  94. }
  95. }
  96. function term_data_copy_to_me(guid){
  97. $("#term_dict").load("term.php?op=copy&wordid="+guid);
  98. }
  99. //我的术语字典进入编辑模式
  100. function term_edit(guid){
  101. $("#term_edit_btn1_"+guid).hide();
  102. $("#term_edit_btn2_"+guid).show();
  103. document.getElementById("term_dict_my_"+guid).style.display="none";
  104. document.getElementById("term_dict_my_edit_"+guid).style.display="block";
  105. }
  106. //我的术语字典退出编辑模式
  107. function term_data_esc_edit(guid){
  108. $("#term_edit_btn1_"+guid).show();
  109. $("#term_edit_btn2_"+guid).hide();
  110. document.getElementById("term_dict_my_"+guid).style.display="block";
  111. document.getElementById("term_dict_my_edit_"+guid).style.display="none";
  112. }
  113. //我的术语字典 编辑模式 保存
  114. function term_data_save(guid){
  115. if(guid==""){
  116. var strWord=$("#term_new_word").val();
  117. var strMean=$("#term_new_mean").val();
  118. var strMean2=$("#term_new_mean2").val();
  119. var strNote=$("#term_new_note").val();
  120. var strTag=$("#term_new_tag").val();
  121. let newTerm = new Object();
  122. newTerm.guid=com_guid();
  123. newTerm.word=strWord;
  124. newTerm.meaning = strMean;
  125. newTerm.other_meaning = strMean2;
  126. arrMyTerm.push(newTerm);
  127. }
  128. else{
  129. var strWord=$("#term_edit_word_"+guid).val();
  130. var strMean=$("#term_edit_mean_"+guid).val();
  131. var strMean2=$("#term_edit_mean2_"+guid).val();
  132. var strNote=$("#term_edit_note_"+guid).val();
  133. }
  134. $.get("term.php",
  135. {
  136. op:"save",
  137. guid:guid,
  138. word:strWord,
  139. mean:strMean,
  140. mean2:strMean2,
  141. tag:strTag,
  142. note:strNote
  143. },
  144. function(data,status){
  145. try{
  146. let result= JSON.parse(data);
  147. if(result.status==0){
  148. note_lookup(result.message,"term_dict");
  149. }
  150. else{
  151. ntf_show("term error"+result.message);
  152. }
  153. }
  154. catch(e){
  155. console.error("term_get_all_pali:"+e+" data:"+data);
  156. ntf_show("term error");
  157. }
  158. });
  159. }
  160. function term_get_all_pali(){
  161. $.get("term.php",
  162. {
  163. op:"allpali"
  164. },
  165. function(data,status){
  166. if(data.length>0){
  167. try{
  168. arrTermAllPali = JSON.parse(data);
  169. }
  170. catch(e){
  171. console.error("term_get_all_pali:"+e+" data:"+data);
  172. }
  173. }
  174. });
  175. }
  176. function term_lookup_all(pali){
  177. for(var x in arrTermAllPali){
  178. if(arrTermAllPali[x].word==pali){
  179. return(arrTermAllPali[x]);
  180. }
  181. }
  182. return(null);
  183. }
  184. function term_get_my(){
  185. $.get("term.php",
  186. {
  187. op:"my"
  188. },
  189. function(data,status){
  190. if(data.length>0){
  191. try{
  192. arrMyTerm = JSON.parse(data);
  193. }
  194. catch(e){
  195. console.error(e.error+" data:"+data);
  196. }
  197. }
  198. });
  199. }
  200. //在我的术语字典里查询
  201. function term_lookup_my(pali){
  202. for(var x in arrMyTerm){
  203. if(arrMyTerm[x].meaning==pali){
  204. return(arrMyTerm[x]);
  205. }
  206. if(arrMyTerm[x].word==pali){
  207. return(arrMyTerm[x]);
  208. }
  209. }
  210. return(null);
  211. }
  212. function term_lookup_my_id(id){
  213. for(var x in arrMyTerm){
  214. if(arrMyTerm[x].guid==id){
  215. return(arrMyTerm[x]);
  216. }
  217. }
  218. return(null);
  219. }
  220. function term_get_all_meaning(word){
  221. $.get("term.php",
  222. {
  223. op:"allmean",
  224. word:word
  225. },
  226. function(data,status){
  227. $("#term_win_other_mean").html(data);
  228. });
  229. }
  230. //刷新文档正在使用的术语数据
  231. function term_array_updata(){
  232. arrTerm2=new Array();
  233. var arrTermDownLoadList=new Array();
  234. var arrWordIdTermId=new Array();
  235. var xAllWord = gXmlBookDataBody.getElementsByTagName("word");
  236. for(var x=0;x<xAllWord.length;x++){
  237. var sNote = getNodeText(xAllWord[x],"note");
  238. var wid=getNodeText(xAllWord[x],"id");
  239. if(sNote.substring(0,6)=="=term("){
  240. var termId=sNote.slice(6,-1);
  241. if(!arrTerm[termId]){
  242. arrTermDownLoadList.push(termId);
  243. }
  244. //person={wid:wid,tid:termId};
  245. arrWordIdTermId.push({wid:wid,tid:termId});
  246. }
  247. }
  248. if(arrTermDownLoadList.length>0){
  249. var idlist=arrTermDownLoadList.join();
  250. idlist = idlist.replace(/,/g,"','");
  251. idlist = "'"+ idlist + "'";
  252. $.get("term.php",
  253. {
  254. op:"extract",
  255. list:idlist
  256. },
  257. function(data,status){
  258. var obj = JSON.parse(data);
  259. for(var x in obj){
  260. arrTerm[obj[x].guid]=obj[x];
  261. arrTerm2.push(obj[x]);
  262. }
  263. if(g_eCurrWord){
  264. updataWordHeadById(getNodeText(g_eCurrWord,"id"));
  265. refreshWordNote(g_eCurrWord.parentNode.parentNode);
  266. }
  267. for(var i=0;i<arrWordIdTermId.length;i++){
  268. var wid=arrWordIdTermId[i].wid;
  269. var sMean=arrTerm[arrWordIdTermId[i].tid].meaning;
  270. doc_setWordDataById(wid,"mean",sMean);
  271. updateWordBodyById(wid);
  272. }
  273. });
  274. }
  275. }
  276. function term_updata_translation(){
  277. termCounter=new Array();
  278. noteCounter=1;
  279. $("term").each(function(){
  280. let status=$(this).attr("status");
  281. let termText = $(this).text();
  282. if(termText.slice(0,1)=="#"){
  283. if(status==0){
  284. $(this).attr("status","1");
  285. $(this).attr("type","1");
  286. $(this).attr("text",termText.slice(1));
  287. }
  288. let noteText=$(this).attr("text");
  289. $(this).html("<a onclick=\"alert('"+noteText+"')\">["+noteCounter+"]</a>");
  290. noteCounter++;
  291. }
  292. else{
  293. if(status==0){
  294. let myterm=term_lookup_my(termText);//我的术语字典
  295. if(myterm){
  296. $(this).attr("status","1");
  297. $(this).attr("type","0");
  298. $(this).attr("guid",myterm.guid);
  299. $(this).attr("pali",myterm.word);
  300. $(this).attr("mean",myterm.meaning);
  301. $(this).attr("mean2",myterm.other_meaning);
  302. $(this).attr("replace",myterm.meaning);
  303. }
  304. else{
  305. $(this).attr("status","2");
  306. }
  307. }
  308. let guid=$(this).attr("guid");
  309. let pali=$(this).attr("pali");
  310. let mean=$(this).attr("mean");
  311. let mean2=$(this).attr("mean2");
  312. var renderTo=$(this).attr("pos");
  313. var noteText="";
  314. if(termCounter[guid]){
  315. termCounter[guid]=2;
  316. }
  317. else{
  318. termCounter[guid]=1;
  319. }
  320. var myterm=term_lookup_my(pali);//我的术语字典
  321. let linkclass="";
  322. if(myterm){
  323. linkclass="term_link";
  324. }
  325. else{
  326. linkclass="term_link_new";
  327. }
  328. if(guid){
  329. if(renderTo=="wbw"){
  330. noteText="%note%";
  331. }
  332. else{
  333. if(termCounter[guid]==1){
  334. noteText=strTermTanslationTmp;
  335. }
  336. else{
  337. noteText=strTermTanslationTmp2;
  338. }
  339. }
  340. noteText=noteText.replace("[","<span class='"+linkclass+"' onclick=\"term_show_win('"+guid+"')\">");
  341. noteText=noteText.replace("]","</span>");
  342. noteText=noteText.replace("%mean%","<span class='term_mean'>"+mean+"</span>");
  343. noteText=noteText.replace("%pali%","<span class='term_pali'>"+pali+"</span>");
  344. noteText=noteText.replace("%mean2%","<span class='term_mean2'>"+mean2+"</span>");
  345. noteText=noteText.replace("%note%","<span class='term_note'>"+""+"</span>");
  346. if(myterm){
  347. if(myterm.meaning!=mean){
  348. noteText=noteText.replace("%mymean%","<span class='term_mean_my'>"+myterm.meaning+"</span>");
  349. }
  350. else{
  351. noteText=noteText.replace("%mymean%","");
  352. }
  353. }
  354. else{
  355. noteText=noteText.replace("%mymean%","");
  356. }
  357. }
  358. else{
  359. noteText="<span class='"+linkclass+"' onclick=\"term_show_win('','"+termText+"')\">"+termText+"</span>";
  360. }
  361. $(this).html(noteText);
  362. }
  363. });
  364. }
  365. function term_show_win(guid,keyWord=""){
  366. if(guid==""){
  367. $(term_body).html("当前词条未创建。<br /><a onclick=\"term_add_new('"+keyWord+"')\">现在创建</a>");
  368. }
  369. else{
  370. let currWord = term_lookup_my_id(guid);
  371. if(currWord){
  372. let termString="";
  373. let pali=currWord.word;
  374. let pali_1= pali.substring(0,1).toUpperCase();
  375. pali=pali_1+pali.substring(1);
  376. let mean=currWord.meaning;
  377. let myterm=term_lookup_my(currWord.word);//我的术语字典
  378. termString+="<div class='term_win_mean'>"+pali+"</div>";
  379. termString+="<div class='term_win_pali'>意思:"+currWord.meaning+"</div>";
  380. termString+="<div class='term_win_mean2'>其他意思:"+currWord.other_meaning+"</div>";
  381. termString+="<div class='term_win_mymean'>我的词库:";
  382. if(myterm){
  383. termString+="<b>"+myterm.meaning+"</b> ";
  384. }
  385. else{
  386. termString+="<input type='input' placeholder='我的释义'>";
  387. }
  388. termString+="<span>其他:</span><span id='term_win_other_mean'></span>";
  389. termString+="</div>";
  390. if(currWord.note){
  391. termString+="<div class='term_win_note'>"+currWord.note+"</div>";
  392. }
  393. else{
  394. termString+="<div class='term_win_note'>Loading</div>";
  395. }
  396. $(term_body).html(termString);
  397. term_get_all_meaning(currWord.word);
  398. if(!currWord.note){
  399. $.get("term.php",
  400. {
  401. op:"load_id",
  402. id:currWord.guid
  403. },
  404. function(data,status,xhr){
  405. switch(status){
  406. case "success":
  407. try{
  408. let loadWord = JSON.parse(data);
  409. $("#term_win_note").html(loadWord[0].note);
  410. //修改内存数据
  411. for(let x in arrMyTerm){
  412. if(arrMyTerm[x].guid==loadWord[0].guid){
  413. arrMyTerm[x].note=loadWord[0].note;
  414. return;
  415. }
  416. }
  417. }
  418. catch(e){
  419. console.error(e+" data:"+data);
  420. }
  421. break;
  422. case "error":
  423. console.error("Error: "+xhr.status+": "+xhr.statusText);
  424. break;
  425. }
  426. });
  427. }
  428. }
  429. else{
  430. $(term_body).html("undefined guid");
  431. }
  432. }
  433. document.getElementById("term_win").style.display="flex";
  434. }
  435. function term_tmp(type,tmp){
  436. if(tmp=="new"){
  437. switch(type){
  438. case "a":
  439. strTermTanslationTmp=$("#term_my_tmp").val();
  440. break;
  441. case "a2":
  442. strTermTanslationTmp2=$("#term_my_tmp").val();
  443. break;
  444. }
  445. }
  446. else{
  447. switch(type){
  448. case "a":
  449. strTermTanslationTmp=tmp;
  450. break;
  451. case "a2":
  452. strTermTanslationTmp2=tmp;
  453. break;
  454. }
  455. }
  456. term_updata_translation();
  457. }
  458. function term_add_new(keyword){
  459. document.getElementById("term_win").style.display="none";
  460. editor_show_right_tool_bar(true);
  461. tab_click_b('term_dict','tab_rb_dict');
  462. note_lookup(keyword,"term_dict");
  463. }