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