-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
146 lines (131 loc) · 4.36 KB
/
index.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>fileTreeW3</title>
<link rel="stylesheet" href="node_modules/jstree/dist/themes/default-dark/style.min.css" />
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/4.7.0/css/font-awesome.min.css">
</head>
<body>
<h1>FileTreeW3</h1>
<div class="row">
<div id="tree" class="column">
<h2>File Tree</h2>
<button onclick="expand()"><i class="fa fa-expand" aria-hidden="true"></i>expand All</button>
<button onclick="collapse()"><i class="fa fa-compress" aria-hidden="true">collapse All</i></button>
<div id="jstree"></div>
</div>
<div class="column" style="
position: fixed;
/* float: right; */
right: 10px;
">
<h2>File Preview</h2>
<div id="data">Supports images and text files. (text files will be truncated after 1000 characters)</div>
<img id="img" src="" height="100%"/>
</div>
</div>
<script src="jquery/jquery.min.js"></script>
<script src="jstree/jstree.min.js"></script>
<script>
function expand() {
$('#jstree').jstree('open_all')
}
function collapse() {
$('#jstree').jstree('close_all')
}
</script>
<script>
const ws = new WebSocket('ws://localhost:9898/')
let treeArr = []
let treeObj = {}
let filesSet = new Set()
function addPath(arr, obj = {}){
let component = arr.shift()
let icon = component.split("~")[1]
component = component.split("~")[0]
let current = obj[component] || (obj[component] = {text:component, icon:icon})
if (arr.length) {
addPath(arr, current.children || (current.children = {}))
}
return obj
}
function makeArray(obj){
let arr = Object.values(obj)
arr.filter(item => item.children).forEach(item => {
item.children = makeArray(item.children)
})
return arr
}
ws.onmessage = function(e) {
if (e.data.startsWith("tree")){
let treedata = e.data.slice(4)
let command = JSON.parse(treedata).event
let path = JSON.parse(treedata).path
if (command =="add"){
filesSet.add(path + "~jstree-file")
}else if (command =="addDir"){
filesSet.add(path + "~jstree-folder")
}else if(command == "unlink"){
filesSet.delete(path + "~jstree-file")
}else if(command == "unlinkDir"){
filesSet.delete(path + "~jstree-folder")
}
let files = (Array.from(filesSet))
let treeObj = files.reduce((obj, path) => addPath(path.split('/'), obj), {})
treeArr = makeArray(treeObj)
$('#jstree').remove()
let div = document.createElement('div')
div.setAttribute("id", "jstree")
let treediv = document.getElementById("tree")
treediv.appendChild(div)
$('#jstree').jstree({'core' : {
"themes": {
"name": "default-dark",
"dots": true,
"icons": true
},
'data' : treeArr}})
$('#jstree').on("changed.jstree", function (e, data) {
let path = $('#jstree').jstree(true).get_path(data.node,"/");
ws.send(path)
})
}else if(e.data.startsWith("data")){
let data = e.data.slice(4)
document.getElementById("data").innerText = data
document.getElementById("img").src = ""
}else if(e.data.startsWith("imgs")){
let imgs = e.data.slice(4)
document.getElementById("img").src = imgs
document.getElementById("data").innerText = ""
}
}
</script>
<style>
body {
background-color:#333;
margin-left: 40px;
color:white;
}
h1, h2 {
color:darkgrey;
}
* {
box-sizing: border-box;
}
/* Create two equal columns that floats next to each other */
.column {
float: left;
width: 50%;
padding: 10px;
height: 300px; /* Should be removed. Only for demonstration */
}
/* Clear floats after the columns */
.row:after {
content: "";
display: table;
clear: both;
}
</style>
</body>
</html>