article.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714
  1. //import {Like,LikeRefresh} from '../widget/like.js';
  2. var _view = "";
  3. var _id = "";
  4. var _articel_id = "";
  5. var _channal = "";
  6. var _lang = "";
  7. var _author = "";
  8. var _display = "";
  9. var _collection_id = "";
  10. var _book=0,_par=0,_start=0,_end=0;
  11. var _sent_data;
  12. var _article_date;
  13. function article_onload() {
  14. historay_init();
  15. }
  16. function articel_load(id, collection_id) {
  17. if (id == "") {
  18. return;
  19. }
  20. $.get(
  21. "../article/get.php",
  22. {
  23. id: id,
  24. collection_id: collection_id,
  25. setting: "",
  26. },
  27. function (data, status) {
  28. if (status == "success") {
  29. try {
  30. let result = JSON.parse(data);
  31. if (result) {
  32. _article_date = result;
  33. $("#article_title").html(result.title);
  34. $("#article_path_title").html(result.title);
  35. $("#page_title").text(result.title);
  36. $("#article_subtitle").html(result.subtitle);
  37. let article_author = result.username.nickname + "@" + result.username.username;
  38. if(result.lang !== "false"){
  39. article_author += result.lang;
  40. }else{
  41. result.lang = "en";
  42. }
  43. $("#article_author").html( article_author );
  44. //将绝对链接转换为 用户连接的主机链接
  45. //result.content = result.content.replace(/www-[A-z]*.wikipali.org/g,location.host);
  46. $("#contents").html(note_init(result.content,"",result.owner,result.lang));
  47. //处理<code>标签作为气泡注释
  48. popup_init();
  49. guide_init();
  50. note_refresh_new(function(){
  51. $.get('templiates/glossary.tpl',function(data){
  52. let TermData = term_get_used();
  53. let rendered = Mustache.render(data,TermData);
  54. $("#glossary").html(rendered);
  55. });
  56. });
  57. }
  58. } catch (e) {
  59. console.error(e);
  60. }
  61. } else {
  62. console.error("ajex error");
  63. }
  64. }
  65. );
  66. }
  67. function collect_load(id) {
  68. if (id == "") {
  69. return;
  70. }
  71. $.get(
  72. "../article/collect_get.php",
  73. {
  74. id: id,
  75. setting: "",
  76. },
  77. function (data, status) {
  78. if (status == "success") {
  79. try {
  80. let result = JSON.parse(data);
  81. if (result) {
  82. $("#article_title").html(result.title);
  83. $("#page_title").text(result.title);
  84. if (result.subtitle) {
  85. $("#article_subtitle").html(result.subtitle);
  86. }
  87. $("#article_author").html(result.username.nickname + "@" + result.username.username);
  88. let htmlLike = "";
  89. htmlLike += "<like liketype='like' restype='collection' resid='"+id+"'></like>";
  90. htmlLike += "<like liketype='favorite' restype='collection' resid='"+id+"'></like>";
  91. $("#like_div").html(htmlLike);
  92. $("#summary").html(result.summary);
  93. $("#contents").html("<div id='content_text'></div><h3>目录</h3><div id='content_toc'></div>");
  94. let article_list = JSON.parse(result.article_list);
  95. render_article_list(article_list);
  96. render_article_list_in_content(article_list);
  97. $("#content_toc").fancytree("getRootNode").visit(function(node){
  98. node.setExpanded(true);
  99. });
  100. Like();
  101. }
  102. } catch (e) {
  103. console.error(e);
  104. }
  105. } else {
  106. console.error("ajex error");
  107. }
  108. }
  109. );
  110. }
  111. function articel_load_article_list(articleId,collectionId) {
  112. $.get(
  113. "../article/collect_get.php",
  114. {
  115. id: collectionId,
  116. setting: "",
  117. },
  118. function (data, status) {
  119. if (status == "success") {
  120. try {
  121. let result = JSON.parse(data);
  122. if (result) {
  123. let article_list = JSON.parse(result.article_list);
  124. render_article_list(article_list,collectionId,articleId);
  125. articleFillFootNavButton(article_list,articleId);
  126. let strTitle = "<a href='../article/?view=collection&collection=" + result.id + "'>" + result.title + "</a> / ";
  127. for (const iterator of tocActivePath) {
  128. strTitle += "<a href='../article/?view=article&id="+iterator.key+"&collection=" + result.id + "'>" + iterator.title + "</a> / ";
  129. }
  130. $("#article_path").html(strTitle);
  131. }
  132. } catch (e) {
  133. console.error(e);
  134. }
  135. } else {
  136. console.error("ajex error");
  137. }
  138. }
  139. );
  140. }
  141. var prevArticle=0,nextArticle=0;
  142. function articleFillFootNavButton(article_list,curr_article){
  143. for (let index = 0; index < article_list.length; index++) {
  144. const element = article_list[index];
  145. if(element.article==curr_article){
  146. if(index!=0){
  147. $("#contents_nav_left_inner").html(article_list[index-1].title);
  148. prevArticle = article_list[index-1].article;
  149. }else{
  150. $("#contents_nav_left_inner").html("无");
  151. }
  152. if(index!=article_list.length-1){
  153. if(article_list[index+1].title==""){
  154. $("#contents_nav_right_inner").html("[unnamed]");
  155. }else{
  156. $("#contents_nav_right_inner").html(article_list[index+1].title);
  157. }
  158. nextArticle = article_list[index+1].article;
  159. }else{
  160. $("#contents_nav_right_inner").html("无");
  161. }
  162. }
  163. }
  164. }
  165. function goto_prev() {
  166. switch (_view) {
  167. case "article":
  168. if(prevArticle==0){
  169. alert("已经到达开始");
  170. }else{
  171. gotoArticle(prevArticle);
  172. }
  173. break;
  174. case "collection":
  175. break;
  176. case "sent":
  177. case "para":
  178. gotoPara(_par-1);
  179. case "chapter":
  180. if(prevChapter>0){
  181. gotoChapter(prevChapter);
  182. }else{
  183. alert("已经到达开始");
  184. }
  185. break;
  186. case "book":
  187. case "series":
  188. break;
  189. case "simsent":
  190. case "sim":
  191. break;
  192. default:
  193. break;
  194. }
  195. }
  196. function goto_next() {
  197. switch (_view) {
  198. case "article":
  199. if(nextArticle==0){
  200. alert("已经到达最后");
  201. }else{
  202. gotoArticle(nextArticle);
  203. }
  204. break;
  205. case "collection":
  206. break;
  207. case "sent":
  208. case "para":
  209. gotoPara(_par+1);
  210. break;
  211. case "chapter":
  212. if(nextChapter>0){
  213. gotoChapter(nextChapter);
  214. }else{
  215. alert("已经到达最后");
  216. }
  217. break;
  218. case "book":
  219. case "series":
  220. break;
  221. case "simsent":
  222. case "sim":
  223. break;
  224. default:
  225. break;
  226. }
  227. }
  228. //在collect 中 的article列表
  229. function render_article_list(article_list,collectId="",articleId="") {
  230. $("#toc_content").fancytree({
  231. autoScroll: true,
  232. source: tocGetTreeData(article_list,articleId),
  233. activate: function(e, data) {
  234. gotoArticle(data.node.key,collectId);
  235. return false;
  236. }
  237. });
  238. }
  239. //在 正文中的目录
  240. function render_article_list_in_content(article_list,collectId="",articleId="") {
  241. $("#content_toc").fancytree({
  242. autoScroll: true,
  243. source: tocGetTreeData(article_list,articleId),
  244. activate: function(e, data) {
  245. gotoArticle(data.node.key,collectId);
  246. return false;
  247. }
  248. });
  249. }
  250. function set_channal(channalid) {
  251. let url = "../article/index.php?";
  252. if (_view != "") {
  253. url += "view=" + _view;
  254. }
  255. if (_id != "") {
  256. url += "&id=" + _id;
  257. }
  258. if (_book != 0) {
  259. url += "&book=" + _book;
  260. }
  261. if (_par != 0) {
  262. url += "&par=" + _par;
  263. }
  264. if (_start != 0) {
  265. url += "&start=" + _start;
  266. }
  267. if (_end != 0) {
  268. url += "&end=" + _end;
  269. }
  270. if (_collection_id != "") {
  271. url += "&collection=" + _collection_id;
  272. }
  273. if (channalid != "") {
  274. url += "&channal=" + channalid;
  275. }
  276. if (_display != "") {
  277. url += "&display=" + _display;
  278. }
  279. if (_mode != "") {
  280. url += "&mode=" + _mode;
  281. }
  282. if (_direction != "") {
  283. url += "&direction=" + _direction;
  284. }
  285. location.assign(url);
  286. }
  287. function setMode(mode = "read") {
  288. let url = "../article/index.php?";
  289. if (_view != "") {
  290. url += "view=" + _view;
  291. }
  292. if (_id != "") {
  293. url += "&id=" + _id;
  294. }
  295. if (_book != 0) {
  296. url += "&book=" + _book;
  297. }
  298. if (_par != 0) {
  299. url += "&par=" + _par;
  300. }
  301. if (_start != 0) {
  302. url += "&start=" + _start;
  303. }
  304. if (_end != 0) {
  305. url += "&end=" + _end;
  306. }
  307. if (_collection_id != "") {
  308. url += "&collection=" + _collection_id;
  309. }
  310. if (_channal != "") {
  311. url += "&channal=" + _channal;
  312. }
  313. if (_display != "") {
  314. if (mode == "read") {
  315. url += "&display=" + _display;
  316. } else {
  317. url += "&display=sent";
  318. }
  319. }
  320. if (mode != "") {
  321. url += "&mode=" + mode;
  322. }
  323. if (_direction != "") {
  324. url += "&direction=" + _direction;
  325. }
  326. location.assign(url);
  327. }
  328. //跳转到另外一个文章
  329. function gotoArticle(articleId) {
  330. let url = "../article/index.php?view=article&id=" + articleId;
  331. if (_collection_id != "") {
  332. url += "&collection=" + _collection_id;
  333. }
  334. if (_channal != "") {
  335. url += "&channal=" + _channal;
  336. }
  337. if (_display != "") {
  338. url += "&display=" + _display;
  339. }
  340. if (_mode != "") {
  341. url += "&mode=" + _mode;
  342. }
  343. if (_direction != "") {
  344. url += "&direction=" + _direction;
  345. }
  346. location.assign(url);
  347. }
  348. function OneHitChapter(book,para,channel){
  349. fetch('/api/v2/view',{
  350. method: 'POST',
  351. credentials: 'same-origin',
  352. headers: {
  353. 'Content-Type': 'application/json'
  354. },
  355. body: JSON.stringify({
  356. target_type:'chapter',
  357. book:book,
  358. para:para,
  359. channel:channel
  360. })
  361. })
  362. .then(response => response.json())
  363. .then(data => console.log(data));
  364. }
  365. function palicanon_load() {
  366. let param;
  367. switch (_view) {
  368. case "sent":
  369. case "para":
  370. case "chapter":
  371. param = {
  372. view: _view,
  373. book: _book,
  374. par: _par,
  375. start: _start,
  376. end: _end,
  377. }
  378. if(_channal !==""){
  379. for (const iterator of _channal.split(",")) {
  380. OneHitChapter(_book,_par,iterator);
  381. }
  382. }
  383. break;
  384. case "simsent":
  385. case "sim":
  386. param = {view: _view,id:_id};
  387. break;
  388. default:
  389. break;
  390. }
  391. $.get(
  392. "../reader/get_para1.php",
  393. param,
  394. function (data, status) {
  395. if (status == "success") {
  396. try {
  397. let result = JSON.parse(data);
  398. if (result) {
  399. _sent_data=result;
  400. if(result.title==""){
  401. $("#article_title").html("[unnamed]");
  402. }else{
  403. $("#article_title").html(result.title);
  404. }
  405. $("#article_path_title").html(result.title);
  406. $("#page_title").text(result.title);
  407. $("#article_subtitle").html(result.subtitle);
  408. //$("#article_author").html(result.username.nickname + "@" + result.username.username);
  409. $("#contents").html(note_init(result.content));
  410. note_refresh_new(function () {
  411. if(document.querySelector("#para_focus")){
  412. document.querySelector("#para_focus").scrollIntoView({
  413. block: "end",
  414. behavior: "smooth",
  415. });
  416. }
  417. $.get('templiates/glossary.tpl',function(data){
  418. let TermData = term_get_used();
  419. let rendered = Mustache.render(data,TermData);
  420. $("#glossary").html(rendered);
  421. });
  422. });
  423. reader_draw_para_menu();
  424. guide_init();
  425. }
  426. } catch (e) {
  427. console.error(e);
  428. }
  429. } else {
  430. console.error("ajex error");
  431. }
  432. }
  433. );
  434. }
  435. function reader_get_path() {
  436. $.get(
  437. "../reader/get_path.php",
  438. {
  439. book: _book,
  440. para: _par,
  441. },
  442. function (data) {
  443. $("#article_path").html(data);
  444. var bookTitle = $("chapter").first().html();
  445. let suttaTitle = $("chapter").last().html();
  446. $("#pali_pedia").html(bookTitle);
  447. if(suttaTitle==""){
  448. $("#article_title").html("[unnamed]");
  449. $("#page_title").text("[unnamed]");
  450. }else{
  451. $("#article_title").html(suttaTitle);
  452. $("#page_title").text(suttaTitle);
  453. }
  454. note_ref_init('_self');
  455. }
  456. );
  457. }
  458. function reader_draw_para_menu() {
  459. $(".page_number").each(function () {
  460. let strPara = $(this).text();
  461. $(this).addClass("case_dropdown");
  462. let html = "<a name='para_" + strPara + "'></a>";
  463. html += "<div class='case_dropdown-content para_menu'>";
  464. if (typeof _view != "undefined" && _view != "para") {
  465. html += "<a onclick=\"junp_to_para('" + _book + "','" + strPara + "')\">" + gLocal.gui.show_this_para_only + "</a>";
  466. }
  467. html += "<a onclick=\"edit_wbw('" + _book + "','" + strPara + "')\">" + gLocal.gui.edit_now + "</a>";
  468. html += "<a onclick='goto_nissaya(" + _book + "," + strPara + ")'>" + gLocal.gui.show_nissaya + "</a>";
  469. html +=
  470. "<a onclick=\"copy_para_ref('" + _book + "','" + strPara + "')\">" + gLocal.gui.copy_link + "</a>";
  471. html +=
  472. "<a onclick=\"copy_text('" +
  473. _book +
  474. "','" +
  475. strPara +
  476. "')\">" +
  477. gLocal.gui.copy +
  478. "“" +
  479. gLocal.gui.pāli +
  480. "”</a>";
  481. html +=
  482. "<a onclick=\"add_to_list('" +
  483. _book +
  484. "','" +
  485. strPara +
  486. "')\">" +
  487. gLocal.gui.add_to_edit_list +
  488. "</a>";
  489. html += "</div>";
  490. $(this).append(html);
  491. });
  492. }
  493. function junp_to_para(book, para) {
  494. let url = "../article/?view=para&book=" + book + "&par=" + para + "&display=sent";
  495. location.assign(url);
  496. }
  497. function copy_para_ref(book, para) {
  498. let output = "";
  499. for (const iterator of _sent_data.sent_list) {
  500. if (iterator.book == book && iterator.paragraph == para) {
  501. output += "{{" + book + "-" + para + "-" + iterator.begin + "-" + iterator.end + "}}\n";
  502. }
  503. }
  504. output += "\n";
  505. copy_to_clipboard(output);
  506. }
  507. function edit_wbw(book, para) {
  508. wbw_channal_list_open(book, [para]);
  509. }
  510. function to_article(){
  511. article_add_dlg_show({
  512. title:_sent_data.title,
  513. content:_sent_data.content,
  514. });
  515. }
  516. var prevChapter=0,nextChapter=0;
  517. var strPrevChapter,strNextChapter;
  518. function render_toc(){
  519. $.getJSON(
  520. "../api/pali_text.php",
  521. {
  522. _method:"index",
  523. view:"toc",
  524. book: _book,
  525. par: _par,
  526. }
  527. ).done(function (data) {
  528. let arrToc = new Array();
  529. for (const it of data.data) {
  530. if(_par==it.paragraph){
  531. nextChapter = it.next_chapter;
  532. prevChapter = it.prev_chapter;
  533. }
  534. let strTitle;
  535. if(it.toc==""){
  536. strTitle = "[unnamed]";
  537. }else{
  538. switch (getCookie('language')) {
  539. case 'my':
  540. strTitle = roman_to_my(it.toc);
  541. break;
  542. case 'si':
  543. strTitle = roman_to_si(it.toc);
  544. break;
  545. default:
  546. strTitle = it.toc;
  547. break;
  548. }
  549. }
  550. arrToc.push({article:it.paragraph,title:strTitle,title_roman:it.toc,level:it.level});
  551. }
  552. $("#toc_content").fancytree({
  553. autoScroll: true,
  554. source: tocGetTreeData(arrToc,_par),
  555. activate: function(e, data) {
  556. gotoChapter(data.node.key);
  557. return false;
  558. }
  559. });
  560. switch (_view) {
  561. case "chapter":
  562. fill_chapter_nav();
  563. break;
  564. case "para":
  565. fill_para_nav();
  566. break;
  567. case "sent":
  568. fill_sent_nav();
  569. default:
  570. fill_default_nav();
  571. break;
  572. }
  573. });
  574. }
  575. function fill_sent_nav(){
  576. $("#contents_nav_left").hide();
  577. $("#contents_nav_right").hide();
  578. }
  579. function fill_sent_nav(){
  580. $("#contents_nav_left_inner").html("");
  581. $("#contents_nav_right_inner").html("");
  582. }
  583. function fill_para_nav(){
  584. $("#contents_nav_left_inner").html(_par-1);
  585. $("#contents_nav_right_inner").html(_par+1);
  586. }
  587. function fill_chapter_nav(){
  588. if(prevChapter>0){
  589. $.getJSON(
  590. "../api/pali_text.php",
  591. {
  592. _method:"show",
  593. view:"toc",
  594. book: _book,
  595. par: prevChapter,
  596. }
  597. ).done(function (data) {
  598. if(data.data.toc==""){
  599. $("#contents_nav_left_inner").html("[unnamed]");
  600. }else{
  601. $("#contents_nav_left_inner").html(data.data.toc);
  602. }
  603. });
  604. }else{
  605. $("#contents_nav_left_inner").html("无");
  606. }
  607. if(nextChapter>0){
  608. $.getJSON(
  609. "../api/pali_text.php",
  610. {
  611. _method:"show",
  612. view:"toc",
  613. book: _book,
  614. par: nextChapter,
  615. }
  616. ).done(function (data) {
  617. if(data.data.toc==""){
  618. $("#contents_nav_right_inner").html("[unnamed]");
  619. }else{
  620. $("#contents_nav_right_inner").html(data.data.toc);
  621. }
  622. });
  623. }else{
  624. $("#contents_nav_right_inner").html("无");
  625. }
  626. }
  627. //跳转到另外一个章节
  628. function gotoChapter(paragraph) {
  629. let url = "../article/index.php?view=chapter";
  630. url += "&book=" + _book;
  631. url += "&par=" + paragraph;
  632. if (_channal != "") {
  633. url += "&channal=" + _channal;
  634. }
  635. if (_display != "") {
  636. url += "&display=" + _display;
  637. }
  638. if (_mode != "") {
  639. url += "&mode=" + _mode;
  640. }
  641. if (_direction != "") {
  642. url += "&direction=" + _direction;
  643. }
  644. location.assign(url);
  645. }
  646. //跳转到另外一个章节
  647. function gotoPara(paragraph) {
  648. let url = "../article/index.php?view=para";
  649. url += "&book=" + _book;
  650. url += "&par=" + paragraph;
  651. if (_channal != "") {
  652. url += "&channal=" + _channal;
  653. }
  654. if (_display != "") {
  655. url += "&display=" + _display;
  656. }
  657. if (_mode != "") {
  658. url += "&mode=" + _mode;
  659. }
  660. if (_direction != "") {
  661. url += "&direction=" + _direction;
  662. }
  663. location.assign(url);
  664. }
  665. function show_channel_detail_pannal(){
  666. if($("#right_pannal").css("display")=="none"){
  667. $("#right_pannal").show();
  668. $(".contents_div").css("width","70%");
  669. }else{
  670. $("#right_pannal").hide();
  671. $(".contents_div").css("width","100%");
  672. }
  673. }