note.js 14 KB

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