note.js 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  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 (
  132. let index = arrChannal.length - 1;
  133. index >= 0;
  134. index--
  135. ) {
  136. const iChannal = arrChannal[index];
  137. $("#" + id)
  138. .parent()
  139. .prepend(
  140. "<div class='tran_div' channal='" +
  141. iChannal +
  142. "'></div>"
  143. );
  144. }
  145. }
  146. $("#" + id)
  147. .parent()
  148. .prepend("<div class='palitext'></div>");
  149. }
  150. $("#" + id)
  151. .parent()
  152. .children(".palitext")
  153. .first()
  154. .append(strPalitext);
  155. let htmlTran = "";
  156. for (const oneTran of iterator.translation) {
  157. let html =
  158. "<span class='tran' lang='" +
  159. oneTran.lang +
  160. "' channal='" +
  161. oneTran.channal +
  162. "'>" +
  163. marked(
  164. term_std_str_to_tran(
  165. oneTran.text,
  166. oneTran.channal,
  167. oneTran.editor,
  168. oneTran.lang
  169. )
  170. ) +
  171. "</span>";
  172. if (_channal == "") {
  173. htmlTran += html;
  174. } else {
  175. $("#" + id)
  176. .siblings(".tran_div[channal='" + oneTran.channal + "']")
  177. .append(html);
  178. }
  179. }
  180. $("#" + id).html(htmlTran);
  181. } else {
  182. //句子模式
  183. strHtml += note_json_html(iterator);
  184. $("#" + id).html(strHtml);
  185. }
  186. }
  187. $(".palitext").click(function () {
  188. let sentid = $(this).parent().attr("info").split("-");
  189. window.open(
  190. "../pcdl/reader.php?view=sent&book=" +
  191. sentid[0] +
  192. "&para=" +
  193. sentid[1] +
  194. "&begin=" +
  195. sentid[2] +
  196. "&end=" +
  197. sentid[3]
  198. );
  199. });
  200. $("pali").click(function () {
  201. window.open(
  202. "../pcdl/reader.php?view=sent&book=" +
  203. $(this).attr("book") +
  204. "&para=" +
  205. $(this).attr("para") +
  206. "&begin=" +
  207. $(this).attr("begin") +
  208. "&end=" +
  209. $(this).attr("end")
  210. );
  211. });
  212. note_ref_init();
  213. term_get_dict();
  214. note_channal_list();
  215. } catch (e) {
  216. console.error(e);
  217. }
  218. }
  219. }
  220. );
  221. }
  222. }
  223. function note_channal_list() {
  224. console.log("note_channal_list start");
  225. let arrSentInfo = new Array();
  226. $("note").each(function () {
  227. let info = $(this).attr("info");
  228. if (info && info != "") {
  229. arrSentInfo.push({ id: "", data: info });
  230. }
  231. });
  232. if (arrSentInfo.length > 0) {
  233. $.post(
  234. "../term/channal_list.php",
  235. {
  236. setting: "",
  237. data: JSON.stringify(arrSentInfo),
  238. },
  239. function (data, status) {
  240. if (status == "success") {
  241. try {
  242. let active = JSON.parse(data);
  243. _channalData = active;
  244. for (const iterator of _my_channal) {
  245. let found = false;
  246. for (const one of active) {
  247. if (iterator.id == one.id) {
  248. found = true;
  249. break;
  250. }
  251. }
  252. if (found == false) {
  253. _channalData.push(iterator);
  254. }
  255. }
  256. let strHtml = "";
  257. for (const iterator of _channalData) {
  258. if (_channal.indexOf(iterator.id) >= 0) {
  259. strHtml += render_channal_list(iterator);
  260. }
  261. }
  262. for (const iterator of _channalData) {
  263. if (_channal.indexOf(iterator.id) == -1) {
  264. strHtml += render_channal_list(iterator);
  265. }
  266. }
  267. $("#channal_list").html(strHtml);
  268. $("[channal_id]").change(function () {
  269. let channal_list = new Array();
  270. $("[channal_id]").each(function () {
  271. if (this.checked) {
  272. channal_list.push($(this).attr("channal_id"));
  273. }
  274. });
  275. set_channal(channal_list.join());
  276. });
  277. } catch (e) {
  278. console.error(e);
  279. }
  280. }
  281. }
  282. );
  283. }
  284. }
  285. function find_channal(id) {
  286. for (const iterator of _channalData) {
  287. if (id == iterator.id) {
  288. return iterator;
  289. }
  290. }
  291. return false;
  292. }
  293. function render_channal_list(channalinfo) {
  294. let output = "";
  295. output += "<div class='list_with_head'>";
  296. let checked = "";
  297. if (_channal.indexOf(channalinfo.id) >= 0) {
  298. checked = "checked";
  299. }
  300. output +=
  301. '<div><input type="checkbox" ' +
  302. checked +
  303. " channal_id='" +
  304. channalinfo.id +
  305. "'></div>";
  306. output += "<div class='head'>";
  307. output += "<span class='head_img'>";
  308. output += channalinfo.nickname.slice(0, 2);
  309. output += "</span>";
  310. output += "</div>";
  311. output += "<div style='width: 100%;'>";
  312. output += "<div>";
  313. // output += "<a href='../wiki/wiki.php?word=" + _word;
  314. // output += "&channal=" + channalinfo.id + "' >";
  315. output += "<a onclick=\"set_channal('" + channalinfo.id + "')\">";
  316. output += channalinfo["name"];
  317. output += "</a>";
  318. output += "</div>";
  319. output += "<div>";
  320. output += channalinfo["nickname"] + "/";
  321. output += "@" + channalinfo["username"];
  322. output += "</div>";
  323. if (channalinfo["final"]) {
  324. //进度
  325. output += "<div>";
  326. let article_len = channalinfo["article_len"];
  327. let svg_width = article_len;
  328. let svg_height = parseInt(article_len / 10);
  329. output +=
  330. '<svg viewBox="0 0 ' + svg_width + " " + svg_height + '" width="100%" >';
  331. let curr_x = 0;
  332. let allFinal = 0;
  333. for (const iterator of channalinfo["final"]) {
  334. let stroke_width = parseInt(iterator.len);
  335. output += "<rect ";
  336. output += ' x="' + curr_x + '"';
  337. output += ' y="0"';
  338. output += ' height="' + svg_height + '"';
  339. output += ' width="' + stroke_width + '"';
  340. if (iterator.final == true) {
  341. allFinal += stroke_width;
  342. output += ' style="stroke-width: 0; fill: rgb(0, 128, 6);"';
  343. } else {
  344. output += ' style="stroke-width: 0; fill: rgb(230, 230, 230);"';
  345. }
  346. output += "/>";
  347. curr_x += stroke_width;
  348. }
  349. output +=
  350. "<rect x='0' y='0' width='" +
  351. svg_width +
  352. "' height='" +
  353. svg_height / 5 +
  354. "' style='stroke-width: 0; fill: rgb(230, 230, 230);'/>";
  355. output +=
  356. "<rect x='0' y='0' width='" +
  357. allFinal +
  358. "' height='" +
  359. svg_height / 5 +
  360. "' style='stroke-width: 0; fill: rgb(100, 228, 100);'/>";
  361. output +=
  362. '<text x="0" y="' +
  363. svg_height +
  364. '" font-size="' +
  365. svg_height * 0.8 +
  366. '">';
  367. output += channalinfo["count"] + "/" + channalinfo["all"];
  368. output += "</text>";
  369. output += "<svg>";
  370. output += "</div>";
  371. //进度结束
  372. }
  373. output += "</div>";
  374. output += "</div>";
  375. return output;
  376. }
  377. //点击引用 需要响应的事件
  378. function note_ref_init() {
  379. $("chapter").click(function () {
  380. let bookid = $(this).attr("book");
  381. let para = $(this).attr("para");
  382. window.open(
  383. "../pcdl/reader.php?view=chapter&book=" + bookid + "&para=" + para,
  384. "_blank"
  385. );
  386. });
  387. $("para").click(function () {
  388. let bookid = $(this).attr("book");
  389. let para = $(this).attr("para");
  390. window.open(
  391. "../pcdl/reader.php?view=para&book=" + bookid + "&para=" + para,
  392. "_blank"
  393. );
  394. });
  395. }
  396. /*
  397. id
  398. palitext
  399. tran
  400. ref
  401. */
  402. function note_json_html(in_json) {
  403. let output = "";
  404. output += "<div class='palitext'>" + in_json.palitext + "</div>";
  405. for (const iterator of in_json.translation) {
  406. output += "<div class='tran' lang='" + iterator.lang + "'>";
  407. output +=
  408. "<span class='edit_button' onclick=\"note_edit_sentence('" +
  409. in_json.book +
  410. "' ,'" +
  411. in_json.para +
  412. "' ,'" +
  413. in_json.begin +
  414. "' ,'" +
  415. in_json.end +
  416. "' ,'" +
  417. iterator.channal +
  418. "')\"></span>";
  419. output +=
  420. "<div class='text' id='tran_text_" +
  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. if (iterator.text == "") {
  432. //let channal = find_channal(iterator.channal);
  433. output += "<span style='color:var(--border-line-color);'></span>";
  434. output +=
  435. "<span style='color:var(--border-line-color);'>" +
  436. iterator.channalinfo.name +
  437. "-" +
  438. iterator.channalinfo.lang +
  439. "</span>";
  440. } else {
  441. output += marked(
  442. term_std_str_to_tran(
  443. iterator.text,
  444. iterator.channal,
  445. iterator.editor,
  446. iterator.lang
  447. )
  448. );
  449. }
  450. output += "</div>";
  451. output += "</div>";
  452. }
  453. output += "<div class='ref'>" + in_json.ref;
  454. output +=
  455. "<span class='sent_no'>" +
  456. in_json.book +
  457. "-" +
  458. in_json.para +
  459. "-" +
  460. in_json.begin +
  461. "-" +
  462. in_json.end +
  463. "<span>" +
  464. "</div>";
  465. return output;
  466. }
  467. function note_edit_sentence(book, para, begin, end, channal) {
  468. let channalInfo;
  469. for (const iterator of _channalData) {
  470. if (iterator.id == channal) {
  471. channalInfo = iterator;
  472. break;
  473. }
  474. }
  475. for (const iterator of _arrData) {
  476. if (
  477. iterator.book == book &&
  478. iterator.para == para &&
  479. iterator.begin == begin &&
  480. iterator.end == end
  481. ) {
  482. for (const tran of iterator.translation) {
  483. if (tran.channal == channal) {
  484. let html = "";
  485. html +=
  486. "<div style='color:blue;'>" +
  487. channalInfo.nickname +
  488. "/" +
  489. channalInfo.name +
  490. "</div>";
  491. html +=
  492. "<textarea id='edit_dialog_text' sent_id='" +
  493. tran.id +
  494. "' book='" +
  495. iterator.book +
  496. "' para='" +
  497. iterator.para +
  498. "' begin='" +
  499. iterator.begin +
  500. "' end='" +
  501. iterator.end +
  502. "' channal='" +
  503. tran.channal +
  504. "' style='width:100%;min-height:260px;'>" +
  505. tran.text +
  506. "</textarea>";
  507. $("#edit_dialog_content").html(html);
  508. break;
  509. }
  510. }
  511. }
  512. }
  513. $("#dialog").dialog("open");
  514. }
  515. function note_sent_save() {
  516. let id = $("#edit_dialog_text").attr("sent_id");
  517. let book = $("#edit_dialog_text").attr("book");
  518. let para = $("#edit_dialog_text").attr("para");
  519. let begin = $("#edit_dialog_text").attr("begin");
  520. let end = $("#edit_dialog_text").attr("end");
  521. let channal = $("#edit_dialog_text").attr("channal");
  522. let text = $("#edit_dialog_text").val();
  523. $.post(
  524. "../usent/sent_post.php",
  525. {
  526. id: id,
  527. book: book,
  528. para: para,
  529. begin: begin,
  530. end: end,
  531. channal: channal,
  532. text: text,
  533. lang: "zh",
  534. },
  535. function (data) {
  536. let result = JSON.parse(data);
  537. if (result.status > 0) {
  538. alert("error" + result.message);
  539. } else {
  540. ntf_show("success");
  541. $(
  542. "#tran_text_" +
  543. result.book +
  544. "_" +
  545. result.para +
  546. "_" +
  547. result.begin +
  548. "_" +
  549. result.end +
  550. "_" +
  551. result.channal
  552. ).html(
  553. marked(
  554. term_std_str_to_tran(
  555. result.text,
  556. result.channal,
  557. result.editor,
  558. result.lang
  559. )
  560. )
  561. );
  562. term_updata_translation();
  563. for (const iterator of _arrData) {
  564. if (
  565. iterator.book == result.book &&
  566. iterator.para == result.para &&
  567. iterator.begin == result.begin &&
  568. iterator.end == result.end
  569. ) {
  570. for (const tran of iterator.translation) {
  571. if (tran.channal == result.channal) {
  572. tran.text = result.text;
  573. break;
  574. }
  575. }
  576. }
  577. }
  578. }
  579. }
  580. );
  581. }