note.js 13 KB

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