paliword.js 15 KB

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