Create confirmation dialog for file deletion.
[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('goog.array');
15 goog.require('goog.dom');
16 goog.require('goog.fx.dom.FadeInAndShow');
17 goog.require('goog.net.XhrIo');
18 goog.require('goog.Uri.QueryData');
19
20 armadillo.App = function() {
21 var start_path = '/';
22 if (window.location.hash) {
23 start_path = window.location.hash.substr(1);
24 }
25 this.list(start_path);
26 this.errorEffect_ =
27 new goog.fx.dom.FadeInAndShow(goog.dom.getElement('error'), 2.0);
28 this.errorEffect_.hide();
29 goog.events.listen(window, goog.events.EventType.HASHCHANGE,
30 this.hashChanged_, false, this);
31 }
32
33 /**
34 * Starts a new XHR service request from the backend.
35 * @param {string} action Action to perform.
36 * @param {Object} extra_data Extra data to add.
37 * @param {Function} callback XHR callback.
38 */
39 armadillo.App.prototype.sendRequest = function(action, extra_data, callback) {
40 var data = new goog.Uri.QueryData();
41 data.set('action', action);
42 data.extend(extra_data);
43 goog.net.XhrIo.send('/service', callback, 'POST', data);
44 };
45
46 /**
47 * Updates the directory listing for a given path.
48 * @param {string} path Path to list; relative to jail.
49 */
50 armadillo.App.prototype.list = function(path) {
51 var callback = function(e) {
52 var data = e.target.getResponseJson();
53 if (data['error']) {
54 app.showError(data['message']);
55 return; // Error.
56 } else {
57 app.clearError();
58 }
59
60 // Update the listing.
61 goog.dom.setTextContent(goog.dom.getElement('pwd'), path);
62 app.currentPath_ = path;
63 window.location.hash = path;
64 var list = goog.dom.getElement('ls');
65 goog.dom.removeChildren(list);
66
67 // Add a previous directory entry.
68 if (path != '/' && path != '')
69 goog.array.insertAt(data, '../', 0);
70
71 // Add items for each entry.
72 goog.array.forEach(data, function(file) {
73 var fileObject = new armadillo.File(file, path);
74 goog.dom.appendChild(list, fileObject.draw());
75 });
76 }
77 this.sendRequest('list', {'path':path}, callback);
78 };
79
80 /**
81 * Navigates to a subpath. Can only handle directories.
82 * @param {string} target Relative path to |currentPath_|.
83 */
84 armadillo.App.prototype.navigate = function(target) {
85 if (target == '../') {
86 this.list(this.stripLastPathComponent_(this.currentPath_));
87 } else {
88 this.list(this.currentPath_ + target);
89 }
90 };
91
92 /**
93 * Event for when the hash changes.
94 * @param {Event} e
95 */
96 armadillo.App.prototype.hashChanged_ = function(e) {
97 if (window.location.hash.length)
98 this.list(window.location.hash.substr(1));
99 };
100
101 /**
102 * Checks whether a path is a directory.
103 * @param {string} path
104 * @returns boolean
105 */
106 armadillo.App.prototype.isDirectory = function(path) {
107 return path[path.length - 1] == '/';
108 };
109
110 /**
111 * Gets the current path of the directory being displayed, absolute to root.
112 * @returns string
113 */
114 armadillo.App.prototype.getCurrentPath = function() {
115 return this.currentPath_;
116 };
117
118 /**
119 * Strips the last path component from a path.
120 * @param {string} path
121 * @returns string
122 */
123 armadillo.App.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);
128 }
129 }
130 }
131 return '/';
132 };
133
134 /**
135 * Clears the error message.
136 */
137 armadillo.App.prototype.clearError = function() {
138 this.errorEffect_.hide();
139 goog.dom.setTextContent(this.errorEffect_.element, '');
140 };
141
142 /**
143 * Shows an error message.
144 * @param {string} message
145 */
146 armadillo.App.prototype.showError = function(message) {
147 goog.dom.setTextContent(this.errorEffect_.element, message);
148 this.errorEffect_.show();
149 };