dict.js 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297
  1. var dict_pre_searching = false;
  2. var dict_pre_search_curr_word = "";
  3. var dict_search_xml_http = null;
  4. var _autoSplit = true;
  5. function dict_search(word, autoSplit = true) {
  6. _autoSplit = autoSplit;
  7. $("#pre_search_result").hide();
  8. if (!localStorage.searchword) {
  9. localStorage.searchword = "";
  10. }
  11. let oldHistory = localStorage.searchword;
  12. let arrOldHistory = oldHistory.split(",");
  13. let isExist = false;
  14. for (let i = 0; i < arrOldHistory.length; i++) {
  15. if (arrOldHistory[i] == word) {
  16. isExist = true;
  17. }
  18. }
  19. if (!isExist) {
  20. localStorage.searchword = word + "," + oldHistory;
  21. }
  22. word = standardize(word);
  23. word = com_getPaliReal(word);
  24. $.get(
  25. "dict_lookup.php",
  26. {
  27. word: word,
  28. },
  29. function (data, status) {
  30. $("#dict_search_result").html(data);
  31. $("#dict_list").append($("#dictlist"));
  32. $("#right_bar").html("");
  33. $("#right_bar").append($("#dict_user"));
  34. $("#search_result_shell").html("");
  35. $("#search_result_shell").append($("#search_summary"));
  36. guide_init();
  37. let word_count = parseInt($("#word_count").val());
  38. if (_autoSplit == true && word_count < 3) {
  39. trubo_split();
  40. }
  41. }
  42. );
  43. }
  44. function standardize(word) {
  45. let word_end = word.slice(-1);
  46. if (word_end == "n" || word_end == "m") {
  47. word_end = "ṃ";
  48. word = word.slice(0, -1) + word_end;
  49. }
  50. return word;
  51. }
  52. function dict_pre_search(word) {
  53. if (dict_pre_searching == true) {
  54. return;
  55. }
  56. dict_pre_searching = true;
  57. dict_pre_search_curr_word = word;
  58. $.get(
  59. "dict_lookup_pre.php",
  60. {
  61. word: word,
  62. },
  63. function (data, status) {
  64. dict_pre_searching = false;
  65. dict_pre_search_curr_word = "";
  66. try {
  67. let result = JSON.parse(data);
  68. let html = "<div>";
  69. for (const iterator of result) {
  70. html += "<div class='dict_word_list' onclick=\"dict_pre_word_click('" + iterator.word + "')\">";
  71. html += "<span class='spell' >" + iterator.word + "(" + iterator.count + ")</span>";
  72. html += "<div class='mean'>" + iterator.mean + "</div>";
  73. html += "</div>";
  74. }
  75. html += "</div>";
  76. $("#pre_search_word_content").html(html);
  77. $("#pre_search_result").css("display", "block");
  78. $(document).one("click", function () {
  79. $("#pre_search_result").hide();
  80. });
  81. } catch (error) {}
  82. }
  83. );
  84. }
  85. function dict_pre_word_click(word) {
  86. $("#dict_ref_search_input").val(word);
  87. $("#pre_search_result").hide();
  88. dict_search(word);
  89. }
  90. function search_on_load(word) {
  91. $("#dict_ref_search_input").val(word);
  92. dict_search(word);
  93. }
  94. function dict_input_change(obj) {
  95. dict_pre_search(obj.value);
  96. }
  97. function dict_input_onfocus() {
  98. if ($("#dict_ref_search_input").val() == "") {
  99. dict_show_history();
  100. }
  101. }
  102. function dict_input_keyup(e, obj) {
  103. var keynum;
  104. var keychar;
  105. var numcheck;
  106. if ($("#dict_ref_search_input").val() == "") {
  107. dict_show_history();
  108. return;
  109. }
  110. if (window.event) {
  111. // IE
  112. keynum = e.keyCode;
  113. } else if (e.which) {
  114. // Netscape/Firefox/Opera
  115. keynum = e.which;
  116. }
  117. var keychar = String.fromCharCode(keynum);
  118. if (keynum == 13) {
  119. dict_search(obj.value);
  120. } else {
  121. dict_input_split(obj.value);
  122. if (obj.value.indexOf("+") == -1) {
  123. dict_pre_search(obj.value);
  124. } else {
  125. dict_input_split(obj.value);
  126. $("#pre_search_result").hide();
  127. }
  128. }
  129. }
  130. var t;
  131. function dict_input_split(word) {
  132. if (word.indexOf("+") >= 0) {
  133. let wordParts = word.split("+");
  134. let strParts = "";
  135. for (const iterator of wordParts) {
  136. strParts += "<part><a onclick='dict_search(\"" + iterator + "\")'>" + iterator + "</a></part>";
  137. }
  138. let html =
  139. "点击查词<div class='dropdown_ctl'><div class='content'><div class='main_view' >" +
  140. strParts +
  141. "</div></div></div>";
  142. $("#manual_split").html(html);
  143. clearTimeout(t);
  144. t = setTimeout("getPartMeaning()", 1000);
  145. } else {
  146. $("#manual_split").html("");
  147. $("#part_mean_shell").slideUp();
  148. }
  149. }
  150. function dict_show_history() {
  151. if (!localStorage.searchword) {
  152. localStorage.searchword = "";
  153. }
  154. var arrHistory = localStorage.searchword.split(",");
  155. var strHistory = "";
  156. if (arrHistory.length > 0) {
  157. strHistory += '<a onclick="cls_word_search_history()">清空历史记录</a>';
  158. }
  159. for (var i = 0; i < arrHistory.length; i++) {
  160. var word = arrHistory[i];
  161. strHistory += "<div class='dict_word_list'>";
  162. strHistory += "<a onclick='dict_pre_word_click(\"" + word + "\")'>" + word + "</a>";
  163. strHistory += "</div>";
  164. }
  165. $("#dict_ref_search_result").html(strHistory);
  166. }
  167. function cls_word_search_history() {
  168. localStorage.searchword = "";
  169. $("#dict_ref_search_result").html("");
  170. }
  171. function trubo_split() {
  172. let strSpliting = "正在自动切分复合词……";
  173. if ($("#input_parts").html() == strSpliting) {
  174. return;
  175. }
  176. $("#pre_search_result").hide();
  177. $("#input_parts").html(strSpliting);
  178. $.post(
  179. "split.php",
  180. {
  181. word: $("#dict_ref_search_input").val(),
  182. },
  183. function (data, status) {
  184. try {
  185. let result = JSON.parse(data);
  186. let html = "<div>";
  187. let firstWord = new Array();
  188. if (result.length > 0) {
  189. html += "拆分";
  190. let level1Count = 0;
  191. for (const part of result[0]["data"]) {
  192. firstWord.push(part[0].word);
  193. html += '<div class="dropdown_ctl">';
  194. html += '<div class="content">';
  195. html +=
  196. '<div class="main_view">' +
  197. "<part>" +
  198. part[0].word.replace(/\+/g, "</part><part>") +
  199. "</part>" +
  200. "</div>";
  201. html += '<div class="more_button">' + part.length + "</div>";
  202. html += "</div>";
  203. html += '<div class="menu" >';
  204. for (const one_part of part) {
  205. html += '<div class="part_list">' + one_part.word + "</div>";
  206. }
  207. html += "</div>";
  208. html += "</div>";
  209. level1Count++;
  210. }
  211. } else {
  212. html += "无法拆分";
  213. }
  214. html += "</div>";
  215. $("#input_parts").html(html);
  216. getPartMeaning(firstWord.join("+"));
  217. $(".more_button").click(function () {
  218. $(this).parent().siblings(".menu").toggle();
  219. });
  220. $(".part_list").click(function () {
  221. let html = "<part>" + $(this).text().replace(/\+/g, "</part><part>") + "</part>";
  222. $(this).parent().parent().find(".main_view").html(html);
  223. $(this).parent().hide();
  224. getPartMeaning($(this).text());
  225. $("part").click(function () {
  226. dict_search($(this).text(), false);
  227. });
  228. });
  229. $("part").click(function () {
  230. dict_search($(this).text(), false);
  231. });
  232. } catch (e) {}
  233. }
  234. );
  235. }
  236. function getPartMeaning(word = "") {
  237. let sWord = word;
  238. if (word == "") {
  239. sWord = $("#dict_ref_search_input").val();
  240. }
  241. $.get(
  242. "../dict/get_first_mean.php",
  243. {
  244. word: sWord,
  245. },
  246. function (data, status) {
  247. try {
  248. let result = JSON.parse(data);
  249. let html = "<div>";
  250. if (result.length > 0) {
  251. for (const part of result) {
  252. html +=
  253. "<div class='auto_mean'><span class='spell'>" +
  254. part.word +
  255. "</span><span class='meaning'>" +
  256. part.mean +
  257. "</span></div>";
  258. }
  259. }
  260. html += "</div>";
  261. $("#part_mean").html(html);
  262. $("#part_mean_shell").slideDown();
  263. } catch (error) {}
  264. }
  265. );
  266. }
  267. function setNaviVisibility(strObjId = "") {
  268. var objNave = document.getElementById("dict_list");
  269. var objblack = document.getElementById("dict_list_shell");
  270. if (strObjId == "") {
  271. objblack.style.display = "none";
  272. objNave.className = "dict_list_off";
  273. } else {
  274. objblack.style.display = "block";
  275. objNave.className = "dict_list_on";
  276. }
  277. }