article.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719
  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. param.channel = _channal;
  380. for (const iterator of _channal.split(",")) {
  381. //增加点击次数
  382. OneHitChapter(_book,_par,iterator);
  383. }
  384. }
  385. break;
  386. case "simsent":
  387. case "sim":
  388. param = {view: _view,id:_id};
  389. break;
  390. default:
  391. break;
  392. }
  393. $.get(
  394. "../reader/get_para1.php",
  395. param,
  396. function (data, status) {
  397. if (status == "success") {
  398. try {
  399. let result = JSON.parse(data);
  400. if (result) {
  401. if(result.debug){
  402. console.log("debug:",result.debug);
  403. }
  404. _sent_data=result;
  405. if(result.title==""){
  406. $("#article_title").html("[unnamed]");
  407. }else{
  408. $("#article_title").html(result.title);
  409. }
  410. $("#article_path_title").html(result.title);
  411. $("#page_title").text(result.title);
  412. $("#article_subtitle").html(result.subtitle);
  413. //$("#article_author").html(result.username.nickname + "@" + result.username.username);
  414. console.log("content:",result.content);
  415. $("#contents").html(note_init(result.content));
  416. note_refresh_new(function () {
  417. if(document.querySelector("#para_focus")){
  418. document.querySelector("#para_focus").scrollIntoView({
  419. block: "end",
  420. behavior: "smooth",
  421. });
  422. }
  423. $.get('templiates/glossary.tpl',function(data){
  424. let TermData = term_get_used();
  425. let rendered = Mustache.render(data,TermData);
  426. $("#glossary").html(rendered);
  427. });
  428. });
  429. reader_draw_para_menu();
  430. guide_init();
  431. }
  432. } catch (e) {
  433. console.error(e);
  434. }
  435. } else {
  436. console.error("ajex error");
  437. }
  438. }
  439. );
  440. }
  441. function reader_get_path() {
  442. $.get(
  443. "../reader/get_path.php",
  444. {
  445. book: _book,
  446. para: _par,
  447. },
  448. function (data) {
  449. $("#article_path").html(data);
  450. var bookTitle = $("chapter").first().html();
  451. let suttaTitle = $("chapter").last().html();
  452. $("#pali_pedia").html(bookTitle);
  453. if(suttaTitle==""){
  454. $("#article_title").html("[unnamed]");
  455. $("#page_title").text("[unnamed]");
  456. }else{
  457. $("#article_title").html(suttaTitle);
  458. $("#page_title").text(suttaTitle);
  459. }
  460. note_ref_init('_self');
  461. }
  462. );
  463. }
  464. function reader_draw_para_menu() {
  465. $(".page_number").each(function () {
  466. let strPara = $(this).text();
  467. $(this).addClass("case_dropdown");
  468. let html = "<a name='para_" + strPara + "'></a>";
  469. html += "<div class='case_dropdown-content para_menu'>";
  470. if (typeof _view != "undefined" && _view != "para") {
  471. html += "<a onclick=\"junp_to_para('" + _book + "','" + strPara + "')\">" + gLocal.gui.show_this_para_only + "</a>";
  472. }
  473. html += "<a onclick=\"edit_wbw('" + _book + "','" + strPara + "')\">" + gLocal.gui.edit_now + "</a>";
  474. html += "<a onclick='goto_nissaya(" + _book + "," + strPara + ")'>" + gLocal.gui.show_nissaya + "</a>";
  475. html +=
  476. "<a onclick=\"copy_para_ref('" + _book + "','" + strPara + "')\">" + gLocal.gui.copy_to_clipboard + "</a>";
  477. html +=
  478. "<a onclick=\"copy_text('" +
  479. _book +
  480. "','" +
  481. strPara +
  482. "')\">" +
  483. gLocal.gui.copy +
  484. "“" +
  485. gLocal.gui.pāli +
  486. "”</a>";
  487. html +=
  488. "<a onclick=\"add_to_list('" +
  489. _book +
  490. "','" +
  491. strPara +
  492. "')\">" +
  493. gLocal.gui.add_to_edit_list +
  494. "</a>";
  495. html += "</div>";
  496. $(this).append(html);
  497. });
  498. }
  499. function junp_to_para(book, para) {
  500. let url = "../article/?view=para&book=" + book + "&par=" + para + "&display=sent";
  501. location.assign(url);
  502. }
  503. function copy_para_ref(book, para) {
  504. let output = "";
  505. for (const iterator of _sent_data.sent_list) {
  506. if (iterator.book == book && iterator.paragraph == para) {
  507. output += "{{" + book + "-" + para + "-" + iterator.begin + "-" + iterator.end + "}}\n";
  508. }
  509. }
  510. output += "\n";
  511. copy_to_clipboard(output);
  512. }
  513. function edit_wbw(book, para) {
  514. wbw_channal_list_open(book, [para]);
  515. }
  516. function to_article(){
  517. article_add_dlg_show({
  518. title:_sent_data.title,
  519. content:_sent_data.content,
  520. });
  521. }
  522. var prevChapter=0,nextChapter=0;
  523. var strPrevChapter,strNextChapter;
  524. function render_toc(){
  525. $.getJSON(
  526. "../api/pali_text.php",
  527. {
  528. _method:"index",
  529. view:"toc",
  530. book: _book,
  531. par: _par,
  532. }
  533. ).done(function (data) {
  534. let arrToc = new Array();
  535. for (const it of data.data) {
  536. if(_par==it.paragraph){
  537. nextChapter = it.next_chapter;
  538. prevChapter = it.prev_chapter;
  539. }
  540. let strTitle;
  541. if(it.toc==""){
  542. strTitle = "[unnamed]";
  543. }else{
  544. switch (getCookie('language')) {
  545. case 'my':
  546. strTitle = roman_to_my(it.toc);
  547. break;
  548. case 'si':
  549. strTitle = roman_to_si(it.toc);
  550. break;
  551. default:
  552. strTitle = it.toc;
  553. break;
  554. }
  555. }
  556. arrToc.push({article:it.paragraph,title:strTitle,title_roman:it.toc,level:it.level});
  557. }
  558. $("#toc_content").fancytree({
  559. autoScroll: true,
  560. source: tocGetTreeData(arrToc,_par),
  561. activate: function(e, data) {
  562. gotoChapter(data.node.key);
  563. return false;
  564. }
  565. });
  566. switch (_view) {
  567. case "chapter":
  568. fill_chapter_nav();
  569. break;
  570. case "para":
  571. fill_para_nav();
  572. break;
  573. case "sent":
  574. fill_sent_nav();
  575. default:
  576. fill_default_nav();
  577. break;
  578. }
  579. });
  580. }
  581. function fill_sent_nav(){
  582. $("#contents_nav_left").hide();
  583. $("#contents_nav_right").hide();
  584. }
  585. function fill_sent_nav(){
  586. $("#contents_nav_left_inner").html("");
  587. $("#contents_nav_right_inner").html("");
  588. }
  589. function fill_para_nav(){
  590. $("#contents_nav_left_inner").html(_par-1);
  591. $("#contents_nav_right_inner").html(_par+1);
  592. }
  593. function fill_chapter_nav(){
  594. if(prevChapter>0){
  595. $.getJSON(
  596. "../api/pali_text.php",
  597. {
  598. _method:"show",
  599. view:"toc",
  600. book: _book,
  601. par: prevChapter,
  602. }
  603. ).done(function (data) {
  604. if(data.data.toc==""){
  605. $("#contents_nav_left_inner").html("[unnamed]");
  606. }else{
  607. $("#contents_nav_left_inner").html(data.data.toc);
  608. }
  609. });
  610. }else{
  611. $("#contents_nav_left_inner").html("无");
  612. }
  613. if(nextChapter>0){
  614. $.getJSON(
  615. "../api/pali_text.php",
  616. {
  617. _method:"show",
  618. view:"toc",
  619. book: _book,
  620. par: nextChapter,
  621. }
  622. ).done(function (data) {
  623. if(data.data.toc==""){
  624. $("#contents_nav_right_inner").html("[unnamed]");
  625. }else{
  626. $("#contents_nav_right_inner").html(data.data.toc);
  627. }
  628. });
  629. }else{
  630. $("#contents_nav_right_inner").html("无");
  631. }
  632. }
  633. //跳转到另外一个章节
  634. function gotoChapter(paragraph) {
  635. let url = "../article/index.php?view=chapter";
  636. url += "&book=" + _book;
  637. url += "&par=" + paragraph;
  638. if (_channal != "") {
  639. url += "&channal=" + _channal;
  640. }
  641. if (_display != "") {
  642. url += "&display=" + _display;
  643. }
  644. if (_mode != "") {
  645. url += "&mode=" + _mode;
  646. }
  647. if (_direction != "") {
  648. url += "&direction=" + _direction;
  649. }
  650. location.assign(url);
  651. }
  652. //跳转到另外一个章节
  653. function gotoPara(paragraph) {
  654. let url = "../article/index.php?view=para";
  655. url += "&book=" + _book;
  656. url += "&par=" + paragraph;
  657. if (_channal != "") {
  658. url += "&channal=" + _channal;
  659. }
  660. if (_display != "") {
  661. url += "&display=" + _display;
  662. }
  663. if (_mode != "") {
  664. url += "&mode=" + _mode;
  665. }
  666. if (_direction != "") {
  667. url += "&direction=" + _direction;
  668. }
  669. location.assign(url);
  670. }
  671. function show_channel_detail_pannal(){
  672. if($("#right_pannal").css("display")=="none"){
  673. $("#right_pannal").show();
  674. $(".contents_div").css("width","70%");
  675. }else{
  676. $("#right_pannal").hide();
  677. $(".contents_div").css("width","100%");
  678. }
  679. }