paliword.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558
  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 += "</ul>";
  169. return html;
  170. }
  171. function onWordFilterStart() {
  172. $(".case_item").children().find(".filter").show();
  173. $("#case_tools").children(".filter").show();
  174. $("#case_tools").children().find(".filter").show();
  175. $("#case_tools").children(".select_button").hide();
  176. }
  177. function filter_cancel() {
  178. $(".filter").hide();
  179. $("#case_tools").children(".select_button").show();
  180. }
  181. function search_filter() {}
  182. function search_book_filter(objid, type) {
  183. if (document.getElementById(objid).checked == true) {
  184. $("." + type).show();
  185. } else {
  186. $("." + type).hide();
  187. }
  188. }
  189. //选择需要过滤的书
  190. function book_select(bookid) {
  191. if (bookid == 0) {
  192. paliword_search(_key_word, _filter_word);
  193. } else {
  194. paliword_search(_key_word, _filter_word, [bookid]);
  195. }
  196. }
  197. function book_search_filter() {
  198. let booklist = new Array();
  199. $(".booklist").each(function () {
  200. if ($(this).prop("checked")) {
  201. booklist.push($(this).attr("book"));
  202. }
  203. });
  204. book_filter_cancel();
  205. paliword_search(_key_word, _filter_word, booklist);
  206. }
  207. function onBookFilterStart() {
  208. $(".book_item").children().find(".filter").show();
  209. $("#book_tools").children(".filter").show();
  210. $("#book_tools").children().find(".filter").show();
  211. $("#book_tools").children(".select_button").hide();
  212. $(".book_tag_div").show();
  213. }
  214. function book_filter_cancel() {
  215. $(".filter").hide();
  216. $(".book_tag_div").hide();
  217. $("#book_tools").children(".select_button").show();
  218. }
  219. function search_pre_search(word) {
  220. $.get(
  221. "../search/paliword_sc_pre.php",
  222. {
  223. op: "pre",
  224. key: word,
  225. },
  226. function (data) {
  227. let words = JSON.parse(data);
  228. let html = "";
  229. for (const iterator of words) {
  230. html += "<a href='../search/paliword.php?key=" + iterator.word + "'>";
  231. if (parseInt(iterator.bold) > 0) {
  232. html += "<span style='font-weight:700;'>";
  233. } else {
  234. html += "<span>";
  235. }
  236. html += iterator.word + "-" + iterator.count;
  237. html += "</span>";
  238. html += "</a>";
  239. }
  240. $("#pre_search_word_content").html(html);
  241. $("#pre_search_word_content").css("display", "block");
  242. $(document).one("click", function () {
  243. $("#pre_search_word_content").hide();
  244. });
  245. event.stopPropagation();
  246. }
  247. );
  248. }
  249. //以下为旧的函数
  250. function dict_bold_word_all_select() {
  251. var wordcount = $("#bold_word_count").val();
  252. for (var i = 0; i < wordcount; i++) {
  253. document.getElementById("bold_word_" + i).checked = document.getElementById("bold_all_word").checked;
  254. }
  255. dict_update_bold(0);
  256. }
  257. function dict_bold_word_select(id) {
  258. var wordcount = $("#bold_word_count").val();
  259. for (var i = 0; i < wordcount; i++) {
  260. document.getElementById("bold_word_" + i).checked = false;
  261. }
  262. document.getElementById("bold_word_" + id).checked = true;
  263. dict_update_bold(0);
  264. }
  265. function dict_bold_book_select(id) {
  266. var bookcount = $("#bold_book_count").val();
  267. for (var i = 0; i < bookcount; i++) {
  268. document.getElementById("bold_book_" + i).checked = false;
  269. }
  270. document.getElementById("bold_book_" + id).checked = true;
  271. dict_update_bold(0);
  272. }
  273. function dict_update_bold(currpage) {
  274. var wordlist = "(";
  275. var wordcount = $("#bold_word_count").val();
  276. for (var i = 0; i < wordcount; i++) {
  277. if (document.getElementById("bold_word_" + i).checked) {
  278. wordlist += "'" + $("#bold_word_" + i).val() + "',";
  279. }
  280. }
  281. wordlist = wordlist.slice(0, -1);
  282. wordlist += ")";
  283. var booklist = "(";
  284. var bookcount = $("#bold_book_count").val();
  285. for (var i = 0; i < bookcount; i++) {
  286. if (document.getElementById("bold_book_" + i).checked) {
  287. booklist += "'" + $("#bold_book_" + i).val() + "',";
  288. }
  289. }
  290. if (booklist.slice(-1) == ",") {
  291. booklist = booklist.slice(0, -1);
  292. }
  293. booklist += ")";
  294. $.get(
  295. "paliword_search.php",
  296. {
  297. op: "update",
  298. target: "bold",
  299. word: "",
  300. wordlist: wordlist,
  301. booklist: booklist,
  302. currpage: currpage,
  303. },
  304. function (data, status) {
  305. //alert("Data: " + data + "\nStatus: " + status);
  306. $("#dict_bold_right").html(data);
  307. $("#bold_book_list").html($("#bold_book_list_new").html());
  308. $("#bold_book_list_new").html("");
  309. }
  310. );
  311. }
  312. function search_search(word) {
  313. $("#pre_search_result").hide();
  314. $("#pre_search_result_1").hide();
  315. if (!localStorage.searchword) {
  316. localStorage.searchword = "";
  317. }
  318. let oldHistory = localStorage.searchword;
  319. let arrOldHistory = oldHistory.split(",");
  320. let isExist = false;
  321. for (let i = 0; i < arrOldHistory.length; i++) {
  322. if (arrOldHistory[i] == word) {
  323. isExist = true;
  324. }
  325. }
  326. if (!isExist) {
  327. localStorage.searchword = word + "," + oldHistory;
  328. }
  329. if (window.XMLHttpRequest) {
  330. // code for IE7, Firefox, Opera, etc.
  331. dict_search_xml_http = new XMLHttpRequest();
  332. } else if (window.ActiveXObject) {
  333. // code for IE6, IE5
  334. dict_search_xml_http = new ActiveXObject("Microsoft.XMLHTTP");
  335. }
  336. if (dict_search_xml_http != null) {
  337. dict_search_xml_http.onreadystatechange = dict_search_serverResponse;
  338. word = word.replace(/\+/g, "%2b");
  339. dict_search_xml_http.open("GET", "paliword_search.php?op=search&word=" + word, true);
  340. dict_search_xml_http.send();
  341. } else {
  342. alert("Your browser does not support XMLHTTP.");
  343. }
  344. }
  345. function dict_search_serverResponse() {
  346. if (dict_search_xml_http.readyState == 4) {
  347. // 4 = "loaded"
  348. if (dict_search_xml_http.status == 200) {
  349. // 200 = "OK"
  350. var serverText = dict_search_xml_http.responseText;
  351. dict_result = document.getElementById("dict_ref_search_result");
  352. if (dict_result) {
  353. dict_result.innerHTML = serverText;
  354. $("#index_list").hide();
  355. $("#dict_ref_dict_link").html($("#dictlist").html());
  356. $("#dictlist").html("");
  357. }
  358. //$("#dict_type").html($("#real_dict_tab").html());
  359. } else {
  360. alert(dict_pre_search_xml_http.statusText, 0);
  361. }
  362. }
  363. }
  364. /*
  365. var dict_pre_search_xml_http = null;
  366. function search_pre_search(word) {
  367. if (dict_pre_searching == true) {
  368. return;
  369. }
  370. dict_pre_searching = true;
  371. dict_pre_search_curr_word = word;
  372. if (window.XMLHttpRequest) {
  373. // code for IE7, Firefox, Opera, etc.
  374. dict_pre_search_xml_http = new XMLHttpRequest();
  375. } else if (window.ActiveXObject) {
  376. // code for IE6, IE5
  377. dict_pre_search_xml_http = new ActiveXObject("Microsoft.XMLHTTP");
  378. }
  379. if (dict_pre_search_xml_http != null) {
  380. dict_pre_search_xml_http.onreadystatechange = dict_pre_search_serverResponse;
  381. dict_pre_search_xml_http.open("GET", "paliword_search.php?op=pre&word=" + word, true);
  382. dict_pre_search_xml_http.send();
  383. } else {
  384. alert("Your browser does not support XMLHTTP.");
  385. }
  386. }
  387. */
  388. function dict_pre_search_serverResponse() {
  389. if (dict_pre_search_xml_http.readyState == 4) {
  390. // 4 = "loaded"
  391. if (dict_pre_search_xml_http.status == 200) {
  392. // 200 = "OK"
  393. var serverText = dict_pre_search_xml_http.responseText;
  394. $("#pre_search_word_content").html(serverText);
  395. } else {
  396. alert(dict_pre_search_xml_http.statusText, 0);
  397. }
  398. dict_pre_searching = false;
  399. var newword = document.getElementById("dict_ref_search_input").value;
  400. if (newword != dict_pre_search_curr_word) {
  401. search_pre_search(newword);
  402. }
  403. }
  404. }
  405. function dict_pre_word_click(word) {
  406. $("#pre_search_result").hide();
  407. $("#pre_search_result_1").hide();
  408. let inputSearch = $("#dict_ref_search_input").val();
  409. let arrSearch = inputSearch.split(" ");
  410. arrSearch[arrSearch.length - 1] = word;
  411. let strSearchWord = arrSearch.join(" ");
  412. $("#dict_ref_search_input").val(strSearchWord);
  413. $("#dict_ref_search_input_1").val(strSearchWord);
  414. search_search(word);
  415. }
  416. function dict_input_change(obj) {
  417. search_pre_search(obj.value);
  418. }
  419. function search_show_history() {
  420. if (!localStorage.searchword) {
  421. localStorage.searchword = "";
  422. }
  423. var arrHistory = localStorage.searchword.split(",");
  424. var strHistory = "";
  425. if (arrHistory.length > 0) {
  426. strHistory += '<a onclick="cls_word_search_history()">清空历史记录</a>';
  427. }
  428. const max_history_len = 20;
  429. let history_len = 0;
  430. if (arrHistory.length > max_history_len) {
  431. history_len = max_history_len;
  432. } else {
  433. history_len = arrHistory.length;
  434. }
  435. for (var i = 0; i < history_len; i++) {
  436. var word = arrHistory[i];
  437. strHistory += "<div class='dict_word_list'>";
  438. strHistory += "<a onclick='dict_pre_word_click(\"" + word + "\")'>" + word + "</a>";
  439. strHistory += "</div>";
  440. }
  441. $("#search_histray").html(strHistory);
  442. }
  443. function search_input_onfocus() {
  444. if ($("#dict_ref_search_input").val() == "") {
  445. //search_show_history();
  446. }
  447. }
  448. function search_input_keyup(e, obj) {
  449. var keynum;
  450. var keychar;
  451. var numcheck;
  452. if ($("#dict_ref_search_input").val() == "") {
  453. //search_show_history();
  454. $("#pre_search_result").hide();
  455. $("#pre_search_result_1").hide();
  456. return;
  457. }
  458. if (window.event) {
  459. // IE
  460. keynum = e.keyCode;
  461. } else if (e.which) {
  462. // Netscape/Firefox/Opera
  463. keynum = e.which;
  464. }
  465. var keychar = String.fromCharCode(keynum);
  466. if (keynum == 13) {
  467. //search_search(obj.value);
  468. window.location.assign("../search/paliword.php?key=" + obj.value);
  469. } else {
  470. if (obj.value.indexOf(" ") >= 0) {
  471. //search_pre_sent(obj.value);
  472. } else {
  473. $("#pre_search_sent").hide();
  474. }
  475. $("#pre_search_result").show();
  476. $("#pre_search_result_1").show();
  477. search_pre_search(obj.value);
  478. }
  479. }
  480. function search_pre_sent(word) {
  481. pali_sent_get_word(word, function (result) {
  482. let html = "";
  483. try {
  484. let arrResult = JSON.parse(result);
  485. for (x in arrResult) {
  486. html += arrResult[x].text + "<br>";
  487. }
  488. $("#pre_search_sent_title_right").html("总共" + arrResult.lenght);
  489. $("#pre_search_sent_content").html(html);
  490. $("#pre_search_sent").show();
  491. } catch (e) {
  492. console.error(e.message);
  493. }
  494. });
  495. }
  496. function cls_word_search_history() {
  497. localStorage.searchword = "";
  498. $("#dict_ref_search_result").html("");
  499. }
  500. function search_edit_now(book, para, title) {
  501. var res_list = new Array();
  502. res_list.push({
  503. type: "1",
  504. album_id: "-1",
  505. book: book,
  506. parNum: para,
  507. parlist: para,
  508. title: title + "-" + para,
  509. });
  510. res_list.push({
  511. type: "6",
  512. album_id: "-1",
  513. book: book,
  514. parNum: para,
  515. parlist: para,
  516. title: title + "-" + para,
  517. });
  518. var res_data = JSON.stringify(res_list);
  519. window.open("../studio/project.php?op=create&data=" + res_data, "_blank");
  520. }