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
) {
52 * Updates the directory listing for a given path.
53 * @param {string} path Path to list; relative to jail.
55 armadillo
.App
.prototype.list
= function(path
) {
56 var callback
= function(data
, status
, xhr
) {
58 app
.showError(data
['message']);
64 // Update the listing.
66 app
.currentPath_
= path
;
67 window
.location
.hash
= path
;
68 document
.title
= path
+ ' - Armadillo';
73 // Add a previous directory entry.
74 if (path
!= '/' && path
!= '')
77 // Add items for each entry.
78 $.each(data
, function(i
, file
) {
79 var fileObject
= new armadillo
.File(file
, path
);
80 list
.append(fileObject
.createDom());
83 this.sendRequest('list', {'path':path
}, callback
);
87 * Navigates to a subpath. Can only handle directories.
88 * @param {string} target Relative path to |currentPath_|.
90 armadillo
.App
.prototype.navigate
= function(target
) {
91 if (target
== '../') {
92 this.list(this.stripLastPathComponent(this.currentPath_
));
94 this.list(this.currentPath_
+ target
);
99 * Event for when the hash changes.
102 armadillo
.App
.prototype.hashChanged_
= function(e
) {
103 if (window
.location
.hash
.length
)
104 this.list(window
.location
.hash
.substr(1));
108 * Checks whether a path is a directory.
109 * @param {string} path
112 armadillo
.App
.prototype.isDirectory
= function(path
) {
113 return path
[path
.length
- 1] == '/';
117 * Gets the current path of the directory being displayed, absolute to root.
120 armadillo
.App
.prototype.getCurrentPath
= function() {
121 return this.currentPath_
;
125 * Strips the last path component from a path.
126 * @param {string} path
129 armadillo
.App
.prototype.stripLastPathComponent
= function(path
) {
130 for (var i
= path
.length
- 1; i
>= 0; --i
) {
131 if (path
[i] == '/') {
132 if (i
!= path
.length
- 1) {
133 return path
.substring(0, i
+ 1);
141 * Joins all the arguments together as a path.
142 * @param {string...} varargs Components to join
144 armadillo
.App
.prototype.joinPath
= function() {
147 var last
= arguments
.length
- 1;
148 $.each(arguments
, function (i
, c
) {
149 if (c
== sep
&& i
!= 0)
152 if (c
[c
.length
- 1] != sep
&& i
!= last
)
159 * Clears the error message.
160 * @param {bool?} animate Whether or not to animate out.
162 armadillo
.App
.prototype.clearError
= function(animate
) {
163 var elm
= $('#error');
164 if (!elm
.text() || !animate
) {
169 elm
.fadeOut(500, function() {
175 * Shows an error message.
176 * @param {string} message
178 armadillo
.App
.prototype.showError
= function(message
) {
179 $('#error').text(message
).fadeIn(1000);
183 * Creates a subdirectory in the current path.
185 armadillo
.App
.prototype.mkdirHandler_
= function() {
186 var name
= prompt('Name the new subdirectory', '');
187 if (name
!= null && name
!= '') {
188 var path
= this.joinPath(this.getCurrentPath(), name
);
189 this.sendRequest('mkdir', {'path':path
}, function(data
, status
, xhr
) {
191 app
.showError(data
['message']);
194 app
.list(app
.getCurrentPath());