index.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. <?php
  2. require_once(dirname(__FILE__) . '/class.db.php');
  3. require_once(dirname(__FILE__) . '/class.tree.php');
  4. if(isset($_GET['operation'])) {
  5. $fs = new tree(db::get('mysqli://root@127.0.0.1/test'), array('structure_table' => 'tree_struct', 'data_table' => 'tree_data', 'data' => array('nm')));
  6. try {
  7. $rslt = null;
  8. switch($_GET['operation']) {
  9. case 'analyze':
  10. var_dump($fs->analyze(true));
  11. die();
  12. break;
  13. case 'get_node':
  14. $node = isset($_GET['id']) && $_GET['id'] !== '#' ? (int)$_GET['id'] : 0;
  15. $temp = $fs->get_children($node);
  16. $rslt = array();
  17. foreach($temp as $v) {
  18. $rslt[] = array('id' => $v['id'], 'text' => $v['nm'], 'children' => ($v['rgt'] - $v['lft'] > 1));
  19. }
  20. break;
  21. case "get_content":
  22. $node = isset($_GET['id']) && $_GET['id'] !== '#' ? $_GET['id'] : 0;
  23. $node = explode(':', $node);
  24. if(count($node) > 1) {
  25. $rslt = array('content' => 'Multiple selected');
  26. }
  27. else {
  28. $temp = $fs->get_node((int)$node[0], array('with_path' => true));
  29. $rslt = array('content' => 'Selected: /' . implode('/',array_map(function ($v) { return $v['nm']; }, $temp['path'])). '/'.$temp['nm']);
  30. }
  31. break;
  32. case 'create_node':
  33. $node = isset($_GET['id']) && $_GET['id'] !== '#' ? (int)$_GET['id'] : 0;
  34. $temp = $fs->mk($node, isset($_GET['position']) ? (int)$_GET['position'] : 0, array('nm' => isset($_GET['text']) ? $_GET['text'] : 'New node'));
  35. $rslt = array('id' => $temp);
  36. break;
  37. case 'rename_node':
  38. $node = isset($_GET['id']) && $_GET['id'] !== '#' ? (int)$_GET['id'] : 0;
  39. $rslt = $fs->rn($node, array('nm' => isset($_GET['text']) ? $_GET['text'] : 'Renamed node'));
  40. break;
  41. case 'delete_node':
  42. $node = isset($_GET['id']) && $_GET['id'] !== '#' ? (int)$_GET['id'] : 0;
  43. $rslt = $fs->rm($node);
  44. break;
  45. case 'move_node':
  46. $node = isset($_GET['id']) && $_GET['id'] !== '#' ? (int)$_GET['id'] : 0;
  47. $parn = isset($_GET['parent']) && $_GET['parent'] !== '#' ? (int)$_GET['parent'] : 0;
  48. $rslt = $fs->mv($node, $parn, isset($_GET['position']) ? (int)$_GET['position'] : 0);
  49. break;
  50. case 'copy_node':
  51. $node = isset($_GET['id']) && $_GET['id'] !== '#' ? (int)$_GET['id'] : 0;
  52. $parn = isset($_GET['parent']) && $_GET['parent'] !== '#' ? (int)$_GET['parent'] : 0;
  53. $rslt = $fs->cp($node, $parn, isset($_GET['position']) ? (int)$_GET['position'] : 0);
  54. break;
  55. default:
  56. throw new Exception('Unsupported operation: ' . $_GET['operation']);
  57. break;
  58. }
  59. header('Content-Type: application/json; charset=utf-8');
  60. echo json_encode($rslt);
  61. }
  62. catch (Exception $e) {
  63. header($_SERVER["SERVER_PROTOCOL"] . ' 500 Server Error');
  64. header('Status: 500 Server Error');
  65. echo $e->getMessage();
  66. }
  67. die();
  68. }
  69. ?>
  70. <!DOCTYPE html>
  71. <html>
  72. <head>
  73. <meta charset="utf-8">
  74. <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
  75. <title>Title</title>
  76. <meta name="viewport" content="width=device-width" />
  77. <link rel="stylesheet" href="./../../dist/themes/default/style.min.css" />
  78. <style>
  79. html, body { background:#ebebeb; font-size:10px; font-family:Verdana; margin:0; padding:0; }
  80. #container { min-width:320px; margin:0px auto 0 auto; background:white; border-radius:0px; padding:0px; overflow:hidden; }
  81. #tree { float:left; min-width:319px; border-right:1px solid silver; overflow:auto; padding:0px 0; }
  82. #data { margin-left:320px; }
  83. #data textarea { margin:0; padding:0; height:100%; width:100%; border:0; background:white; display:block; line-height:18px; }
  84. #data, #code { font: normal normal normal 12px/18px 'Consolas', monospace !important; }
  85. </style>
  86. </head>
  87. <body>
  88. <div id="container" role="main">
  89. <div id="tree"></div>
  90. <div id="data">
  91. <div class="content code" style="display:none;"><textarea id="code" readonly="readonly"></textarea></div>
  92. <div class="content folder" style="display:none;"></div>
  93. <div class="content image" style="display:none; position:relative;"><img src="" alt="" style="display:block; position:absolute; left:50%; top:50%; padding:0; max-height:90%; max-width:90%;" /></div>
  94. <div class="content default" style="text-align:center;">Select a node from the tree.</div>
  95. </div>
  96. </div>
  97. <script src="//ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js"></script>
  98. <script src="./../../dist/jstree.min.js"></script>
  99. <script>
  100. $(function () {
  101. $(window).resize(function () {
  102. var h = Math.max($(window).height() - 0, 420);
  103. $('#container, #data, #tree, #data .content').height(h).filter('.default').css('lineHeight', h + 'px');
  104. }).resize();
  105. $('#tree')
  106. .jstree({
  107. 'core' : {
  108. 'data' : {
  109. 'url' : '?operation=get_node',
  110. 'data' : function (node) {
  111. return { 'id' : node.id };
  112. }
  113. },
  114. 'check_callback' : true,
  115. 'themes' : {
  116. 'responsive' : false
  117. }
  118. },
  119. 'force_text' : true,
  120. 'plugins' : ['state','dnd','contextmenu','wholerow']
  121. })
  122. .on('delete_node.jstree', function (e, data) {
  123. $.get('?operation=delete_node', { 'id' : data.node.id })
  124. .fail(function () {
  125. data.instance.refresh();
  126. });
  127. })
  128. .on('create_node.jstree', function (e, data) {
  129. $.get('?operation=create_node', { 'id' : data.node.parent, 'position' : data.position, 'text' : data.node.text })
  130. .done(function (d) {
  131. data.instance.set_id(data.node, d.id);
  132. })
  133. .fail(function () {
  134. data.instance.refresh();
  135. });
  136. })
  137. .on('rename_node.jstree', function (e, data) {
  138. $.get('?operation=rename_node', { 'id' : data.node.id, 'text' : data.text })
  139. .fail(function () {
  140. data.instance.refresh();
  141. });
  142. })
  143. .on('move_node.jstree', function (e, data) {
  144. $.get('?operation=move_node', { 'id' : data.node.id, 'parent' : data.parent, 'position' : data.position })
  145. .fail(function () {
  146. data.instance.refresh();
  147. });
  148. })
  149. .on('copy_node.jstree', function (e, data) {
  150. $.get('?operation=copy_node', { 'id' : data.original.id, 'parent' : data.parent, 'position' : data.position })
  151. .always(function () {
  152. data.instance.refresh();
  153. });
  154. })
  155. .on('changed.jstree', function (e, data) {
  156. if(data && data.selected && data.selected.length) {
  157. $.get('?operation=get_content&id=' + data.selected.join(':'), function (d) {
  158. $('#data .default').html(d.content).show();
  159. });
  160. }
  161. else {
  162. $('#data .content').hide();
  163. $('#data .default').html('Select a file from the tree.').show();
  164. }
  165. });
  166. });
  167. </script>
  168. </body>
  169. </html>