note.js 12 KB

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