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