单文件版在线代码编辑器 aceditor

单文件版在线代码编辑器 aceditor,第1张

* 单文件版在线代码编辑器 editor.php 版本: v1.21

* 非常方便地在线编辑您网站上的任意文本文件,对于维护网站,和在线写代码非常好用

* 密码加密方式:

* md5(自设密码+$ace) //$ace为cdn镜像地址

*

* 使用方法:

* 1.确认 $pwd 变量值为 false, 上传本文件到PHP空间并访问

* 2.第一次访问提示设置密码,设置密码并牢记

* 3.使用第一次设置的密码登录后,默认编辑的是本php文件,

* 4.本文件是编辑器核心文件,请不要随意修改

* 5.保存编辑的文件请用 Ctrl + S 按键组合,等待执行结果

* 6.保存动作执行后请务必等待保存成功信息返回

* 7.重置 *** 作会修改本程序的文件名,以防他人猜测路径

* 8.刷新功能仅是刷新本程序文件,不能刷新其他

*

* 建议在 chrome 浏览器中使用本编辑器


项目详细见

http://git.oschina.net/ymk18/aceditor 单文件版在线代码编辑器 aceditor,第2张

  1. <?php
  2. /**
  3. * 单文件版在线代码编辑器 editor.php 版本: v1.21
  4. *
  5. * 密码加密方式:
  6. * md5(自设密码+$ace) //$ace为cdn镜像地址
  7. *
  8. * 使用方法:
  9. * 1.确认 $pwd 变量值为 false, 上传本文件到PHP空间并访问
  10. * 2.第一次访问提示设置密码,设置密码并牢记
  11. * 3.使用第一次设置的密码登录后,默认编辑的是本php文件,
  12. * 4.本文件是编辑器核心文件,请不要随意修改
  13. * 5.保存编辑的文件请用 Ctrl + S 按键组合,等待执行结果
  14. * 6.保存动作执行后请务必等待保存成功信息返回
  15. * 7.重置 *** 作会修改本程序的文件名,以防他人猜测路径
  16. * 8.刷新功能仅是刷新本程序文件,不能刷新其他
  17. *
  18. * 建议在 chrome 浏览器中使用本编辑器
  19. */
  20. session_start();
  21. $curr_file = __FILE__; //默认编辑当前文件
  22. $curr_file_path = str_replace(dirname(__FILE__), '', __FILE__);
  23. $pwd = false; //密码初始化默认值为 false
  24. $ace = 'http://cdn.staticfile.org/ace/1.1.3/ace.js'; //编辑器核心js
  25. $tip['core'] = 'http://cdn.staticfile.org/alertify.js/0.3.11/alertify.core.min.css';
  26. $tip['css'] = 'http://cdn.staticfile.org/alertify.js/0.3.11/alertify.default.min.css';
  27. $tip['js'] = 'http://cdn.staticfile.org/alertify.js/0.3.11/alertify.min.js';
  28. $jquery = 'http://cdn.staticfile.org/jquery/2.1.1-rc2/jquery.min.js';
  29. if ( false !== $pwd ) {
  30. define('DEFAULT_PWD', $pwd);
  31. }
  32. //文件后缀名对应的语法解析器
  33. $lng = array(
  34. 'as' => 'actionscript', 'js' => 'javascript',
  35. 'php' => 'php', 'css' => 'css', 'html' => 'html',
  36. 'htm' => 'html', 'ini' => 'ini', 'json' => 'json',
  37. 'jsp' => 'jsp', 'txt' => 'text', 'sql' => 'mysql',
  38. 'xml' => 'xml', 'yaml' => 'yaml', 'py' => 'python',
  39. 'md' => 'markdown', 'htaccess' => 'apache_conf',
  40. 'bat' => 'batchfile', 'go' => 'golang',
  41. );
  42. //判断用户是否登录
  43. function is_logged() {
  44. $flag = false;
  45. if ( isset($_SESSION['pwd']) && defined('DEFAULT_PWD') ) {
  46. if ( $_SESSION['pwd'] === DEFAULT_PWD ) {
  47. $flag = true;
  48. }
  49. }
  50. return $flag;
  51. }
  52. //重新载入到本页面
  53. function reload() {
  54. $file = pathinfo(__FILE__, PATHINFO_BASENAME);
  55. die(header("Location: {$file}"));
  56. }
  57. //判断请求是否是ajax请求
  58. function is_ajax() {
  59. $flag = false;
  60. if ( isset($_SERVER['HTTP_X_REQUESTED_WITH']) ) {
  61. $flag = strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) === 'xmlhttprequest';
  62. }
  63. return $flag;
  64. }
  65. //销毁SESSION和COOKIE
  66. function exterminate() {
  67. $_SESSION = array();
  68. foreach ( $_COOKIE as $key ) {
  69. setcookie($key, null);
  70. }
  71. session_destroy();
  72. $_COOKIE = array();
  73. return true;
  74. }
  75. //获取一个目录下的文件列表
  76. function list_dir($path, $type = 'array') {
  77. $flag = false;
  78. $lst = array('dir'=>array(), 'file'=>array());
  79. $base = !is_dir($path) ? dirname($path) : $path;
  80. $tmp = scandir($base);
  81. foreach ( $tmp as $k=>$v ) {
  82. //过滤掉上级目录,本级目录和程序自身文件名
  83. if ( !in_array($v, array('.', '..')) ) {
  84. $file = $full_path = rtrim($base, '/').DIRECTORY_SEPARATOR.$v;
  85. if ( $full_path == __FILE__ ) {
  86. continue; //屏蔽自身文件不在列表出现
  87. }
  88. $file = str_replace(dirname(__FILE__), '', $file);
  89. $file = str_replace("\", '/', $file); //过滤win下的路径
  90. $file = str_replace('//', '/', $file); //过滤双斜杠
  91. if ( is_dir($full_path) ) {
  92. if ( 'html' === $type ) {
  93. $v = '
  94. <li class="dir" path="'.$file
  95. .'" onclick="load();">'.$v.'
  96. ';
  97. }
  98. array_push($lst['dir'], $v);
  99. } else {
  100. if ( 'html' === $type ) {
  101. $v = '
  102. <li class="file" path="'.$file
  103. .'" onclick="load()">'.$v.'
  104. ';
  105. }
  106. array_push($lst['file'], $v);
  107. }
  108. }
  109. }
  110. $lst = array_merge($lst['dir'], $lst['file']);
  111. $lst = array_filter($lst);
  112. $flag = $lst;
  113. if ( 'html' === $type ) {
  114. $flag = '
      '. implode('', $lst) .'
    ';
  115. }
  116. return $flag;
  117. }
  118. //递归删除一个非空目录
  119. function deldir($dir) {
  120. $dh = opendir($dir);
  121. while ( $file = readdir($dh) ) {
  122. if ( $file != '.' && $file != '..' ) {
  123. $fullpath = $dir.'/'.$file;
  124. if ( !is_dir($fullpath) ) {
  125. unlink($fullpath);
  126. } else {
  127. deldir($fullpath);
  128. }
  129. }
  130. }
  131. return rmdir($dir);
  132. }
  133. //退出登录
  134. if ( isset($_GET['logout']) ) {
  135. if ( exterminate() ) {
  136. reload();
  137. }
  138. }
  139. //ajax输出文件内容
  140. if ( is_logged() && is_ajax() && isset($_POST['file']) ) {
  141. $file = dirname(__FILE__).$_POST['file'];
  142. $ext = pathinfo($file, PATHINFO_EXTENSION);
  143. $mode = isset($lng[$ext]) ? $lng[$ext] : false;
  144. die(json_encode(array(
  145. 'file' => $file, 'html' => file_get_contents($file),
  146. 'mode' => $mode,
  147. )));
  148. }
  149. //ajax输出目录列表
  150. if ( is_logged() && is_ajax() && isset($_POST['dir']) ) {
  151. $dir = dirname(__FILE__).$_POST['dir'];
  152. $list_dir = list_dir($dir, 'html');
  153. die(json_encode(array(
  154. 'dir' => $dir, 'html' => $list_dir,
  155. )));
  156. }
  157. //ajax保存文件
  158. if ( is_logged() && is_ajax() && isset($_POST['action']) ) {
  159. $arr = array('result'=>'error', 'msg'=>'文件保存失败!');
  160. $content = $_POST['content'];
  161. if ( 'save_file' === $_POST['action'] ) {
  162. if ( isset($_POST['file_path']) ) {
  163. $file = dirname(__FILE__).$_POST['file_path'];
  164. } else {
  165. $file = __FILE__;
  166. }
  167. file_put_contents($file, $content);
  168. $arr['result'] = 'success';
  169. $arr['msg'] = '保存成功!';
  170. }
  171. die(json_encode($arr));
  172. }
  173. //ajax删除文件或文件夹
  174. if ( is_logged() && is_ajax() && isset($_POST['del']) ) {
  175. $path = dirname(__FILE__).$_POST['del'];
  176. $arr = array('result'=>'error', 'msg'=>'删除 *** 作失败!');
  177. if ( $_POST['del'] && $path ) {
  178. $flag = is_dir($path) ? deldir($path) : unlink($path);
  179. if ( $flag ) {
  180. $arr['msg'] = '删除 *** 作成功!';
  181. $arr['result'] = 'success';
  182. }
  183. }
  184. die(json_encode($arr));
  185. }
  186. //ajax新建文件或文件夹
  187. if ( is_logged() && is_ajax() && isset($_POST['create']) ) {
  188. $flag = false;
  189. $arr = array('result'=>'error', 'msg'=>' *** 作失败!');
  190. if ( isset($_POST['target']) ) {
  191. $target = dirname(__FILE__).$_POST['target'];
  192. $target = is_dir($target) ? $target : dirname($target);
  193. }
  194. if ( $_POST['create'] && $target ) {
  195. $base_name = pathinfo($_POST['create'], PATHINFO_BASENAME);
  196. $exp = explode('.', $base_name);
  197. $full_path = $target.'/'.$base_name;
  198. $new_path = str_replace(dirname(__FILE__), '', $full_path);
  199. if ( count($exp) > 1 && isset($lng[array_pop($exp)]) ) {
  200. file_put_contents($full_path, '');
  201. $arr['result'] = 'success';
  202. $arr['msg'] = '新建文件成功!';
  203. $arr['type'] = 'file';
  204. } else {
  205. mkdir($full_path, 0777, true);
  206. $arr['result'] = 'success';
  207. $arr['msg'] = '新建目录成功!';
  208. $arr['type'] = 'dir';
  209. }
  210. if ( $base_name && $new_path ) {
  211. $arr['new_name'] = $base_name;
  212. $arr['new_path'] = $new_path;
  213. }
  214. }
  215. die(json_encode($arr));
  216. }
  217. //ajax重命名文件或文件夹
  218. if ( is_logged() && is_ajax() && isset($_POST['rename']) ) {
  219. $arr = array('result'=>'error', 'msg'=>'重命名 *** 作失败!');
  220. if ( isset($_POST['target']) ) {
  221. $target = dirname(__FILE__).$_POST['target'];
  222. }
  223. if ( $_POST['rename'] ) {
  224. $base_name = pathinfo($_POST['rename'], PATHINFO_BASENAME);
  225. if ( $base_name ) {
  226. $rename = dirname($target).'/'.$base_name;
  227. $new_path = str_replace(dirname(__FILE__), '', $rename);
  228. }
  229. }
  230. if ( $rename && $target && rename($target, $rename) ) {
  231. $arr['new_name'] = $base_name;
  232. $arr['new_path'] = $new_path;
  233. $arr['msg'] = '重命名 *** 作成功!';
  234. $arr['result'] = 'success';
  235. }
  236. if ( $target == __FILE__ ) {
  237. $arr['redirect'] = $new_path;
  238. }
  239. die(json_encode($arr));
  240. }
  241. //获取代码文件内容
  242. $code = file_get_contents($curr_file);
  243. $tree = '
    • ROOT'.list_dir($curr_file, 'html').'
    • ';
    • //登陆和设置密码共用模版
    • $first = <<<htmlstr
    • 【标题】
    • body {
    • overflow: hidden; background-color: #2D2D2D; color: #CCCCCC; font-size: 12px; margin: 0;
    • font-family: 'Monaco', 'Menlo', 'Ubuntu Mono', 'Consolas', 'source-code-pro', monospace;
    • }
    • form { display: none; position: absolute; }
    • form h5 { font-size: 14px; font-weight: normal; margin: 0; line-height: 2em; }
    • form input {
    • color: #fff; border: 1px solid #369; border-radius: 3px; background: #333; height: 22px;
    • line-height: 1.6em; width: 125px; margin-right: 5px; vertical-align: middle;
    • }
    • form button {
    • line-height: 1.6em; border: 1px solid #369; border-radius: 3px;
    • background: #369; color: #fff; vertical-align: middle;
    • }
    • var editor = false;
    • $(function(){
    • $('form').prepend('
      '+ document.title +'
      ');
    • $('form').css({
    • left: ($(window).width()-$('form').width())/2,
    • top: ($(window).height()-$('form').height())/2
    • });
    • $('form').show();
    • });
    • HTMLSTR;
    • //判断是否第一次登录
    • if ( false === $pwd && empty($_POST) ) {
    • die(str_replace(
    • array('【标题】', '【动作】'),
    • array('第一次使用,请先设置密码!', '设置'),
    • $first
    • ));
    • }
    • //第一次设置登录密码
    • if ( false === $pwd && !empty($_POST) ) {
    • if ( isset($_POST['pwd']) && strlen($_POST['pwd']) ) {
    • $pwd = $_SESSION['pwd'] = md5($_POST['pwd'].$ace);
    • $code = preg_replace('#$pwd = false;#', '$pwd = "'.$pwd.'";', $code, 1);
    • file_put_contents($curr_file, $code);
    • } else {
    • reload();
    • }
    • }
    • //用户登录验证
    • if ( false !== $pwd && !empty($_POST) ) {
    • $tmp = md5($_POST['pwd'].$ace);
    • if ( $tmp && $pwd && $tmp === $pwd ) {
    • $_SESSION['pwd'] = $pwd;
    • reload();
    • }
    • }
    • //处理一下html实体
    • $code = htmlspecialchars($code);
    • $dir_icon = str_replace(array("rn", "r", "n"), '',
    • 'data:image/jpg;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAANCAYAAACgu+4kAAAAGXRFWHRTb2Z0d2
    • FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAQVJREFUeNqkkk1uwjAQhd84bsNP1FUXLCtu0H3XPSoX4Qrd9wR
    • sCjQEcIY3DiiJUYiqRhp5Mra/92YSUVVgLSW49B7H+NApRh75XkHfFoCG+02tyflUeQTw2y9UYYP8cCStc9SM
    • PeVA/Sy6Dw555q3au1z+EhBYk1cgO7OSNdaFNT0x5sCkYDha0WPiHZgVqPzLO+8seai6E2jed42bCL06tNyEH
    • AX9kv3jh3HqH7BctFWLMOmAbcg05mHK5+sQpd1HYijN47zcDUCShGEHtzxtwQS9WTcAQmJROrJDLXQB9s1Tu6
    • MtRED4bwsHLnUzxEeKac3+GeP6eo8yevhjC3F1qC4CDAAl3HwuyNAIdwAAAABJRU5ErkJggg==');
    • $file_icon = str_replace(array("rn", "r", "n"), '',
    • 'data:image/jpg;base64,iVBORw0KGgoAAAANSUhEUgAAAA8AAAAQCAYAAADJViUEAAAAGXRFWHRTb2Z0d2
    • FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAS1JREFUeNqMU01KxkAMTaez7aYbNwreQdBzeopS6EXEW+jug7Z
    • C6X+/iUloSr6xioFHJkPee5mUJgBwT7gjpPB3XAgfiBjs5dOyLF/btl0pkEFngdbzPGNRFK/U+0hwJAAMjmcm
    • DsOA4zge6Pseu67DpmlEqK5rLMvyRkDJor6uq2SGktu2FfdpmpANqqoSASYnO/kthABJkoCOxCASkCBkWSYuQ
    • qCeNE1fqHz3fMkXzjnJ2sRinL33QBNIzWJ5nh/L8npQohVTJwYTyfFm/d6Oo2HGE8ffwseuZ1PEjhrOutmsRF
    • 0iC8QmPibEtT4hftrhHI95JqJT/HC2JOt0to+zN6MVsZ/oZKqwmyCTA33DkbN1sws0i+Pega6v0kd42H9JB/8
    • LJl5I6PNbgAEAa9MP7QWoNLoAAAAASUVORK5CYII=');
    • $loading = str_replace(array("rn", "r", "n"), '',
    • 'data:image/gif;base64,R0lGODlhFAAUALMIAPh2AP+TMsZiALlcAKNOAOp4ANVqAP+PFv///wAAAAAAAA
    • AAAAAAAAAAAAAAAAAAACH/C05FVFNDQVBFMi4wAwEAAAAh+QQFCgAIACwAAAAAFAAUAAAEUxDJSau9iBDMteb
    • TMEjehgTBJYqkiaLWOlZvGs8WDO6UIPCHw8TnAwWDEuKPcxQml0Ynj2cwYACAS7VqwWItWyuiUJB4s2AxmWxG
    • g9bl6YQtl0cAACH5BAUKAAgALAEAAQASABIAAAROEMkpx6A4W5upENUmEQT2feFIltMJYivbvhnZ3Z1h4FMQI
    • Dodz+cL7nDEn5CH8DGZhcLtcMBEoxkqlXKVIgAAibbK9YLBYvLtHH5K0J0IACH5BAUKAAgALAEAAQASABIAAA
    • ROEMkphaA4W5upMdUmDQP2feFIltMJYivbvhnZ3V1R4BNBIDodz+cL7nDEn5CH8DGZAMAtEMBEoxkqlXKVIg4
    • HibbK9YLBYvLtHH5K0J0IACH5BAUKAAgALAEAAQASABIAAAROEMkpjaE4W5tpKdUmCQL2feFIltMJYivbvhnZ
    • 3R0A4NMwIDodz+cL7nDEn5CH8DGZh8ONQMBEoxkqlXKVIgIBibbK9YLBYvLtHH5K0J0IACH5BAUKAAgALAEAA
    • QASABIAAAROEMkpS6E4W5spANUmGQb2feFIltMJYivbvhnZ3d1x4JMgIDodz+cL7nDEn5CH8DGZgcBtMMBEox
    • kqlXKVIggEibbK9YLBYvLtHH5K0J0IACH5BAUKAAgALAEAAQASABIAAAROEMkpAaA4W5vpOdUmFQX2feFIltM
    • JYivbvhnZ3V0Q4JNhIDodz+cL7nDEn5CH8DGZBMJNIMBEoxkqlXKVIgYDibbK9YLBYvLtHH5K0J0IACH5BAUK
    • AAgALAEAAQASABIAAAROEMkpz6E4W5tpCNUmAQD2feFIltMJYivbvhnZ3R1B4FNRIDodz+cL7nDEn5CH8DGZg
    • 8HNYMBEoxkqlXKVIgQCibbK9YLBYvLtHH5K0J0IACH5BAkKAAgALAEAAQASABIAAAROEMkpQ6A4W5spIdUmHQ
    • f2feFIltMJYivbvhnZ3d0w4BMAIDodz+cL7nDEn5CH8DGZAsGtUMBEoxkqlXKVIgwGibbK9YLBYvLtHH5K0J0
    • IADs=');
    • //编辑器模版
    • $html = <<<htmlstr
    • ACE代码编辑器
    • a { text-decoration: none; }
    • body {
    • overflow: hidden; background-color: #2D2D2D; font-size: 12px;
    • font-family: 'Consolas', 'Monaco', 'Menlo', 'Ubuntu Mono', monospace;
    • scrollbar-arrow-color: #ccc; scrollbar-base-color: #333;
    • scrollbar-dark-shadow-color: #00ffff; scrollbar-track-color: #272822;
    • scrollbar-highlight-color: #272822; scrollbar-3d-light-color: #272822;
    • scrollbar-face-color: #2D2D2D; scrollbar-shadow-color: #333;
    • }
    • ::-webkit-scrollbar { width:5px; height:6px; background-color:#444; }
    • ::-webkit-scrollbar:hover { background-color:#444; }
    • ::-webkit-scrollbar-thumb:hover { min-height:5px; min-width:5px; background-color: #AAA; }
    • ::-webkit-scrollbar-thumb:active { -webkit-border-radius:20px; background-color: #AAA; }
    • ::-webkit-scrollbar-thumb {
    • min-height:5px; min-width:5px; -webkit-border-radius:20px;
    • ::-webkit-border-radius:1px; background-color: #AAA;
    • }
    • body > pre { color: #666; }
    • #sider { margin: 0; position: absolute; top: 25px; bottom: 0; left: 0; right: 85%; }
    • #editor { margin: 0; position: absolute; top: 0; bottom: 0; left: 15%; right: 0; }
    • #dir_tree { margin:0; padding: 0; height: 100%; overflow: auto; position: relative; left: 5px; }
    • #dir_tree, #dir_tree ul, #dir_tree li { margin: 0; padding: 0; list-style: none inside; }
    • #dir_tree ul { padding-left: 20px; position: relative; }
    • #dir_tree li { text-indent: 2em; line-height: 1.6em; cursor: default; color: #ccc; }
    • #dir_tree li.hover > span, #dir_tree li:hover > span { color: #66D9EF; }
    • #dir_tree li#on > span { color: red; }
    • #dir_tree li.dir { background: url({$dir_icon}) no-repeat 3px 3px; }
    • #dir_tree li.file { background: url({$file_icon}) no-repeat 3px 0; }
    • #dir_tree li.loading { background: url({$loading}) no-repeat 3px 0; }
    • #logout { position: absolute; top: 0; left: 0; }
    • #logout a { display: inline-block; color: #aaa; line-height: 25px; padding: 0 4px; }
    • #logout a:hover { background: #000; color: #ddd; }
    • #contextmenu { position: absolute; top: 0; left: 0; background: #fff; color: #333; border: 1px solid #000; padding: 1px; }
    • #contextmenu span { display: block; line-height: 24px; text-indent: 20px; width: 80px; cursor: default; }
    • #contextmenu span:hover { background-color: #369; color: #fff; }
    • #alertify .alertify-message, #alertify .alertify-message {
    • text-align: left !important; text-indent: 0; font-weight: bold; font-size: 16px;
    • }
    • #alertify .alertify-dialog, #alertify .alertify-dialog {
    • font-family: 'Consolas'; padding: 10px !important; color: #333 !important;
    • }
    • #alertify .alertify-button {
    • border-radius: 3px !important; font-weight: normal !important;
    • font-size: 14px !important; padding: 3px 15px !important;
    • }
    • .alertify-buttons { text-align: right !important; }
    • 保存
    • 刷新
    • 重置
    • 退出
    • </htmlstr</htmlstr

  244. {$tree}

    {$code}
  245. var load = false;
  246. var curr_file = false;
  247. window.location.hash = '';
  248. alertify.set({delay: 1000}); //n秒后自动消失
  249. alertify.set({labels: {ok:'确定',cancel:'取消'}});
  250. var editor = false;
  251. $(function(){
  252. //实例化代码编辑器
  253. editor = ace.edit("editor");
  254. //设置编辑器的语法和高亮
  255. editor.setTheme("ace/theme/monokai");
  256. editor.getSession().setMode("ace/mode/php");
  257. //设置编辑器自动换行
  258. editor.getSession().setWrapLimitRange(null, null);
  259. editor.getSession().setUseWrapMode(true);
  260. //不显示垂直衬线
  261. editor.renderer.setShowPrintMargin(false);
  262. //editor.setReadOnly(true); //设置编辑器为只读
  263. //editor.gotoLine(325); //跳转到指定行
  264. //使编辑器获得输入焦点
  265. editor.focus();
  266. //绑定组合按键
  267. var commands = editor.commands;
  268. commands.addCommand({
  269. name: "save",
  270. bindKey: {win: "Ctrl-S", mac: "Command-S"},
  271. exec: save_file
  272. });
  273. //保存动作
  274. function save_file() {
  275. if ( false == editor ) { return false; }
  276. var obj = {
  277. content: editor.getValue(),
  278. action: 'save_file'
  279. };
  280. if ( false !== curr_file ) {
  281. obj.file_path = curr_file;
  282. }
  283. alertify.log('正在保存...');
  284. $.post(window.location.href, obj, function(data){
  285. if ( data.msg && 'success' == data.result ) {
  286. alertify.success(data.msg);
  287. } else {
  288. alertify.error(data.msg);
  289. }
  290. }, 'json');
  291. }
  292. //加载目录列表或文件
  293. load = function(ele) {
  294. var curr = $(event.srcElement);
  295. if ( ele ) { curr = ele; }
  296. if ( curr.is('span') ) { curr = curr.parent('li'); }
  297. $('#dir_tree #on').removeAttr('id');
  298. curr.attr('id', 'on');
  299. var type = curr.attr('class');
  300. var path = curr.attr('path');
  301. window.location.hash = path;
  302. if ( 'file' === type ) {
  303. alertify.log('正在加载...');
  304. curr.addClass('loading');
  305. $.post(window.location.href, {file:path}, function(data){
  306. curr.removeClass('loading');
  307. if ( data.mode ) {
  308. editor.getSession().setMode("ace/mode/"+data.mode);
  309. }
  310. //注意,空文件应当允许编辑
  311. if ( true || data.html ) {
  312. curr.attr('disabled', 'disabled');
  313. curr_file = path; //当前编辑的文件路径
  314. //动态赋值编辑器中的内容
  315. editor.session.doc.setValue(data.html);
  316. editor.renderer.scrollToRow(0); //滚动到第一行
  317. editor.focus(); //编辑器获得焦点
  318. setTimeout(function(){
  319. editor.gotoLine(0);
  320. }, 800);
  321. }
  322. }, 'json');
  323. event.stopPropagation();
  324. event.preventDefault();
  325. return false;
  326. }
  327. if ( 'dir' === type ) {
  328. if ( curr.attr('loaded') ) {
  329. curr.children('ul').toggle();
  330. event.stopPropagation();
  331. event.preventDefault();
  332. return false;
  333. } else {
  334. curr.attr('loaded', 'yes');
  335. }
  336. alertify.log('正在加载...');
  337. curr.addClass('loading');
  338. $.post(window.location.href, {dir:path}, function(data){
  339. curr.find('ul').remove();
  340. curr.removeClass('loading');
  341. if ( data.html ) {
  342. curr.append(data.html);
  343. }
  344. }, 'json');
  345. }
  346. return false;
  347. }
  348. //绑定右键菜单
  349. $('#sider').bind('contextmenu', function(e){
  350. var path = false;
  351. var target = $(event.srcElement);
  352. if ( target.is('span') ) {
  353. target = target.parent('li');
  354. }
  355. if ( target.attr('path') ) {
  356. path = target.attr('path');
  357. } else {
  358. return false;
  359. }
  360. target.addClass('hover');
  361. var right_menu = $('#contextmenu');
  362. if ( !right_menu.get(0) ) {
  363. var timer = false;
  364. right_menu = $('

    ');
  365. right_menu.hover(function(){
  366. if ( timer ) { clearTimeout(timer); }
  367. }, function(){
  368. timer = setTimeout(function(){
  369. hide_menu(right_menu);
  370. }, 500);
  371. });
  372. $('body').append(right_menu);
  373. }
  374. if ( path ) {
  375. right_menu.html('');
  376. var menu = $('新建浏览重命名删除');
  377. right_menu.append(menu);
  378. menu_area(right_menu, {left: e.pageX, top: e.pageY});
  379. right_menu.find('span').click(function(){
  380. switch ( $(this).text() ) {
  381. case '新建' : create_new(target, path); break;
  382. case '浏览' : preview(target, path); break;
  383. case '重命名' : re_name(target, path); break;
  384. case '删除' : del_file(target, path); break;
  385. }
  386. hide_menu(right_menu);
  387. });
  388. }
  389. path ? right_menu.show() : hide_menu(right_menu);
  390. return false;
  391. });
  392. //隐藏右键菜单
  393. function hide_menu(menu) {
  394. $('#sider li.hover').removeClass('hover');
  395. if ( menu ) {
  396. menu.hide();
  397. }
  398. }
  399. //右键菜单区域
  400. function menu_area(menu, cfg) {
  401. if ( menu && cfg ) {
  402. var w = $('#sider').width() - menu.width();
  403. var h = $('#sider').height() - menu.height();
  404. if ( cfg.left > w ) { cfg.left = w; }
  405. if ( cfg.top > h ) { cfg.top = h; }
  406. menu.css(cfg);
  407. }
  408. }
  409. //保存按钮
  410. $('#logout>a:contains("保存")').click(function(){
  411. save_file();
  412. return false;
  413. });
  414. //刷新按钮
  415. $('#logout>a:contains("刷新")').click(function(){
  416. window.location.href = window.location.pathname;
  417. return false;
  418. });
  419. //重置按钮
  420. $('#logout>a:contains("重置")').click(function(){
  421. alertify.confirm('是否修改 {$curr_file_path} 程序文件名?', function (e) {
  422. if ( !e ) { return 'cancel'; }
  423. re_name($(''), '{$curr_file_path}');
  424. });
  425. return false;
  426. });
  427. //新建 *** 作
  428. function create_new(obj, path) {
  429. if ( !obj || !path ) { return false; }
  430. alertify.prompt('请输入新建文件或文件夹名:', function (e, str) {
  431. if ( !e || !str ) { return false; }
  432. alertify.log('正在 *** 作中...');
  433. $('#dir_tree #on').removeAttr('loaded').removeAttr('id');
  434. $.post(window.location.href, {create:str,target:path}, function(data){
  435. if ( data.msg && 'success' == data.result ) {
  436. alertify.success(data.msg);
  437. if ( obj.attr('class') == 'dir' ) {
  438. load(obj); //重新加载子节点
  439. } else {
  440. load(obj.parent().parent());
  441. }
  442. } else {
  443. alertify.error(data.msg);
  444. }
  445. }, 'json');
  446. });
  447. }
  448. //浏览 *** 作
  449. function preview(obj, path) {
  450. if ( !obj || !path ) { return false; }
  451. window.open(path, '_blank');
  452. }
  453. //重命名
  454. function re_name(obj, path) {
  455. if ( !obj || !path ) { return false; }
  456. alertify.prompt('重命名 '+path+' 为:', function (e, str) {
  457. if ( !e || !str ) { return false; }
  458. alertify.log('正在 *** 作中...');
  459. $.post(window.location.href, {rename:str,target:path}, function(data){
  460. if ( data.msg && 'success' == data.result ) {
  461. alertify.success(data.msg);
  462. if ( data.redirect ) {
  463. window.location.href = data.redirect;
  464. }
  465. if ( data.new_name ) {
  466. obj.children('span').first().text(data.new_name);
  467. obj.attr('path', data.new_path);
  468. }
  469. } else {
  470. alertify.error(data.msg);
  471. }
  472. }, 'json');
  473. });
  474. }
  475. //删除文件动作
  476. function del_file(obj, path) {
  477. if ( !obj || !path ) { return false; }
  478. alertify.confirm('您确定要删除:'+path+' 吗?', function (e) {
  479. if ( !e ) { return 'cancel'; }
  480. alertify.log('正在删除中...');
  481. $.post(window.location.href, {del:path}, function(data){
  482. if ( data.msg && 'success' == data.result ) {
  483. alertify.success(data.msg);
  484. obj.remove();
  485. } else {
  486. alertify.error(data.msg);
  487. }
  488. }, 'json');
  489. });
  490. }
  491. });
  492. HTMLSTR;
  493. //判断是否已经登录
  494. if ( !is_logged() ) {
  495. die(str_replace(
  496. array('【标题】', '【动作】'),
  497. array('请输入您第一次设置的密码!', '登录'),
  498. $first
  499. ));
  500. } else {
  501. echo $html;
  502. }
  503. 复制代码

    欢迎分享,转载请注明来源:内存溢出

    原文地址: http://www.outofmemory.cn/zaji/329599.html

    (0)
    打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
    上一篇 2018-05-31
    下一篇 2018-05-31

    发表评论

    登录后才能评论

    评论列表(0条)

    保存