2 // Armadillo File Manager
3 // Copyright (c) 2010, Robert Sesek <http://www.bluestatic.org>
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.
10 goog
.provide('armadillo');
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');
18 armadillo
= function() {
20 if (window
.location
.hash
) {
21 start_path
= window
.location
.hash
.substr(1);
23 this.list(start_path
);
24 this.listeners_
= new Array();
25 goog
.events
.listen(window
, goog
.events
.EventType
.HASHCHANGE
,
26 this.hashChanged_
, false, this);
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.
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
);
43 * Updates the directory listing for a given path.
44 * @param {string} path Path to list; relative to jail.
46 armadillo
.prototype.list
= function(path
) {
47 var callback
= function(e
) {
49 var data
= e
.target
.getResponseJson();
51 app
.showError_(data
['message']);
54 // Unlisten all current listeners.
55 goog
.array
.forEach(app
.listeners_
, function(e
) {
56 goog
.events
.unlistenByKey(e
);
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
);
66 // Add a previous directory entry.
67 if (path
!= '/' && path
!= '')
68 goog
.array
.insertAt(data
, '../', 0);
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
));
79 this.sendRequest_('list', {'path':path
}, callback
);
83 * Click handler for elements.
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
);
96 * Event for when the hash changes.
99 armadillo
.prototype.hashChanged_
= function(e
) {
100 if (window
.location
.hash
.length
)
101 this.list(window
.location
.hash
.substr(1));
105 * Checks whether a path is a directory.
106 * @param {string} path
109 armadillo
.prototype.isDirectory_
= function(path
) {
110 return path
[path
.length
- 1] == '/';
114 * Strips the last path component from a path.
115 * @param {string} path
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);
130 * Clears the error message.
132 armadillo
.prototype.clearError_
= function() {
133 var elm
= goog
.dom
.getElement('error');
134 goog
.dom
.setTextContent(elm
, '');
135 goog
.dom
.setProperties(elm
, {'display':'none'});
139 * Shows an error message.
140 * @param {string} message
142 armadillo
.prototype.showError_
= function(message
) {
144 var elm
= goog
.dom
.getElement('error');
145 goog
.dom
.setTextContent(elm
, message
);
146 goog
.fx
.dom
.FadeInAndShow(elm
, 10.0);