-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.html
103 lines (89 loc) · 3.08 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
<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<link id="pagestyle" rel="stylesheet" href="dir_list.css" type="text/css" />
<title>Web2</title>
</head>
<body>
<h1>CS2003 Directory Lister (Remote File System Browser) </h1>
<h2>Test Directory </h2>
<hr />
<div id="string"></div>
<hr />
<script src="dir_list_json.js"></script>
<script>
let wsUrl = "ws://" + location.hostname + ":" + location.port;
let ws = new WebSocket(wsUrl);
/**
* On receiving a message from the server, if it's directory information it displays
* it on the html page.
*/
ws.onmessage = function (m) {
let d_rx = JSON.parse(m.data);
console.log(d_rx);
if (d_rx["response"] == "dirinfo") {
console.log(d_rx["info"]);
try {
let markup = dirInfo2Markup(d_rx["info"]);
//console.log(markup);
document.getElementById("string").innerHTML = markup;
} catch (error) {
document.getElementById("string").innerHTML = error.message;
}
}
}
//If the user presses the back button the function gets the parent directory from the
//directoryname tag and requests its information from the server.
function back() {
let currentDirectory = document.getElementsByTagName("directoryname")[0].innerHTML;
let position = currentDirectory.lastIndexOf("/");
let parent = currentDirectory.slice(0, position);
try {
let request = { "request": "dirinfo", "dirpath": parent };
let string = JSON.stringify(request);
if (string.includes("/test1Dir")) {
ws.send(string);
} else {
alert("Client not allowed to access beyond root directory");
}
console.log(string);
} catch (error) {
console.log(error);
}
}
/*when a folder is pressed, it sends its own id(which is always its name), appends it
*appends it to the directory name and sends a request to the server on its socket.
*/
function getDirectory(localdir) {
try {
let directory = document.getElementsByTagName("directoryname")[0].innerHTML;
let y = { "request": "dirinfo", "dirpath": directory + "/" + localdir };
let string = JSON.stringify(y);
ws.send(string);
} catch (error) {
console.log(error);
}
}
/*Each of the column headers sends a string representing its column to this function whn
it is clicked. This function then goes through the visible html and makes html of the filetyp
hidden.
*/
function hide(filetype) {
let column = document.getElementsByTagName(filetype);
for (i = 1; i < column.length; ++i) {
if (column[i].style.visibility != "hidden") {
column[i].style.visibility = "hidden";
} else {
column[i].style.visibility = "visible";
}
}
console.log(filetype + " column has been hidden");
}
//Informs the user if the socket closes, so connection to the server is lost.
ws.onclose = function (e) {
alert("WebSocket closed.");
}
</script>
</body>
</html>