Add the LICENSE.txt file
[armadillo.git] / frontend / main.js
1 //
2 // Armadillo File Manager
3 // Copyright (c) 2010-2011, Robert Sesek <http://www.bluestatic.org>
4 //
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.
8 //
9
10 $.namespace('armadillo.App');
11
12 $(document).ready(function() {
13 app = new armadillo.App();
14 });
15
16 armadillo.App = function() {
17 var start_path = '/';
18 if (window.location.hash) {
19 start_path = window.location.hash.substr(1);
20 }
21 this.list(start_path);
22
23 $(window).bind('hashchange', this.hashChanged_.bind(this));
24
25 this.clearError(false);
26
27 $('#mkdir').click(this.mkdirHandler_.bind(this));
28
29 var version = 'Armadillo ' + armadillo.Version.MAJOR + '.' + armadillo.Version.MINOR +
30 ' (' + armadillo.Version.BUILD + ')';
31 $('#footer').text(version);
32 }
33
34 /**
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.
40 */
41 armadillo.App.prototype.sendRequest = function(action, data, callback) {
42 return $.ajax({
43 url: 'service/' + action,
44 type: 'POST',
45 data: data,
46 success: callback,
47 error: function(xhr, status, error) {
48 console.log(xhr);
49 app.showError(xhr.responseText);
50 }
51 });
52 };
53
54 /**
55 * Updates the directory listing for a given path.
56 * @param {string} path Path to list; relative to jail.
57 */
58 armadillo.App.prototype.list = function(path) {
59 var callback = function(data, status, xhr) {
60 app.clearError(true);
61
62 // Update the listing.
63 $('#pwd').text(path);
64 app.currentPath_ = path;
65 window.location.hash = path;
66 document.title = path + ' - Armadillo';
67
68 var list = $('#ls');
69 list.empty();
70
71 // Add a previous directory entry.
72 if (path != '/' && path != '')
73 data.unshift('../');
74
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());
79 });
80 }
81 this.sendRequest('list', {'path':path}, callback);
82 };
83
84 /**
85 * Navigates to a subpath. Can only handle directories.
86 * @param {string} target Relative path to |currentPath_|.
87 */
88 armadillo.App.prototype.navigate = function(target) {
89 if (target == '../') {
90 this.list(this.stripLastPathComponent(this.currentPath_));
91 } else {
92 this.list(this.currentPath_ + target);
93 }
94 };
95
96 /**
97 * Event for when the hash changes.
98 * @param {Event} e
99 */
100 armadillo.App.prototype.hashChanged_ = function(e) {
101 if (window.location.hash.length)
102 this.list(window.location.hash.substr(1));
103 };
104
105 /**
106 * Checks whether a path is a directory.
107 * @param {string} path
108 * @returns boolean
109 */
110 armadillo.App.prototype.isDirectory = function(path) {
111 return path[path.length - 1] == '/';
112 };
113
114 /**
115 * Gets the current path of the directory being displayed, absolute to root.
116 * @returns string
117 */
118 armadillo.App.prototype.getCurrentPath = function() {
119 return this.currentPath_;
120 };
121
122 /**
123 * Strips the last path component from a path.
124 * @param {string} path
125 * @returns string
126 */
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);
132 }
133 }
134 }
135 return '/';
136 };
137
138 /**
139 * Joins all the arguments together as a path.
140 * @param {string...} varargs Components to join
141 */
142 armadillo.App.prototype.joinPath = function() {
143 var path = '';
144 var sep = '/';
145 var last = arguments.length - 1;
146 $.each(arguments, function (i, c) {
147 if (c == sep && i != 0)
148 return;
149 path += c;
150 if (c[c.length - 1] != sep && i != last)
151 path += sep;
152 });
153 return path;
154 };
155
156 /**
157 * Clears the error message.
158 * @param {bool?} animate Whether or not to animate out.
159 */
160 armadillo.App.prototype.clearError = function(animate) {
161 var elm = $('#error');
162 if (!elm.text() || !animate) {
163 elm.hide();
164 return;
165 }
166
167 elm.fadeOut(500, function() {
168 elm.text('');
169 });
170 };
171
172 /**
173 * Shows an error message.
174 * @param {string} message
175 */
176 armadillo.App.prototype.showError = function(message) {
177 $('#error').text(message).fadeIn(1000);
178 };
179
180 /**
181 * Creates a subdirectory in the current path.
182 */
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());
190 });
191 }
192 };