-
Notifications
You must be signed in to change notification settings - Fork 0
/
frontend.js
96 lines (80 loc) · 2.5 KB
/
frontend.js
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
var root = document.getElementById('root');
// Create a new XHR for communicating requests to our Tessel server
var req = new XMLHttpRequest();
req.open('GET', '/dir');
req.onload = function(e) {
if (req.status === 200) {
var response = JSON.parse(req.responseText);
var files = response.files;
files.forEach(function(file) {
var link = document.createElement('a');
var href = document.createAttribute('href');
var li = document.createElement('li');
var linkClass = document.createAttribute('class');
link.innerHTML = file;
if (file.includes('.')) {
linkClass.value = 'file';
link.addEventListener('click', handleFile);
} else {
linkClass.value = 'folder';
link.addEventListener('click', handleDir);
}
href.value = file;
link.setAttributeNode(href);
link.setAttributeNode(linkClass);
li.appendChild(link);
root.appendChild(li);
});
}
}
req.send();
// and delete the `li` in the html?
function handleDir(e) {
// aint trying to change pages rn
e.preventDefault();
var currentLinks = document.getElementsByTagName('li');
clearOut(currentLinks);
var req = new XMLHttpRequest();
var parentDir = this.pathname;
req.open('GET', parentDir);
req.onload = function(e) {
if (req.status === 200) {
var response = JSON.parse(req.responseText);
var files = response.files;
files.forEach(function(file) {
var link = document.createElement('a');
var href = document.createAttribute('href');
var li = document.createElement('li');
var linkClass = document.createAttribute('class');
link.innerHTML = file;
if (file.includes('.')) {
linkClass.value = 'file';
link.addEventListener('click', handleFile);
} else {
linkClass.value = 'folder';
link.addEventListener('click', handleDir);
}
href.value = parentDir + '/' + file;
link.setAttributeNode(href);
link.setAttributeNode(linkClass);
li.appendChild(link);
root.appendChild(li);
});
}
}
req.send();
}
function handleFile(e) {
e.preventDefault();
var iframe = document.getElementById('content');
var src = document.createAttribute('src');
var title = document.getElementById('file-title');
src.value = this.href;
title.innerText = this.text;
iframe.setAttributeNode(src);
}
function clearOut(links) {
for (var i = 0; i < links.length; i++) {
links[i].remove();
}
}