* Fix a bug in sendRequest_()
[armadillo.git] / web_frontend / main.js
1 //
2 // Armadillo File Manager
3 // Copyright (c) 2010, Robert Sesek <http://www.bluestatic.org>
4 //
5 // This program is free software: you can redistribute it and/or modify it under
6 // the terms of the GNU General Public License as published by the Free Software
7 // Foundation, either version 3 of the License, or any later version.
8 //
9
10 goog.provide('armadillo');
11
12 goog.require('goog.array');
13 goog.require('goog.dom');
14 goog.require('goog.net.XhrIo');
15 goog.require('goog.Uri.QueryData');
16
17 armadillo = function() {
18 var start_path = '/';
19 if (window.location.hash) {
20 start_path = window.location.hash.substr(1);
21 }
22 this.list(start_path);
23 this.listeners_ = new Array();
24 goog.events.listen(window, goog.events.EventType.HASHCHANGE,
25 this.hashChanged_, false, this);
26 }
27
28 /**
29 * Starts a new XHR service request from the backend.
30 * @param {string} action Action to perform.
31 * @param {Object} extra_data Extra data to add.
32 * @param {Function} callback XHR callback.
33 */
34 armadillo.prototype.sendRequest_ = function(action, extra_data, callback) {
35 var data = new goog.Uri.QueryData();
36 data.set('action', action);
37 data.extend(extra_data);
38 goog.net.XhrIo.send('/service', callback, 'POST', data);
39 };
40
41 /**
42 * Updates the directory listing for a given path.
43 * @param {string} path Path to list; relative to jail.
44 */
45 armadillo.prototype.list = function(path) {
46 var callback = function(e) {
47 var data = e.target.getResponseJson();
48 if (data['error']) {
49 return; // Error.
50 }
51 // Unlisten all current listeners.
52 goog.array.forEach(app.listeners_, function(e) {
53 goog.events.unlistenByKey(e);
54 });
55
56 // Update the listing.
57 goog.dom.setTextContent(goog.dom.getElement('pwd'), path);
58 app.currentPath_ = path;
59 window.location.hash = path;
60 var list = goog.dom.getElement('ls');
61 goog.dom.removeChildren(list);
62
63 // Add a previous directory entry.
64 if (path != '/' && path != '')
65 goog.array.insertAt(data, '../', 0);
66
67 // Add items for each entry.
68 goog.array.forEach(data, function(file) {
69 var elm = goog.dom.createElement('li');
70 goog.dom.setTextContent(elm, file);
71 goog.dom.appendChild(list, elm);
72 app.listeners_.push(goog.events.listen(elm,
73 goog.events.EventType.CLICK, app.clickHandler_, false, app));
74 });
75 }
76 this.sendRequest_('list', {'path':path}, callback);
77 };
78
79 /**
80 * Click handler for elements.
81 * @param {Event} e
82 */
83 armadillo.prototype.clickHandler_ = function(e) {
84 var target = goog.dom.getTextContent(e.target);
85 if (target == '../') {
86 this.list(this.stripLastPathComponent_(this.currentPath_));
87 } else if (this.isDirectory_(target)) {
88 this.list(this.currentPath_ + target);
89 }
90 };
91
92 /**
93 * Event for when the hash changes.
94 * @param {Event} e
95 */
96 armadillo.prototype.hashChanged_ = function(e) {
97 if (window.location.hash.length)
98 this.list(window.location.hash.substr(1));
99 };
100
101 /**
102 * Checks whether a path is a directory.
103 * @param {string} path
104 * @returns boolean
105 */
106 armadillo.prototype.isDirectory_ = function(path) {
107 return path[path.length - 1] == '/';
108 };
109
110 /**
111 * Strips the last path component from a path.
112 * @param {string} path
113 * @returns string
114 */
115 armadillo.prototype.stripLastPathComponent_ = function(path) {
116 for (var i = path.length - 1; i >= 0; --i) {
117 if (path[i] == '/') {
118 if (i != path.length - 1) {
119 return path.substring(0, i + 1);
120 }
121 }
122 }
123 return '/';
124 };