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();
26 new goog
.fx
.dom
.FadeInAndShow(goog
.dom
.getElement('error'), 2.0);
27 this.errorEffect_
.hide();
28 goog
.events
.listen(window
, goog
.events
.EventType
.HASHCHANGE
,
29 this.hashChanged_
, false, this);
33 * Starts a new XHR service request from the backend.
34 * @param {string} action Action to perform.
35 * @param {Object} extra_data Extra data to add.
36 * @param {Function} callback XHR callback.
38 armadillo
.prototype.sendRequest_
= function(action
, extra_data
, callback
) {
39 var data
= new goog
.Uri
.QueryData();
40 data
.set('action', action
);
41 data
.extend(extra_data
);
42 goog
.net
.XhrIo
.send('/service', callback
, 'POST', data
);
46 * Updates the directory listing for a given path.
47 * @param {string} path Path to list; relative to jail.
49 armadillo
.prototype.list
= function(path
) {
50 var callback
= function(e
) {
51 var data
= e
.target
.getResponseJson();
53 app
.showError_(data
['message']);
59 // Unlisten all current listeners.
60 goog
.array
.forEach(app
.listeners_
, function(e
) {
61 goog
.events
.unlistenByKey(e
);
64 // Update the listing.
65 goog
.dom
.setTextContent(goog
.dom
.getElement('pwd'), path
);
66 app
.currentPath_
= path
;
67 window
.location
.hash
= path
;
68 var list
= goog
.dom
.getElement('ls');
69 goog
.dom
.removeChildren(list
);
71 // Add a previous directory entry.
72 if (path
!= '/' && path
!= '')
73 goog
.array
.insertAt(data
, '../', 0);
75 // Add items for each entry.
76 goog
.array
.forEach(data
, function(file
) {
77 var elm
= goog
.dom
.createElement('li');
78 goog
.dom
.setTextContent(elm
, file
);
79 goog
.dom
.appendChild(list
, elm
);
80 app
.listeners_
.push(goog
.events
.listen(elm
,
81 goog
.events
.EventType
.CLICK
, app
.clickHandler_
, false, app
));
84 this.sendRequest_('list', {'path':path
}, callback
);
88 * Click handler for elements.
91 armadillo
.prototype.clickHandler_
= function(e
) {
92 var target
= goog
.dom
.getTextContent(e
.target
);
93 if (target
== '../') {
94 this.list(this.stripLastPathComponent_(this.currentPath_
));
95 } else if (this.isDirectory_(target
)) {
96 this.list(this.currentPath_
+ target
);
101 * Event for when the hash changes.
104 armadillo
.prototype.hashChanged_
= function(e
) {
105 if (window
.location
.hash
.length
)
106 this.list(window
.location
.hash
.substr(1));
110 * Checks whether a path is a directory.
111 * @param {string} path
114 armadillo
.prototype.isDirectory_
= function(path
) {
115 return path
[path
.length
- 1] == '/';
119 * Strips the last path component from a path.
120 * @param {string} path
123 armadillo
.prototype.stripLastPathComponent_
= function(path
) {
124 for (var i
= path
.length
- 1; i
>= 0; --i
) {
125 if (path
[i] == '/') {
126 if (i
!= path
.length
- 1) {
127 return path
.substring(0, i
+ 1);
135 * Clears the error message.
137 armadillo
.prototype.clearError_
= function() {
138 this.errorEffect_
.hide();
139 goog
.dom
.setTextContent(this.errorEffect_
.element
, '');
143 * Shows an error message.
144 * @param {string} message
146 armadillo
.prototype.showError_
= function(message
) {
147 goog
.dom
.setTextContent(this.errorEffect_
.element
, message
);
148 this.errorEffect_
.show();