You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

525 lines
15 KiB

3 years ago
  1. var editormd;
  2. var template_list;
  3. var json_table_data = '|' + lang["params"] + '|' + lang["type"] + '|' + lang["description"] + '|\n' +
  4. '|:-------|:-------|:-------|\n';
  5. $(function() {
  6. //给按钮文字加上颜色,点击后则去掉颜色
  7. if (is_showdoc_online()) {
  8. set_text_color("runapi", "red");
  9. };
  10. /*加载目录*/
  11. secondCatList();
  12. function secondCatList() {
  13. var default_second_cat_id = $("#default_second_cat_id").val();
  14. var item_id = $("#item_id").val();
  15. $.post(
  16. DocConfig.server+"/api/catalog/secondCatList", {
  17. "item_id": item_id,
  18. },
  19. function(data) {
  20. $("#cat_id").html('<OPTION value="0">' + lang["none"] + '</OPTION>');
  21. if (data.error_code == 0) {
  22. json = data.data;
  23. console.log(json);
  24. for (var i = 0; i < json.length; i++) {
  25. cat_html = '<OPTION value="' + json[i].cat_id + '" ';
  26. if (default_second_cat_id == json[i].cat_id) {
  27. cat_html += ' selected ';
  28. }
  29. cat_html += ' ">' + json[i].cat_name + '</OPTION>';
  30. $("#cat_id").append(cat_html);
  31. };
  32. getChildCatList();
  33. };
  34. },
  35. "json"
  36. );
  37. }
  38. function getChildCatList() {
  39. var cat_id = $("#cat_id").val();
  40. var default_child_cat_id = $("#default_child_cat_id").val();
  41. $.post(
  42. DocConfig.server+"/api/catalog/childCatList", {
  43. "cat_id": cat_id
  44. },
  45. function(data) {
  46. $("#parent_cat_id").html('<OPTION value="0">' + lang["none"] + '</OPTION>');
  47. if (data.error_code == 0) {
  48. json = data.data;
  49. console.log(json);
  50. for (var i = 0; i < json.length; i++) {
  51. cat_html = '<OPTION value="' + json[i].cat_id + '" ';
  52. if (default_child_cat_id == json[i].cat_id) {
  53. cat_html += ' selected ';
  54. }
  55. cat_html += ' ">' + json[i].cat_name + '</OPTION>';
  56. $("#parent_cat_id").append(cat_html);
  57. };
  58. } else {}
  59. },
  60. "json"
  61. );
  62. }
  63. //监听是否选择了目录。如果选择了,则跟后台判断是否还子目录
  64. $("#cat_id").change(function() {
  65. getChildCatList();
  66. });
  67. var keyMap = {
  68. // 保存
  69. "Ctrl-S": function() {
  70. $("#save").click();
  71. }
  72. };
  73. initEditorOutsideKeys();
  74. function initEditorOutsideKeys() {
  75. if (!editormd) return;
  76. var $doc = $(document);
  77. $.each(keyMap, function(key, fn) {
  78. $doc.on('keydown', null, key.replace('-', '+'), function(e) {
  79. e.preventDefault();
  80. fn();
  81. });
  82. });
  83. }
  84. // 如果是新增页面,则光标为标题文本框
  85. if (location.href.indexOf('type=new') !== -1) {
  86. setTimeout(function() {
  87. $('#page_title').focus();
  88. }, 1000);
  89. }
  90. /*初始化编辑器*/
  91. editormd = editormd("editormd", {
  92. width: "90%",
  93. height: 1000,
  94. syncScrolling: "single",
  95. path: DocConfig.pubile + "/editor.md/lib/",
  96. placeholder: lang["editormd_placeholder"],
  97. taskList: true,
  98. tex: true, // 默认不解析
  99. flowChart: true, // 默认不解析
  100. sequenceDiagram: true, // 默认不解析
  101. htmlDecode : "style,script,iframe|filterXSS",//解析html
  102. imageUpload: true,
  103. imageFormats: ["jpg", "jpeg", "gif", "png", "bmp", "webp", "JPG", "JPEG", "GIF", "PNG", "BMP", "WEBP"],
  104. imageUploadURL: "?s=home/page/uploadImg",
  105. onload: function() {
  106. this.addKeyMap(keyMap);
  107. }
  108. });
  109. /*插入API接口模板*/
  110. $("#api-doc").click(function() {
  111. var tmpl = $("#api-doc-templ").html();
  112. editormd.insertValue(tmpl);
  113. });
  114. /*插入数据字典模板*/
  115. $("#database-doc").click(function() {
  116. var tmpl = $("#database-doc-templ").html();
  117. editormd.insertValue(tmpl);
  118. });
  119. /*JSON转参数表格*/
  120. $("#jsons").click(function() {
  121. $("#json-templ").show();
  122. });
  123. /*JSON格式美化*/
  124. $("#beautify-json").click(function() {
  125. $("#beautify-json-dialog").show();
  126. });
  127. $("#json-templ .editormd-enter-btn").click(function() {
  128. var datas = $("#json-templ .jsons").val();
  129. try {
  130. Change($.parseJSON(datas));
  131. } catch (e) {
  132. alert(lang["json_fail"] + e);
  133. }
  134. //datas=processJSONImport(datas);
  135. //alert(datas);
  136. /*var datas='||||||\n'+
  137. '|:-------|:-------|:-------|:-------|:-------|\n'+
  138. '|uid|int(10)|否|||\n'+
  139. '|username|varchar(20)|否||用户名|';*/
  140. //alert(json_table_data);return;
  141. editormd.insertValue(json_table_data);
  142. json_table_data = '|' + lang["filed"] + '|' + lang["type"] + '|' + lang["description"] + '|\n' +
  143. '|:-------|:-------|:-------|\n';
  144. $("#json-templ .jsons").val("");
  145. $("#json-templ").hide();
  146. });
  147. $("#beautify-json-dialog .editormd-enter-btn").click(function() {
  148. var data = $("#beautify-json-dialog .jsons").val();
  149. try {
  150. data = data.replace(/(^\s*)|(\s*$)/g, "");
  151. op1 = data.substr(0, 1) == "[" ? "[" : "{";
  152. op2 = (op1 == "[") ? "]" : "}";
  153. var text = "\n ``` \n " + op1 + " \n" + dump(JSON.parse(data)) + " " + op2 + " \n\n ```\n\n"; //整体加个大括号
  154. //$("#beautify-json-dialog .jsons").val(text);
  155. $("#beautify-json-dialog .jsons").val("");
  156. editormd.insertValue(text);
  157. } catch (e) {
  158. //非json数据直接显示
  159. //$("#beautify-json-dialog .jsons").val(data);
  160. $("#beautify-json-dialog .jsons").val("");
  161. editormd.insertValue(data);
  162. }
  163. $("#beautify-json-dialog").hide();
  164. });
  165. //{"dgfgdfg":"gdfgdfg"}
  166. //格式化json数据
  167. function dump(arr, level) {
  168. var dumped_text = "";
  169. if (!level) level = 0;
  170. //The padding given at the beginning of the line.
  171. var level_padding = "";
  172. for (var j = 0; j < level + 1; j++) level_padding += " ";
  173. if (typeof(arr) == 'object') { //Array/Hashes/Objects
  174. var i = 0;
  175. for (var item in arr) {
  176. var value = arr[item];
  177. if (typeof(value) == 'object') { //If it is an array,
  178. dumped_text += level_padding + "\"" + item + "\" : \{ \n";
  179. dumped_text += dump(value, level + 1);
  180. dumped_text += level_padding + "\}";
  181. } else {
  182. if (typeof(value) == "number") {
  183. dumped_text += level_padding + "\"" + item + "\" : " + value ;
  184. }else{
  185. dumped_text += level_padding + "\"" + item + "\" : \"" + value + "\"";
  186. }
  187. }
  188. if (i < Object.getOwnPropertyNames(arr).length - 1) {
  189. dumped_text += ", \n";
  190. } else {
  191. dumped_text += " \n";
  192. }
  193. i++;
  194. }
  195. } else { //Stings/Chars/Numbers etc.
  196. dumped_text = "===>" + arr + "<===(" + typeof(arr) + ")";
  197. }
  198. return dumped_text;
  199. }
  200. /*保存*/
  201. $("#save").click(function() {
  202. var page_id = $("#page_id").val();
  203. var item_id = $("#item_id").val();
  204. var page_title = $("#page_title").val();
  205. var page_comments = $("#page_comments").val();
  206. var page_content = $("#page_content").val();
  207. var item_id = $("#item_id").val();
  208. var s_number = $("#s_number").val();
  209. var cat_id = $("#cat_id").val();
  210. var parent_cat_id = $("#parent_cat_id").val();
  211. if (parent_cat_id > 0) {
  212. cat_id = parent_cat_id;
  213. };
  214. $("#save").html('保存中...').attr('disabled','disabled');
  215. $.post(
  216. DocConfig.server+"/api/page/save", {
  217. "page_id": page_id,
  218. "cat_id": cat_id,
  219. "s_number": s_number,
  220. "page_content": page_content,
  221. "page_title": page_title,
  222. "page_comments": page_comments,
  223. "item_id": item_id
  224. },
  225. function(data) {
  226. if (data.error_code == 0) {
  227. localStorage.removeItem("page_content");
  228. window.location.href = "?s=home/item/show&page_id=" + data.data.page_id + "&item_id=" + item_id;
  229. } else {
  230. $.alert(lang["save_fail"]);
  231. $("#save").html('保存').removeAttr('disabled');
  232. }
  233. },
  234. 'json'
  235. )
  236. });
  237. $(".editormd-preview-container").bind('DOMNodeInserted', function(e) {
  238. $(".editormd-preview-container table thead tr").css({
  239. "background-color": "#08c",
  240. "color": "#fff"
  241. });
  242. $(".editormd-preview-container table tr").eq(0).css({
  243. "background-color": "#08c",
  244. "color": "#fff"
  245. });
  246. $(".editormd-preview-container table tr").each(function() {
  247. if ($(this).find("td").eq(1).html() == "object" || $(this).find("td").eq(1).html() == "array[object]") {
  248. $(this).css({
  249. "background-color": "#99CC99",
  250. "color": "#000"
  251. });
  252. }
  253. });
  254. });
  255. function closeDiv(target) {
  256. $(target).hide();
  257. }
  258. function Change(data) {
  259. var level_str = "- ";
  260. if (arguments.length > 1) {
  261. var level;
  262. arguments[1] > 0 ? level = arguments[1] : level = 1;
  263. for (var i = 0; i < level; i++) {
  264. level_str += "- ";
  265. }
  266. }
  267. for (var key in data) {
  268. var value = data[key];
  269. var type = typeof(value);
  270. if (type == "object") {
  271. json_table_data += '| ' + level_str + key + ' |' + type + ' | ' + lang["none"] + ' |\n';
  272. if (value instanceof Array) {
  273. var j = level + 1;
  274. Change(value[0], j);
  275. continue;
  276. }
  277. //else
  278. //{
  279. Change(value, level);
  280. //}
  281. } else {
  282. json_table_data += '| ' + key + ' | ' + type + '| ' + lang["none"] + ' |\n';
  283. }
  284. }
  285. }
  286. //{"Result":[{"name":"test1","list":{"pros":"prosfsf","ppps":{"images":[{"22":"22"}]}}}]}
  287. $("#save-to-templ").click(function() {
  288. $.prompt({
  289. title: lang["save_templ_title"]
  290. }, function(template_title, index) {
  291. if (template_title != null && template_title != "") {
  292. var template_content = $("#page_content").val();
  293. $.post(
  294. "?s=home/template/save", {
  295. "template_title": template_title,
  296. "template_content": template_content
  297. },
  298. function(data) {
  299. if (data.error_code == 0) {
  300. $.closeDialog(index);
  301. $.alert(lang["saved_templ_msg1"] + template_title + lang["saved_templ_msg2"]);
  302. } else {
  303. $.alert(lang["save_fail"]);
  304. }
  305. },
  306. "json"
  307. );
  308. }
  309. $("#save-btn-group").removeClass("open");
  310. return false;
  311. });
  312. });
  313. $("#more-templ").click(function() {
  314. $.post(
  315. "?s=home/template/getList", {},
  316. function(data) {
  317. if (data.error_code == 0) {
  318. var html = '<TR><td>' + lang["save_time"] + '</td><td>' + lang["templ_title"] + '</td><td>' + lang["operation"] + '</td></TR>';
  319. template_list = data.data;
  320. json = data.data;
  321. for (var i = 0; i < json.length; i++) {
  322. html += '<TR><td>' + json[i]['addtime'] + '</td>';
  323. html += '<td>' + json[i]['template_title'] + '</td>';
  324. html += '<td><a href="javascript:use_template(' + json[i]['id'] + ')">' + lang["use_this_template"] + '</a> | <a href="javascript:delete_template(' + json[i]['id'] + ')">' + lang["delete_this_template"] + '</a></td>';
  325. html += '</TR>';
  326. };
  327. $("#templ-table").html(html);
  328. $("#more-templ-modal").modal();
  329. } else {
  330. //$.alert("获取模板列表失败");
  331. $("#more-templ-modal").modal("hide");
  332. $.alert(lang["no_templ_msg"]);
  333. }
  334. },
  335. "json"
  336. );
  337. });
  338. $("#add-page-comments").click(function() {
  339. var page_comments = prompt(lang["add_page_comments_msg"], "");
  340. if (page_comments != null && page_comments != "") {
  341. $("#page_comments").val(page_comments);
  342. $("#save").click();
  343. }
  344. $("#save-btn-group").removeClass("open");
  345. return false;
  346. });
  347. /** 粘贴上传图片 **/
  348. document.getElementById("editormd").addEventListener('paste', function(e) {
  349. var clipboard = e.clipboardData;
  350. for (var i = 0, len = clipboard.items.length; i < len; i++) {
  351. if (clipboard.items[i].kind == 'file' || clipboard.items[i].type.indexOf('image') > -1) {
  352. var imageFile = clipboard.items[i].getAsFile();
  353. var form = new FormData;
  354. form.append('t', 'ajax-uploadpic');
  355. form.append('editormd-image-file', imageFile);
  356. var layer_index = {};
  357. var callback = function(type, data) {
  358. type = type || 'before';
  359. var $the = $('#content');
  360. switch (type) {
  361. // 开始上传
  362. case 'before':
  363. layer_index = layer.load(1, {
  364. shade: [0.1, '#fff'] //0.1透明度的白色背景
  365. });
  366. break;
  367. // 服务器返回错误
  368. case 'error':
  369. $the.attr('disabled', false);
  370. $.closeDialog(layer_index);
  371. $.alert('图片上传失败');
  372. break;
  373. // 上传成功
  374. case 'success':
  375. $the.attr('disabled', false);
  376. $.closeDialog(layer_index);
  377. if (data.success == 1) {
  378. var value = '![](' + data.url + ')';
  379. editormd.insertValue(value);
  380. } else {
  381. $.alert(data.message);
  382. }
  383. break;
  384. }
  385. };
  386. $.ajax({
  387. url: "?s=home/page/uploadImg",
  388. type: "POST",
  389. dataType: "json",
  390. data: form,
  391. processData: false,
  392. contentType: false,
  393. beforeSend: function() {
  394. callback('before');
  395. },
  396. error: function() {
  397. callback('error');
  398. },
  399. success: function(data) {
  400. callback('success', data);
  401. }
  402. })
  403. e.preventDefault();
  404. }
  405. }
  406. });
  407. //更改选择文件框的文件类型让它更快地弹出
  408. $("#editormd").on("mouseover", '[name=editormd-image-file]', function(){
  409. $(this).attr("accept","image/png,image/jpg,image/jpeg,imge/bmp,image/gif")
  410. });
  411. //定时保存文本内容到localStorage
  412. setInterval(function(){
  413. localStorage.page_content= $("#page_content").val() ;
  414. }, 60000);
  415. //检测是否有定时保存的内容
  416. if (localStorage.page_content && localStorage.page_content.length > 0) {
  417. $.confirm("检测到有上次编辑时自动保存的草稿。是否自动填充上次的草稿内容?",
  418. {},
  419. function(){
  420. editormd.clear();
  421. editormd.insertValue(localStorage.page_content);
  422. $.closeAll();
  423. localStorage.removeItem("page_content");
  424. },
  425. function(){
  426. localStorage.removeItem("page_content");
  427. }
  428. )
  429. };
  430. });
  431. //使用模板
  432. function use_template(id) {
  433. for (var i = 0; i < template_list.length; i++) {
  434. if (id > 0 && id == template_list[i]['id']) {
  435. editormd.insertValue(template_list[i]['template_content']);
  436. $("#more-templ-modal").modal("hide");
  437. };
  438. };
  439. }
  440. //删除模板
  441. function delete_template(id) {
  442. $.post(
  443. "?s=home/template/delete", {
  444. "id": id
  445. },
  446. function(data) {
  447. if (data.error_code == 0) {
  448. $("#more-templ").click();
  449. } else {
  450. $.alert(lang["save_fail"]);
  451. }
  452. },
  453. "json"
  454. );
  455. }