note.js 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615
  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 output = "<div>";
  58. let newString = input.replace(/\{\{/g, '<note info="');
  59. newString = newString.replace(/\}\}/g, '"></note>');
  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 += '<div class="case_dropdown" style="position: absolute; right: 0;width:1.5em;">';
  378. output += "<svg class='icon' >";
  379. output += "<use xlink:href='../studio/svg/icon.svg#ic_more'></use>";
  380. output += "</svg>";
  381. output += "<div class='case_dropdown-content sent_menu'>";
  382. if (typeof _reader_view != "undefined" && _reader_view != "sent") {
  383. output += "<a onclick='junp_to(this)'>跳转至此句</a>";
  384. }
  385. output +=
  386. "<a onclick=\"copy_ref('" +
  387. in_json.book +
  388. "','" +
  389. in_json.para +
  390. "','" +
  391. in_json.begin +
  392. "','" +
  393. in_json.end +
  394. "')\">" +
  395. gLocal.gui.copy_link +
  396. "</a>";
  397. output += "<a onclick='copy_text(this)'>" + gLocal.gui.copy + "“" + gLocal.gui.pāli + "”</a>";
  398. output +=
  399. "<a onclick=\"edit_in_studio('" +
  400. in_json.book +
  401. "','" +
  402. in_json.para +
  403. "','" +
  404. in_json.begin +
  405. "','" +
  406. in_json.end +
  407. "')\">" +
  408. gLocal.gui.edit_now +
  409. "</a>";
  410. output += "<a onclick='add_to_list()'>" + gLocal.gui.add_to_edit_list + "</a>";
  411. output += "</div>";
  412. output += "</div>";
  413. output += " </div>";
  414. output += "<div class='palitext'>" + in_json.palitext + "</div>";
  415. for (const iterator of in_json.translation) {
  416. output += "<div class='tran' lang='" + iterator.lang + "'";
  417. output += ">";
  418. output += "<span class='edit_button' ";
  419. output +=
  420. " onclick=\"note_edit_sentence('" +
  421. in_json.book +
  422. "' ,'" +
  423. in_json.para +
  424. "' ,'" +
  425. in_json.begin +
  426. "' ,'" +
  427. in_json.end +
  428. "' ,'" +
  429. iterator.channal +
  430. "')\"";
  431. output += "></span>";
  432. output +=
  433. "<div class='text' id='tran_text_" +
  434. in_json.book +
  435. "_" +
  436. in_json.para +
  437. "_" +
  438. in_json.begin +
  439. "_" +
  440. in_json.end +
  441. "_" +
  442. iterator.channal +
  443. "'>";
  444. if (iterator.text == "") {
  445. //let channal = find_channal(iterator.channal);
  446. output += "<span style='color:var(--border-line-color);'></span>";
  447. output +=
  448. "<span style='color:var(--border-line-color);'>" +
  449. iterator.channalinfo.name +
  450. "-" +
  451. iterator.channalinfo.lang +
  452. "</span>";
  453. } else {
  454. //note_init处理句子链接 marked不处理
  455. //output += marked(term_std_str_to_tran(iterator.text, iterator.channal, iterator.editor, iterator.lang));
  456. output += note_init(term_std_str_to_tran(iterator.text, iterator.channal, iterator.editor, iterator.lang));
  457. }
  458. output += "</div>";
  459. output += "</div>";
  460. }
  461. output += "<div class='ref'>" + in_json.ref;
  462. output +=
  463. "<span class='sent_no'>" +
  464. in_json.book +
  465. "-" +
  466. in_json.para +
  467. "-" +
  468. in_json.begin +
  469. "-" +
  470. in_json.end +
  471. "<span>" +
  472. "</div>";
  473. return output;
  474. }
  475. function note_edit_sentence(book, para, begin, end, channal) {
  476. let channalInfo;
  477. for (const iterator of _channalData) {
  478. if (iterator.id == channal) {
  479. channalInfo = iterator;
  480. break;
  481. }
  482. }
  483. for (const iterator of _arrData) {
  484. if (iterator.book == book && iterator.para == para && iterator.begin == begin && iterator.end == end) {
  485. for (const tran of iterator.translation) {
  486. if (tran.channal == channal) {
  487. let html = "";
  488. html += "<div style='color:blue;'>" + channalInfo.nickname + "/" + channalInfo.name + "</div>";
  489. html +=
  490. "<textarea id='edit_dialog_text' sent_id='" +
  491. tran.id +
  492. "' book='" +
  493. iterator.book +
  494. "' para='" +
  495. iterator.para +
  496. "' begin='" +
  497. iterator.begin +
  498. "' end='" +
  499. iterator.end +
  500. "' channal='" +
  501. tran.channal +
  502. "' style='width:100%;min-height:260px;'>" +
  503. tran.text +
  504. "</textarea>";
  505. $("#edit_dialog_content").html(html);
  506. $("#note_sent_edit_dlg").dialog("open");
  507. return;
  508. }
  509. }
  510. }
  511. }
  512. alert("未找到句子");
  513. }
  514. function note_sent_save() {
  515. let id = $("#edit_dialog_text").attr("sent_id");
  516. let book = $("#edit_dialog_text").attr("book");
  517. let para = $("#edit_dialog_text").attr("para");
  518. let begin = $("#edit_dialog_text").attr("begin");
  519. let end = $("#edit_dialog_text").attr("end");
  520. let channal = $("#edit_dialog_text").attr("channal");
  521. let text = $("#edit_dialog_text").val();
  522. $.post(
  523. "../usent/sent_post.php",
  524. {
  525. id: id,
  526. book: book,
  527. para: para,
  528. begin: begin,
  529. end: end,
  530. channal: channal,
  531. text: text,
  532. lang: "zh",
  533. },
  534. function (data) {
  535. let result = JSON.parse(data);
  536. if (result.status > 0) {
  537. alert("error" + result.message);
  538. } else {
  539. ntf_show("success");
  540. $(
  541. "#tran_text_" +
  542. result.book +
  543. "_" +
  544. result.para +
  545. "_" +
  546. result.begin +
  547. "_" +
  548. result.end +
  549. "_" +
  550. result.channal
  551. ).html(marked(term_std_str_to_tran(result.text, result.channal, result.editor, result.lang)));
  552. term_updata_translation();
  553. for (const iterator of _arrData) {
  554. if (
  555. iterator.book == result.book &&
  556. iterator.para == result.para &&
  557. iterator.begin == result.begin &&
  558. iterator.end == result.end
  559. ) {
  560. for (const tran of iterator.translation) {
  561. if (tran.channal == result.channal) {
  562. tran.text = result.text;
  563. break;
  564. }
  565. }
  566. }
  567. }
  568. }
  569. }
  570. );
  571. }
  572. function copy_ref(book, para, begin, end) {
  573. let strRef = "{{" + book + "-" + para + "-" + begin + "-" + end + "}}";
  574. copy_to_clipboard(strRef);
  575. }
  576. function edit_in_studio(book, para, begin, end) {
  577. wbw_channal_list_open(book, [para]);
  578. }