]>
src.bluestatic.org Git - armadillo.git/blob - frontend/main.js
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
) {
48 app
.showError(xhr
.responseText
);
57 * Updates the directory listing for a given path.
58 * @param {string} path Path to list; relative to jail.
60 armadillo
.App
.prototype.list = function(path
) {
61 var callback = function(data
, status
, xhr
) {
63 app
.showError(data
['message']);
69 // Update the listing.
71 app
.currentPath_
= path
;
72 window
.location
.hash
= path
;
73 document
.title
= path
+ ' - Armadillo';
78 // Add a previous directory entry.
79 if (path
!= '/' && path
!= '')
82 // Add items for each entry.
83 $.each(data
, function(i
, file
) {
84 var fileObject
= new armadillo
.File(file
, path
);
85 list
.append(fileObject
.createDom());
88 this.sendRequest('list', {'path':path
}, callback
);
92 * Navigates to a subpath. Can only handle directories.
93 * @param {string} target Relative path to |currentPath_|.
95 armadillo
.App
.prototype.navigate = function(target
) {
96 if (target
== '../') {
97 this.list(this.stripLastPathComponent(this.currentPath_
));
99 this.list(this.currentPath_
+ target
);
104 * Event for when the hash changes.
107 armadillo
.App
.prototype.hashChanged_ = function(e
) {
108 if (window
.location
.hash
.length
)
109 this.list(window
.location
.hash
.substr(1));
113 * Checks whether a path is a directory.
114 * @param {string} path
117 armadillo
.App
.prototype.isDirectory = function(path
) {
118 return path
[path
.length
- 1] == '/';
122 * Gets the current path of the directory being displayed, absolute to root.
125 armadillo
.App
.prototype.getCurrentPath = function() {
126 return this.currentPath_
;
130 * Strips the last path component from a path.
131 * @param {string} path
134 armadillo
.App
.prototype.stripLastPathComponent = function(path
) {
135 for (var i
= path
.length
- 1; i
>= 0; --i
) {
136 if (path
[i
] == '/') {
137 if (i
!= path
.length
- 1) {
138 return path
.substring(0, i
+ 1);
146 * Joins all the arguments together as a path.
147 * @param {string...} varargs Components to join
149 armadillo
.App
.prototype.joinPath = function() {
152 var last
= arguments
.length
- 1;
153 $.each(arguments
, function (i
, c
) {
154 if (c
== sep
&& i
!= 0)
157 if (c
[c
.length
- 1] != sep
&& i
!= last
)
164 * Clears the error message.
165 * @param {bool?} animate Whether or not to animate out.
167 armadillo
.App
.prototype.clearError = function(animate
) {
168 var elm
= $('#error');
169 if (!elm
.text() || !animate
) {
174 elm
.fadeOut(500, function() {
180 * Shows an error message.
181 * @param {string} message
183 armadillo
.App
.prototype.showError = function(message
) {
184 $('#error').text(message
).fadeIn(1000);
188 * Creates a subdirectory in the current path.
190 armadillo
.App
.prototype.mkdirHandler_ = function() {
191 var name
= prompt('Name the new subdirectory', '');
192 if (name
!= null && name
!= '') {
193 var path
= this.joinPath(this.getCurrentPath(), name
);
194 this.sendRequest('mkdir', {'path':path
}, function(data
, status
, xhr
) {
196 app
.showError(data
['message']);
199 app
.list(app
.getCurrentPath());