Use %f for the build string
[armadillo.git] / web_frontend / main.js
1 //
2 // Armadillo File Manager
3 // Copyright (c) 2010, 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.dom');
17 goog.require('goog.fx.dom.FadeInAndShow');
18 goog.require('goog.fx.dom.FadeOutAndHide');
19 goog.require('goog.net.XhrIo');
20 goog.require('goog.string.format');
21 goog.require('goog.Uri.QueryData');
22
23 armadillo.App = function() {
24 var start_path = '/';
25 if (window.location.hash) {
26 start_path = window.location.hash.substr(1);
27 }
28 this.list(start_path);
29 goog.events.listen(window, goog.events.EventType.HASHCHANGE,
30 this.hashChanged_, false, this);
31
32 this.clearError(false);
33
34 var version = goog.string.format('Armadillo %d.%d (%f)',
35 armadillo.Version.MAJOR, armadillo.Version.MINOR,
36 armadillo.Version.BUILD);
37 goog.dom.setTextContent(goog.dom.getElement('footer'), version)
38 }
39
40 /**
41 * Starts a new XHR service request from the backend.
42 * @param {string} action Action to perform.
43 * @param {Object} extra_data Extra data to add.
44 * @param {Function} callback XHR callback.
45 */
46 armadillo.App.prototype.sendRequest = function(action, extra_data, callback) {
47 var data = new goog.Uri.QueryData();
48 data.set('action', action);
49 data.extend(extra_data);
50 goog.net.XhrIo.send('/service', callback, 'POST', data);
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(e) {
59 var data = e.target.getResponseJson();
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 goog.dom.setTextContent(goog.dom.getElement('pwd'), path);
69 app.currentPath_ = path;
70 window.location.hash = path;
71 document.title = path + ' - Armadillo';
72 var list = goog.dom.getElement('ls');
73 goog.dom.removeChildren(list);
74
75 // Add a previous directory entry.
76 if (path != '/' && path != '')
77 goog.array.insertAt(data, '../', 0);
78
79 // Add items for each entry.
80 goog.array.forEach(data, function(file) {
81 var fileObject = new armadillo.File(file, path);
82 goog.dom.appendChild(list, 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 goog.array.forEach(arguments, function (c, i) {
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 = goog.dom.getElement('error');
166 var anim = new goog.fx.dom.FadeOutAndHide(elm, 500);
167 if (!goog.dom.getTextContent(elm) || !animate) {
168 anim.hide();
169 return;
170 }
171 goog.events.listenOnce(anim, goog.fx.Animation.EventType.END, function() {
172 goog.dom.setTextContent(elm, '');
173 });
174 anim.play();
175 };
176
177 /**
178 * Shows an error message.
179 * @param {string} message
180 */
181 armadillo.App.prototype.showError = function(message) {
182 var elm = goog.dom.getElement('error');
183 goog.dom.setTextContent(elm, message);
184 var anim = new goog.fx.dom.FadeInAndShow(elm, 1000);
185 anim.play();
186 };