term_edit_dlg.js 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304
  1. function term_edit_dlg_init(title = gLocal.gui.dict_terms) {
  2. $("body").append('<div id="term_edit_dlg" title="' + title + '"><div id="term_edit_dlg_content"></div></div>');
  3. $("#term_edit_dlg").dialog({
  4. autoOpen: false,
  5. width: 550,
  6. outerHeight: "80vh",
  7. buttons: [
  8. {
  9. id:"term_edit_dlg_save",
  10. text: gLocal.gui.submit,
  11. click: function () {
  12. term_edit_dlg_save();
  13. $(this).dialog("close");
  14. },
  15. },
  16. {
  17. text: gLocal.gui.cancel,
  18. click: function () {
  19. $(this).dialog("close");
  20. },
  21. },
  22. ],
  23. });
  24. }
  25. /*
  26. obj:调用此函数的按钮的handle
  27. */
  28. function term_edit_dlg_open(id = "", word = "",channel="",lang="",obj=null) {
  29. if (id == "") {
  30. let newWord = new Object();
  31. newWord.guid = "";
  32. newWord.word = word;
  33. newWord.meaning = "";
  34. newWord.other_meaning = "";
  35. newWord.tag = "";
  36. newWord.note = "";
  37. newWord.language = lang;
  38. newWord.channel = channel;
  39. let html = term_edit_dlg_render(newWord,obj);
  40. $("#term_edit_dlg_content").html(html);
  41. $("#term_edit_dlg").dialog("open");
  42. } else {
  43. $.post(
  44. "../term/term_get_id.php",
  45. {
  46. id: id,
  47. },
  48. function (data) {
  49. let word = JSON.parse(data);
  50. let html = term_edit_dlg_render(word,obj);
  51. $("#term_edit_dlg_content").html(html);
  52. $("#term_edit_dlg").dialog("open");
  53. }
  54. );
  55. }
  56. }
  57. function term_edit_dlg_render(word = null,obj=null) {
  58. if (word == null) {
  59. word = new Object();
  60. word.guid = "";
  61. word.word = "";
  62. word.meaning = "";
  63. word.other_meaning = "";
  64. word.tag = "";
  65. word.note = "";
  66. }
  67. let output = "";
  68. output += "<form action='##' id='form_term'>";
  69. output += "<input type='hidden' id='term_edit_form_id' name='id' value='" + word.guid + "'>";
  70. output += "<fieldset>";
  71. output += "<legend>" + gLocal.gui.spell + "</legend>";
  72. if(word.guid === "" && word.word === ""){
  73. //新建术语 而且词头为空 允许修改word 拼写
  74. "<input type='input' id='term_edit_form_word' name='word' value='" +
  75. word.word +
  76. ">";
  77. }else{
  78. output += "<div style='font-size:200%;font-weight:700;'>"+word.word+"</div>";
  79. output +=
  80. "<input type='hidden' id='term_edit_form_word' name='word' value='" +
  81. word.word +
  82. "'placeholder=" +
  83. gLocal.gui.required +
  84. ">";
  85. }
  86. output += "</fieldset>";
  87. output += "<fieldset>";
  88. output += "<legend>" + gLocal.gui.first_choice_word + "</legend>";
  89. output +=
  90. "<input type='input' id='term_edit_form_meaning' name='mean' value='" +
  91. word.meaning +
  92. "' placeholder=" +
  93. gLocal.gui.required +
  94. ">";
  95. output += "</fieldset>";
  96. output += "<fieldset>";
  97. output += "<legend>" + gLocal.gui.other_meaning + "</legend>";
  98. output +=
  99. "<input type='input' id='term_edit_form_othermeaning' name='mean2' value='" +
  100. word.other_meaning +
  101. "' placeholder=" +
  102. gLocal.gui.optional +
  103. ">";
  104. output += "</fieldset>";
  105. output += "<fieldset>";
  106. output += "<legend>" + gLocal.gui.language + "</legend>";
  107. output +=
  108. "<input type='input' id='term_edit_form_language' name='language' value='" +
  109. word.language +
  110. "' placeholder=" +
  111. gLocal.gui.required +
  112. " >";
  113. output += "</fieldset>";
  114. output += "<fieldset>";
  115. output += "<legend>" + gLocal.gui.tag + "</legend>";
  116. output +=
  117. "<input type='input' id='term_edit_form_tag name='tag' name='tag' value='" +
  118. word.tag +
  119. "' placeholder=" +
  120. gLocal.gui.optional +
  121. " >";
  122. output += "</fieldset>";
  123. output += "<fieldset>";
  124. output += "<legend>" + gLocal.gui.encyclopedia + "</legend>";
  125. output += "<textarea id='term_edit_form_note' name='note' placeholder=" + gLocal.gui.optional + " style='height:5em;'>";
  126. output += word.note ;
  127. output += "</textarea>";
  128. output += "</fieldset>";
  129. output += "<fieldset>";
  130. output += "<legend>" + gLocal.gui.channel + "</legend>";
  131. let currChannel=null;//当前单词的channel
  132. if(typeof word.channel == "undefined" && typeof word.channal != "undefined"){
  133. word.channel = word.channal;
  134. }
  135. for (const iterator of _my_channal) {
  136. if(iterator.uid==word.channel){
  137. currChannel = iterator;
  138. }
  139. }
  140. //查询术语所在句子的channel
  141. let sentChannel=null;
  142. let sentChannelId=null;
  143. if(obj){
  144. let sentObj = find_sent_tran_div(obj);
  145. if(sentObj){
  146. sentChannelId = sentObj.attr('channel');
  147. for (const iterator of _my_channal) {
  148. if(iterator.uid==sentChannelId){
  149. sentChannel = iterator;
  150. }
  151. }
  152. }
  153. }
  154. let style='display:none;';
  155. if(word.guid === ''){
  156. //新建术语 可以修改channel
  157. style = 'display:block;';
  158. }else{
  159. //修改术语 不能修改channel
  160. output +="<div>当前:" ;
  161. if(word.channel === ''){
  162. output += "通用于<b>所有版本</b>";
  163. }else{
  164. output += "仅使用于版本<b>";
  165. if(currChannel !== null){
  166. //我有写权限
  167. output += currChannel.name;
  168. }else{
  169. //我没有写权限 设置按钮为disable
  170. output += word.channel;
  171. }
  172. output += "</b>";
  173. }
  174. output += "</div>";
  175. output +="<div><input type='checkbox' name='save_as' onchange='term_save_as(this)' />另存为</div>";
  176. }
  177. output += "<div id='term_save_as_channel' style='"+style+"' >";
  178. output += "<select id='term_edit_form_channal' name='channal'>";
  179. //TODO 句子channel 是我自己的才显示通用于所有版本
  180. if(sentChannelId){
  181. if(sentChannel && sentChannel.power==30){
  182. output += "<option value=''>通用于我的所有版本</option>";
  183. }
  184. }else{
  185. //术语不在句子里,也显示
  186. output += "<option value=''>通用于我的所有版本</option>";
  187. }
  188. /*
  189. 按照当前的默认匹配逻辑,先匹配句子所在channel 里面的术语,没有才匹配通用的
  190. 所以如果此术语匹配到了channel 说明这个channel一定是这个句子的channel
  191. 在这种情况下,如果没有在我的有写权限的channel列表中找到,这个句子channel一定是我没有写权限的。
  192. */
  193. //句子channel 我有写权限才显示仅用于此channel
  194. let ignoreChannelId=null;
  195. if(word.channel === ''){
  196. if(sentChannel !== null){
  197. output += "<option value='"+sentChannel.uid+"'>[本句版本]"+sentChannel.name+"</option>";
  198. ignoreChannelId = sentChannel.uid;
  199. }
  200. }
  201. if(word.guid==''){
  202. //新建术语
  203. for (const iterator of _my_channal) {
  204. if(iterator.uid==word.channel){
  205. //这句我有写权限
  206. output += "<option value='"+word.channel+"'>[本句版本]"+iterator.name+"</option>";
  207. ignoreChannelId = word.channel;
  208. }
  209. }
  210. }
  211. for (const iterator of _my_channal) {
  212. if(currChannel && currChannel.uid==iterator.uid){
  213. continue;
  214. }
  215. if(ignoreChannelId === iterator.uid){
  216. continue;
  217. }
  218. if(word.channel=="" || (word.channel!="" && iterator.uid !== word.channel)){
  219. output += "<option value='"+iterator.uid+"' onclick=\"set_term_dlg_channel_msg('此术语将仅仅用于这个版本')\">仅用于"+iterator.name+"</option>";
  220. }
  221. }
  222. output += "</select>";
  223. output += "<div class='msg' id='term_dlg_channel_msg'>只有选择<b>通用版本</b>或者<b>本句版本</b>才会应用到这个句子</div>";
  224. output += "</div>";
  225. output += "</fieldset>";
  226. output += "</form>";
  227. return output;
  228. }
  229. function set_term_dlg_channel_msg(msg){
  230. $("#term_dlg_channel_msg").text(msg);
  231. }
  232. function term_edit_dlg_save() {
  233. $.ajax({
  234. type: "POST", //方法类型
  235. dataType: "json", //预期服务器返回的数据类型
  236. url: "../term/term_post.php", //url
  237. data: $("#form_term").serialize(),
  238. success: function (result) {
  239. console.log(result); //打印服务端返回的数据(调试用)
  240. if (result.status == 0) {
  241. alert(result.message + gLocal.gui.saved + gLocal.gui.successful);
  242. for (let index = 0; index < arrMyTerm.length; index++) {
  243. const element = arrMyTerm[index];
  244. if(element.guid==result.data.guid){
  245. arrMyTerm.splice(index,1);
  246. break;
  247. }
  248. }
  249. arrMyTerm.push(result.data);
  250. term_updata_translation();
  251. } else {
  252. alert("error:" + result.message);
  253. }
  254. },
  255. error: function (data, status) {
  256. alert("异常!" + data.responseText);
  257. switch (status) {
  258. case "timeout":
  259. break;
  260. case "error":
  261. break;
  262. case "notmodified":
  263. break;
  264. case "parsererror":
  265. break;
  266. default:
  267. break;
  268. }
  269. },
  270. });
  271. }
  272. function term_save_as(obj){
  273. if(obj.checked){
  274. $("#term_save_as_channel").show();
  275. }else{
  276. $("#term_save_as_channel").hide();
  277. }
  278. }