paliword.js 15 KB

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