paliword.js 15 KB

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