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.File');
12 goog
.require('armadillo.Actor');
13 goog
.require('goog.Disposable');
14 goog
.require('goog.dom');
17 * A file in a directory listing.
18 * @param {string} File name.
19 * @param {string} The path the file resides at.
22 armadillo
.File
= function(name
, path
) {
23 goog
.Disposable
.call(this);
26 this.isDirectory_
= app
.isDirectory(name
);
28 goog
.inherits(armadillo
.File
, goog
.Disposable
);
34 armadillo
.File
.prototype.disposeInternal
= function() {
35 armadillo
.File
.superClass_
.disposeInternal
.call(this);
38 goog
.events
.unlistenByKey(this.linkListener_
);
39 goog
.events
.unlistenByKey(this.actorListener_
);
43 * Returns the name of the file.
46 armadillo
.File
.prototype.getName
= function() {
51 * Returns the path the file without the name. This is equivalent to calling
52 * dirname on the absolute path.
55 armadillo
.File
.prototype.getParentPath
= function() {
60 * Gets the fully qualified path of the file, from the root of the jail to the
64 armadillo
.File
.prototype.getFullPath
= function() {
65 return this.path_
+ this.name_
;
69 * Returns whether or not this is a directory.
72 armadillo
.File
.prototype.isDirectory
= function() {
73 return this.isDirectory_
;
77 * Constructs the Elements that make up the UI.
78 * @returns {Element} An element ready for insertion into DOM.
80 armadillo
.File
.prototype.draw
= function() {
81 // Create the element if it does not exist. If it does, remove all children.
83 this.element_
= goog
.dom
.createElement('li');
84 this.element_
.representedObject
= this;
85 var handler
= (this.isSpecial_() ? this.clickHandler_
: this.actorHandler_
);
86 this.actorListener_
= goog
.events
.listen(this.element_
,
87 goog
.events
.EventType
.CLICK
, handler
, false, this);
89 goog
.dom
.removeChildren(this.element_
);
91 // Set the name of the entry.
92 if (this.isDirectory()) {
93 this.link_
= goog
.dom
.createDom('a', null, this.name_
);
94 this.linkListener_
= goog
.events
.listen(this.link_
,
95 goog
.events
.EventType
.CLICK
, this.clickHandler_
, false, this);
96 goog
.dom
.appendChild(this.element_
, this.link_
);
98 goog
.dom
.setTextContent(this.element_
, this.name_
);
101 return this.element_
;
105 * Deletes the given file in the backend by sending a request. On return, it
106 * will re-query the directory.
108 armadillo
.File
.prototype.remove
= function() {
110 var callback
= function(data
) {
112 app
.showError(data
['message']);
117 app
.list(file
.path_
);
119 app
.sendRequest('remove', {'path':this.path_
+ this.name_
}, callback
);
123 * Moves a file from one absolute path to another. On success, it will navigate
125 * @param {string} dest The destination path.
127 armadillo
.File
.prototype.move
= function(dest
) {
129 var callback
= function(data
) {
131 app
.showError(data
['message']);
134 app
.list(app
.stripLastPathComponent(dest
));
137 app
.sendRequest('move', {'source':this.getFullPath(), 'target':dest
}, callback
);
141 * Click handler for the list element.
144 armadillo
.File
.prototype.clickHandler_
= function(e
) {
145 if (armadillo
.Actor
.isModal()) {
148 if (this.isDirectory_
) {
149 app
.navigate(this.name_
);
155 * Click handler for the button element.
158 armadillo
.File
.prototype.actorHandler_
= function(e
) {
159 if (armadillo
.Actor
.isModal())
162 var actor
= new armadillo
.Actor(this);
163 actor
.show(e
.clientX
, e
.clientY
);
167 * Returns TRUE if this File is not a real file, but a special kind.
170 armadillo
.File
.prototype.isSpecial_
= function() {
171 return this.name_
== '../';