9
by mattgiuca
Added prototypes directory. Added ajax file browser proof-of-concept demo. |
1 |
/* browser.js
|
2 |
* Client-side interaction for both file browser and editor.
|
|
3 |
* PROOF-OF-CONCEPT ONLY
|
|
4 |
*
|
|
5 |
* Author: Matt Giuca
|
|
6 |
*/
|
|
7 |
||
8 |
var gpathlist; |
|
9 |
||
10 |
/* Removes all children of a given element */
|
|
11 |
function removechildren(elem) |
|
12 |
{
|
|
13 |
while (elem.lastChild != null) |
|
14 |
elem.removeChild(elem.lastChild); |
|
15 |
}
|
|
16 |
||
17 |
/* Clears all dynamic data from the page, so it can be refilled. */
|
|
18 |
function clearfiles() |
|
19 |
{
|
|
20 |
path = document.getElementById("path"); |
|
21 |
removechildren(path); |
|
22 |
files = document.getElementById("files"); |
|
23 |
removechildren(files); |
|
24 |
}
|
|
25 |
||
26 |
function makebutton(text, action) |
|
27 |
{
|
|
28 |
button = document.createElement("input"); |
|
29 |
button.setAttribute("type", "button"); |
|
30 |
button.setAttribute("value", text); |
|
31 |
button.setAttribute("onclick", action); |
|
32 |
return button; |
|
33 |
}
|
|
34 |
||
35 |
/* Makes a TD element with text in it */
|
|
36 |
function maketd(text) |
|
37 |
{
|
|
38 |
td = document.createElement("td"); |
|
39 |
td.appendChild(document.createTextNode(text)); |
|
40 |
return td; |
|
41 |
}
|
|
42 |
||
43 |
function maketdwithlink(text, href, onclick) |
|
44 |
{
|
|
45 |
td = document.createElement("td"); |
|
46 |
link = document.createElement("a"); |
|
47 |
link.setAttribute("href", href); |
|
48 |
if (onclick != null) |
|
49 |
link.setAttribute("onclick", onclick); |
|
50 |
link.appendChild(document.createTextNode(text)); |
|
51 |
td.appendChild(link); |
|
52 |
return td; |
|
53 |
}
|
|
54 |
||
11
by mattgiuca
browser prototype: Added sample subversion status, add and commit actions to |
55 |
/* file is a listing object, with fields as returned by the server. */
|
56 |
function maketdwithactions(file) |
|
9
by mattgiuca
Added prototypes directory. Added ajax file browser proof-of-concept demo. |
57 |
{
|
58 |
td = document.createElement("td"); |
|
11
by mattgiuca
browser prototype: Added sample subversion status, add and commit actions to |
59 |
if (!file.isdir) |
60 |
{
|
|
61 |
td.appendChild(makebutton("Delete", "rm(\"" + file.filename + "\")")); |
|
62 |
td.appendChild(makebutton("Rename", "rename(\"" + file.filename + "\")")); |
|
63 |
td.appendChild(makebutton("Edit", "edit(\"" + file.filename + "\")")); |
|
64 |
}
|
|
65 |
if (file.svnstatus == "unversioned") |
|
66 |
td.appendChild(makebutton("Add", "svnadd(\"" + file.filename + "\")")); |
|
67 |
else if (file.svnstatus == "added" || file.svnstatus == "modified") |
|
68 |
td.appendChild(makebutton("Commit", "svncommit(\"" + file.filename + "\")")); |
|
9
by mattgiuca
Added prototypes directory. Added ajax file browser proof-of-concept demo. |
69 |
return td; |
70 |
}
|
|
71 |
||
72 |
/* Converts a list of directories into a path name, with a slash at the end */
|
|
73 |
function pathlisttopath(pathlist) |
|
74 |
{
|
|
75 |
ret = ""; |
|
76 |
for each (dir in pathlist) |
|
77 |
{
|
|
78 |
ret += dir + "/"; |
|
79 |
}
|
|
80 |
return ret; |
|
81 |
}
|
|
82 |
||
83 |
function presentpath(pathlist) |
|
84 |
{
|
|
85 |
path = document.getElementById("path"); |
|
86 |
gpathlist = pathlist; |
|
87 |
pathlist = ["home"].concat(pathlist); |
|
88 |
navlist = []; |
|
89 |
home = true; |
|
90 |
||
91 |
/* Create all of the paths */
|
|
92 |
for each (dir in pathlist) |
|
93 |
{
|
|
94 |
if (home == false) |
|
95 |
navlist.push(dir); |
|
96 |
home = false; |
|
97 |
/* Make an 'a' element */
|
|
98 |
link = document.createElement("a"); |
|
99 |
link.setAttribute("href", "#"); |
|
100 |
link.setAttribute("onclick", "nav(" + JSON.stringify(navlist) + ")"); |
|
101 |
link.appendChild(document.createTextNode(dir)); |
|
102 |
path.appendChild(link); |
|
103 |
path.appendChild(document.createTextNode("/")); |
|
104 |
}
|
|
105 |
}
|
|
106 |
||
107 |
/* Presents a directory listing to the page.
|
|
108 |
* pathlist is an array of strings, the names of the directories in the path.
|
|
109 |
* listing is a list of objects where each object is a file (containing
|
|
110 |
* several fields about the file).
|
|
111 |
*/
|
|
112 |
function presentlisting(pathlist, listing) |
|
113 |
{
|
|
114 |
presentpath(pathlist); |
|
115 |
files = document.getElementById("files"); |
|
116 |
||
117 |
/* Create all of the files */
|
|
118 |
for each (file in listing) |
|
119 |
{
|
|
120 |
/* Make a 'tr' element */
|
|
121 |
row = document.createElement("tr"); |
|
122 |
if (file.isdir) |
|
123 |
{
|
|
124 |
row.appendChild(maketd("dir")); |
|
125 |
navlink = JSON.stringify(navlist.concat([file.filename])); |
|
126 |
navlink = "nav(" + navlink + ")"; |
|
127 |
row.appendChild(maketdwithlink(file.filename, "#", navlink)); |
|
128 |
}
|
|
129 |
else
|
|
130 |
{
|
|
131 |
row.appendChild(maketd("file")); |
|
132 |
row.appendChild(maketd(file.filename)); |
|
133 |
}
|
|
11
by mattgiuca
browser prototype: Added sample subversion status, add and commit actions to |
134 |
row.appendChild(maketd(file.svnstatus)); |
135 |
row.appendChild(maketd(file.size)); |
|
136 |
row.appendChild(maketd(file.mtime)); |
|
137 |
row.appendChild(maketdwithactions(file)); |
|
9
by mattgiuca
Added prototypes directory. Added ajax file browser proof-of-concept demo. |
138 |
files.appendChild(row); |
139 |
}
|
|
140 |
}
|
|
141 |
||
142 |
/* AJAX */
|
|
143 |
||
144 |
function obj_to_query_string(pagename, obj) |
|
145 |
{
|
|
146 |
var hadone = false; |
|
147 |
qs = pagename; |
|
148 |
for (key in obj) |
|
149 |
{
|
|
11
by mattgiuca
browser prototype: Added sample subversion status, add and commit actions to |
150 |
vals = obj[key]; |
151 |
// vals can be an array, to make multiple args with the same name
|
|
152 |
// To handle this, make non-array objects into an array, then loop
|
|
153 |
if (!(vals instanceof Array)) |
|
154 |
vals = [vals]; |
|
155 |
for each (val in vals) |
|
9
by mattgiuca
Added prototypes directory. Added ajax file browser proof-of-concept demo. |
156 |
{
|
11
by mattgiuca
browser prototype: Added sample subversion status, add and commit actions to |
157 |
if (hadone == false) |
158 |
{
|
|
159 |
qs += "?"; |
|
160 |
hadone = true; |
|
161 |
}
|
|
162 |
else
|
|
163 |
qs += "&"; |
|
164 |
qs += encodeURI(key) + "=" + encodeURI(val); |
|
9
by mattgiuca
Added prototypes directory. Added ajax file browser proof-of-concept demo. |
165 |
}
|
166 |
}
|
|
167 |
return qs; |
|
168 |
}
|
|
169 |
||
170 |
/* ACTIONS */
|
|
171 |
||
172 |
function nav(pathlist) |
|
173 |
{
|
|
174 |
gpathlist = pathlist; |
|
175 |
refresh(); |
|
176 |
}
|
|
177 |
||
178 |
function refresh() |
|
179 |
{
|
|
180 |
path = pathlisttopath(gpathlist); |
|
181 |
url = obj_to_query_string("files.py/ls", {"path" : path}); |
|
182 |
var xmlhttp = new XMLHttpRequest(); |
|
183 |
// false -> SYNCHRONOUS (wait for response)
|
|
184 |
// (No need for a callback function)
|
|
185 |
xmlhttp.open("GET", url, false); |
|
186 |
xmlhttp.send(""); |
|
187 |
listing = JSON.parse(xmlhttp.responseText); |
|
188 |
if (listing == null) |
|
189 |
alert("An error occured"); |
|
190 |
else if ("error" in listing) |
|
191 |
alert("Error: " + listing.error); |
|
192 |
else
|
|
193 |
{
|
|
194 |
clearfiles(); |
|
195 |
presentlisting(gpathlist, listing); |
|
196 |
}
|
|
197 |
}
|
|
198 |
||
199 |
function rm(filename) |
|
200 |
{
|
|
201 |
path = pathlisttopath(gpathlist); |
|
202 |
url = obj_to_query_string("files.py/rm", |
|
203 |
{"path" : path, "filename" : filename}); |
|
204 |
var xmlhttp = new XMLHttpRequest(); |
|
205 |
// false -> SYNCHRONOUS (wait for response)
|
|
206 |
// (No need for a callback function)
|
|
207 |
xmlhttp.open("GET", url, false); |
|
208 |
xmlhttp.send(""); |
|
209 |
listing = JSON.parse(xmlhttp.responseText); |
|
210 |
if (listing == null) |
|
211 |
alert("An error occured"); |
|
212 |
else if ("error" in listing) |
|
213 |
alert("Error: " + listing.error); |
|
214 |
else
|
|
215 |
{
|
|
216 |
clearfiles(); |
|
217 |
presentlisting(gpathlist, listing); |
|
218 |
}
|
|
219 |
}
|
|
220 |
||
221 |
function rename(fromfilename) |
|
222 |
{
|
|
223 |
tofilename = prompt("Rename file \"" + fromfilename + "\" to?"); |
|
224 |
path = pathlisttopath(gpathlist); |
|
225 |
url = obj_to_query_string("files.py/rename", |
|
226 |
{"path" : path, "fromfilename" : fromfilename, |
|
227 |
"tofilename" : tofilename}); |
|
228 |
var xmlhttp = new XMLHttpRequest(); |
|
229 |
// false -> SYNCHRONOUS (wait for response)
|
|
230 |
// (No need for a callback function)
|
|
231 |
xmlhttp.open("GET", url, false); |
|
232 |
xmlhttp.send(""); |
|
233 |
listing = JSON.parse(xmlhttp.responseText); |
|
234 |
if (listing == null) |
|
235 |
alert("An error occured"); |
|
236 |
else if ("error" in listing) |
|
237 |
alert("Error: " + listing.error); |
|
238 |
else
|
|
239 |
{
|
|
240 |
clearfiles(); |
|
241 |
presentlisting(gpathlist, listing); |
|
242 |
}
|
|
243 |
}
|
|
244 |
||
11
by mattgiuca
browser prototype: Added sample subversion status, add and commit actions to |
245 |
/* filenames is a list of filenames */
|
246 |
function svnadd(filenames) |
|
247 |
{
|
|
248 |
path = pathlisttopath(gpathlist); |
|
249 |
url = obj_to_query_string("files.py/svnadd", |
|
250 |
{"path" : path, "filename" : filenames}); |
|
251 |
var xmlhttp = new XMLHttpRequest(); |
|
252 |
// false -> SYNCHRONOUS (wait for response)
|
|
253 |
// (No need for a callback function)
|
|
254 |
xmlhttp.open("GET", url, false); |
|
255 |
xmlhttp.send(""); |
|
256 |
listing = JSON.parse(xmlhttp.responseText); |
|
257 |
if (listing == null) |
|
258 |
alert("An error occured"); |
|
259 |
else if ("error" in listing) |
|
260 |
alert("Error: " + listing.error); |
|
261 |
else
|
|
262 |
{
|
|
263 |
clearfiles(); |
|
264 |
presentlisting(gpathlist, listing); |
|
265 |
}
|
|
266 |
}
|
|
267 |
||
268 |
/* filenames is a list of filenames */
|
|
269 |
function svncommit(filenames) |
|
270 |
{
|
|
271 |
path = pathlisttopath(gpathlist); |
|
272 |
url = obj_to_query_string("files.py/svncommit", |
|
273 |
{"path" : path, "filename" : filenames}); |
|
274 |
var xmlhttp = new XMLHttpRequest(); |
|
275 |
// false -> SYNCHRONOUS (wait for response)
|
|
276 |
// (No need for a callback function)
|
|
277 |
xmlhttp.open("GET", url, false); |
|
278 |
xmlhttp.send(""); |
|
279 |
listing = JSON.parse(xmlhttp.responseText); |
|
280 |
if (listing == null) |
|
281 |
alert("An error occured"); |
|
282 |
else if ("error" in listing) |
|
283 |
alert("Error: " + listing.error); |
|
284 |
else
|
|
285 |
{
|
|
286 |
clearfiles(); |
|
287 |
presentlisting(gpathlist, listing); |
|
288 |
}
|
|
289 |
}
|
|
290 |
||
291 |
function svncommitall() |
|
292 |
{
|
|
293 |
path = pathlisttopath(gpathlist); |
|
294 |
url = obj_to_query_string("files.py/svncommit", |
|
295 |
{"path" : path, "commitall" : "yes"}); |
|
296 |
var xmlhttp = new XMLHttpRequest(); |
|
297 |
// false -> SYNCHRONOUS (wait for response)
|
|
298 |
// (No need for a callback function)
|
|
299 |
xmlhttp.open("GET", url, false); |
|
300 |
xmlhttp.send(""); |
|
301 |
listing = JSON.parse(xmlhttp.responseText); |
|
302 |
if (listing == null) |
|
303 |
alert("An error occured"); |
|
304 |
else if ("error" in listing) |
|
305 |
alert("Error: " + listing.error); |
|
306 |
else
|
|
307 |
{
|
|
308 |
clearfiles(); |
|
309 |
presentlisting(gpathlist, listing); |
|
310 |
}
|
|
311 |
}
|
|
312 |
||
9
by mattgiuca
Added prototypes directory. Added ajax file browser proof-of-concept demo. |
313 |
function newfile() |
314 |
{
|
|
315 |
window.location = "edit.html"; |
|
316 |
}
|
|
317 |
||
318 |
function edit(filename) |
|
319 |
{
|
|
320 |
window.location = "edit.html?filename=" |
|
321 |
+ pathlisttopath(gpathlist) + filename; |
|
322 |
}
|
|
323 |
||
324 |
function savefile() |
|
325 |
{
|
|
326 |
filename = document.getElementById("filename").value; |
|
327 |
data = document.getElementById("data").value; |
|
328 |
url = obj_to_query_string("files.py/putfile", |
|
329 |
{"path" : "", "filename" : filename, "data" : data}); |
|
330 |
var xmlhttp = new XMLHttpRequest(); |
|
331 |
// false -> SYNCHRONOUS (wait for response)
|
|
332 |
// (No need for a callback function)
|
|
333 |
xmlhttp.open("GET", url, false); |
|
334 |
xmlhttp.send(""); |
|
335 |
listing = JSON.parse(xmlhttp.responseText); |
|
336 |
if (listing == null) |
|
337 |
alert("An error occured"); |
|
338 |
else if ("error" in listing) |
|
339 |
alert("Error: " + listing.error); |
|
340 |
}
|
|
341 |
||
342 |
function loadfile() |
|
343 |
{
|
|
344 |
filename = document.getElementById("filename").value; |
|
345 |
if (filename == null || filename == "") |
|
346 |
return; |
|
347 |
url = obj_to_query_string("files.py/getfile", |
|
348 |
{"path" : "", "filename" : filename}); |
|
349 |
var xmlhttp = new XMLHttpRequest(); |
|
350 |
// false -> SYNCHRONOUS (wait for response)
|
|
351 |
// (No need for a callback function)
|
|
352 |
xmlhttp.open("GET", url, false); |
|
353 |
xmlhttp.send(""); |
|
354 |
document.getElementById("data").value = xmlhttp.responseText; |
|
355 |
}
|
|
356 |
||
12
by mattgiuca
browser prototype: Added "Save and Commit" button to editor. |
357 |
function saveandcommit() |
358 |
{
|
|
359 |
filename = document.getElementById("filename").value; |
|
360 |
data = document.getElementById("data").value; |
|
361 |
url = obj_to_query_string("files.py/putfile", |
|
362 |
{"path" : "", "filename" : filename, "data" : data}); |
|
363 |
var xmlhttp = new XMLHttpRequest(); |
|
364 |
// false -> SYNCHRONOUS (wait for response)
|
|
365 |
// (No need for a callback function)
|
|
366 |
xmlhttp.open("GET", url, false); |
|
367 |
xmlhttp.send(""); |
|
368 |
||
369 |
url = obj_to_query_string("files.py/svncommit", |
|
370 |
{"path" : "", "filename" : filename}); |
|
371 |
xmlhttp.open("GET", url, false); |
|
372 |
xmlhttp.send(""); |
|
373 |
||
374 |
listing = JSON.parse(xmlhttp.responseText); |
|
375 |
if (listing == null) |
|
376 |
alert("An error occured"); |
|
377 |
else if ("error" in listing) |
|
378 |
alert("Error: " + listing.error); |
|
379 |
}
|
|
380 |
||
9
by mattgiuca
Added prototypes directory. Added ajax file browser proof-of-concept demo. |
381 |
/* Called on page load */
|
382 |
function init_browser() |
|
383 |
{
|
|
384 |
gpathlist = []; |
|
385 |
refresh(); |
|
386 |
}
|
|
387 |
||
388 |
function init_edit() |
|
389 |
{
|
|
390 |
document.getElementById("filename").value = getURLParam("filename"); |
|
391 |
loadfile(); |
|
392 |
}
|
|
393 |
||
394 |
/* BORROWED CODE */
|
|
395 |
||
396 |
/* http://www.11tmr.com/11tmr.nsf/d6plinks/MWHE-695L9Z */
|
|
397 |
function getURLParam(strParamName){ |
|
398 |
var strReturn = ""; |
|
399 |
var strHref = window.location.href; |
|
400 |
if ( strHref.indexOf("?") > -1 ){ |
|
401 |
var strQueryString = strHref.substr(strHref.indexOf("?")); |
|
402 |
var aQueryString = strQueryString.split("&"); |
|
403 |
for ( var iParam = 0; iParam < aQueryString.length; iParam++ ){ |
|
404 |
if ( |
|
405 |
aQueryString[iParam].indexOf(strParamName.toLowerCase() + "=") > -1 ){ |
|
406 |
var aParam = aQueryString[iParam].split("="); |
|
407 |
strReturn = aParam[1]; |
|
408 |
break; |
|
409 |
}
|
|
410 |
}
|
|
411 |
}
|
|
11
by mattgiuca
browser prototype: Added sample subversion status, add and commit actions to |
412 |
return decodeURI(strReturn); |
9
by mattgiuca
Added prototypes directory. Added ajax file browser proof-of-concept demo. |
413 |
}
|