Traversing directories is now possible.
[armadillo.git] / web_frontend / main.js
1 goog.provide('armadillo');
2
3 goog.require('goog.array');
4 goog.require('goog.dom');
5 goog.require('goog.net.XhrIo');
6 goog.require('goog.Uri.QueryData');
7
8 armadillo = function() {
9 this.list('/');
10 this.currentPath_ = '/';
11 this.listeners_ = new Array();
12 }
13
14 /**
15 * Starts a new XHR service request from the backend.
16 * @param {string} action Action to perform.
17 * @param {Object} extra_data Extra data to add.
18 * @param {Function} callback XHR callback.
19 */
20 armadillo.prototype.sendRequest_ = function(action, extra_data, callback) {
21 var data = new goog.Uri.QueryData();
22 data.set('action', 'list');
23 data.extend(extra_data);
24 goog.net.XhrIo.send('/service', callback, 'POST', data);
25 };
26
27 /**
28 * Updates the directory listing for a given path.
29 * @param {string} path Path to list; relative to jail.
30 */
31 armadillo.prototype.list = function(path) {
32 var callback = function(e) {
33 var data = e.target.getResponseJson();
34 if (data['status']) {
35 return; // Error.
36 }
37 // Unlisten all current listeners.
38 goog.array.forEach(app.listeners_, function(e) {
39 goog.events.unlistenByKey(e);
40 });
41
42 // Update the listing.
43 goog.dom.setTextContent(goog.dom.getElement('pwd'), path);
44 app.currentPath_ = path;
45 var list = goog.dom.getElement('ls');
46 goog.dom.removeChildren(list);
47
48 // Add items for each entry.
49 goog.array.forEach(data, function(file) {
50 var elm = goog.dom.createElement('li');
51 goog.dom.setTextContent(elm, file);
52 goog.dom.appendChild(list, elm);
53 app.listeners_.push(goog.events.listen(elm,
54 goog.events.EventType.CLICK, app.clickHandler_, false, app));
55 });
56 }
57 this.sendRequest_('list', {'path':path}, callback);
58 };
59
60 /**
61 * Click handler for elements.
62 * @param {Event} e
63 */
64 armadillo.prototype.clickHandler_ = function(e) {
65 if (this.isDirectory_(goog.dom.getTextContent(e.target))) {
66 this.list(this.currentPath_ + goog.dom.getTextContent(e.target));
67 }
68 };
69
70 /**
71 * Checks whether a path is a directory.
72 * @param {string} path
73 * @returns boolean
74 */
75 armadillo.prototype.isDirectory_ = function(path) {
76 return path[path.length - 1] == '/';
77 };