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.net.XhrIo');
15 goog
.require('goog.Uri.QueryData');
17 armadillo
= function() {
19 if (window
.location
.hash
) {
20 start_path
= window
.location
.hash
.substr(1);
22 this.list(start_path
);
23 this.listeners_
= new Array();
24 goog
.events
.listen(window
, goog
.events
.EventType
.HASHCHANGE
,
25 this.hashChanged_
, false, this);
29 * Starts a new XHR service request from the backend.
30 * @param {string} action Action to perform.
31 * @param {Object} extra_data Extra data to add.
32 * @param {Function} callback XHR callback.
34 armadillo
.prototype.sendRequest_
= function(action
, extra_data
, callback
) {
35 var data
= new goog
.Uri
.QueryData();
36 data
.set('action', action
);
37 data
.extend(extra_data
);
38 goog
.net
.XhrIo
.send('/service', callback
, 'POST', data
);
42 * Updates the directory listing for a given path.
43 * @param {string} path Path to list; relative to jail.
45 armadillo
.prototype.list
= function(path
) {
46 var callback
= function(e
) {
47 var data
= e
.target
.getResponseJson();
51 // Unlisten all current listeners.
52 goog
.array
.forEach(app
.listeners_
, function(e
) {
53 goog
.events
.unlistenByKey(e
);
56 // Update the listing.
57 goog
.dom
.setTextContent(goog
.dom
.getElement('pwd'), path
);
58 app
.currentPath_
= path
;
59 window
.location
.hash
= path
;
60 var list
= goog
.dom
.getElement('ls');
61 goog
.dom
.removeChildren(list
);
63 // Add a previous directory entry.
64 if (path
!= '/' && path
!= '')
65 goog
.array
.insertAt(data
, '../', 0);
67 // Add items for each entry.
68 goog
.array
.forEach(data
, function(file
) {
69 var elm
= goog
.dom
.createElement('li');
70 goog
.dom
.setTextContent(elm
, file
);
71 goog
.dom
.appendChild(list
, elm
);
72 app
.listeners_
.push(goog
.events
.listen(elm
,
73 goog
.events
.EventType
.CLICK
, app
.clickHandler_
, false, app
));
76 this.sendRequest_('list', {'path':path
}, callback
);
80 * Click handler for elements.
83 armadillo
.prototype.clickHandler_
= function(e
) {
84 var target
= goog
.dom
.getTextContent(e
.target
);
85 if (target
== '../') {
86 this.list(this.stripLastPathComponent_(this.currentPath_
));
87 } else if (this.isDirectory_(target
)) {
88 this.list(this.currentPath_
+ target
);
93 * Event for when the hash changes.
96 armadillo
.prototype.hashChanged_
= function(e
) {
97 if (window
.location
.hash
.length
)
98 this.list(window
.location
.hash
.substr(1));
102 * Checks whether a path is a directory.
103 * @param {string} path
106 armadillo
.prototype.isDirectory_
= function(path
) {
107 return path
[path
.length
- 1] == '/';
111 * Strips the last path component from a path.
112 * @param {string} path
115 armadillo
.prototype.stripLastPathComponent_
= function(path
) {
116 for (var i
= path
.length
- 1; i
>= 0; --i
) {
117 if (path
[i] == '/') {
118 if (i
!= path
.length
- 1) {
119 return path
.substring(0, i
+ 1);