note.js 14 KB

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