commit.js 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  1. var _commit_data;
  2. var previewWin;
  3. var sentData;
  4. function commit_init(param) {
  5. previewWin = model_win_init({ container: "model_win" });
  6. _commit_data = param;
  7. previewWin.show(commit_preview_render());
  8. }
  9. function commit_render_head(step) {
  10. let html = "";
  11. html += "<div class='commit_head step" + step + "'>";
  12. html += "<div class='commit_step commit_step_1'><div class='num'>1</div><div>选择版本</div></div>";
  13. html += "<div class='commit_step commit_step_2'><div class='num'>2</div><div>文本比对</div></div>";
  14. html += "<div class='commit_step commit_step_3'><div class='num'>3</div><div>完成</div></div>";
  15. html += "</div>";
  16. return html;
  17. }
  18. function commit_render_channel_select() {
  19. let html = "";
  20. html += "<div class='commit_win_inner' >";
  21. html += commit_render_head(1);
  22. html += "<div style='display:flex;'>";
  23. if (typeof _commit_data.src != "undefined") {
  24. html += "<div>译文来源:";
  25. let isFound = false;
  26. for (const iterator of _my_channal) {
  27. if (_commit_data.src == iterator.id) {
  28. html += iterator.name;
  29. isFound = true;
  30. break;
  31. }
  32. }
  33. if (!isFound) {
  34. for (const iterator of _channalData) {
  35. if (_commit_data.src == iterator.id) {
  36. html += iterator.name;
  37. isFound = true;
  38. break;
  39. }
  40. }
  41. }
  42. if (!isFound) {
  43. html += "无法找到Channel";
  44. }
  45. html += "</div>";
  46. } else {
  47. html += "<div>请选择译文来源";
  48. html += "</div>";
  49. }
  50. html += "<div>➡目标译文:</div>";
  51. html += "<div>";
  52. html += "<select id='dest_channel' onchange='dest_change(this)'>";
  53. if (typeof _commit_data.dest == "undefined") {
  54. let lastDest = localStorage.getItem("commit_dest_" + _commit_data.dest);
  55. if (typeof lastDest == "undefined") {
  56. html += "<option value='' selected>请选择目标版本</option>";
  57. } else {
  58. _commit_data.dest = lastDest;
  59. }
  60. }
  61. for (const iterator of _my_channal) {
  62. if (iterator.status > 0 && _commit_data.src != iterator.id) {
  63. html += "<option value='" + iterator.id + "' ";
  64. if (_commit_data.dest == iterator.id) {
  65. html += " selected ";
  66. }
  67. if (typeof _commit_data.src != "undefined" && _commit_data.src == iterator.id) {
  68. html += "style:'display:none;' ";
  69. }
  70. html += " >" + iterator.name + "-";
  71. if (iterator.power >= 30) {
  72. html += gLocal.gui.your;
  73. } else if (iterator.power >= 20) {
  74. html += "可编辑";
  75. } else if (iterator.power >= 10) {
  76. html += "只读";
  77. } else {
  78. html += "停用";
  79. }
  80. html += "</option>";
  81. }
  82. }
  83. html += "</select>";
  84. html += "</div>";
  85. html += "<div id='commit_preview'>";
  86. if (typeof _commit_data.express != "undefined" && _commit_data.express == true) {
  87. if (typeof _commit_data.sent != "undefined" && _commit_data.sent.length != 0) {
  88. html += "<button onclick='commit_pull()'>✔</button>";
  89. } else {
  90. html += "没有句子数据";
  91. }
  92. } else {
  93. html += "<button onclick='previewWin.show(commit_preview_render())'>文本比对</button>";
  94. }
  95. html += "</div>";
  96. html += "</div>";
  97. html += "</div>";
  98. return html;
  99. }
  100. function commit_preview_render() {
  101. let html = "";
  102. html += "<div class='commit_win_inner'>";
  103. html += commit_render_head(2);
  104. _commit_data.dest = $("#dest_channel").val();
  105. if (
  106. typeof _commit_data.src != "undefined" &&
  107. _commit_data.src != null &&
  108. _commit_data.src != "" &&
  109. typeof _commit_data.dest != "undefined" &&
  110. _commit_data.dest != null &&
  111. _commit_data.dest != ""
  112. ) {
  113. if (typeof _commit_data.sent == "undefined" || _commit_data.sent.length == 0) {
  114. let sentList = new Array();
  115. for (const iterator of _arrData) {
  116. sentList.push(iterator.book + "-" + iterator.para + "-" + iterator.begin + "-" + iterator.end);
  117. }
  118. _commit_data.sent = sentList;
  119. }
  120. let arrSentInfo = new Array();
  121. for (const iterator of _commit_data.sent) {
  122. let id = com_guid();
  123. arrSentInfo.push({ id: id, data: iterator });
  124. }
  125. if (arrSentInfo.length > 0) {
  126. let setting = new Object();
  127. setting.lang = "";
  128. setting.channal = _commit_data.src + "," + _commit_data.dest;
  129. $.post(
  130. "../term/note.php",
  131. {
  132. setting: JSON.stringify(setting),
  133. data: JSON.stringify(arrSentInfo),
  134. },
  135. function (data, status) {
  136. if (status == "success") {
  137. try {
  138. sentData = JSON.parse(data);
  139. } catch (e) {}
  140. previewWin.show(commit_render_comp(0));
  141. }
  142. }
  143. );
  144. html += "加载中 请稍等……";
  145. html += "</div>";
  146. return html;
  147. } else {
  148. html += "没有句子被选择";
  149. html += "</div>";
  150. return html;
  151. }
  152. } else {
  153. return commit_render_channel_select();
  154. }
  155. }
  156. function commit_compare_mode_change(obj) {
  157. previewWin.show(commit_render_comp(parseInt($(obj).val())));
  158. }
  159. /*
  160. { id: 0, string: "自动" },
  161. { id: 1, string: "全选" },
  162. { id: 2, string: "全不选" },
  163. */
  164. function commit_render_comp(mode) {
  165. let html = "";
  166. html += "<div class='commit_win_inner'>";
  167. html += commit_render_head(2);
  168. html += "<div>";
  169. html += "<button onclick='previewWin.show(commit_render_channel_select())'>返回</button>";
  170. html += "<button onclick='commit_pull()'>推送</button>";
  171. html += "<button onclick='commit_close()'>放弃</button>";
  172. html += "</div>";
  173. html += "<div class='commit_compare'>";
  174. html += "<div >";
  175. html += "<select onchange='commit_compare_mode_change(this)'>";
  176. let compareMode = [
  177. { id: 0, string: "自动" },
  178. { id: 1, string: "全选" },
  179. { id: 2, string: "全不选" },
  180. ];
  181. for (const iterator of compareMode) {
  182. html += "<option value='" + iterator.id + "' ";
  183. if (mode == iterator.id) {
  184. html += " selected ";
  185. }
  186. html += ">" + iterator.string + "</option>";
  187. }
  188. html += "</select>";
  189. html += "</div>";
  190. html += "<div class='pali'>巴利原文</div>";
  191. html += "<div class='src_text'>当前版本:" + channal_getById(_commit_data.src).name + "</div>";
  192. html += "<div class='dest_text'>推送到:" + channal_getById(_commit_data.dest).name + "</div>";
  193. html += "</div>";
  194. let textCount = 0;
  195. let sentIndex = 0;
  196. for (let iterator of sentData) {
  197. iterator.checked = false;
  198. if (iterator.translation[0].text != iterator.translation[1].text) {
  199. textCount++;
  200. if (iterator.translation[0].id != "") {
  201. html += "<div class='commit_compare'>";
  202. html += "<div >";
  203. html += "<input class='sent_checkbox' index='" + sentIndex + "' type='checkbox' ";
  204. switch (mode) {
  205. case 0:
  206. if (iterator.translation[1].id == "") {
  207. html += " checked ";
  208. iterator.checked = true;
  209. } else {
  210. if (iterator.translation[0].update_time > iterator.translation[1].update_time) {
  211. html += " checked ";
  212. iterator.checked = true;
  213. }
  214. }
  215. break;
  216. case 1:
  217. html += " checked ";
  218. iterator.checked = true;
  219. break;
  220. case 2:
  221. break;
  222. default:
  223. break;
  224. }
  225. html += " sent_id='" + iterator.pali_sent_id;
  226. html += "' onclick='commit_sent_select(this)' /></div>";
  227. html += "<div class='pali'>" + iterator.palitext + "</div>";
  228. html += "<div class='src_text'>";
  229. html += iterator.translation[0].text;
  230. html += "</div>";
  231. html += "<div ";
  232. html += "channel='" + _commit_data.dest + "'";
  233. html += "sent_id='" + iterator.pali_sent_id + "'";
  234. html += " class='dest_text'>";
  235. switch (mode) {
  236. case 0:
  237. if (iterator.translation[1].id == "") {
  238. html += "<ins>" + iterator.translation[0].text + "</ins>";
  239. } else {
  240. if (iterator.translation[0].update_time > iterator.translation[1].update_time) {
  241. html += commit_render_diff(iterator.translation[1].text, iterator.translation[0].text);
  242. //html += "<del>" + iterator.translation[1].text + "</del><br>";
  243. //html += "<ins>" + iterator.translation[0].text + "</ins>";
  244. } else {
  245. html += "[新]" + iterator.translation[1].text;
  246. }
  247. }
  248. break;
  249. case 1:
  250. html += commit_render_diff(iterator.translation[1].text, iterator.translation[0].text);
  251. //html += "<del>" + iterator.translation[1].text + "</del><br>";
  252. //html += "<ins>" + iterator.translation[0].text + "</ins>";
  253. break;
  254. case 2:
  255. html += iterator.translation[1].text;
  256. break;
  257. }
  258. html += "</div>";
  259. html += "</div>";
  260. }
  261. }
  262. sentIndex++;
  263. }
  264. if (textCount == 0) {
  265. html += "全部相同,无需推送。";
  266. }
  267. html += "</div>";
  268. return html;
  269. }
  270. function commit_render_diff(str1, str2) {
  271. let output = "";
  272. const diff = Diff.diffChars(str1, str2);
  273. diff.forEach((part) => {
  274. // green for additions, red for deletions
  275. // grey for common parts
  276. if (part.added) {
  277. output += "<ins>" + part.value + "</ins>";
  278. } else if (part.removed) {
  279. output += "<del>" + part.value + "</del>";
  280. } else {
  281. output += part.value;
  282. }
  283. });
  284. return output;
  285. }
  286. function commit_sent_select(obj) {
  287. let sent_id = $(obj).attr("sent_id");
  288. for (let iterator of sentData) {
  289. if (iterator.pali_sent_id == sent_id) {
  290. let html = "";
  291. iterator.checked = obj.checked;
  292. if (obj.checked) {
  293. if (iterator.translation[1].id != "") {
  294. html += "<del>" + iterator.translation[1].text + "</del><br>";
  295. }
  296. html += "<ins>" + iterator.translation[0].text + "</ins>";
  297. } else {
  298. html += iterator.translation[1].text;
  299. }
  300. $(".dest_text[sent_id='" + sent_id + "']").html(html);
  301. }
  302. }
  303. }
  304. function commit_render_final(result) {
  305. let html = "";
  306. html += "<div class='commit_win_inner'>";
  307. html += commit_render_head(3);
  308. if (typeof result.update != "undefined") {
  309. html += "<div>修改:" + result.update + "</div>";
  310. }
  311. if (typeof result.insert != "undefined") {
  312. html += "<div>新增:" + result.insert + "</div>";
  313. }
  314. if (typeof result.pr != "undefined") {
  315. html += "<div>提交修改建议:" + result.pr + "</div>";
  316. }
  317. html +=
  318. "<div><a href='' onclick='window.reload()'>刷新页面</a>查看修改结果。<a onclick='previewWin.close()'>关闭</a></div>";
  319. html += "</div>";
  320. return html;
  321. }
  322. function commit_pull() {
  323. localStorage.setItem("commit_src_" + _commit_data.src, _commit_data.dest);
  324. let pullData = new Array();
  325. for (const iterator of sentData) {
  326. if (iterator.checked) {
  327. for (const iterator of _arrData) {
  328. pullData.push(iterator.book + "-" + iterator.para + "-" + iterator.begin + "-" + iterator.end);
  329. }
  330. _commit_data.sent = pullData;
  331. }
  332. }
  333. if (pullData.length == 0) {
  334. alert("没有数据被选择");
  335. return;
  336. }
  337. $.post(
  338. "../commit/commit.php",
  339. {
  340. data: JSON.stringify(_commit_data),
  341. },
  342. function (data, status) {
  343. if (status == "success") {
  344. let html = "";
  345. try {
  346. let result = JSON.parse(data);
  347. if (result.status == 0) {
  348. previewWin.show(commit_render_final(result));
  349. } else {
  350. alert(result.message);
  351. }
  352. } catch (e) {}
  353. }
  354. }
  355. );
  356. }
  357. function commit_close() {}
  358. function dest_change(obj) {
  359. _commit_data.dest = $(obj).val();
  360. localStorage.setItem("commit_src_" + _commit_data.src, _commit_data.dest);
  361. }
  362. function src_change(obj) {
  363. _commit_data.src = $(obj).val();
  364. localStorage.setItem("commit_last_src", _commit_data.src);
  365. }