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 $(document
).ready(function() {
13 app
= new armadillo
.App();
16 armadillo
.App
= function() {
18 if (window
.location
.hash
) {
19 start_path
= window
.location
.hash
.substr(1);
21 this.list(start_path
);
23 $(window
).bind('hashchange', this.hashChanged_
.bind(this));
25 this.clearError(false);
27 $('#mkdir').click(this.mkdirHandler_
.bind(this));
29 var version
= 'Armadillo ' + armadillo
.Version
.MAJOR
+ '.' + armadillo
.Version
.MINOR
+
30 ' (' + armadillo
.Version
.BUILD
+ ')';
31 $('#footer').text(version
);
35 * Starts a new XHR service request from the backend.
36 * @param {string} action Action to perform.
37 * @param {Object} data Extra data to add.
38 * @param {Function} callback XHR callback.
39 * @return {jqXHR} The jQuery XHR object.
41 armadillo
.App
.prototype.sendRequest
= function(action
, data
, callback
) {
43 url
: 'service/' + action
,
47 error
: function(xhr
, status
, error
) {
49 app
.showError(xhr
.responseText
);
55 * Updates the directory listing for a given path.
56 * @param {string} path Path to list; relative to jail.
58 armadillo
.App
.prototype.list
= function(path
) {
59 var callback
= function(data
, status
, xhr
) {
62 // Update the listing.
64 app
.currentPath_
= path
;
65 window
.location
.hash
= path
;
66 document
.title
= path
+ ' - Armadillo';
71 // Add a previous directory entry.
72 if (path
!= '/' && path
!= '')
75 // Add items for each entry.
76 $.each(data
, function(i
, file
) {
77 var fileObject
= new armadillo
.File(file
, path
);
78 list
.append(fileObject
.createDom());
81 this.sendRequest('list', {'path':path
}, callback
);
85 * Navigates to a subpath. Can only handle directories.
86 * @param {string} target Relative path to |currentPath_|.
88 armadillo
.App
.prototype.navigate
= function(target
) {
89 if (target
== '../') {
90 this.list(this.stripLastPathComponent(this.currentPath_
));
92 this.list(this.currentPath_
+ target
);
97 * Event for when the hash changes.
100 armadillo
.App
.prototype.hashChanged_
= function(e
) {
101 if (window
.location
.hash
.length
)
102 this.list(window
.location
.hash
.substr(1));
106 * Checks whether a path is a directory.
107 * @param {string} path
110 armadillo
.App
.prototype.isDirectory
= function(path
) {
111 return path
[path
.length
- 1] == '/';
115 * Gets the current path of the directory being displayed, absolute to root.
118 armadillo
.App
.prototype.getCurrentPath
= function() {
119 return this.currentPath_
;
123 * Strips the last path component from a path.
124 * @param {string} path
127 armadillo
.App
.prototype.stripLastPathComponent
= function(path
) {
128 for (var i
= path
.length
- 1; i
>= 0; --i
) {
129 if (path
[i] == '/') {
130 if (i
!= path
.length
- 1) {
131 return path
.substring(0, i
+ 1);
139 * Joins all the arguments together as a path.
140 * @param {string...} varargs Components to join
142 armadillo
.App
.prototype.joinPath
= function() {
145 var last
= arguments
.length
- 1;
146 $.each(arguments
, function (i
, c
) {
147 if (c
== sep
&& i
!= 0)
150 if (c
[c
.length
- 1] != sep
&& i
!= last
)
157 * Clears the error message.
158 * @param {bool?} animate Whether or not to animate out.
160 armadillo
.App
.prototype.clearError
= function(animate
) {
161 var elm
= $('#error');
162 if (!elm
.text() || !animate
) {
167 elm
.fadeOut(500, function() {
173 * Shows an error message.
174 * @param {string} message
176 armadillo
.App
.prototype.showError
= function(message
) {
177 $('#error').text(message
).fadeIn(1000);
181 * Creates a subdirectory in the current path.
183 armadillo
.App
.prototype.mkdirHandler_
= function() {
184 var name
= prompt('Name the new subdirectory', '');
185 if (name
!= null && name
!= '') {
186 var path
= this.joinPath(this.getCurrentPath(), name
);
187 this.sendRequest('mkdir', {'path':path
}, function(data
, status
, xhr
) {
188 app
.clearError(true);
189 app
.list(app
.getCurrentPath());