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