dict.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. var dict_pre_searching = false;
  2. var dict_pre_search_curr_word = "";
  3. var dict_search_xml_http = null;
  4. function dict_search(word) {
  5. $("#pre_search_result").hide();
  6. if (!localStorage.searchword) {
  7. localStorage.searchword = "";
  8. }
  9. let oldHistory = localStorage.searchword;
  10. let arrOldHistory = oldHistory.split(",");
  11. let isExist = false;
  12. for (let i = 0; i < arrOldHistory.length; i++) {
  13. if (arrOldHistory[i] == word) {
  14. isExist = true;
  15. }
  16. }
  17. if (!isExist) {
  18. localStorage.searchword = word + "," + oldHistory;
  19. }
  20. word = standardize(word);
  21. $.get("dict_lookup.php",
  22. {
  23. op: "search",
  24. word: word
  25. },
  26. function (data, status) {
  27. $("#dict_search_result").html(data);
  28. $("#dict_list").html($("#dictlist").html());
  29. $("#dictlist").html("");
  30. guide_init();
  31. });
  32. }
  33. function standardize(word) {
  34. let word_end = word.slice(-1);
  35. if (word_end == "n" || word_end == "m") {
  36. word_end = "ṃ";
  37. word = word.slice(0, -1) + word_end;
  38. }
  39. return (word);
  40. }
  41. function dict_pre_search(word) {
  42. if (dict_pre_searching == true) { return; }
  43. dict_pre_searching = true;
  44. dict_pre_search_curr_word = word;
  45. $.get("dict_lookup.php",
  46. {
  47. op: "pre",
  48. word: word
  49. },
  50. function (data, status) {
  51. dict_pre_searching = false;
  52. dict_pre_search_curr_word = "";
  53. $("#pre_search_word_content").html(data);
  54. $("#pre_search_result").css("display", "block");
  55. });
  56. }
  57. function dict_pre_word_click(word) {
  58. $("#dict_ref_search_input").val(word);
  59. $("#pre_search_result").hide();
  60. dict_search(word);
  61. }
  62. function dict_input_change(obj) {
  63. dict_pre_search(obj.value);
  64. }
  65. function dict_input_onfocus() {
  66. if ($("#dict_ref_search_input").val() == "") {
  67. dict_show_history();
  68. }
  69. }
  70. function dict_input_keyup(e, obj) {
  71. var keynum
  72. var keychar
  73. var numcheck
  74. if ($("#dict_ref_search_input").val() == "") {
  75. dict_show_history();
  76. return;
  77. }
  78. if (window.event) // IE
  79. {
  80. keynum = e.keyCode
  81. }
  82. else if (e.which) // Netscape/Firefox/Opera
  83. {
  84. keynum = e.which
  85. }
  86. var keychar = String.fromCharCode(keynum)
  87. if (keynum == 13) {
  88. dict_search(obj.value);
  89. }
  90. else {
  91. dict_input_split(obj.value);
  92. if (obj.value.indexOf("+") == -1) {
  93. dict_pre_search(obj.value);
  94. }
  95. else {
  96. dict_input_split(obj.value);
  97. $("#pre_search_result").hide();
  98. }
  99. }
  100. }
  101. function dict_input_split(word) {
  102. if (word.indexOf("+") >= 0) {
  103. var wordParts = word.split("+");
  104. var strParts = "";
  105. for (var i in wordParts) {
  106. //strParts += "<div class='part_list'><a onclick='dict_search(\"" + wordParts[i] + "\")'>" + wordParts[i] + "</a></div>";
  107. strParts += "<part><a onclick='dict_search(\"" + wordParts[i] + "\")'>" + wordParts[i] + "</a></part>";
  108. }
  109. strParts = "<div class='dropdown_ctl'><div class='content'><div class='main_view' >" + strParts + "</div></div></div>";
  110. $("#input_parts").html(strParts);
  111. }
  112. else {
  113. $("#input_parts").html("");
  114. }
  115. }
  116. function dict_show_history() {
  117. if (!localStorage.searchword) {
  118. localStorage.searchword = "";
  119. }
  120. var arrHistory = localStorage.searchword.split(",");
  121. var strHistory = "";
  122. if (arrHistory.length > 0) {
  123. strHistory += "<a onclick=\"cls_word_search_history()\">清空历史记录</a>";
  124. }
  125. for (var i = 0; i < arrHistory.length; i++) {
  126. var word = arrHistory[i];
  127. strHistory += "<div class='dict_word_list'>";
  128. strHistory += "<a onclick='dict_pre_word_click(\"" + word + "\")'>" + word + "</a>";
  129. strHistory += "</div>";
  130. }
  131. $("#dict_ref_search_result").html(strHistory);
  132. }
  133. function cls_word_search_history() {
  134. localStorage.searchword = "";
  135. $("#dict_ref_search_result").html("");
  136. }
  137. function trubo_split() {
  138. $("#pre_search_result").hide();
  139. $.post("split.php",
  140. {
  141. word: $("#dict_ref_search_input").val()
  142. },
  143. function (data, status) {
  144. try {
  145. let result = JSON.parse(data);
  146. let html = "<div>";
  147. if (result.length > 0) {
  148. for (const part of result[0]["data"]) {
  149. html += '<div class="dropdown_ctl">';
  150. html += '<div class="content">';
  151. html += '<div class="main_view">' + "<part>" + part[0].word.replace(/\+/g, "</part><part>") + "</part>" + '</div>';
  152. html += '<div class="more_button">' + part.length + '</div>';
  153. html += '</div>';
  154. html += '<div class="menu" >';
  155. for (const one_part of part) {
  156. html += '<div class="part_list">' + one_part.word + '</div>';
  157. }
  158. html += '</div>';
  159. html += '</div>';
  160. }
  161. }
  162. html += "</div>";
  163. $("#input_parts").html(html);
  164. $(".more_button").click(function () {
  165. $(this).parent().siblings(".menu").toggle();
  166. }
  167. );
  168. $(".part_list").click(function () {
  169. let html = "<part>" + $(this).text().replace(/\+/g, "</part><part>") + "</part>";
  170. $(this).parent().parent().find(".main_view").html(html);
  171. $(this).parent().hide();
  172. $("part").click(function () {
  173. dict_search($(this).text());
  174. });
  175. }
  176. );
  177. $("part").click(function () {
  178. dict_search($(this).text());
  179. }
  180. );
  181. }
  182. catch (e) {
  183. }
  184. });
  185. }