paliword.js 15 KB

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