]> src.bluestatic.org Git - armadillo.git/blob - web_frontend/main.js
Improvements to the name-editable path control.
[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.net.XhrIo');
19 goog.require('goog.string.format');
20 goog.require('goog.Uri.QueryData');
21
22 armadillo.App = function() {
23 var start_path = '/';
24 if (window.location.hash) {
25 start_path = window.location.hash.substr(1);
26 }
27 this.list(start_path);
28 this.errorEffect_ =
29 new goog.fx.dom.FadeInAndShow(goog.dom.getElement('error'), 2.0);
30 this.errorEffect_.hide();
31 goog.events.listen(window, goog.events.EventType.HASHCHANGE,
32 this.hashChanged_, false, this);
33
34 var version = goog.string.format('Armadillo %d.%d (%d)',
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();
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 var list = goog.dom.getElement('ls');
72 goog.dom.removeChildren(list);
73
74 // Add a previous directory entry.
75 if (path != '/' && path != '')
76 goog.array.insertAt(data, '../', 0);
77
78 // Add items for each entry.
79 goog.array.forEach(data, function(file) {
80 var fileObject = new armadillo.File(file, path);
81 goog.dom.appendChild(list, fileObject.draw());
82 });
83 }
84 this.sendRequest('list', {'path':path}, callback);
85 };
86
87 /**
88 * Navigates to a subpath. Can only handle directories.
89 * @param {string} target Relative path to |currentPath_|.
90 */
91 armadillo.App.prototype.navigate = function(target) {
92 if (target == '../') {
93 this.list(this.stripLastPathComponent(this.currentPath_));
94 } else {
95 this.list(this.currentPath_ + target);
96 }
97 };
98
99 /**
100 * Event for when the hash changes.
101 * @param {Event} e
102 */
103 armadillo.App.prototype.hashChanged_ = function(e) {
104 if (window.location.hash.length)
105 this.list(window.location.hash.substr(1));
106 };
107
108 /**
109 * Checks whether a path is a directory.
110 * @param {string} path
111 * @returns boolean
112 */
113 armadillo.App.prototype.isDirectory = function(path) {
114 return path[path.length - 1] == '/';
115 };
116
117 /**
118 * Gets the current path of the directory being displayed, absolute to root.
119 * @returns string
120 */
121 armadillo.App.prototype.getCurrentPath = function() {
122 return this.currentPath_;
123 };
124
125 /**
126 * Strips the last path component from a path.
127 * @param {string} path
128 * @returns string
129 */
130 armadillo.App.prototype.stripLastPathComponent = function(path) {
131 for (var i = path.length - 1; i >= 0; --i) {
132 if (path[i] == '/') {
133 if (i != path.length - 1) {
134 return path.substring(0, i + 1);
135 }
136 }
137 }
138 return '/';
139 };
140
141 /**
142 * Joins all the arguments together as a path.
143 * @param {string...} varargs Components to join
144 */
145 armadillo.App.prototype.joinPath = function() {
146 var path = '';
147 var sep = '/';
148 var last = arguments.length - 1;
149 goog.array.forEach(arguments, function (c, i) {
150 if (c == sep && i != 0)
151 return;
152 path += c;
153 if (c[c.length - 1] != sep && i != last)
154 path += sep;
155 });
156 return path;
157 };
158
159 /**
160 * Clears the error message.
161 */
162 armadillo.App.prototype.clearError = function() {
163 this.errorEffect_.hide();
164 goog.dom.setTextContent(this.errorEffect_.element, '');
165 };
166
167 /**
168 * Shows an error message.
169 * @param {string} message
170 */
171 armadillo.App.prototype.showError = function(message) {
172 goog.dom.setTextContent(this.errorEffect_.element, message);
173 this.errorEffect_.show();
174 };