note.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645
  1. var _display = "";
  2. var _word = "";
  3. var _channal = "";
  4. var _lang = "";
  5. var _author = "";
  6. var _arrData = new Array();
  7. var _channalData;
  8. const MAX_NOTE_NEST = 2;
  9. /*
  10. {{203-1654-23-45@11@en@*}}
  11. <note>203-1654-23-45@11@en@*</note>
  12. <note id=guid book=203 para=1654 begin=23 end=45 author=11 lang=en tag=*></note>
  13. <note id=guid book=203 para=1654 begin=23 end=45 author=11 lang=en tag=*>
  14. <div class=text>
  15. pali text
  16. </div>
  17. <tran>
  18. </tran>
  19. <ref>
  20. </ref>
  21. </note>
  22. */
  23. /*
  24. 解析百科字符串
  25. {{203-1654-23-45@11@en@*}}
  26. <note id=12345 info="203-1654-23-45@11@en@*"><note>
  27. <note id="guid" book=203 para=1654 begin=23 end=45 author=11 lang=en tag=*></note>
  28. */
  29. function note_create() {
  30. wbw_channal_list_init();
  31. note_sent_edit_dlg_init();
  32. term_edit_dlg_init();
  33. }
  34. function note_sent_edit_dlg_init() {
  35. $("body").append('<div id="note_sent_edit_dlg" title="Edit"><div id="edit_dialog_content"></div></div>');
  36. $("#note_sent_edit_dlg").dialog({
  37. autoOpen: false,
  38. width: 550,
  39. buttons: [
  40. {
  41. text: "Save",
  42. click: function () {
  43. note_sent_save();
  44. $(this).dialog("close");
  45. },
  46. },
  47. {
  48. text: "Cancel",
  49. click: function () {
  50. $(this).dialog("close");
  51. },
  52. },
  53. ],
  54. });
  55. }
  56. function note_init(input) {
  57. let newString = input.replace(/\{\{/g, '<note info="');
  58. newString = newString.replace(/\}\}/g, '"></note>');
  59. let output = "<div>";
  60. output += marked(newString);
  61. output += "</div>";
  62. return output;
  63. }
  64. function note_update_background_style() {
  65. var mSentsBook = new Array();
  66. var mBgIndex = 1;
  67. $("note").each(function () {
  68. let info = $(this).attr("info").split("-");
  69. if (info.length >= 2) {
  70. let book = info[0];
  71. $(this).attr("book", book);
  72. if (!mSentsBook[book]) {
  73. mSentsBook[book] = mBgIndex;
  74. mBgIndex++;
  75. }
  76. $(this).addClass("bg_color_" + mSentsBook[book]);
  77. }
  78. });
  79. }
  80. //
  81. function note_refresh_new() {
  82. note_update_background_style();
  83. let objNotes = document.querySelectorAll("note");
  84. let arrSentInfo = new Array();
  85. for (const iterator of objNotes) {
  86. let id = iterator.id;
  87. if (id == null || id == "") {
  88. //查看这个节点是第几层note嵌套。大于预定层数退出。
  89. let layout = 1;
  90. let parent = iterator.parentNode;
  91. while (parent.nodeType == 1) {
  92. if (parent.nodeName == "NOTE") {
  93. layout++;
  94. if (layout > MAX_NOTE_NEST) {
  95. return false;
  96. }
  97. } else if (parent.nodeName == "BODY") {
  98. break;
  99. }
  100. parent = parent.parentNode;
  101. }
  102. id = com_guid();
  103. iterator.id = id;
  104. let info = iterator.getAttributeNode("info").value;
  105. let arrInfo = info.split("-");
  106. if (arrInfo.length >= 2) {
  107. let book = arrInfo[0];
  108. let para = arrInfo[1];
  109. }
  110. if (info && info != "") {
  111. arrSentInfo.push({ id: id, data: info });
  112. }
  113. }
  114. }
  115. if (arrSentInfo.length > 0) {
  116. let setting = new Object();
  117. setting.lang = "";
  118. setting.channal = _channal;
  119. $.post(
  120. "../term/note.php",
  121. {
  122. setting: JSON.stringify(setting),
  123. data: JSON.stringify(arrSentInfo),
  124. },
  125. function (data, status) {
  126. if (status == "success") {
  127. try {
  128. let sentData = JSON.parse(data);
  129. for (const iterator of sentData) {
  130. let id = iterator.id;
  131. let strHtml = "<a name='" + id + "'></a>";
  132. if (_display && _display == "para") {
  133. //段落模式
  134. let strPalitext =
  135. "<pali book='" +
  136. iterator.book +
  137. "' para='" +
  138. iterator.para +
  139. "' begin='" +
  140. iterator.begin +
  141. "' end='" +
  142. iterator.end +
  143. "' >" +
  144. iterator.palitext +
  145. "</pali>";
  146. let divPali = $("#" + id)
  147. .parent()
  148. .children(".palitext");
  149. if (divPali.length == 0) {
  150. if (_channal != "") {
  151. let arrChannal = _channal.split(",");
  152. for (let index = arrChannal.length - 1; index >= 0; index--) {
  153. const iChannal = arrChannal[index];
  154. $("#" + id)
  155. .parent()
  156. .prepend("<div class='tran_div' channal='" + iChannal + "'></div>");
  157. }
  158. }
  159. $("#" + id)
  160. .parent()
  161. .prepend("<div class='palitext'></div>");
  162. }
  163. $("#" + id)
  164. .parent()
  165. .children(".palitext")
  166. .first()
  167. .append(strPalitext);
  168. let htmlTran = "";
  169. for (const oneTran of iterator.translation) {
  170. let html =
  171. "<span class='tran' lang='" +
  172. oneTran.lang +
  173. "' channal='" +
  174. oneTran.channal +
  175. "'>";
  176. html += marked(
  177. term_std_str_to_tran(
  178. oneTran.text,
  179. oneTran.channal,
  180. oneTran.editor,
  181. oneTran.lang
  182. )
  183. );
  184. html += "</span>";
  185. if (_channal == "") {
  186. htmlTran += html;
  187. } else {
  188. $("#" + id)
  189. .siblings(".tran_div[channal='" + oneTran.channal + "']")
  190. .append(html);
  191. }
  192. }
  193. $("#" + id).html(htmlTran);
  194. } else {
  195. //句子模式
  196. strHtml += note_json_html(iterator);
  197. $("#" + id).html(strHtml);
  198. }
  199. }
  200. //刷新句子链接递归,有加层数限制。
  201. note_refresh_new();
  202. _arrData = _arrData.concat(sentData);
  203. note_ref_init();
  204. term_get_dict();
  205. note_channal_list();
  206. } catch (e) {
  207. console.error(e);
  208. }
  209. }
  210. }
  211. );
  212. } else {
  213. //term_get_dict();
  214. }
  215. }
  216. function note_channal_list() {
  217. console.log("note_channal_list start");
  218. let arrSentInfo = new Array();
  219. $("note").each(function () {
  220. let info = $(this).attr("info");
  221. if (info && info != "") {
  222. arrSentInfo.push({ id: "", data: info });
  223. }
  224. });
  225. if (arrSentInfo.length > 0) {
  226. $.post(
  227. "../term/channal_list.php",
  228. {
  229. setting: "",
  230. data: JSON.stringify(arrSentInfo),
  231. },
  232. function (data, status) {
  233. if (status == "success") {
  234. try {
  235. let active = JSON.parse(data);
  236. _channalData = active;
  237. for (const iterator of _my_channal) {
  238. let found = false;
  239. for (const one of active) {
  240. if (iterator.id == one.id) {
  241. found = true;
  242. break;
  243. }
  244. }
  245. if (found == false) {
  246. _channalData.push(iterator);
  247. }
  248. }
  249. let strHtml = "";
  250. for (const iterator of _channalData) {
  251. if (_channal.indexOf(iterator.id) >= 0) {
  252. strHtml += render_channal_list(iterator);
  253. }
  254. }
  255. for (const iterator of _channalData) {
  256. if (_channal.indexOf(iterator.id) == -1) {
  257. strHtml += render_channal_list(iterator);
  258. }
  259. }
  260. $("#channal_list").html(strHtml);
  261. $("[channal_id]").change(function () {
  262. let channal_list = new Array();
  263. $("[channal_id]").each(function () {
  264. if (this.checked) {
  265. channal_list.push($(this).attr("channal_id"));
  266. }
  267. });
  268. set_channal(channal_list.join());
  269. });
  270. } catch (e) {
  271. console.error(e);
  272. }
  273. }
  274. }
  275. );
  276. }
  277. }
  278. function find_channal(id) {
  279. for (const iterator of _channalData) {
  280. if (id == iterator.id) {
  281. return iterator;
  282. }
  283. }
  284. return false;
  285. }
  286. function render_channal_list(channalinfo) {
  287. let output = "";
  288. output += "<div class='list_with_head'>";
  289. let checked = "";
  290. if (_channal.indexOf(channalinfo.id) >= 0) {
  291. checked = "checked";
  292. }
  293. output += '<div><input type="checkbox" ' + checked + " channal_id='" + channalinfo.id + "'></div>";
  294. output += "<div class='head'>";
  295. output += "<span class='head_img'>";
  296. output += channalinfo.nickname.slice(0, 2);
  297. output += "</span>";
  298. output += "</div>";
  299. output += "<div style='width: 100%;overflow-x: hidden;'>";
  300. output += "<div>";
  301. // output += "<a href='../wiki/wiki.php?word=" + _word;
  302. // output += "&channal=" + channalinfo.id + "' >";
  303. output += "<a onclick=\"set_channal('" + channalinfo.id + "')\">";
  304. output += channalinfo["name"];
  305. output += "</a>";
  306. output += "</div>";
  307. output += "<div>";
  308. output += channalinfo["nickname"] + "/";
  309. output += "@" + channalinfo["username"];
  310. output += "</div>";
  311. if (channalinfo["final"]) {
  312. //进度
  313. output += "<div>";
  314. let article_len = channalinfo["article_len"];
  315. let svg_width = article_len;
  316. let svg_height = parseInt(article_len / 10);
  317. output += '<svg viewBox="0 0 ' + svg_width + " " + svg_height + '" width="100%" >';
  318. let curr_x = 0;
  319. let allFinal = 0;
  320. for (const iterator of channalinfo["final"]) {
  321. let stroke_width = parseInt(iterator.len);
  322. output += "<rect ";
  323. output += ' x="' + curr_x + '"';
  324. output += ' y="0"';
  325. output += ' height="' + svg_height + '"';
  326. output += ' width="' + stroke_width + '"';
  327. if (iterator.final == true) {
  328. allFinal += stroke_width;
  329. output += ' class="progress_bar_done" ';
  330. } else {
  331. output += ' class="progress_bar_undone" ';
  332. }
  333. output += "/>";
  334. curr_x += stroke_width;
  335. }
  336. output +=
  337. "<rect x='0' y='0' width='" + svg_width + "' height='" + svg_height / 5 + "' class='progress_bar_bg' />";
  338. output +=
  339. "<rect x='0' y='0' width='" +
  340. allFinal +
  341. "' height='" +
  342. svg_height / 5 +
  343. "' class='progress_bar_percent' style='stroke-width: 0; fill: rgb(100, 228, 100);'/>";
  344. output += '<text x="0" y="' + svg_height + '" font-size="' + svg_height * 0.8 + '">';
  345. output += channalinfo["count"] + "/" + channalinfo["all"];
  346. output += "</text>";
  347. output += "<svg>";
  348. output += "</div>";
  349. //进度结束
  350. }
  351. output += "</div>";
  352. output += "</div>";
  353. return output;
  354. }
  355. //点击引用 需要响应的事件
  356. function note_ref_init() {
  357. $("chapter").click(function () {
  358. let bookid = $(this).attr("book");
  359. let para = $(this).attr("para");
  360. window.open("../reader/?view=chapter&book=" + bookid + "&para=" + para, "_blank");
  361. });
  362. $("para").click(function () {
  363. let bookid = $(this).attr("book");
  364. let para = $(this).attr("para");
  365. window.open("../reader/?view=para&book=" + bookid + "&para=" + para, "_blank");
  366. });
  367. }
  368. /*
  369. id
  370. palitext
  371. tran
  372. ref
  373. */
  374. function note_json_html(in_json) {
  375. let output = "";
  376. output += '<div class="note_tool_bar" style=" position: relative;">';
  377. output +=
  378. '<div class="case_dropdown note_tool_context" style="position: absolute; right: 0;width:1.5em;text-align: right;">';
  379. output += "<svg class='icon' >";
  380. output += "<use xlink:href='../studio/svg/icon.svg#ic_more'></use>";
  381. output += "</svg>";
  382. output += "<div class='case_dropdown-content sent_menu'>";
  383. if (typeof _reader_view != "undefined" && _reader_view != "sent") {
  384. output += "<a onclick='junp_to(this)'>跳转至此句</a>";
  385. }
  386. output +=
  387. "<a onclick=\"copy_ref('" +
  388. in_json.book +
  389. "','" +
  390. in_json.para +
  391. "','" +
  392. in_json.begin +
  393. "','" +
  394. in_json.end +
  395. "')\">" +
  396. gLocal.gui.copy_link +
  397. "</a>";
  398. output += "<a onclick='copy_text(this)'>" + gLocal.gui.copy + "“" + gLocal.gui.pāli + "”</a>";
  399. output +=
  400. "<a onclick=\"edit_in_studio('" +
  401. in_json.book +
  402. "','" +
  403. in_json.para +
  404. "','" +
  405. in_json.begin +
  406. "','" +
  407. in_json.end +
  408. "')\">" +
  409. gLocal.gui.edit_now +
  410. "</a>";
  411. output += "<a onclick='add_to_list()'>" + gLocal.gui.add_to_edit_list + "</a>";
  412. output += "</div>";
  413. output += "</div>";
  414. output += " </div>";
  415. output += "<div class='palitext'>" + in_json.palitext + "</div>";
  416. for (const iterator of in_json.translation) {
  417. output += "<div class='tran' lang='" + iterator.lang + "'>";
  418. output +=
  419. "<div class='text' id='tran_text_" +
  420. in_json.book +
  421. "_" +
  422. in_json.para +
  423. "_" +
  424. in_json.begin +
  425. "_" +
  426. in_json.end +
  427. "_" +
  428. iterator.channal +
  429. "'>";
  430. if (iterator.text == "") {
  431. output +=
  432. "<span style='color:var(--border-line-color);'>" +
  433. iterator.channalinfo.name +
  434. "-" +
  435. iterator.channalinfo.lang +
  436. "</span>";
  437. } else {
  438. //note_init处理句子链接 marked不处理
  439. //output += marked(term_std_str_to_tran(iterator.text, iterator.channal, iterator.editor, iterator.lang));
  440. output += note_init(term_std_str_to_tran(iterator.text, iterator.channal, iterator.editor, iterator.lang));
  441. }
  442. output += "</div>";
  443. //句子工具栏
  444. output += "<div class='tran_text_tool_bar'>";
  445. output += "<span class = 'tip_buttom' ";
  446. output +=
  447. " onclick=\"note_edit_sentence('" +
  448. in_json.book +
  449. "' ,'" +
  450. in_json.para +
  451. "' ,'" +
  452. in_json.begin +
  453. "' ,'" +
  454. in_json.end +
  455. "' ,'" +
  456. iterator.channal +
  457. "')\"";
  458. output += ">Edit</span>";
  459. output += "<span class = 'tip_buttom'>Like</span>";
  460. output += "<span class = 'tip_buttom'>Comment</span>";
  461. output += "<span class = 'tip_buttom'>Share</span>";
  462. output += "<span class = 'tip_buttom'>Copy</span>";
  463. output += "<span class = 'tip_buttom'>Digest</span>";
  464. output += "<span class = 'tip_buttom'>History</span>";
  465. output += "<span class = 'tip_buttom'>Expand</span>";
  466. output += "</div>";
  467. //句子工具栏结束
  468. output += "</div>";
  469. }
  470. output += "<div class='ref'>" + in_json.ref;
  471. output +=
  472. "<span class='sent_no'>" +
  473. in_json.book +
  474. "-" +
  475. in_json.para +
  476. "-" +
  477. in_json.begin +
  478. "-" +
  479. in_json.end +
  480. "<span>" +
  481. "</div>";
  482. return output;
  483. }
  484. function note_edit_sentence(book, para, begin, end, channal) {
  485. let channalInfo;
  486. for (const iterator of _channalData) {
  487. if (iterator.id == channal) {
  488. channalInfo = iterator;
  489. break;
  490. }
  491. }
  492. for (const iterator of _arrData) {
  493. if (iterator.book == book && iterator.para == para && iterator.begin == begin && iterator.end == end) {
  494. for (const tran of iterator.translation) {
  495. if (tran.channal == channal) {
  496. let html = "";
  497. html += "<div style='color:blue;'>" + channalInfo.nickname + "/" + channalInfo.name + "</div>";
  498. html +=
  499. "<textarea id='edit_dialog_text' sent_id='" +
  500. tran.id +
  501. "' book='" +
  502. iterator.book +
  503. "' para='" +
  504. iterator.para +
  505. "' begin='" +
  506. iterator.begin +
  507. "' end='" +
  508. iterator.end +
  509. "' channal='" +
  510. tran.channal +
  511. "' style='width:100%;min-height:260px;'>" +
  512. tran.text +
  513. "</textarea>";
  514. $("#edit_dialog_content").html(html);
  515. $("#note_sent_edit_dlg").dialog("open");
  516. return;
  517. }
  518. }
  519. }
  520. }
  521. alert("未找到句子");
  522. }
  523. function note_sent_save() {
  524. let id = $("#edit_dialog_text").attr("sent_id");
  525. let book = $("#edit_dialog_text").attr("book");
  526. let para = $("#edit_dialog_text").attr("para");
  527. let begin = $("#edit_dialog_text").attr("begin");
  528. let end = $("#edit_dialog_text").attr("end");
  529. let channal = $("#edit_dialog_text").attr("channal");
  530. let text = $("#edit_dialog_text").val();
  531. $.post(
  532. "../usent/sent_post.php",
  533. {
  534. id: id,
  535. book: book,
  536. para: para,
  537. begin: begin,
  538. end: end,
  539. channal: channal,
  540. text: text,
  541. lang: "zh",
  542. },
  543. function (data) {
  544. let result = JSON.parse(data);
  545. if (result.status > 0) {
  546. alert("error" + result.message);
  547. } else {
  548. ntf_show("success");
  549. if (result.text == "") {
  550. let channel_info = "Empty";
  551. let thisChannel = find_channal(result.channal);
  552. if (thisChannel) {
  553. channel_info = thisChannel.name + "-" + thisChannel.nickname;
  554. }
  555. $(
  556. "#tran_text_" +
  557. result.book +
  558. "_" +
  559. result.para +
  560. "_" +
  561. result.begin +
  562. "_" +
  563. result.end +
  564. "_" +
  565. result.channal
  566. ).html("<span style='color:var(--border-line-color);'>" + channel_info + "</span>");
  567. } else {
  568. $(
  569. "#tran_text_" +
  570. result.book +
  571. "_" +
  572. result.para +
  573. "_" +
  574. result.begin +
  575. "_" +
  576. result.end +
  577. "_" +
  578. result.channal
  579. ).html(marked(term_std_str_to_tran(result.text, result.channal, result.editor, result.lang)));
  580. term_updata_translation();
  581. for (const iterator of _arrData) {
  582. if (
  583. iterator.book == result.book &&
  584. iterator.para == result.para &&
  585. iterator.begin == result.begin &&
  586. iterator.end == result.end
  587. ) {
  588. for (const tran of iterator.translation) {
  589. if (tran.channal == result.channal) {
  590. tran.text = result.text;
  591. break;
  592. }
  593. }
  594. }
  595. }
  596. }
  597. }
  598. }
  599. );
  600. }
  601. function copy_ref(book, para, begin, end) {
  602. let strRef = "{{" + book + "-" + para + "-" + begin + "-" + end + "}}";
  603. copy_to_clipboard(strRef);
  604. }
  605. function edit_in_studio(book, para, begin, end) {
  606. wbw_channal_list_open(book, [para]);
  607. }