Remove most uses of jQuery from main.js
[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.array');
16 goog.require('goog.string.format');
17
18 armadillo.App = function() {
19 var start_path = '/';
20 if (window.location.hash) {
21 start_path = window.location.hash.substr(1);
22 }
23 this.list(start_path);
24
25 $(window).bind('hashchange', this.hashChanged_.bind(this));
26
27 this.clearError(false);
28
29 $('#mkdir').click(this.mkdirHandler_.bind(this));
30
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);
35 }
36
37 /**
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.
43 */
44 armadillo.App.prototype.sendRequest = function(action, data, callback) {
45 data.action = action;
46 return $.ajax({
47 url: 'service',
48 type: 'POST',
49 data: data,
50 success: callback
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 if (data['error']) {
61 app.showError(data['message']);
62 return; // Error.
63 } else {
64 app.clearError(true);
65 }
66
67 // Update the listing.
68 $('#pwd').text(path);
69 app.currentPath_ = path;
70 window.location.hash = path;
71 document.title = path + ' - Armadillo';
72
73 var list = $('#ls');
74 list.empty();
75
76 // Add a previous directory entry.
77 if (path != '/' && path != '')
78 goog.array.insertAt(data, '../', 0);
79
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());
84 });
85 }
86 this.sendRequest('list', {'path':path}, callback);
87 };
88
89 /**
90 * Navigates to a subpath. Can only handle directories.
91 * @param {string} target Relative path to |currentPath_|.
92 */
93 armadillo.App.prototype.navigate = function(target) {
94 if (target == '../') {
95 this.list(this.stripLastPathComponent(this.currentPath_));
96 } else {
97 this.list(this.currentPath_ + target);
98 }
99 };
100
101 /**
102 * Event for when the hash changes.
103 * @param {Event} e
104 */
105 armadillo.App.prototype.hashChanged_ = function(e) {
106 if (window.location.hash.length)
107 this.list(window.location.hash.substr(1));
108 };
109
110 /**
111 * Checks whether a path is a directory.
112 * @param {string} path
113 * @returns boolean
114 */
115 armadillo.App.prototype.isDirectory = function(path) {
116 return path[path.length - 1] == '/';
117 };
118
119 /**
120 * Gets the current path of the directory being displayed, absolute to root.
121 * @returns string
122 */
123 armadillo.App.prototype.getCurrentPath = function() {
124 return this.currentPath_;
125 };
126
127 /**
128 * Strips the last path component from a path.
129 * @param {string} path
130 * @returns string
131 */
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);
137 }
138 }
139 }
140 return '/';
141 };
142
143 /**
144 * Joins all the arguments together as a path.
145 * @param {string...} varargs Components to join
146 */
147 armadillo.App.prototype.joinPath = function() {
148 var path = '';
149 var sep = '/';
150 var last = arguments.length - 1;
151 $.each(arguments, function (i, c) {
152 if (c == sep && i != 0)
153 return;
154 path += c;
155 if (c[c.length - 1] != sep && i != last)
156 path += sep;
157 });
158 return path;
159 };
160
161 /**
162 * Clears the error message.
163 * @param {bool?} animate Whether or not to animate out.
164 */
165 armadillo.App.prototype.clearError = function(animate) {
166 var elm = $('#error');
167 if (!elm.text() || !animate) {
168 elm.hide();
169 return;
170 }
171
172 elm.fadeOut(500, function() {
173 elm.text('');
174 });
175 };
176
177 /**
178 * Shows an error message.
179 * @param {string} message
180 */
181 armadillo.App.prototype.showError = function(message) {
182 $('#error').text(message).fadeIn(1000);
183 };
184
185 /**
186 * Creates a subdirectory in the current path.
187 */
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) {
193 if (data['error']) {
194 app.showError(data['message']);
195 } else {
196 app.clearError();
197 app.list(app.getCurrentPath());
198 }
199 });
200 }
201 };