Display error messages from the backend to the user.
[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
12 goog.require('goog.array');
13 goog.require('goog.dom');
14 goog.require('goog.fx.dom.FadeInAndShow');
15 goog.require('goog.net.XhrIo');
16 goog.require('goog.Uri.QueryData');
17
18 armadillo = 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 this.listeners_ = new Array();
25 goog.events.listen(window, goog.events.EventType.HASHCHANGE,
26 this.hashChanged_, false, this);
27 }
28
29 /**
30 * Starts a new XHR service request from the backend.
31 * @param {string} action Action to perform.
32 * @param {Object} extra_data Extra data to add.
33 * @param {Function} callback XHR callback.
34 */
35 armadillo.prototype.sendRequest_ = function(action, extra_data, callback) {
36 var data = new goog.Uri.QueryData();
37 data.set('action', action);
38 data.extend(extra_data);
39 goog.net.XhrIo.send('/service', callback, 'POST', data);
40 };
41
42 /**
43 * Updates the directory listing for a given path.
44 * @param {string} path Path to list; relative to jail.
45 */
46 armadillo.prototype.list = function(path) {
47 var callback = function(e) {
48 app.clearError_();
49 var data = e.target.getResponseJson();
50 if (data['error']) {
51 app.showError_(data['message']);
52 return; // Error.
53 }
54 // Unlisten all current listeners.
55 goog.array.forEach(app.listeners_, function(e) {
56 goog.events.unlistenByKey(e);
57 });
58
59 // Update the listing.
60 goog.dom.setTextContent(goog.dom.getElement('pwd'), path);
61 app.currentPath_ = path;
62 window.location.hash = path;
63 var list = goog.dom.getElement('ls');
64 goog.dom.removeChildren(list);
65
66 // Add a previous directory entry.
67 if (path != '/' && path != '')
68 goog.array.insertAt(data, '../', 0);
69
70 // Add items for each entry.
71 goog.array.forEach(data, function(file) {
72 var elm = goog.dom.createElement('li');
73 goog.dom.setTextContent(elm, file);
74 goog.dom.appendChild(list, elm);
75 app.listeners_.push(goog.events.listen(elm,
76 goog.events.EventType.CLICK, app.clickHandler_, false, app));
77 });
78 }
79 this.sendRequest_('list', {'path':path}, callback);
80 };
81
82 /**
83 * Click handler for elements.
84 * @param {Event} e
85 */
86 armadillo.prototype.clickHandler_ = function(e) {
87 var target = goog.dom.getTextContent(e.target);
88 if (target == '../') {
89 this.list(this.stripLastPathComponent_(this.currentPath_));
90 } else if (this.isDirectory_(target)) {
91 this.list(this.currentPath_ + target);
92 }
93 };
94
95 /**
96 * Event for when the hash changes.
97 * @param {Event} e
98 */
99 armadillo.prototype.hashChanged_ = function(e) {
100 if (window.location.hash.length)
101 this.list(window.location.hash.substr(1));
102 };
103
104 /**
105 * Checks whether a path is a directory.
106 * @param {string} path
107 * @returns boolean
108 */
109 armadillo.prototype.isDirectory_ = function(path) {
110 return path[path.length - 1] == '/';
111 };
112
113 /**
114 * Strips the last path component from a path.
115 * @param {string} path
116 * @returns string
117 */
118 armadillo.prototype.stripLastPathComponent_ = function(path) {
119 for (var i = path.length - 1; i >= 0; --i) {
120 if (path[i] == '/') {
121 if (i != path.length - 1) {
122 return path.substring(0, i + 1);
123 }
124 }
125 }
126 return '/';
127 };
128
129 /**
130 * Clears the error message.
131 */
132 armadillo.prototype.clearError_ = function() {
133 var elm = goog.dom.getElement('error');
134 goog.dom.setTextContent(elm, '');
135 goog.dom.setProperties(elm, {'display':'none'});
136 };
137
138 /**
139 * Shows an error message.
140 * @param {string} message
141 */
142 armadillo.prototype.showError_ = function(message) {
143 this.clearError_();
144 var elm = goog.dom.getElement('error');
145 goog.dom.setTextContent(elm, message);
146 goog.fx.dom.FadeInAndShow(elm, 10.0);
147 };