palicanon.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435
  1. var main_tag = "";
  2. var list_tag = new Array();
  3. var currTagLevel0 = new Array();
  4. var allTags = new Array();
  5. var arrMyTerm = new Array();
  6. palicanon_load_term();
  7. function palicanon_onload() {
  8. $("span[tag]").click(function () {
  9. $(this).siblings().removeClass("select");
  10. $(this).addClass("select");
  11. main_tag = $(this).attr("tag");
  12. list_tag = new Array();
  13. tag_changed();
  14. render_tag_list();
  15. });
  16. $("#tag_input").keypress(function () {
  17. tag_render_others();
  18. });
  19. }
  20. function palicanon_load_term() {
  21. $.post(
  22. "../term/get_term_index.php",
  23. {
  24. lang: getCookie("language"),
  25. },
  26. function (data) {
  27. let result = JSON.parse(data);
  28. if (result.status == 0) {
  29. arrMyTerm = result.data;
  30. render_main_tag();
  31. } else {
  32. alert(result.error);
  33. }
  34. }
  35. );
  36. }
  37. function render_main_tag() {
  38. $("#main_tag")
  39. .children()
  40. .each(function () {
  41. $(this).html(tag_get_local_word($(this).attr("tag")));
  42. });
  43. }
  44. function tag_changed() {
  45. let strTags = "";
  46. if (list_tag.length > 0) {
  47. strTags = main_tag + "," + list_tag.join();
  48. } else {
  49. strTags = main_tag;
  50. }
  51. console.log(strTags);
  52. let lang = getCookie("language");
  53. if (lang == "zh-cn") {
  54. lang = "zh-hans";
  55. } else if (lang == "zh-tw") {
  56. lang = "zh-hant";
  57. } else if (lang == "") {
  58. lang = "en";
  59. }
  60. $.get(
  61. "./book_tag.php",
  62. {
  63. tag: strTags,
  64. lang: lang,
  65. },
  66. function (data, status) {
  67. let arrBookList = JSON.parse(data);
  68. let html = "";
  69. allTags = new Array();
  70. let arrChapter = new Array();
  71. for (const iterator of arrBookList) {
  72. let tag0 = "";
  73. let tags = iterator.tag.split("::");
  74. let currTag = new Array();
  75. currTag[main_tag] = 1;
  76. for (const scondTag of list_tag) {
  77. currTag[scondTag] = 1;
  78. }
  79. for (let tag of tags) {
  80. if (tag.slice(0, 1) == ":") {
  81. tag = tag.slice(1);
  82. }
  83. if (tag.slice(-1) == ":") {
  84. tag = tag.slice(0, -1);
  85. }
  86. if (currTagLevel0.hasOwnProperty(tag)) {
  87. tag0 = tag;
  88. }
  89. if (!currTag.hasOwnProperty(tag)) {
  90. if (allTags.hasOwnProperty(tag)) {
  91. allTags[tag] += 1;
  92. } else {
  93. allTags[tag] = 1;
  94. }
  95. }
  96. }
  97. if (arrBookList.length < 50 || (arrBookList.length > 50 && iterator.level == 1)) {
  98. arrChapter.push(iterator);
  99. }
  100. }
  101. let newTags = new Array();
  102. for (const oneTag in allTags) {
  103. if (allTags[oneTag] < arrBookList.length) {
  104. newTags[oneTag] = allTags[oneTag];
  105. }
  106. }
  107. allTags = newTags;
  108. allTags.sort(sortNumber);
  109. tag_render_others();
  110. palicanon_chapter_list_apply(0);
  111. $("#list-1").html(render_chapter_list(arrChapter));
  112. if (arrBookList.length < 50) {
  113. $("#list_shell_1").removeClass("book_view");
  114. } else {
  115. $("#list_shell_1").addClass("book_view");
  116. }
  117. }
  118. );
  119. }
  120. function palicanon_load_chapter(book, para, div_index = 1) {
  121. let lang = getCookie("language");
  122. if (lang == "zh-cn") {
  123. lang = "zh-hans";
  124. } else if (lang == "zh-tw") {
  125. lang = "zh-hant";
  126. } else if (lang == "") {
  127. lang = "en";
  128. }
  129. $.get(
  130. "../palicanon/get_chapter_info.php",
  131. {
  132. book: book,
  133. para: para,
  134. lang: lang,
  135. },
  136. function (data, status) {
  137. palicanon_chapter_list_apply(div_index);
  138. let arrChapterInfo = JSON.parse(data);
  139. let html = render_chapter_head(arrChapterInfo, div_index);
  140. $("#chapter_head_" + (parseInt(div_index) + 1)).html(html);
  141. let lang = getCookie("language");
  142. if (lang == "zh-cn") {
  143. lang = "zh-hans";
  144. } else if (lang == "zh-tw") {
  145. lang = "zh-hant";
  146. } else if (lang == "") {
  147. lang = "en";
  148. }
  149. $.get(
  150. "./get_chapter_children.php",
  151. {
  152. book: book,
  153. para: para,
  154. lang: lang,
  155. },
  156. function (data, status) {
  157. let arrChapterList = JSON.parse(data);
  158. $("#list-" + (parseInt(div_index) + 1)).html(render_chapter_list(arrChapterList));
  159. //palicanon_chapter_list_apply(arrChapterList, div_index);
  160. }
  161. );
  162. }
  163. );
  164. }
  165. function render_chapter_head(chapter_info, parent) {
  166. let html = "";
  167. html = "<div class='chapter_head_tool_bar'>";
  168. html +=
  169. "<div><span class='chapter_back_button' id='chapter_back_" +
  170. (parent + 1) +
  171. "' onclick=\"chapter_back('" +
  172. parent +
  173. "')\">back</span></div>";
  174. html +=
  175. "<div><span class='chapter_close_button' id='chapter_close_" +
  176. (parent + 1) +
  177. "' onclick=\"chapter_back('" +
  178. parent +
  179. "')\">back</span></div>";
  180. html += "</div>";
  181. let link = "../reader/?view=chapter&book=" + chapter_info.book + "&para=" + chapter_info.paragraph;
  182. html += "<div class='title'>";
  183. if (typeof chapter_info.trans_title == "undefined") {
  184. html += " <div class='title_1'><a href='" + link + "' target='_blank'>" + chapter_info.text + "</a></div>";
  185. } else {
  186. html +=
  187. " <div class='title_1'><a href='" + link + "' target='_blank'>" + chapter_info.trans_title + "</a></div>";
  188. }
  189. html += "<div class='title_2'>" + chapter_info.text + "</div>";
  190. html += "</div>";
  191. html += "<div class='res res_more'>";
  192. html += "<h2>译文</h2>";
  193. html += "<div class='progress'>";
  194. if (chapter_info.progress && chapter_info.progress.length > 0) {
  195. let r = 12;
  196. let perimeter = 2 * Math.PI * r;
  197. for (const iterator of chapter_info.progress) {
  198. let stroke1 = parseInt(perimeter * iterator.all_trans);
  199. let stroke2 = perimeter - stroke1;
  200. html += ' <div class="item">';
  201. html += '<svg class="progress_circle" width="30" height="30" viewbox="0,0,30,30">';
  202. html += '<circle class="progress_bg" cx="15" cy="15" r="12" stroke-width="5" fill="none"></circle>';
  203. html +=
  204. '<circle class="progress_color" cx="15" cy="15" r="12" stroke-width="5" fill="none" stroke-dasharray="' +
  205. stroke1 +
  206. " " +
  207. stroke2 +
  208. '"></circle>';
  209. html += "</svg>";
  210. html += "<div class='lang'>" + iterator.lang + "-" + parseInt(iterator.all_trans * 100) + "%</div>";
  211. html += " </div>";
  212. }
  213. } else {
  214. html += "无译文";
  215. }
  216. html += "</div>";
  217. html += "</div>";
  218. return html;
  219. }
  220. function render_chapter_list(chapterList) {
  221. let html = "";
  222. for (const iterator of chapterList) {
  223. html += palicanon_render_chapter_row(iterator);
  224. }
  225. return html;
  226. }
  227. function palicanon_chapter_list_apply(div_index) {
  228. let iDiv = parseInt(div_index);
  229. let html = "";
  230. html += "<div id='chapter_head_" + (iDiv + 1) + "' class='chapter_head'></div>";
  231. html += "<ul id='list-" + (iDiv + 1) + "' class='grid' level='" + (iDiv + 1) + "'>";
  232. /*
  233. for (const iterator of chapterList) {
  234. html += palicanon_render_chapter_row(iterator);
  235. }
  236. */
  237. html += "</ul>";
  238. $("#list_shell_" + (iDiv + 1)).html(html);
  239. $("#list_shell_" + (iDiv + 1)).removeClass();
  240. $("#list_shell_" + (iDiv + 1)).addClass("show");
  241. //隐藏之后的列表
  242. for (let index = iDiv + 2; index <= 8; index++) {
  243. $("#list_shell_" + index).removeClass();
  244. $("#list_shell_" + index).addClass("hidden");
  245. }
  246. //收缩当前的
  247. $("#list-" + iDiv).removeClass();
  248. $("#list-" + iDiv).addClass("list");
  249. $("#list_shell_" + iDiv).removeClass();
  250. $("#list_shell_" + iDiv).addClass("list");
  251. }
  252. function chapter_onclick(obj) {
  253. let book = $(obj).attr("book");
  254. let para = $(obj).attr("para");
  255. let level = $(obj).parent().attr("level");
  256. $(obj).siblings().removeClass("selected");
  257. $(obj).addClass("selected");
  258. $("#tag_list").slideUp();
  259. palicanon_load_chapter(book, para, level);
  260. }
  261. function palicanon_render_chapter_row(chapter) {
  262. let html = "";
  263. let levelClass = "";
  264. if (chapter.level == 1) {
  265. levelClass = " level_1";
  266. }
  267. html +=
  268. '<li class="' +
  269. levelClass +
  270. '" book="' +
  271. chapter.book +
  272. '" para="' +
  273. chapter.para +
  274. '" onclick="chapter_onclick(this)">';
  275. html += '<div class="title">';
  276. if (typeof chapter.trans_title == "undefined") {
  277. html += " <div class='title_1'>" + chapter.title + "</div>";
  278. } else {
  279. html += " <div class='title_1'>" + chapter.trans_title + "</div>";
  280. }
  281. html += ' <div class="title_2" lang="pali">' + chapter.title + "</div>";
  282. html += "</div>";
  283. html += '<div class="resource">';
  284. if (chapter.progress) {
  285. let r = 12;
  286. let perimeter = 2 * Math.PI * r;
  287. let stroke1 = parseInt(perimeter * chapter.progress.all_trans);
  288. let stroke2 = perimeter - stroke1;
  289. html += '<svg class="progress_circle" width="30" height="30" viewbox="0,0,30,30">';
  290. html += '<circle class="progress_bg" cx="15" cy="15" r="12" stroke-width="5" fill="none"></circle>';
  291. html +=
  292. '<circle class="progress_color" cx="15" cy="15" r="12" stroke-width="5" fill="none" stroke-dasharray="' +
  293. stroke1 +
  294. " " +
  295. stroke2 +
  296. '"></circle>';
  297. html += "</svg>";
  298. }
  299. html += '<div class="res_more">';
  300. html += " ";
  301. html += " </div>";
  302. html += "</div>";
  303. html += "</li>";
  304. return html;
  305. }
  306. function tag_get_local_word(word) {
  307. let termKey = term_lookup_my(word, "", getCookie("userid"), getCookie("language"));
  308. if (termKey) {
  309. return termKey.meaning;
  310. } else {
  311. return word;
  312. }
  313. }
  314. function tag_render_others() {
  315. let strOthersTag = "";
  316. currTagLevel0 = new Array();
  317. $(".tag_others").html("");
  318. document.getElementById("main_tag").style.margin = 1 + "em";
  319. document.getElementById("main_tag").style.fontSize = 100 + "%";
  320. for (const key in allTags) {
  321. if (allTags.hasOwnProperty(key)) {
  322. if ($("#tag_input").val().length > 0) {
  323. if (key.indexOf($("#tag_input").val()) >= 0) {
  324. strOthersTag =
  325. '<button class="canon-tag" onclick ="tag_click(\'' + key + "')\" >" + key + "</button>";
  326. }
  327. } else {
  328. let keyname = tag_get_local_word(key);
  329. strOthersTag =
  330. '<button class="canon-tag" title="' +
  331. key +
  332. '" onclick ="tag_click(\'' +
  333. key +
  334. "')\" >" +
  335. keyname +
  336. "</button>";
  337. }
  338. let thisLevel = 100;
  339. if (tag_level.hasOwnProperty(key)) {
  340. thisLevel = tag_level[key].level;
  341. if (tag_level[key].level == 0) {
  342. currTagLevel0[key] = 1;
  343. }
  344. }
  345. $(".tag_others[level='" + thisLevel + "']").html(
  346. $(".tag_others[level='" + thisLevel + "']").html() + strOthersTag
  347. );
  348. }
  349. }
  350. }
  351. function tag_click(tag) {
  352. list_tag.push(tag);
  353. render_tag_list();
  354. tag_changed();
  355. }
  356. function render_tag_list() {
  357. $("#tag_list").slideDown();
  358. let strListTag = gLocal.gui.selected + ":";
  359. for (const iterator of list_tag) {
  360. strListTag += '<tag><span class="textt" title="' + iterator + '">' + tag_get_local_word(iterator) + "</span>";
  361. strListTag += '<span class="tag-delete" onclick ="tag_remove(\'' + iterator + "')\">✕</span></tag>";
  362. }
  363. strListTag +=
  364. "<div style='display:inline-block;width:20em;'>" +
  365. "<input id='tag_input' type='input' placeholder='tag' size='20' />" +
  366. "</div>";
  367. $("#tag_selected").html(strListTag);
  368. }
  369. function tag_remove(tag) {
  370. for (let i = 0; i < list_tag.length; i++) {
  371. if (list_tag[i] == tag) {
  372. list_tag.splice(i, 1);
  373. }
  374. }
  375. render_tag_list();
  376. tag_changed();
  377. }
  378. function sortNumber(a, b) {
  379. return b - a;
  380. }
  381. function tag_list_slide_toggle(element) {
  382. if ($(element).html().indexOf("⮟") != -1) {
  383. $(element).html("⮝");
  384. } else {
  385. $(element).html("⮟");
  386. }
  387. $("#tag_list").slideToggle();
  388. }
  389. function chapter_back(parent) {
  390. let curr = parseInt(parent) + 1;
  391. let prt = parseInt(parent);
  392. //隐藏当前的
  393. $("#list_shell_" + curr).removeClass();
  394. $("#list_shell_" + curr).addClass("hidden");
  395. //展开上一个
  396. $("#list-" + prt).removeClass();
  397. $("#list-" + prt).addClass("grid");
  398. $("#list_shell_" + prt).removeClass();
  399. $("#list_shell_" + prt).addClass("show");
  400. }