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);
27 this.isDirectory_
= app
.isDirectory(name
);
29 goog
.inherits(armadillo
.File
, goog
.Disposable
);
31 armadillo
.File
.Highlight
= {
33 SELECTED
: 'file-selected',
34 ACTIVE
: 'file-active'
41 armadillo
.File
.prototype.disposeInternal
= function() {
42 armadillo
.File
.superClass_
.disposeInternal
.call(this);
45 goog
.events
.unlistenByKey(this.linkListener_
);
46 goog
.events
.unlistenByKey(this.actorListener_
);
50 * Returns the name of the file.
53 armadillo
.File
.prototype.getName
= function() {
58 * Returns the path the file without the name. This is equivalent to calling
59 * dirname on the absolute path.
62 armadillo
.File
.prototype.getParentPath
= function() {
67 * Gets the fully qualified path of the file, from the root of the jail to the
71 armadillo
.File
.prototype.getFullPath
= function() {
72 return this.path_
+ this.name_
;
76 * Returns whether or not this is a directory.
79 armadillo
.File
.prototype.isDirectory
= function() {
80 return this.isDirectory_
;
84 * Sets the highlight state.
86 armadillo
.File
.prototype.setHighlight
= function(h
) {
89 goog.dom.classes.addRemove(this.element_, this.highlight_, h);
95 * Constructs the Elements that make up the UI.
96 * @returns {Element} An element ready for insertion into DOM.
98 armadillo
.File
.prototype.draw
= function() {
99 // Create the element if it does not exist. If it does, remove all children.
100 if (!this.element_
) {
101 this.element_
= goog
.dom
.createElement('li');
102 this.element_
.representedObject
= this;
103 var handler
= (this.isSpecial_() ? this.clickHandler_
: this.actorHandler_
);
104 this.actorListener_
= goog
.events
.listen(this.element_
,
105 goog
.events
.EventType
.CLICK
, handler
, false, this);
107 goog
.dom
.removeChildren(this.element_
);
109 // Set the name of the entry.
110 if (this.isDirectory()) {
111 this.link_
= goog
.dom
.createDom('a', null, this.name_
);
112 this.linkListener_
= goog
.events
.listen(this.link_
,
113 goog
.events
.EventType
.CLICK
, this.clickHandler_
, false, this);
114 goog
.dom
.appendChild(this.element_
, this.link_
);
116 goog
.dom
.setTextContent(this.element_
, this.name_
);
119 return this.element_
;
123 * Deletes the given file in the backend by sending a request. On return, it
124 * will re-query the directory.
126 armadillo
.File
.prototype.remove
= function() {
128 var callback
= function(data
) {
130 app
.showError(data
['message']);
135 app
.list(file
.path_
);
137 app
.sendRequest('remove', {'path':this.path_
+ this.name_
}, callback
);
141 * Moves a file from one absolute path to another. On success, it will navigate
143 * @param {string} dest The destination path.
145 armadillo
.File
.prototype.move
= function(dest
) {
147 var callback
= function(data
) {
149 app
.showError(data
['message']);
152 app
.list(app
.stripLastPathComponent(dest
));
155 app
.sendRequest('move', {'source':this.getFullPath(), 'target':dest
}, callback
);
159 * Click handler for the link element; only for directories.
162 armadillo
.File
.prototype.clickHandler_
= function(e
) {
163 if (armadillo
.Actor
.isModal()) {
166 if (this.isDirectory_
) {
167 app
.navigate(this.name_
);
173 * Click handler for the row, which brings up the Actor interface.
176 armadillo
.File
.prototype.actorHandler_
= function(e
) {
177 if (armadillo
.Actor
.isModal())
180 var actor
= new armadillo
.Actor(this);
181 // Adjust the mouse position so that if "Open" is the first tile, it is easy
185 if (this.isDirectory()) {
193 * Returns TRUE if this File is not a real file, but a special kind.
196 armadillo
.File
.prototype.isSpecial_
= function() {
197 return this.name_
== '../';