dict.js 7.9 KB

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