term_edit_dlg.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310
  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. //判断是否只读
  164. if(sentChannel !==null && sentChannel.power !== 30){
  165. output += "(只读)";
  166. }
  167. }else{
  168. output += "仅使用于版本<b>";
  169. if(currChannel !== null){
  170. //我有写权限
  171. output += currChannel.name;
  172. }else{
  173. //我没有写权限 设置按钮为disable
  174. output += word.channel;
  175. output += "(只读)";
  176. }
  177. output += "</b>";
  178. }
  179. output += "</div>";
  180. output +="<div><input type='checkbox' name='save_as' onchange='term_save_as(this)' />另存为</div>";
  181. }
  182. output += "<div id='term_save_as_channel' style='"+style+"' >";
  183. output += "<select id='term_edit_form_channal' name='channal'>";
  184. //TODO 句子channel 是我自己的才显示通用于所有版本
  185. if(sentChannelId){
  186. if(sentChannel && sentChannel.power==30){
  187. output += "<option value=''>通用于我的所有版本</option>";
  188. }
  189. }else{
  190. //术语不在句子里,也显示
  191. output += "<option value=''>通用于我的所有版本</option>";
  192. }
  193. /*
  194. 按照当前的默认匹配逻辑,先匹配句子所在channel 里面的术语,没有才匹配通用的
  195. 所以如果此术语匹配到了channel 说明这个channel一定是这个句子的channel
  196. 在这种情况下,如果没有在我的有写权限的channel列表中找到,这个句子channel一定是我没有写权限的。
  197. */
  198. //句子channel 我有写权限才显示仅用于此channel
  199. let ignoreChannelId=null;
  200. if(word.channel === ''){
  201. if(sentChannel !== null){
  202. output += "<option value='"+sentChannel.uid+"'>[本句版本]"+sentChannel.name+"</option>";
  203. ignoreChannelId = sentChannel.uid;
  204. }
  205. }
  206. if(word.guid==''){
  207. //新建术语
  208. for (const iterator of _my_channal) {
  209. if(iterator.uid==word.channel){
  210. //这句我有写权限
  211. output += "<option value='"+word.channel+"'>[本句版本]"+iterator.name+"</option>";
  212. ignoreChannelId = word.channel;
  213. }
  214. }
  215. }
  216. for (const iterator of _my_channal) {
  217. if(currChannel && currChannel.uid==iterator.uid){
  218. continue;
  219. }
  220. if(ignoreChannelId === iterator.uid){
  221. continue;
  222. }
  223. if(word.channel=="" || (word.channel!="" && iterator.uid !== word.channel)){
  224. output += "<option value='"+iterator.uid+"' onclick=\"set_term_dlg_channel_msg('此术语将仅仅用于这个版本')\">仅用于"+iterator.name+"</option>";
  225. }
  226. }
  227. output += "</select>";
  228. output += "<div class='msg' id='term_dlg_channel_msg'>只有选择<b>通用版本</b>或者<b>本句版本</b>才会应用到这个句子</div>";
  229. output += "</div>";
  230. output += "</fieldset>";
  231. output += "</form>";
  232. return output;
  233. }
  234. function set_term_dlg_channel_msg(msg){
  235. $("#term_dlg_channel_msg").text(msg);
  236. }
  237. function term_edit_dlg_save() {
  238. $.ajax({
  239. type: "POST", //方法类型
  240. dataType: "json", //预期服务器返回的数据类型
  241. url: "../term/term_post.php", //url
  242. data: $("#form_term").serialize(),
  243. success: function (result) {
  244. console.log(result); //打印服务端返回的数据(调试用)
  245. if (result.status == 0) {
  246. alert(result.message + gLocal.gui.saved + gLocal.gui.successful);
  247. for (let index = 0; index < arrMyTerm.length; index++) {
  248. const element = arrMyTerm[index];
  249. if(element.guid==result.data.guid){
  250. arrMyTerm.splice(index,1);
  251. break;
  252. }
  253. }
  254. arrMyTerm.push(result.data);
  255. term_updata_translation();
  256. } else {
  257. alert("error:" + result.message);
  258. }
  259. },
  260. error: function (data, status) {
  261. alert("异常!" + data.responseText);
  262. switch (status) {
  263. case "timeout":
  264. break;
  265. case "error":
  266. break;
  267. case "notmodified":
  268. break;
  269. case "parsererror":
  270. break;
  271. default:
  272. break;
  273. }
  274. },
  275. });
  276. }
  277. function term_save_as(obj){
  278. if(obj.checked){
  279. $("#term_save_as_channel").show();
  280. }else{
  281. $("#term_save_as_channel").hide();
  282. }
  283. }