note.js 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637
  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. /*
  188. $(".palitext").click(function () {
  189. let sentid = $(this).parent().attr("info").split("-");
  190. window.open(
  191. "../reader/?view=sent&book=" +
  192. sentid[0] +
  193. "&para=" +
  194. sentid[1] +
  195. "&begin=" +
  196. sentid[2] +
  197. "&end=" +
  198. sentid[3]
  199. );
  200. });
  201. $("pali").click(function () {
  202. window.open(
  203. "../reader/?view=sent&book=" +
  204. $(this).attr("book") +
  205. "&para=" +
  206. $(this).attr("para") +
  207. "&begin=" +
  208. $(this).attr("begin") +
  209. "&end=" +
  210. $(this).attr("end")
  211. );
  212. });
  213. */
  214. note_ref_init();
  215. term_get_dict();
  216. note_channal_list();
  217. } catch (e) {
  218. console.error(e);
  219. }
  220. }
  221. }
  222. );
  223. }
  224. }
  225. function note_channal_list() {
  226. console.log("note_channal_list start");
  227. let arrSentInfo = new Array();
  228. $("note").each(function () {
  229. let info = $(this).attr("info");
  230. if (info && info != "") {
  231. arrSentInfo.push({ id: "", data: info });
  232. }
  233. });
  234. if (arrSentInfo.length > 0) {
  235. $.post(
  236. "../term/channal_list.php",
  237. {
  238. setting: "",
  239. data: JSON.stringify(arrSentInfo),
  240. },
  241. function (data, status) {
  242. if (status == "success") {
  243. try {
  244. let active = JSON.parse(data);
  245. _channalData = active;
  246. for (const iterator of _my_channal) {
  247. let found = false;
  248. for (const one of active) {
  249. if (iterator.id == one.id) {
  250. found = true;
  251. break;
  252. }
  253. }
  254. if (found == false) {
  255. _channalData.push(iterator);
  256. }
  257. }
  258. let strHtml = "";
  259. for (const iterator of _channalData) {
  260. if (_channal.indexOf(iterator.id) >= 0) {
  261. strHtml += render_channal_list(iterator);
  262. }
  263. }
  264. for (const iterator of _channalData) {
  265. if (_channal.indexOf(iterator.id) == -1) {
  266. strHtml += render_channal_list(iterator);
  267. }
  268. }
  269. $("#channal_list").html(strHtml);
  270. $("[channal_id]").change(function () {
  271. let channal_list = new Array();
  272. $("[channal_id]").each(function () {
  273. if (this.checked) {
  274. channal_list.push($(this).attr("channal_id"));
  275. }
  276. });
  277. set_channal(channal_list.join());
  278. });
  279. } catch (e) {
  280. console.error(e);
  281. }
  282. }
  283. }
  284. );
  285. }
  286. }
  287. function find_channal(id) {
  288. for (const iterator of _channalData) {
  289. if (id == iterator.id) {
  290. return iterator;
  291. }
  292. }
  293. return false;
  294. }
  295. function render_channal_list(channalinfo) {
  296. let output = "";
  297. output += "<div class='list_with_head'>";
  298. let checked = "";
  299. if (_channal.indexOf(channalinfo.id) >= 0) {
  300. checked = "checked";
  301. }
  302. output +=
  303. '<div><input type="checkbox" ' +
  304. checked +
  305. " channal_id='" +
  306. channalinfo.id +
  307. "'></div>";
  308. output += "<div class='head'>";
  309. output += "<span class='head_img'>";
  310. output += channalinfo.nickname.slice(0, 2);
  311. output += "</span>";
  312. output += "</div>";
  313. output += "<div style='width: 100%;'>";
  314. output += "<div>";
  315. // output += "<a href='../wiki/wiki.php?word=" + _word;
  316. // output += "&channal=" + channalinfo.id + "' >";
  317. output += "<a onclick=\"set_channal('" + channalinfo.id + "')\">";
  318. output += channalinfo["name"];
  319. output += "</a>";
  320. output += "</div>";
  321. output += "<div>";
  322. output += channalinfo["nickname"] + "/";
  323. output += "@" + channalinfo["username"];
  324. output += "</div>";
  325. if (channalinfo["final"]) {
  326. //进度
  327. output += "<div>";
  328. let article_len = channalinfo["article_len"];
  329. let svg_width = article_len;
  330. let svg_height = parseInt(article_len / 10);
  331. output +=
  332. '<svg viewBox="0 0 ' + svg_width + " " + svg_height + '" width="100%" >';
  333. let curr_x = 0;
  334. let allFinal = 0;
  335. for (const iterator of channalinfo["final"]) {
  336. let stroke_width = parseInt(iterator.len);
  337. output += "<rect ";
  338. output += ' x="' + curr_x + '"';
  339. output += ' y="0"';
  340. output += ' height="' + svg_height + '"';
  341. output += ' width="' + stroke_width + '"';
  342. if (iterator.final == true) {
  343. allFinal += stroke_width;
  344. output += ' class="progress_bar_done" ';
  345. } else {
  346. output += ' class="progress_bar_undone" ';
  347. }
  348. output += "/>";
  349. curr_x += stroke_width;
  350. }
  351. output +=
  352. "<rect x='0' y='0' width='" +
  353. svg_width +
  354. "' height='" +
  355. svg_height / 5 +
  356. "' class='progress_bar_bg' />";
  357. output +=
  358. "<rect x='0' y='0' width='" +
  359. allFinal +
  360. "' height='" +
  361. svg_height / 5 +
  362. "' class='progress_bar_percent' style='stroke-width: 0; fill: rgb(100, 228, 100);'/>";
  363. output +=
  364. '<text x="0" y="' +
  365. svg_height +
  366. '" font-size="' +
  367. svg_height * 0.8 +
  368. '">';
  369. output += channalinfo["count"] + "/" + channalinfo["all"];
  370. output += "</text>";
  371. output += "<svg>";
  372. output += "</div>";
  373. //进度结束
  374. }
  375. output += "</div>";
  376. output += "</div>";
  377. return output;
  378. }
  379. //点击引用 需要响应的事件
  380. function note_ref_init() {
  381. $("chapter").click(function () {
  382. let bookid = $(this).attr("book");
  383. let para = $(this).attr("para");
  384. window.open(
  385. "../reader/?view=chapter&book=" + bookid + "&para=" + para,
  386. "_blank"
  387. );
  388. });
  389. $("para").click(function () {
  390. let bookid = $(this).attr("book");
  391. let para = $(this).attr("para");
  392. window.open(
  393. "../reader/?view=para&book=" + bookid + "&para=" + para,
  394. "_blank"
  395. );
  396. });
  397. }
  398. /*
  399. id
  400. palitext
  401. tran
  402. ref
  403. */
  404. function note_json_html(in_json) {
  405. let output = "";
  406. output +='<div class="note_tool_bar" style=" position: relative;">';
  407. output +='<div class="case_dropdown" style="position: absolute; right: 0;width:1.5em;">';
  408. output +="<svg class='icon' >";
  409. output +="<use xlink:href='../studio/svg/icon.svg#ic_more'></use>";
  410. output +="</svg>";
  411. output +="<div class='case_dropdown-content sent_menu'>";
  412. if(typeof _reader_view !="undefined" && _reader_view != "sent"){
  413. output +="<a onclick='junp_to(this)'>跳转至此句</a>";
  414. }
  415. output +="<a onclick=\"copy_ref('"+in_json.book+"','"+in_json.para+"','"+in_json.begin+"','"+in_json.end+"')\">复制引用</a>";
  416. output +="<a onclick='copy_text(this)'>复制文本</a>";
  417. output +="<a onclick='add_to_list()'>添加到选择列表</a>";
  418. output +="</div>";
  419. output +='</div>';
  420. output +=' </div>';
  421. output += "<div class='palitext'>" + in_json.palitext + "</div>";
  422. for (const iterator of in_json.translation) {
  423. output += "<div class='tran' lang='" + iterator.lang + "'>";
  424. output +=
  425. "<span class='edit_button' onclick=\"note_edit_sentence('" +
  426. in_json.book +
  427. "' ,'" +
  428. in_json.para +
  429. "' ,'" +
  430. in_json.begin +
  431. "' ,'" +
  432. in_json.end +
  433. "' ,'" +
  434. iterator.channal +
  435. "')\"></span>";
  436. output +=
  437. "<div class='text' id='tran_text_" +
  438. in_json.book +
  439. "_" +
  440. in_json.para +
  441. "_" +
  442. in_json.begin +
  443. "_" +
  444. in_json.end +
  445. "_" +
  446. iterator.channal +
  447. "'>";
  448. if (iterator.text == "") {
  449. //let channal = find_channal(iterator.channal);
  450. output += "<span style='color:var(--border-line-color);'></span>";
  451. output +=
  452. "<span style='color:var(--border-line-color);'>" +
  453. iterator.channalinfo.name +
  454. "-" +
  455. iterator.channalinfo.lang +
  456. "</span>";
  457. } else {
  458. output += marked(
  459. term_std_str_to_tran(
  460. iterator.text,
  461. iterator.channal,
  462. iterator.editor,
  463. iterator.lang
  464. )
  465. );
  466. }
  467. output += "</div>";
  468. output += "</div>";
  469. }
  470. output += "<div class='ref'>" + in_json.ref;
  471. output +=
  472. "<span class='sent_no'>" +
  473. in_json.book +
  474. "-" +
  475. in_json.para +
  476. "-" +
  477. in_json.begin +
  478. "-" +
  479. in_json.end +
  480. "<span>" +
  481. "</div>";
  482. return output;
  483. }
  484. function note_edit_sentence(book, para, begin, end, channal) {
  485. let channalInfo;
  486. for (const iterator of _channalData) {
  487. if (iterator.id == channal) {
  488. channalInfo = iterator;
  489. break;
  490. }
  491. }
  492. for (const iterator of _arrData) {
  493. if (
  494. iterator.book == book &&
  495. iterator.para == para &&
  496. iterator.begin == begin &&
  497. iterator.end == end
  498. ) {
  499. for (const tran of iterator.translation) {
  500. if (tran.channal == channal) {
  501. let html = "";
  502. html +=
  503. "<div style='color:blue;'>" +
  504. channalInfo.nickname +
  505. "/" +
  506. channalInfo.name +
  507. "</div>";
  508. html +=
  509. "<textarea id='edit_dialog_text' sent_id='" +
  510. tran.id +
  511. "' book='" +
  512. iterator.book +
  513. "' para='" +
  514. iterator.para +
  515. "' begin='" +
  516. iterator.begin +
  517. "' end='" +
  518. iterator.end +
  519. "' channal='" +
  520. tran.channal +
  521. "' style='width:100%;min-height:260px;'>" +
  522. tran.text +
  523. "</textarea>";
  524. $("#edit_dialog_content").html(html);
  525. break;
  526. }
  527. }
  528. }
  529. }
  530. $("#dialog").dialog("open");
  531. }
  532. function note_sent_save() {
  533. let id = $("#edit_dialog_text").attr("sent_id");
  534. let book = $("#edit_dialog_text").attr("book");
  535. let para = $("#edit_dialog_text").attr("para");
  536. let begin = $("#edit_dialog_text").attr("begin");
  537. let end = $("#edit_dialog_text").attr("end");
  538. let channal = $("#edit_dialog_text").attr("channal");
  539. let text = $("#edit_dialog_text").val();
  540. $.post(
  541. "../usent/sent_post.php",
  542. {
  543. id: id,
  544. book: book,
  545. para: para,
  546. begin: begin,
  547. end: end,
  548. channal: channal,
  549. text: text,
  550. lang: "zh",
  551. },
  552. function (data) {
  553. let result = JSON.parse(data);
  554. if (result.status > 0) {
  555. alert("error" + result.message);
  556. } else {
  557. ntf_show("success");
  558. $(
  559. "#tran_text_" +
  560. result.book +
  561. "_" +
  562. result.para +
  563. "_" +
  564. result.begin +
  565. "_" +
  566. result.end +
  567. "_" +
  568. result.channal
  569. ).html(
  570. marked(
  571. term_std_str_to_tran(
  572. result.text,
  573. result.channal,
  574. result.editor,
  575. result.lang
  576. )
  577. )
  578. );
  579. term_updata_translation();
  580. for (const iterator of _arrData) {
  581. if (
  582. iterator.book == result.book &&
  583. iterator.para == result.para &&
  584. iterator.begin == result.begin &&
  585. iterator.end == result.end
  586. ) {
  587. for (const tran of iterator.translation) {
  588. if (tran.channal == result.channal) {
  589. tran.text = result.text;
  590. break;
  591. }
  592. }
  593. }
  594. }
  595. }
  596. }
  597. );
  598. }
  599. function copy_ref(book,para,begin,end) {
  600. let strRef = "{{" + book + "-" + para + "-" + begin + "-" + end + "}}";
  601. copy_to_clipboard(strRef);
  602. }