index.php 6.3KB

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