dict.js 4.8 KB

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