note.js 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511
  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. $("#" + id)
  130. .parent()
  131. .prepend("<div class='palitext'></div>");
  132. }
  133. $("#" + id)
  134. .parent()
  135. .children(".palitext")
  136. .first()
  137. .append(strPalitext);
  138. let htmlTran = "";
  139. for (const oneTran of iterator.translation) {
  140. htmlTran +=
  141. "<span class='tran'>" +
  142. marked(term_std_str_to_tran(oneTran.text)) +
  143. "</span>";
  144. }
  145. $("#" + id).html(htmlTran);
  146. } else {
  147. //句子模式
  148. strHtml += note_json_html(iterator);
  149. $("#" + id).html(strHtml);
  150. }
  151. }
  152. $(".palitext").click(function () {
  153. let sentid = $(this).parent().attr("info").split("-");
  154. window.open(
  155. "../pcdl/reader.php?view=sent&book=" +
  156. sentid[0] +
  157. "&para=" +
  158. sentid[1] +
  159. "&begin=" +
  160. sentid[2] +
  161. "&end=" +
  162. sentid[3]
  163. );
  164. });
  165. $("pali").click(function () {
  166. window.open(
  167. "../pcdl/reader.php?view=sent&book=" +
  168. $(this).attr("book") +
  169. "&para=" +
  170. $(this).attr("para") +
  171. "&begin=" +
  172. $(this).attr("begin") +
  173. "&end=" +
  174. $(this).attr("end")
  175. );
  176. });
  177. note_ref_init();
  178. term_get_dict();
  179. note_channal_list();
  180. } catch (e) {
  181. console.error(e);
  182. }
  183. }
  184. }
  185. );
  186. }
  187. }
  188. function note_channal_list() {
  189. console.log("note_channal_list start");
  190. let objNotes = document.querySelectorAll("note");
  191. let arrSentInfo = new Array();
  192. for (const iterator of objNotes) {
  193. {
  194. let info = iterator.getAttributeNode("info").value;
  195. if (info && info != "") {
  196. arrSentInfo.push({ id: "", data: info });
  197. }
  198. }
  199. }
  200. if (arrSentInfo.length > 0) {
  201. $.post(
  202. "../term/channal_list.php",
  203. {
  204. setting: "",
  205. data: JSON.stringify(arrSentInfo),
  206. },
  207. function (data, status) {
  208. if (status == "success") {
  209. try {
  210. let active = JSON.parse(data);
  211. _channalData = active;
  212. for (const iterator of _my_channal) {
  213. let found = false;
  214. for (const one of active) {
  215. if (iterator.id == one.id) {
  216. found = true;
  217. break;
  218. }
  219. }
  220. if (found == false) {
  221. _channalData.push(iterator);
  222. }
  223. }
  224. let strHtml = "";
  225. for (const iterator of _channalData) {
  226. if (_channal.indexOf(iterator.id) >= 0) {
  227. strHtml += render_channal_list(iterator);
  228. }
  229. }
  230. for (const iterator of _channalData) {
  231. if (_channal.indexOf(iterator.id) == -1) {
  232. strHtml += render_channal_list(iterator);
  233. }
  234. }
  235. $("#channal_list").html(strHtml);
  236. $("[channal_id]").change(function () {
  237. let channal_list = new Array();
  238. $("[channal_id]").each(function () {
  239. if (this.checked) {
  240. channal_list.push($(this).attr("channal_id"));
  241. }
  242. });
  243. set_channal(channal_list.join());
  244. });
  245. } catch (e) {
  246. console.error(e);
  247. }
  248. }
  249. }
  250. );
  251. }
  252. }
  253. function find_channal(id) {
  254. for (const iterator of _channalData) {
  255. if (id == iterator.id) {
  256. return iterator;
  257. }
  258. }
  259. return false;
  260. }
  261. function render_channal_list(channalinfo) {
  262. let output = "";
  263. output += "<div class='list_with_head'>";
  264. let checked = "";
  265. if (_channal.indexOf(channalinfo.id) >= 0) {
  266. checked = "checked";
  267. }
  268. output +=
  269. '<div><input type="checkbox" ' +
  270. checked +
  271. " channal_id='" +
  272. channalinfo.id +
  273. "'></div>";
  274. output += "<div class='head'>";
  275. output += "<span class='head_img'>";
  276. output += channalinfo.nickname.slice(0, 2);
  277. output += "</span>";
  278. output += "</div>";
  279. output += "<div style='width: 100%;'>";
  280. output += "<div>";
  281. // output += "<a href='../wiki/wiki.php?word=" + _word;
  282. // output += "&channal=" + channalinfo.id + "' >";
  283. output += "<a onclick=\"set_channal('" + channalinfo.id + "')\">";
  284. output += channalinfo["nickname"];
  285. output += "/" + channalinfo["name"];
  286. output += "</a>";
  287. output += "</div>";
  288. output += "<div>";
  289. output += "@" + channalinfo["username"];
  290. output += "</div>";
  291. output += "<div style='background-color: #e0dfdffa;'>";
  292. output +=
  293. "<span style='display: inline-block;background-color: #65ff65;width: " +
  294. (channalinfo["count"] * 100) / channalinfo["all"] +
  295. "%;'>";
  296. output += channalinfo["count"] + "/" + channalinfo["all"];
  297. output += "</span>";
  298. output += "</div>";
  299. output += "</div>";
  300. output += "</div>";
  301. return output;
  302. }
  303. function note_ref_init() {
  304. $("chapter").click(function () {
  305. let bookid = $(this).attr("book");
  306. let para = $(this).attr("para");
  307. window.open(
  308. "../pcdl/reader.php?view=chapter&book=" + bookid + "&para=" + para,
  309. "_blank"
  310. );
  311. });
  312. $("para").click(function () {
  313. let bookid = $(this).attr("book");
  314. let para = $(this).attr("para");
  315. window.open(
  316. "../pcdl/reader.php?view=para&book=" + bookid + "&para=" + para,
  317. "_blank"
  318. );
  319. });
  320. }
  321. /*
  322. id
  323. palitext
  324. tran
  325. ref
  326. */
  327. function note_json_html(in_json) {
  328. let output = "";
  329. output += "<div class='palitext'>" + in_json.palitext + "</div>";
  330. for (const iterator of in_json.translation) {
  331. output += "<div class='tran'>";
  332. output +=
  333. "<span class='edit_button' onclick=\"note_edit_sentence('" +
  334. in_json.book +
  335. "' ,'" +
  336. in_json.para +
  337. "' ,'" +
  338. in_json.begin +
  339. "' ,'" +
  340. in_json.end +
  341. "' ,'" +
  342. iterator.channal +
  343. "')\"></span>";
  344. output +=
  345. "<div class='text' id='tran_text_" +
  346. in_json.book +
  347. "_" +
  348. in_json.para +
  349. "_" +
  350. in_json.begin +
  351. "_" +
  352. in_json.end +
  353. "_" +
  354. iterator.channal +
  355. "'>";
  356. if (iterator.text == "") {
  357. //let channal = find_channal(iterator.channal);
  358. output += "<span style='color:var(--border-line-color);'>新建译文</span>";
  359. } else {
  360. output += marked(term_std_str_to_tran(iterator.text));
  361. }
  362. output += "</div>";
  363. output += "</div>";
  364. }
  365. output += "<div class='ref'>" + in_json.ref;
  366. output +=
  367. "<span class='sent_no'>" +
  368. in_json.book +
  369. "-" +
  370. in_json.para +
  371. "-" +
  372. in_json.begin +
  373. "-" +
  374. in_json.end +
  375. "<span>" +
  376. "</div>";
  377. return output;
  378. }
  379. function note_edit_sentence(book, para, begin, end, channal) {
  380. let channalInfo;
  381. for (const iterator of _channalData) {
  382. if (iterator.id == channal) {
  383. channalInfo = iterator;
  384. break;
  385. }
  386. }
  387. for (const iterator of _arrData) {
  388. if (
  389. iterator.book == book &&
  390. iterator.para == para &&
  391. iterator.begin == begin &&
  392. iterator.end == end
  393. ) {
  394. for (const tran of iterator.translation) {
  395. if (tran.channal == channal) {
  396. let html = "";
  397. html +=
  398. "<div style='color:blue;'>" +
  399. channalInfo.nickname +
  400. "/" +
  401. channalInfo.name +
  402. "</div>";
  403. html +=
  404. "<textarea id='edit_dialog_text' sent_id='" +
  405. tran.id +
  406. "' book='" +
  407. iterator.book +
  408. "' para='" +
  409. iterator.para +
  410. "' begin='" +
  411. iterator.begin +
  412. "' end='" +
  413. iterator.end +
  414. "' channal='" +
  415. tran.channal +
  416. "' style='width:100%;min-height:260px;'>" +
  417. tran.text +
  418. "</textarea>";
  419. $("#edit_dialog_content").html(html);
  420. break;
  421. }
  422. }
  423. }
  424. }
  425. $("#dialog").dialog("open");
  426. }
  427. function note_sent_save() {
  428. let id = $("#edit_dialog_text").attr("sent_id");
  429. let book = $("#edit_dialog_text").attr("book");
  430. let para = $("#edit_dialog_text").attr("para");
  431. let begin = $("#edit_dialog_text").attr("begin");
  432. let end = $("#edit_dialog_text").attr("end");
  433. let channal = $("#edit_dialog_text").attr("channal");
  434. let text = $("#edit_dialog_text").val();
  435. $.post(
  436. "../usent/sent_post.php",
  437. {
  438. id: id,
  439. book: book,
  440. para: para,
  441. begin: begin,
  442. end: end,
  443. channal: channal,
  444. text: text,
  445. lang: "zh",
  446. },
  447. function (data) {
  448. let result = JSON.parse(data);
  449. if (result.status > 0) {
  450. alert("error" + result.message);
  451. } else {
  452. alert("成功");
  453. $(
  454. "#tran_text_" +
  455. result.book +
  456. "_" +
  457. result.para +
  458. "_" +
  459. result.begin +
  460. "_" +
  461. result.end +
  462. "_" +
  463. result.channal
  464. ).html(marked(result.text));
  465. for (const iterator of _arrData) {
  466. if (
  467. iterator.book == result.book &&
  468. iterator.para == result.para &&
  469. iterator.begin == result.begin &&
  470. iterator.end == result.end
  471. ) {
  472. for (const tran of iterator.translation) {
  473. if (tran.channal == result.channal) {
  474. tran.text = result.text;
  475. break;
  476. }
  477. }
  478. }
  479. }
  480. }
  481. }
  482. );
  483. }