-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtreeview.js
295 lines (266 loc) · 7.43 KB
/
treeview.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
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
/* shorthand functions (createElement is defined at bottom)*/
const div = (props, ...children) => createElement("div", props, ...children);
const ul = (props, ...children) => createElement("ul", props, ...children);
const li = (props, ...children) => createElement("li", props, ...children);
const i = (props, ...children) => createElement("i", props, ...children);
const span = (props, ...children) => createElement("span", props, ...children);
const header = (props, ...children) =>
createElement("header", props, ...children);
const p = (props, ...children) => createElement("p", props, ...children);
const section = (props, ...children) =>
createElement("section", props, ...children);
const button = (props, ...children) =>
createElement("button", props, ...children);
const fileTypeIcons = {
// Code Files
js: "code",
json: "description",
css: "style",
html: "language",
xml: "code",
jsx: "code",
ts: "code",
tsx: "code",
php: "code",
py: "code",
rb: "code",
java: "code",
cpp: "code",
c: "code",
cs: "code",
swift: "code",
go: "code",
rs: "code", // Rust
r: "code", // R
// Image Files
png: "image",
jpg: "image",
jpeg: "image",
gif: "image",
bmp: "image",
svg: "image",
webp: "image",
tiff: "image",
// Document Files
md: "article",
txt: "article",
pdf: "picture_as_pdf",
doc: "description",
docx: "description",
xls: "description",
xlsx: "description",
csv: "description",
ppt: "description",
pptx: "description",
odt: "description", // OpenDocument Text
ods: "description", // OpenDocument Spreadsheet
odp: "description", // OpenDocument Presentation
rtf: "description", // Rich Text Format
tex: "description", // LaTeX
// Book Files
epub: "book",
mobi: "book",
azw: "book",
azw3: "book",
fb2: "book",
ibooks: "book",
chm: "book",
// Audio Files
mp3: "audiotrack",
wav: "audiotrack",
ogg: "audiotrack",
flac: "audiotrack",
aac: "audiotrack",
wma: "audiotrack",
m4a: "audiotrack",
aiff: "audiotrack",
alac: "audiotrack",
pcm: "audiotrack",
dsd: "audiotrack",
// Video Files
mp4: "videocam",
mkv: "videocam",
webm: "videocam",
avi: "videocam",
mov: "videocam",
wmv: "videocam",
flv: "videocam",
f4v: "videocam",
m4v: "videocam",
mpeg: "videocam",
mpg: "videocam",
mpe: "videocam",
m1v: "videocam",
m2v: "videocam",
ts: "videocam",
ogv: "videocam",
vob: "videocam",
swf: "videocam",
rmvb: "videocam",
// Archive Files
zip: "archive",
rar: "archive",
tar: "archive",
gz: "archive",
bz2: "archive",
"7z": "archive",
iso: "archive",
// Executable Files
exe: "computer",
msi: "computer",
sh: "terminal",
bat: "terminal",
deb: "computer",
dmg: "computer",
rpm: "computer",
jar: "computer",
// Android Files
apk: "android",
aab: "android",
xapk: "android",
obb: "android",
// Fonts
ttf: "font_download",
otf: "font_download",
woff: "font_download",
woff2: "font_download",
// Windows System Files
ini: "settings",
inf: "settings",
reg: "settings",
cmd: "settings",
ps1: "settings",
psd1: "settings",
psm1: "settings",
dll: "settings",
sys: "settings",
cab: "settings",
// Others
pt: "memory",
// Default
default: "insert_drive_file"
};
function getFileIcon(fileName) {
const extension = fileName.split('.').pop().toLowerCase();
return fileTypeIcons[extension] || fileTypeIcons.default;
}
/* File */
const File = ({ name }) => {
return div(
{ className: "file" },
i({ className: "material-icons", style: "opacity: 0;" }, "arrow_right"),
i({ className: "material-icons" }, getFileIcon(name)),
span(null, name)
);
};
/* Folder */
const openedFolderIcon = "folder_open";
const closedFolderIcon = "folder";
const openedArrowIcon = "arrow_drop_down";
const closedArrowIcon = "arrow_right";
function changeOpened(event) {
const folderHeader = event.target.classList.contains("folder-header")
? event.target
: event.target.parentElement;
const opened = folderHeader.getAttribute("opened") == "true";
const newOpened = !opened;
let icons = folderHeader.querySelectorAll(".material-icons");
icons.forEach(icon => {
if (/arrow/i.test(icon.textContent)) {
icon.textContent = newOpened ? openedArrowIcon : closedArrowIcon;
} else {
icon.textContent = newOpened ? openedFolderIcon : closedFolderIcon;
}
});
try {
const sibling = folderHeader.nextElementSibling;
if (newOpened) {
sibling.classList.remove("hide");
} else {
sibling.classList.add("hide");
}
} catch (e) {
console.warn(`No sibling of elem ${folderHeader} found ...`);
}
folderHeader.setAttribute("opened", newOpened);
}
const Folder = (props, ...children) => {
const opened = props.opened || false;
const arrowIcon = opened ? openedArrowIcon : closedArrowIcon;
const folderIcon = opened ? openedFolderIcon : closedFolderIcon;
const folderName = props.name || "unknown";
return div(
{ className: "folder" },
header(
{
onClick: changeOpened,
className: "folder-header",
opened: opened
},
i({ className: "material-icons" }, arrowIcon),
i({ className: "material-icons" }, folderIcon),
span(null, folderName)
),
ul({ className: opened ? "" : "hide" }, ...children)
);
};
/* My react-clone mini library */
function appendChildren(parent, children) {
for (let child of children) {
if (!child) continue;
switch (typeof child) {
case "string":
const el = document.createTextNode(child);
parent.appendChild(el);
break;
default:
parent.appendChild(child);
break;
}
}
}
function setStyle(el, style) {
if (typeof style == "string") {
el.setAttribute("style", style);
} else {
Object.assign(el.style, style);
}
}
function setClass(el, className) {
className.split(/\s/).forEach(element => {
if (element) {
el.classList.add(element);
}
});
}
function setProps(el, props) {
const eventRegex = /^on([a-z]+)$/i;
for (let propName in props) {
if (!propName) continue;
if (propName === "style") {
setStyle(el, props[propName]);
} else if (propName === "className") {
setClass(el, props[propName]);
} else if (eventRegex.test(propName)) {
const eventToListen = propName.replace(eventRegex, "$1").toLowerCase();
el.addEventListener(eventToListen, props[propName]);
} else {
el.setAttribute(propName, props[propName]);
}
}
}
//type, [props], [...children]
function createElement(type, props, ...children) {
if (typeof type === "function") {
return type(props);
} else {
const el = document.createElement(type);
if (props && typeof props === "object") {
setProps(el, props);
}
if (children) {
appendChildren(el, children);
}
return el;
}
}