editpage.js 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. define(['jquery', 'bootstrap', 'backend', 'form', 'ace/ace', 'tools'], function ($, undefined, Backend, Form, ace, undefined) {
  2. var Controller = {
  3. index: function () {
  4. //初始化编辑器
  5. editor = Controller.api.editpageInit(false, true);
  6. //设置标题
  7. var parent_title = $('.layui-layer-title:last', window.parent.document).html() + ' ' + Config.filepath;
  8. $('.layui-layer-title:last', window.parent.document).html(parent_title);
  9. //改变的时候记录未保存
  10. editor.on("change", function () {
  11. $('#update_file').html('<b style="color: #f00"> 未保存</b>');
  12. });
  13. // 复制路径
  14. $(document).on("click", ".btn-copy", function () {
  15. Controller.api.copy($(this).data('path'));
  16. });
  17. //Ctrl + S 保存
  18. $(document).bind("keydown", function(e) {
  19. console.log(e.which);
  20. if (e.ctrlKey && (e.which == 83)) {
  21. e.preventDefault();
  22. $("form[role=form]").submit();
  23. return false;
  24. }
  25. });
  26. //点击保存事件
  27. $(document).on("click", "#editpage_btn", function () {
  28. $("form[role=form]").submit();
  29. });
  30. //表单提交事件绑定
  31. Form.api.bindevent($("#add-form"), function (data, ret) {
  32. return false;
  33. }, function (data, ret) {
  34. var content = editor.getValue();
  35. var file = $('#file').val();
  36. Fast.api.ajax({
  37. url: 'editpage/savefile',
  38. data: {file: file, content: content}
  39. }, function (data, ret) {
  40. $('#update_file').html('已保存');
  41. });
  42. return false;
  43. });
  44. },
  45. command: function () {
  46. //初始化编辑器
  47. editor = Controller.api.editpageInit('text', false);
  48. //点击命令按钮
  49. $('.btn').click(function() {
  50. var val = $(this).data('val');
  51. editor.setValue($('#' + val).html() + "\n", true);
  52. })
  53. $(document).keydown(function(){
  54. if (event.keyCode == 13) {//回车键的键值为13
  55. var content = editor.getValue();
  56. var content = content.split(/[\n,]/g);
  57. for(var i =0;i<content.length;i++){
  58. if(content[i] == ""){
  59. content.splice(i, 1);
  60. //删除数组索引位置应保持不变
  61. i--;
  62. }
  63. }
  64. var len = content.length - 1;
  65. content = content[len];
  66. if(content.indexOf("php think") == 0){
  67. $("form[role=form]").submit();
  68. }
  69. }
  70. });
  71. //表单提交事件绑定
  72. Form.api.bindevent($("form[role=form]"), function (data, ret) {
  73. return false;
  74. }, function (data, ret) {
  75. var content = editor.getValue();
  76. var content = content.split(/[\n,]/g);
  77. for(var i =0;i<content.length;i++){
  78. if(content[i] == ""){
  79. content.splice(i, 1);
  80. //删除数组索引位置应保持不变
  81. i--;
  82. }
  83. }
  84. var len = content.length - 1;
  85. content = content[len];
  86. if(content.indexOf("php think") == 0){
  87. Fast.api.ajax({
  88. url: 'editpage/command',
  89. data: {content: content}
  90. }, function (data, ret) {
  91. editor.setValue(editor.getValue() + ret.msg + "\n", true);
  92. });
  93. }
  94. return false;
  95. });
  96. },
  97. edit: function () {
  98. Controller.api.bindevent();
  99. },
  100. api: {
  101. bindevent: function () {
  102. Form.api.bindevent($("form[role=form]"));
  103. },
  104. editpageInit: function (language, menu) {
  105. //loading层
  106. var index = Layer.load(1, {
  107. shade: [0.1, '#fff'] //0.1透明度的白色背景
  108. });
  109. //初始化对象
  110. editor = ace.edit("code");
  111. //设置风格和语言(更多风格和语言,请到github上相应目录查看)
  112. theme = Config.editpage_config.theme;//clouds,chaos
  113. if(language){
  114. language = language;
  115. }else{
  116. language = Config.language_type;
  117. }
  118. editor.setTheme("ace/theme/" + theme);
  119. editor.session.setMode("ace/mode/" + language);
  120. //字体大小
  121. editor.setFontSize(parseInt(Config.editpage_config.font_size));
  122. //设置只读(true时只读,用于展示代码)
  123. editor.setReadOnly(parseInt(Config.editpage_config.setreadonly));
  124. //自动换行,设置为off关闭
  125. editor.setOption("wrap", Config.editpage_config.auto_wrap);
  126. //启用提示菜单
  127. if(menu){
  128. ace.require("ace/ext/language_tools");
  129. editor.setOptions({
  130. enableBasicAutocompletion: true,
  131. enableSnippets: true,
  132. enableLiveAutocompletion: true
  133. });
  134. }
  135. $('#add-form').show();
  136. Layer.close(index);
  137. return editor;
  138. },
  139. copy: function (val) {
  140. var oInput = document.createElement('input');
  141. oInput.value = val;
  142. document.body.appendChild(oInput);
  143. oInput.select(); // 选择对象
  144. document.execCommand("Copy"); // 执行浏览器复制命令
  145. oInput.className = 'oInput';
  146. oInput.style.display='none';
  147. Layer.alert('<b>拷贝路径成功,请使用Ctrl+V粘贴。</b><br/>' + val);
  148. }
  149. }
  150. };
  151. return Controller;
  152. });