paliword.js 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. var dict_pre_searching = false;
  2. var dict_pre_search_curr_word = "";
  3. var dict_search_xml_http = null;
  4. var _key_word = "";
  5. var _filter_word = new Array();
  6. $(document).ready(function () {
  7. paliword_search(_key_word);
  8. });
  9. function paliword_search(keyword, words = new Array(), book = new Array()) {
  10. $.get(
  11. "../search/paliword_sc.php",
  12. {
  13. op: "search",
  14. key: keyword,
  15. words: JSON.stringify(words),
  16. book: JSON.stringify(book),
  17. },
  18. function (data) {
  19. let result = JSON.parse(data);
  20. let html = "";
  21. for (const iterator of result.data) {
  22. html += render_word_result(iterator);
  23. }
  24. $("#contents").html(html);
  25. html = "";
  26. html += "<div class='case_item'>";
  27. html += "<div class='spell'><a onclick='case_filter_all()'>all</a> " + result.case_num + " Words</div>";
  28. html += "<div class='tag'>" + result.case_count + "</div>";
  29. html += "</div>";
  30. for (const iterator of result.case) {
  31. html += render_case(iterator);
  32. }
  33. $("#case_content").html(html);
  34. html = "";
  35. html += "<div class='book_tag_div filter'>";
  36. if (result.book_tag) {
  37. for (const iterator of result.book_tag) {
  38. html += render_book_tag(iterator);
  39. }
  40. }
  41. html += "</div>";
  42. if (result.book_list) {
  43. for (const iterator of result.book_list) {
  44. html += render_book_list(iterator);
  45. }
  46. }
  47. $("#book_list").html(html);
  48. $("#contents_nav").html(render_nav(result));
  49. }
  50. );
  51. }
  52. function highlightWords(line, word) {
  53. var regex = new RegExp("(" + word + ")", "gi");
  54. return line.replace(regex, "<highlight>$1</highlight>");
  55. }
  56. function render_word_result(worddata) {
  57. let html = "";
  58. html += "<div class='search_result'>";
  59. let keyword = worddata.keyword;
  60. let link = "<a href='../reader/?view=para&book=" + worddata.book + "&para=" + worddata.para + "' target='_blank'>";
  61. html += "<div class='title'>" + link + worddata.title + "</a></div>";
  62. let newStr = highlightWords(worddata.palitext, keyword);
  63. html += "<div class='wizard_par_div'>" + newStr + "</div>";
  64. html += "<div class='path'>" + worddata.path + "</div>";
  65. html += "<div class='path'>" + worddata.book + "-" + worddata.para + "-" + worddata.wt + "</div>";
  66. html += "</div>";
  67. return html;
  68. }
  69. //渲染单词变格表
  70. function render_case(data) {
  71. let html = "";
  72. let wordid = data.id;
  73. html += "<div class='fileter_item case_item'>";
  74. html += "<div class='spell ";
  75. if (data.selected === true) {
  76. html += "selected";
  77. } else {
  78. html += "invalid";
  79. }
  80. html += "'>";
  81. html += "<input id='bold_word_" + wordid + "' class='wordcase filter' type='checkbox' ";
  82. if (data.selected === true) {
  83. html += " checked ";
  84. }
  85. html += "wordid='" + wordid + "' />";
  86. html += '<a onclick="word_select(' + wordid + ')">';
  87. html += data.spell;
  88. html += "</a>";
  89. html += "</div>";
  90. html += "<div class='tag'>" + data.count + "</div>";
  91. html += "</div>";
  92. return html;
  93. }
  94. //选择需要过滤的单词
  95. function word_search_filter() {
  96. let wordlist = new Array();
  97. $(".wordcase").each(function () {
  98. if ($(this).prop("checked")) {
  99. wordlist.push($(this).attr("wordid"));
  100. }
  101. });
  102. filter_cancel();
  103. _filter_word = wordlist;
  104. paliword_search(_key_word, wordlist);
  105. }
  106. function case_filter_all() {
  107. paliword_search(_key_word);
  108. }
  109. function render_book_tag(data) {
  110. let html = "";
  111. html += "<div class='fileter_item book_tag'>";
  112. html += "<div class='spell'>";
  113. html += "<input booktag='" + data.tag + "' class='book_tag' type='checkbox' checked />";
  114. html += data.title;
  115. html += "</div>";
  116. html += "<div class='tag'>" + data.count + "</div>";
  117. html += "</div>";
  118. return html;
  119. }
  120. function render_book_list(data) {
  121. let html = "";
  122. let bookid = data.book;
  123. html += "<div class='fileter_item book_item'>";
  124. html += "<div class='spell'>";
  125. html += "<input book='" + bookid + "' class='book_list filter' type='checkbox' checked />";
  126. html += '<a onclick="dict_bold_word_select({$i})">';
  127. html += data.title;
  128. html += "</a>";
  129. html += "</div>";
  130. html += "<div class='tag'>" + data.count + "</div>";
  131. html += "</div>";
  132. return html;
  133. }
  134. function render_nav(result) {
  135. let html = "<ul class='page_nav'>";
  136. if (result["record_count"] > 20) {
  137. let pages = parseInt(result["record_count"] / 20);
  138. for (let index = 0; index < pages; index++) {
  139. html += "<li >" + (index + 1) + "</li> ";
  140. }
  141. }
  142. html += "</ul>";
  143. return html;
  144. }
  145. function onWordFilterStart() {
  146. $(".case_item").children().find(".filter").show();
  147. $("#case_tools").children(".filter").show();
  148. $("#case_tools").children().find(".filter").show();
  149. $("#case_tools").children(".select_button").hide();
  150. }
  151. function filter_cancel() {
  152. $(".filter").hide();
  153. $("#case_tools").children(".select_button").show();
  154. }
  155. function search_filter() {}
  156. function word_select(wordid) {
  157. paliword_search(_key_word, [wordid]);
  158. }
  159. function search_book_filter(objid, type) {
  160. if (document.getElementById(objid).checked == true) {
  161. $("." + type).show();
  162. } else {
  163. $("." + type).hide();
  164. }
  165. }
  166. //选择需要过滤的书
  167. function book_select(bookid) {
  168. paliword_search(_key_word, _filter_word, [bookid]);
  169. }
  170. function book_search_filter() {
  171. let booklist = new Array();
  172. $(".booklist").each(function () {
  173. if ($(this).prop("checked")) {
  174. booklist.push($(this).attr("book"));
  175. }
  176. });
  177. book_filter_cancel();
  178. paliword_search(_key_word, _filter_word, booklist);
  179. }
  180. function onBookFilterStart() {
  181. $(".book_item").children().find(".filter").show();
  182. $("#book_tools").children(".filter").show();
  183. $("#book_tools").children().find(".filter").show();
  184. $("#book_tools").children(".select_button").hide();
  185. $(".book_tag_div").show();
  186. }
  187. function book_filter_cancel() {
  188. $(".filter").hide();
  189. $(".book_tag_div").hide();
  190. $("#book_tools").children(".select_button").show();
  191. }
  192. function search_pre_search(word) {
  193. $.get(
  194. "../search/paliword_sc.php",
  195. {
  196. op: "pre",
  197. key: word,
  198. },
  199. function (data) {
  200. let words = JSON.parse(data);
  201. let html = "";
  202. for (const iterator of words) {
  203. html += "<a href='../search/paliword.php?key=" + iterator.word + "'>";
  204. if (parseInt(iterator.bold) > 0) {
  205. html += "<span style='font-weight:700;'>";
  206. } else {
  207. html += "<span>";
  208. }
  209. html += iterator.word + "-" + iterator.count;
  210. html += "</span>";
  211. html += "</a>";
  212. }
  213. $("#pre_search_word_content").html(html);
  214. $("#pre_search_word_content").css("display", "block");
  215. $(document).one("click", function () {
  216. $("#pre_search_word_content").hide();
  217. });
  218. event.stopPropagation();
  219. }
  220. );
  221. }
  222. //以下为旧的函数
  223. function dict_bold_word_all_select() {
  224. var wordcount = $("#bold_word_count").val();
  225. for (var i = 0; i < wordcount; i++) {
  226. document.getElementById("bold_word_" + i).checked = document.getElementById("bold_all_word").checked;
  227. }
  228. dict_update_bold(0);
  229. }
  230. function dict_bold_word_select(id) {
  231. var wordcount = $("#bold_word_count").val();
  232. for (var i = 0; i < wordcount; i++) {
  233. document.getElementById("bold_word_" + i).checked = false;
  234. }
  235. document.getElementById("bold_word_" + id).checked = true;
  236. dict_update_bold(0);
  237. }
  238. function dict_bold_book_select(id) {
  239. var bookcount = $("#bold_book_count").val();
  240. for (var i = 0; i < bookcount; i++) {
  241. document.getElementById("bold_book_" + i).checked = false;
  242. }
  243. document.getElementById("bold_book_" + id).checked = true;
  244. dict_update_bold(0);
  245. }
  246. function dict_update_bold(currpage) {
  247. var wordlist = "(";
  248. var wordcount = $("#bold_word_count").val();
  249. for (var i = 0; i < wordcount; i++) {
  250. if (document.getElementById("bold_word_" + i).checked) {
  251. wordlist += "'" + $("#bold_word_" + i).val() + "',";
  252. }
  253. }
  254. wordlist = wordlist.slice(0, -1);
  255. wordlist += ")";
  256. var booklist = "(";
  257. var bookcount = $("#bold_book_count").val();
  258. for (var i = 0; i < bookcount; i++) {
  259. if (document.getElementById("bold_book_" + i).checked) {
  260. booklist += "'" + $("#bold_book_" + i).val() + "',";
  261. }
  262. }
  263. if (booklist.slice(-1) == ",") {
  264. booklist = booklist.slice(0, -1);
  265. }
  266. booklist += ")";
  267. $.get(
  268. "paliword_search.php",
  269. {
  270. op: "update",
  271. target: "bold",
  272. word: "",
  273. wordlist: wordlist,
  274. booklist: booklist,
  275. currpage: currpage,
  276. },
  277. function (data, status) {
  278. //alert("Data: " + data + "\nStatus: " + status);
  279. $("#dict_bold_right").html(data);
  280. $("#bold_book_list").html($("#bold_book_list_new").html());
  281. $("#bold_book_list_new").html("");
  282. }
  283. );
  284. }
  285. function search_search(word) {
  286. $("#pre_search_result").hide();
  287. $("#pre_search_result_1").hide();
  288. if (!localStorage.searchword) {
  289. localStorage.searchword = "";
  290. }
  291. let oldHistory = localStorage.searchword;
  292. let arrOldHistory = oldHistory.split(",");
  293. let isExist = false;
  294. for (let i = 0; i < arrOldHistory.length; i++) {
  295. if (arrOldHistory[i] == word) {
  296. isExist = true;
  297. }
  298. }
  299. if (!isExist) {
  300. localStorage.searchword = word + "," + oldHistory;
  301. }
  302. if (window.XMLHttpRequest) {
  303. // code for IE7, Firefox, Opera, etc.
  304. dict_search_xml_http = new XMLHttpRequest();
  305. } else if (window.ActiveXObject) {
  306. // code for IE6, IE5
  307. dict_search_xml_http = new ActiveXObject("Microsoft.XMLHTTP");
  308. }
  309. if (dict_search_xml_http != null) {
  310. dict_search_xml_http.onreadystatechange = dict_search_serverResponse;
  311. word = word.replace(/\+/g, "%2b");
  312. dict_search_xml_http.open("GET", "paliword_search.php?op=search&word=" + word, true);
  313. dict_search_xml_http.send();
  314. } else {
  315. alert("Your browser does not support XMLHTTP.");
  316. }
  317. }
  318. function dict_search_serverResponse() {
  319. if (dict_search_xml_http.readyState == 4) {
  320. // 4 = "loaded"
  321. if (dict_search_xml_http.status == 200) {
  322. // 200 = "OK"
  323. var serverText = dict_search_xml_http.responseText;
  324. dict_result = document.getElementById("dict_ref_search_result");
  325. if (dict_result) {
  326. dict_result.innerHTML = serverText;
  327. $("#index_list").hide();
  328. $("#dict_ref_dict_link").html($("#dictlist").html());
  329. $("#dictlist").html("");
  330. }
  331. //$("#dict_type").html($("#real_dict_tab").html());
  332. } else {
  333. alert(dict_pre_search_xml_http.statusText, 0);
  334. }
  335. }
  336. }
  337. /*
  338. var dict_pre_search_xml_http = null;
  339. function search_pre_search(word) {
  340. if (dict_pre_searching == true) {
  341. return;
  342. }
  343. dict_pre_searching = true;
  344. dict_pre_search_curr_word = word;
  345. if (window.XMLHttpRequest) {
  346. // code for IE7, Firefox, Opera, etc.
  347. dict_pre_search_xml_http = new XMLHttpRequest();
  348. } else if (window.ActiveXObject) {
  349. // code for IE6, IE5
  350. dict_pre_search_xml_http = new ActiveXObject("Microsoft.XMLHTTP");
  351. }
  352. if (dict_pre_search_xml_http != null) {
  353. dict_pre_search_xml_http.onreadystatechange = dict_pre_search_serverResponse;
  354. dict_pre_search_xml_http.open("GET", "paliword_search.php?op=pre&word=" + word, true);
  355. dict_pre_search_xml_http.send();
  356. } else {
  357. alert("Your browser does not support XMLHTTP.");
  358. }
  359. }
  360. */
  361. function dict_pre_search_serverResponse() {
  362. if (dict_pre_search_xml_http.readyState == 4) {
  363. // 4 = "loaded"
  364. if (dict_pre_search_xml_http.status == 200) {
  365. // 200 = "OK"
  366. var serverText = dict_pre_search_xml_http.responseText;
  367. $("#pre_search_word_content").html(serverText);
  368. } else {
  369. alert(dict_pre_search_xml_http.statusText, 0);
  370. }
  371. dict_pre_searching = false;
  372. var newword = document.getElementById("dict_ref_search_input").value;
  373. if (newword != dict_pre_search_curr_word) {
  374. search_pre_search(newword);
  375. }
  376. }
  377. }
  378. function dict_pre_word_click(word) {
  379. $("#pre_search_result").hide();
  380. $("#pre_search_result_1").hide();
  381. let inputSearch = $("#dict_ref_search_input").val();
  382. let arrSearch = inputSearch.split(" ");
  383. arrSearch[arrSearch.length - 1] = word;
  384. let strSearchWord = arrSearch.join(" ");
  385. $("#dict_ref_search_input").val(strSearchWord);
  386. $("#dict_ref_search_input_1").val(strSearchWord);
  387. search_search(word);
  388. }
  389. function dict_input_change(obj) {
  390. search_pre_search(obj.value);
  391. }
  392. function search_show_history() {
  393. if (!localStorage.searchword) {
  394. localStorage.searchword = "";
  395. }
  396. var arrHistory = localStorage.searchword.split(",");
  397. var strHistory = "";
  398. if (arrHistory.length > 0) {
  399. strHistory += '<a onclick="cls_word_search_history()">清空历史记录</a>';
  400. }
  401. const max_history_len = 20;
  402. let history_len = 0;
  403. if (arrHistory.length > max_history_len) {
  404. history_len = max_history_len;
  405. } else {
  406. history_len = arrHistory.length;
  407. }
  408. for (var i = 0; i < history_len; i++) {
  409. var word = arrHistory[i];
  410. strHistory += "<div class='dict_word_list'>";
  411. strHistory += "<a onclick='dict_pre_word_click(\"" + word + "\")'>" + word + "</a>";
  412. strHistory += "</div>";
  413. }
  414. $("#search_histray").html(strHistory);
  415. }
  416. function search_input_onfocus() {
  417. if ($("#dict_ref_search_input").val() == "") {
  418. //search_show_history();
  419. }
  420. }
  421. function search_input_keyup(e, obj) {
  422. var keynum;
  423. var keychar;
  424. var numcheck;
  425. if ($("#dict_ref_search_input").val() == "") {
  426. //search_show_history();
  427. $("#pre_search_result").hide();
  428. $("#pre_search_result_1").hide();
  429. return;
  430. }
  431. if (window.event) {
  432. // IE
  433. keynum = e.keyCode;
  434. } else if (e.which) {
  435. // Netscape/Firefox/Opera
  436. keynum = e.which;
  437. }
  438. var keychar = String.fromCharCode(keynum);
  439. if (keynum == 13) {
  440. //search_search(obj.value);
  441. window.location.assign("../search/paliword.php?key=" + obj.value);
  442. } else {
  443. if (obj.value.indexOf(" ") >= 0) {
  444. //search_pre_sent(obj.value);
  445. } else {
  446. $("#pre_search_sent").hide();
  447. }
  448. $("#pre_search_result").show();
  449. $("#pre_search_result_1").show();
  450. search_pre_search(obj.value);
  451. }
  452. }
  453. function search_pre_sent(word) {
  454. pali_sent_get_word(word, function (result) {
  455. let html = "";
  456. try {
  457. let arrResult = JSON.parse(result);
  458. for (x in arrResult) {
  459. html += arrResult[x].text + "<br>";
  460. }
  461. $("#pre_search_sent_title_right").html("总共" + arrResult.lenght);
  462. $("#pre_search_sent_content").html(html);
  463. $("#pre_search_sent").show();
  464. } catch (e) {
  465. console.error(e.message);
  466. }
  467. });
  468. }
  469. function cls_word_search_history() {
  470. localStorage.searchword = "";
  471. $("#dict_ref_search_result").html("");
  472. }
  473. function search_edit_now(book, para, title) {
  474. var res_list = new Array();
  475. res_list.push({
  476. type: "1",
  477. album_id: "-1",
  478. book: book,
  479. parNum: para,
  480. parlist: para,
  481. title: title + "-" + para,
  482. });
  483. res_list.push({
  484. type: "6",
  485. album_id: "-1",
  486. book: book,
  487. parNum: para,
  488. parlist: para,
  489. title: title + "-" + para,
  490. });
  491. var res_data = JSON.stringify(res_list);
  492. window.open("../studio/project.php?op=create&data=" + res_data, "_blank");
  493. }