note.js 16 KB

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