2 // Armadillo File Manager
3 // Copyright (c) 2010, Robert Sesek <http://www.bluestatic.org>
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.
10 goog
.provide('armadillo');
11 goog
.provide('armadillo.App');
13 goog
.require('armadillo.File');
14 goog
.require('goog.array');
15 goog
.require('goog.dom');
16 goog
.require('goog.fx.dom.FadeInAndShow');
17 goog
.require('goog.net.XhrIo');
18 goog
.require('goog.Uri.QueryData');
20 armadillo
.App
= function() {
22 if (window
.location
.hash
) {
23 start_path
= window
.location
.hash
.substr(1);
25 this.list(start_path
);
27 new goog
.fx
.dom
.FadeInAndShow(goog
.dom
.getElement('error'), 2.0);
28 this.errorEffect_
.hide();
29 goog
.events
.listen(window
, goog
.events
.EventType
.HASHCHANGE
,
30 this.hashChanged_
, false, this);
34 * Starts a new XHR service request from the backend.
35 * @param {string} action Action to perform.
36 * @param {Object} extra_data Extra data to add.
37 * @param {Function} callback XHR callback.
39 armadillo
.App
.prototype.sendRequest
= function(action
, extra_data
, callback
) {
40 var data
= new goog
.Uri
.QueryData();
41 data
.set('action', action
);
42 data
.extend(extra_data
);
43 goog
.net
.XhrIo
.send('/service', callback
, 'POST', data
);
47 * Updates the directory listing for a given path.
48 * @param {string} path Path to list; relative to jail.
50 armadillo
.App
.prototype.list
= function(path
) {
51 var callback
= function(e
) {
52 var data
= e
.target
.getResponseJson();
54 app
.showError(data
['message']);
60 // Update the listing.
61 goog
.dom
.setTextContent(goog
.dom
.getElement('pwd'), path
);
62 app
.currentPath_
= path
;
63 window
.location
.hash
= path
;
64 var list
= goog
.dom
.getElement('ls');
65 goog
.dom
.removeChildren(list
);
67 // Add a previous directory entry.
68 if (path
!= '/' && path
!= '')
69 goog
.array
.insertAt(data
, '../', 0);
71 // Add items for each entry.
72 goog
.array
.forEach(data
, function(file
) {
73 var fileObject
= new armadillo
.File(file
, path
);
74 goog
.dom
.appendChild(list
, fileObject
.draw());
77 this.sendRequest('list', {'path':path
}, callback
);
81 * Navigates to a subpath. Can only handle directories.
82 * @param {string} target Relative path to |currentPath_|.
84 armadillo
.App
.prototype.navigate
= function(target
) {
85 if (target
== '../') {
86 this.list(this.stripLastPathComponent_(this.currentPath_
));
88 this.list(this.currentPath_
+ target
);
93 * Event for when the hash changes.
96 armadillo
.App
.prototype.hashChanged_
= function(e
) {
97 if (window
.location
.hash
.length
)
98 this.list(window
.location
.hash
.substr(1));
102 * Checks whether a path is a directory.
103 * @param {string} path
106 armadillo
.App
.prototype.isDirectory
= function(path
) {
107 return path
[path
.length
- 1] == '/';
111 * Gets the current path of the directory being displayed, absolute to root.
114 armadillo
.App
.prototype.getCurrentPath
= function() {
115 return this.currentPath_
;
119 * Strips the last path component from a path.
120 * @param {string} path
123 armadillo
.App
.prototype.stripLastPathComponent_
= function(path
) {
124 for (var i
= path
.length
- 1; i
>= 0; --i
) {
125 if (path
[i] == '/') {
126 if (i
!= path
.length
- 1) {
127 return path
.substring(0, i
+ 1);
135 * Clears the error message.
137 armadillo
.App
.prototype.clearError
= function() {
138 this.errorEffect_
.hide();
139 goog
.dom
.setTextContent(this.errorEffect_
.element
, '');
143 * Shows an error message.
144 * @param {string} message
146 armadillo
.App
.prototype.showError
= function(message
) {
147 goog
.dom
.setTextContent(this.errorEffect_
.element
, message
);
148 this.errorEffect_
.show();