-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtree.js
190 lines (173 loc) · 6.66 KB
/
tree.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
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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
var baseURL = {
api: 'https://api.github.com/repos/TheGrandCircuit/IT2/',
raw: 'https://raw.githubusercontent.com/TheGrandCircuit/IT2/master/',
page: 'https://thegrandcircuit.github.io/IT2/',
parameters: {
token: 'access_token=8301ab86754739755fce14e5a8598761ece47714'
}
}
var Descriptions = {}
var Tree = {}
async function getSha() {
return new Promise(resolve => {
var xhr = new XMLHttpRequest()
xhr.open('GET', baseURL.api + 'branches?' + baseURL.parameters.token, true)
xhr.onload = function () {
if (this.status == 200) {
resolve(
JSON.parse(this.response)[0].commit.sha
)
} else {
alert(this.response)
}
}
xhr.send()
})
}
/**@description Gets the raw treedata */
async function getRawTree() {
var tree = await getSha()
return new Promise(resolve => {
var xhr = new XMLHttpRequest()
xhr.open('GET', baseURL.api + 'git/trees/' + tree + '?recursive=1&' + baseURL.parameters.token, true)
xhr.onload = function () {
if (this.status == 200) {
resolve(JSON.parse(this.response))
} else {
alert(this.response)
}
}
xhr.send()
})
}
/**@description Parses the raw tree */
async function parseTree(rawTree) {
return new Promise(async resolve => {
var tree = { content: {}, sha: rawTree.sha, url: rawTree.url, path: 'Root', type: 'tree' }
for (let i = 0; i < rawTree.tree.length; i++) {
const element = rawTree.tree[i];
/**@type {Array} */
var path = element.path.split('/')
if (path[path.length - 1] == 'DESCRIPTION.TXT') {
loadDesc(await getTextFile(element.path))
continue
}
var target = tree
for (let j = 0; j < path.length - 1; j++) {
target = target.content[path[j]]
}
element.content = {}
target.content[path[path.length - 1]] = element
}
var tree2 = { content: {}, sha: rawTree.sha, url: rawTree.url, path: 'tree2' }
tree = { content: { tree: tree }, type: 'tree' }
resolve(tree)
})
}
/**@description loads the tree to the DOM */
async function loadTree() {
var tree = await parseTree(await getRawTree())
return new Promise(resolve => {
var div = document.getElementById('tree')
/**@param item
* @param {HTMLElement} parent */
function loadContent(item, parent, indent) {
var files = []
for (const key in item.content) {
const element = item.content[key];
/**@description main element (contains name and content) */
var DOMElement = document.createElement('div')
DOMElement.classList.add('item')
DOMElement.id = element.path
/**@description button to expand a folder */
var Button = document.createElement('div')
Button.classList.add('treeButton')
Button.classList.add('closed')
DOMElement.appendChild(Button)
//sets the file icon
Button.innerHTML = '▢'
//if element is a folder
//set the icon to an arrow and add functionality
if (element.type == 'tree') {
Button.innerHTML = '▶'
Button.addEventListener('click', e => {
//open the folder
if (e.target.classList.contains('closed')) {
e.path[1].classList.remove('closed')
e.target.classList.remove('closed')
e.target.classList.add('open')
e.target.innerHTML = '▼'
//close the folder
} else if (e.target.classList.contains('open')) {
e.target.classList.remove('open')
e.path[1].classList.add('closed')
e.target.classList.add('closed')
e.target.innerHTML = '▶'
}
})
}
/**@description contains the name of the file/folder */
var name = document.createElement('div')
name.innerHTML = element.path.split('/').pop()
name.dataset.path = element.path
name.classList.add('name')
name.addEventListener('click', e => {
viewDesc(e.target.dataset.path)
})
//add a link
name.addEventListener('dblclick', e => {
window.open(baseURL.page + e.target.dataset.path)
})
DOMElement.appendChild(name)
//if folder load the contents
if (element.type == 'tree') {
DOMElement.classList.add('tree')
DOMElement.classList.add('closed')
parent.appendChild(DOMElement)
loadContent(element, DOMElement, indent + 1)
}
else {
files.push(DOMElement)
}
}
//add files to the end of the list
for (let i = 0; i < files.length; i++) {
parent.appendChild(files[i])
}
}
loadContent(tree, div, 1)
resolve()
})
}
/**@description gets the text content of a file given a path */
async function getTextFile(path) {
return new Promise(resolve => {
var xhr = new XMLHttpRequest()
xhr.open('GET', baseURL.raw + path, true)
xhr.onload = function () {
if (this.status == 200) {
resolve(this.response)
} else {
alert(this.response)
}
}
xhr.send()
})
}
/**@description parses and collects descriptions in one place */
async function loadDesc(text) {
var descriptions = text.split('#')
for (let i = 1; i < descriptions.length; i += 2) {
Descriptions[descriptions[i].split(';')[0]] = descriptions[i + 1].replace('\n', '').trim();
}
}
/**@description puts description in an element(ID="desc") given a path */
function viewDesc(path) {
var box = document.getElementById('desc')
box.innerHTML = Descriptions[path] || 'This item has no description.'
}
/**@param {XMLHttpRequest} xhr */
function buildAuthHeader(xhr){
xhr.setRequestHeader('Authorization', 'token '+localStorage.authToken)
}
loadTree()