2
* Client-side interaction for both file browser and editor.
3
* PROOF-OF-CONCEPT ONLY
10
/* Removes all children of a given element */
11
function removechildren(elem)
13
while (elem.lastChild != null)
14
elem.removeChild(elem.lastChild);
17
/* Clears all dynamic data from the page, so it can be refilled. */
20
path = document.getElementById("path");
22
files = document.getElementById("files");
23
removechildren(files);
26
function makebutton(text, action)
28
button = document.createElement("input");
29
button.setAttribute("type", "button");
30
button.setAttribute("value", text);
31
button.setAttribute("onclick", action);
35
/* Makes a TD element with text in it */
38
td = document.createElement("td");
39
td.appendChild(document.createTextNode(text));
43
function maketdwithlink(text, href, onclick)
45
td = document.createElement("td");
46
link = document.createElement("a");
47
link.setAttribute("href", href);
49
link.setAttribute("onclick", onclick);
50
link.appendChild(document.createTextNode(text));
55
function maketdwithactions(filename)
57
td = document.createElement("td");
58
td.appendChild(makebutton("Delete", "rm(\"" + filename + "\")"));
59
td.appendChild(makebutton("Rename", "rename(\"" + filename + "\")"));
60
td.appendChild(makebutton("Edit", "edit(\"" + filename + "\")"));
64
/* Converts a list of directories into a path name, with a slash at the end */
65
function pathlisttopath(pathlist)
68
for each (dir in pathlist)
75
function presentpath(pathlist)
77
path = document.getElementById("path");
79
pathlist = ["home"].concat(pathlist);
83
/* Create all of the paths */
84
for each (dir in pathlist)
89
/* Make an 'a' element */
90
link = document.createElement("a");
91
link.setAttribute("href", "#");
92
link.setAttribute("onclick", "nav(" + JSON.stringify(navlist) + ")");
93
link.appendChild(document.createTextNode(dir));
94
path.appendChild(link);
95
path.appendChild(document.createTextNode("/"));
99
/* Presents a directory listing to the page.
100
* pathlist is an array of strings, the names of the directories in the path.
101
* listing is a list of objects where each object is a file (containing
102
* several fields about the file).
104
function presentlisting(pathlist, listing)
106
presentpath(pathlist);
107
files = document.getElementById("files");
109
/* Create all of the files */
110
for each (file in listing)
112
/* Make a 'tr' element */
113
row = document.createElement("tr");
116
row.appendChild(maketd("dir"));
117
navlink = JSON.stringify(navlist.concat([file.filename]));
118
navlink = "nav(" + navlink + ")";
119
row.appendChild(maketdwithlink(file.filename, "#", navlink));
120
row.appendChild(maketd(file.size));
121
row.appendChild(maketd(file.mtime));
122
row.appendChild(maketd(""));
126
row.appendChild(maketd("file"));
127
row.appendChild(maketd(file.filename));
128
row.appendChild(maketd(file.size));
129
row.appendChild(maketd(file.mtime));
130
row.appendChild(maketdwithactions(file.filename));
132
files.appendChild(row);
139
function obj_to_query_string(pagename, obj)
152
qs += escape(key) + "=" + escape(obj[key]);
159
function nav(pathlist)
161
gpathlist = pathlist;
167
path = pathlisttopath(gpathlist);
168
url = obj_to_query_string("files.py/ls", {"path" : path});
169
var xmlhttp = new XMLHttpRequest();
170
// false -> SYNCHRONOUS (wait for response)
171
// (No need for a callback function)
172
xmlhttp.open("GET", url, false);
174
listing = JSON.parse(xmlhttp.responseText);
176
alert("An error occured");
177
else if ("error" in listing)
178
alert("Error: " + listing.error);
182
presentlisting(gpathlist, listing);
186
function rm(filename)
188
path = pathlisttopath(gpathlist);
189
url = obj_to_query_string("files.py/rm",
190
{"path" : path, "filename" : filename});
191
var xmlhttp = new XMLHttpRequest();
192
// false -> SYNCHRONOUS (wait for response)
193
// (No need for a callback function)
194
xmlhttp.open("GET", url, false);
196
listing = JSON.parse(xmlhttp.responseText);
198
alert("An error occured");
199
else if ("error" in listing)
200
alert("Error: " + listing.error);
204
presentlisting(gpathlist, listing);
208
function rename(fromfilename)
210
tofilename = prompt("Rename file \"" + fromfilename + "\" to?");
211
path = pathlisttopath(gpathlist);
212
url = obj_to_query_string("files.py/rename",
213
{"path" : path, "fromfilename" : fromfilename,
214
"tofilename" : tofilename});
215
var xmlhttp = new XMLHttpRequest();
216
// false -> SYNCHRONOUS (wait for response)
217
// (No need for a callback function)
218
xmlhttp.open("GET", url, false);
220
listing = JSON.parse(xmlhttp.responseText);
222
alert("An error occured");
223
else if ("error" in listing)
224
alert("Error: " + listing.error);
228
presentlisting(gpathlist, listing);
234
window.location = "edit.html";
237
function edit(filename)
239
window.location = "edit.html?filename="
240
+ pathlisttopath(gpathlist) + filename;
245
filename = document.getElementById("filename").value;
246
data = document.getElementById("data").value;
247
url = obj_to_query_string("files.py/putfile",
248
{"path" : "", "filename" : filename, "data" : data});
249
var xmlhttp = new XMLHttpRequest();
250
// false -> SYNCHRONOUS (wait for response)
251
// (No need for a callback function)
252
xmlhttp.open("GET", url, false);
254
listing = JSON.parse(xmlhttp.responseText);
256
alert("An error occured");
257
else if ("error" in listing)
258
alert("Error: " + listing.error);
263
filename = document.getElementById("filename").value;
264
if (filename == null || filename == "")
266
url = obj_to_query_string("files.py/getfile",
267
{"path" : "", "filename" : filename});
268
var xmlhttp = new XMLHttpRequest();
269
// false -> SYNCHRONOUS (wait for response)
270
// (No need for a callback function)
271
xmlhttp.open("GET", url, false);
273
document.getElementById("data").value = xmlhttp.responseText;
276
/* Called on page load */
277
function init_browser()
285
document.getElementById("filename").value = getURLParam("filename");
291
/* http://www.11tmr.com/11tmr.nsf/d6plinks/MWHE-695L9Z */
292
function getURLParam(strParamName){
294
var strHref = window.location.href;
295
if ( strHref.indexOf("?") > -1 ){
296
var strQueryString = strHref.substr(strHref.indexOf("?"));
297
var aQueryString = strQueryString.split("&");
298
for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){
300
aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1 ){
301
var aParam = aQueryString[iParam].split("=");
302
strReturn = aParam[1];
307
return unescape(strReturn);