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
);
28 this.actor_
= new armadillo
.Actor(this);
30 goog
.inherits(armadillo
.File
, goog
.Disposable
);
32 armadillo
.File
.Highlight
= {
34 SELECTED
: 'file-selected',
35 ACTIVE
: 'file-active'
42 armadillo
.File
.prototype.disposeInternal
= function() {
43 armadillo
.File
.superClass_
.disposeInternal
.call(this);
46 goog
.events
.unlistenByKey(this.linkListener_
);
47 goog
.events
.unlistenByKey(this.actorListener_
);
48 this.actor_
.dispose();
52 * Returns the name of the file.
55 armadillo
.File
.prototype.getName
= function() {
60 * Returns the path the file without the name. This is equivalent to calling
61 * dirname on the absolute path.
64 armadillo
.File
.prototype.getParentPath
= function() {
69 * Gets the fully qualified path of the file, from the root of the jail to the
73 armadillo
.File
.prototype.getFullPath
= function() {
74 return this.path_
+ this.name_
;
78 * Returns whether or not this is a directory.
81 armadillo
.File
.prototype.isDirectory
= function() {
82 return this.isDirectory_
;
86 * Returns the extension of the file, or an empty string if theh file is a
87 * directory or does not have an extension.
90 armadillo
.File
.prototype.getExtension
= function() {
91 if (this.isDirectory())
93 var index
= this.getName().lastIndexOf('.');
96 return this.getName().substring(index
);
100 * Sets the highlight state.
102 armadillo
.File
.prototype.setHighlight
= function(h
) {
105 goog.dom.classes.addRemove(this.element_, this.highlight_, h);
111 * Constructs the Elements that make up the UI.
112 * @returns {Element} An element ready for insertion into DOM.
114 armadillo
.File
.prototype.draw
= function() {
115 // Create the element if it does not exist. If it does, remove all children.
116 if (!this.element_
) {
117 this.element_
= goog
.dom
.createElement('li');
118 this.element_
.representedObject
= this;
119 var handler
= (this.isSpecial_() ? this.clickHandler_
: this.actorHandler_
);
121 goog
.dom
.removeChildren(this.element_
);
123 // Set the name of the entry.
124 this.title_
= goog
.dom
.createDom('div', null);
125 if (this.isDirectory()) {
126 this.link_
= goog
.dom
.createDom('a', null, this.name_
);
127 this.linkListener_
= goog
.events
.listen(this.link_
,
128 goog
.events
.EventType
.CLICK
, this.clickHandler_
, false, this);
129 goog
.dom
.appendChild(this.title_
, this.link_
);
131 goog
.dom
.setTextContent(this.title_
, this.name_
);
133 goog
.dom
.appendChild(this.element_
, this.title_
);
134 this.actorListener_
= goog
.events
.listen(this.title_
,
135 goog
.events
.EventType
.CLICK
, handler
, false, this);
137 return this.element_
;
141 * Deletes the given file in the backend by sending a request. On return, it
142 * will re-query the directory.
144 armadillo
.File
.prototype.remove
= function() {
146 var callback
= function(data
) {
148 app
.showError(data
['message']);
153 app
.list(file
.path_
);
155 app
.sendRequest('remove', {'path':this.path_
+ this.name_
}, callback
);
159 * Moves a file from one absolute path to another. On success, it will navigate
161 * @param {string} dest The destination path.
163 armadillo
.File
.prototype.move
= function(dest
) {
165 var callback
= function(data
) {
167 app
.showError(data
['message']);
170 app
.list(app
.stripLastPathComponent(dest
));
173 app
.sendRequest('move', {'source':this.getFullPath(), 'target':dest
}, callback
);
177 * Click handler for the link element; only for directories.
180 armadillo
.File
.prototype.clickHandler_
= function(e
) {
181 if (this.isDirectory_
) {
182 app
.navigate(this.name_
);
188 * Click handler for the row, which brings up the Actor interface.
191 armadillo
.File
.prototype.actorHandler_
= function(e
) {
193 if (!this.actor_
.isInDocument())
194 this.actor_
.render(this.element_
);
195 $(this.actor_
.getElement()).slideToggle('fast');
199 * Returns TRUE if this File is not a real file, but a special kind.
202 armadillo
.File
.prototype.isSpecial_
= function() {
203 return this.name_
== '../';