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