2 // Armadillo File Manager
3 // Copyright (c) 2010-2011, 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 $.namespace('armadillo.App');
12 armadillo
.App
= function() {
14 if (window
.location
.hash
) {
15 start_path
= window
.location
.hash
.substr(1);
17 this.list(start_path
);
19 $(window
).bind('hashchange', this.hashChanged_
.bind(this));
21 this.clearError(false);
23 $('#mkdir').click(this.mkdirHandler_
.bind(this));
25 var version
= 'Armadillo ' + armadillo
.Version
.MAJOR
+ '.' + armadillo
.Version
.MINOR
+
26 ' (' + armadillo
.Version
.BUILD
+ ')';
27 $('#footer').text(version
);
31 * Starts a new XHR service request from the backend.
32 * @param {string} action Action to perform.
33 * @param {Object} data Extra data to add.
34 * @param {Function} callback XHR callback.
35 * @return {jqXHR} The jQuery XHR object.
37 armadillo
.App
.prototype.sendRequest
= function(action
, data
, callback
) {
48 * Updates the directory listing for a given path.
49 * @param {string} path Path to list; relative to jail.
51 armadillo
.App
.prototype.list
= function(path
) {
52 var callback
= function(data
, status
, xhr
) {
54 app
.showError(data
['message']);
60 // Update the listing.
62 app
.currentPath_
= path
;
63 window
.location
.hash
= path
;
64 document
.title
= path
+ ' - Armadillo';
69 // Add a previous directory entry.
70 if (path
!= '/' && path
!= '')
73 // Add items for each entry.
74 $.each(data
, function(i
, file
) {
75 var fileObject
= new armadillo
.File(file
, path
);
76 list
.append(fileObject
.draw());
79 this.sendRequest('list', {'path':path
}, callback
);
83 * Navigates to a subpath. Can only handle directories.
84 * @param {string} target Relative path to |currentPath_|.
86 armadillo
.App
.prototype.navigate
= function(target
) {
87 if (target
== '../') {
88 this.list(this.stripLastPathComponent(this.currentPath_
));
90 this.list(this.currentPath_
+ target
);
95 * Event for when the hash changes.
98 armadillo
.App
.prototype.hashChanged_
= function(e
) {
99 if (window
.location
.hash
.length
)
100 this.list(window
.location
.hash
.substr(1));
104 * Checks whether a path is a directory.
105 * @param {string} path
108 armadillo
.App
.prototype.isDirectory
= function(path
) {
109 return path
[path
.length
- 1] == '/';
113 * Gets the current path of the directory being displayed, absolute to root.
116 armadillo
.App
.prototype.getCurrentPath
= function() {
117 return this.currentPath_
;
121 * Strips the last path component from a path.
122 * @param {string} path
125 armadillo
.App
.prototype.stripLastPathComponent
= function(path
) {
126 for (var i
= path
.length
- 1; i
>= 0; --i
) {
127 if (path
[i] == '/') {
128 if (i
!= path
.length
- 1) {
129 return path
.substring(0, i
+ 1);
137 * Joins all the arguments together as a path.
138 * @param {string...} varargs Components to join
140 armadillo
.App
.prototype.joinPath
= function() {
143 var last
= arguments
.length
- 1;
144 $.each(arguments
, function (i
, c
) {
145 if (c
== sep
&& i
!= 0)
148 if (c
[c
.length
- 1] != sep
&& i
!= last
)
155 * Clears the error message.
156 * @param {bool?} animate Whether or not to animate out.
158 armadillo
.App
.prototype.clearError
= function(animate
) {
159 var elm
= $('#error');
160 if (!elm
.text() || !animate
) {
165 elm
.fadeOut(500, function() {
171 * Shows an error message.
172 * @param {string} message
174 armadillo
.App
.prototype.showError
= function(message
) {
175 $('#error').text(message
).fadeIn(1000);
179 * Creates a subdirectory in the current path.
181 armadillo
.App
.prototype.mkdirHandler_
= function() {
182 var name
= prompt('Name the new subdirectory', '');
183 if (name
!= null && name
!= '') {
184 var path
= this.joinPath(this.getCurrentPath(), name
);
185 this.sendRequest('mkdir', {'path':path
}, function(data
, status
, xhr
) {
187 app
.showError(data
['message']);
190 app
.list(app
.getCurrentPath());