-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdirfaker.js
191 lines (178 loc) · 3.59 KB
/
dirfaker.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
directory_tree = {
name:"~",
tag: "Your Home Directory",
type: "folder",
children: {
"Documents": {
type: "folder",
tag: "where the documents go",
children: {
"document": {
type: "wordfile"
},
"school": {
type: "folder",
children: {}
}
}
},
"Photos": {
type: "folder",
children: {
"photo.png": {
type: "photofile"
},
"photo2.png": {
type: "photofile"
},
"photo3.png": {
type: "photofile"
}
}
},
"Notes": {
type: "folder",
children: {
"todo_list": {
type: "textfile"
},
"murderlist": {
type: "textfile"
},
"csc318_notes_blob": {
type: "textfile"
},
"really_real_notes": {
type: "textfile"
},
"tfw_no_ports": {
type: "textfile"
},
"fake_text_file": {
type: "textfile"
}
}
},
"Reading List": {
type: "folder",
children: {}
},
"Downloads": {
type: "folder",
children: {}
},
"School": {
type: "folder",
children: {}
},
"Misc": {
type: "folder",
children: {}
},
".config": {
type: "dotfolder",
children: {}
},
".vimrc": {
type: "dotfile"
},
".bashrc": {
type: "dotfile"
},
"notes.md": {
type: "textfile"
},
"dolan.jpg": {
type: "photofile"
},
"achive.zip": {
type: "zipfile"
},
"My Mother's Straightjacket.mp3": {
type: "audiofile"
},
"Why I Should Be President.odt": {
type: "wordfile"
}
}
}
directory_tree_backup = $.extend(true, {}, directory_tree);
function reset_file_tree() {
console.log("reset_file_tree")
directory_tree = $.extend(true, {}, directory_tree_backup);
console.log(directory_tree)
};
path = '~'
cwd = directory_tree;
function cwdd(c, spl) {
if (spl.length == 0){
return c
} else {
c = c.children[spl.splice(0,1)]
if (c == undefined) {
throw UserException("error traversing file tree")
}
return cwdd(c, spl)
}
}
function compress_path(spl) {
for (var i=0; i<spl.length; i++) {
if (spl[i] == "." || spl[i] == "") {
spl.splice(i,1);
i--;
} else if (spl[i] == "..") {
spl.splice(i-1, 2);
i-=2;
}
}
return spl
}
function update_cwd(newpath) {
console.log("updating cwd :\""+newpath+"\"");
spl = newpath.split("/");
spl_before = path.split("/");
joined = spl_before.concat(spl);
joined = compress_path(joined);
joined.splice(0,1);
console.log(joined);
var nextpath;
if (joined.length > 0) {
nextpath = "~/"+joined.join("/");
} else {
nextpath = "~"
}
temp = null
try {
temp = cwdd(directory_tree, joined)
path = nextpath;
} catch(err) {
return 0
}
cwd = temp
return 1
}
function update_cwd_tree(newpath) {
if (update_cwd(newpath)) {
var spath = path.split("/")
var lpath = spath[spath.length-1]
$("#sidebar h2").text(lpath);
$("#navbar b").text(path+"/");
$("#sidebar ul").empty()
if (cwd.tag != undefined) {
$("#navbar span").text(": " + cwd.tag);
} else {
$("#navbar span").text("");
}
for (key in cwd.children) {
$("#sidebar ul").append($("<li data-filetype="+cwd.children[key].type+">"+key+"</li>"));
}
return 1
}
return 0
}
function deletefile(pth) {
var x = cwd.children[pth]
delete cwd.children[pth];
return (x == undefined)
}
update_cwd_tree("");