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 goog
.provide('armadillo');
11 goog
.provide('armadillo.App');
13 goog
.require('armadillo.File');
14 goog
.require('armadillo.Version');
15 goog
.require('goog.array');
16 goog
.require('goog.string.format');
18 armadillo
.App
= function() {
20 if (window
.location
.hash
) {
21 start_path
= window
.location
.hash
.substr(1);
23 this.list(start_path
);
25 $(window
).bind('hashchange', this.hashChanged_
.bind(this));
27 this.clearError(false);
29 $('#mkdir').click(this.mkdirHandler_
.bind(this));
31 var version
= goog
.string
.format('Armadillo %d.%d (%f)',
32 armadillo
.Version
.MAJOR
, armadillo
.Version
.MINOR
,
33 armadillo
.Version
.BUILD
);
34 $('#footer').text(version
);
38 * Starts a new XHR service request from the backend.
39 * @param {string} action Action to perform.
40 * @param {Object} data Extra data to add.
41 * @param {Function} callback XHR callback.
42 * @return {jqXHR} The jQuery XHR object.
44 armadillo
.App
.prototype.sendRequest
= function(action
, data
, callback
) {
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
) {
61 app
.showError(data
['message']);
67 // Update the listing.
69 app
.currentPath_
= path
;
70 window
.location
.hash
= path
;
71 document
.title
= path
+ ' - Armadillo';
76 // Add a previous directory entry.
77 if (path
!= '/' && path
!= '')
78 goog
.array
.insertAt(data
, '../', 0);
80 // Add items for each entry.
81 $.each(data
, function(i
, file
) {
82 var fileObject
= new armadillo
.File(file
, path
);
83 list
.append(fileObject
.draw());
86 this.sendRequest('list', {'path':path
}, callback
);
90 * Navigates to a subpath. Can only handle directories.
91 * @param {string} target Relative path to |currentPath_|.
93 armadillo
.App
.prototype.navigate
= function(target
) {
94 if (target
== '../') {
95 this.list(this.stripLastPathComponent(this.currentPath_
));
97 this.list(this.currentPath_
+ target
);
102 * Event for when the hash changes.
105 armadillo
.App
.prototype.hashChanged_
= function(e
) {
106 if (window
.location
.hash
.length
)
107 this.list(window
.location
.hash
.substr(1));
111 * Checks whether a path is a directory.
112 * @param {string} path
115 armadillo
.App
.prototype.isDirectory
= function(path
) {
116 return path
[path
.length
- 1] == '/';
120 * Gets the current path of the directory being displayed, absolute to root.
123 armadillo
.App
.prototype.getCurrentPath
= function() {
124 return this.currentPath_
;
128 * Strips the last path component from a path.
129 * @param {string} path
132 armadillo
.App
.prototype.stripLastPathComponent
= function(path
) {
133 for (var i
= path
.length
- 1; i
>= 0; --i
) {
134 if (path
[i] == '/') {
135 if (i
!= path
.length
- 1) {
136 return path
.substring(0, i
+ 1);
144 * Joins all the arguments together as a path.
145 * @param {string...} varargs Components to join
147 armadillo
.App
.prototype.joinPath
= function() {
150 var last
= arguments
.length
- 1;
151 $.each(arguments
, function (i
, c
) {
152 if (c
== sep
&& i
!= 0)
155 if (c
[c
.length
- 1] != sep
&& i
!= last
)
162 * Clears the error message.
163 * @param {bool?} animate Whether or not to animate out.
165 armadillo
.App
.prototype.clearError
= function(animate
) {
166 var elm
= $('#error');
167 if (!elm
.text() || !animate
) {
172 elm
.fadeOut(500, function() {
178 * Shows an error message.
179 * @param {string} message
181 armadillo
.App
.prototype.showError
= function(message
) {
182 $('#error').text(message
).fadeIn(1000);
186 * Creates a subdirectory in the current path.
188 armadillo
.App
.prototype.mkdirHandler_
= function() {
189 var name
= prompt('Name the new subdirectory', '');
190 if (name
!= null && name
!= '') {
191 var path
= this.joinPath(this.getCurrentPath(), name
);
192 this.sendRequest('mkdir', {'path':path
}, function(data
, status
, xhr
) {
194 app
.showError(data
['message']);
197 app
.list(app
.getCurrentPath());