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');
15 * A file in a directory listing.
16 * @param {string} File name.
17 * @param {string} The path the file resides at.
20 armadillo
.File
= function(name
, path
) {
24 this.isDirectory_
= app
.isDirectory(name
);
25 this.actor_
= new armadillo
.Actor(this);
28 armadillo
.File
.Highlight
= {
30 SELECTED
: 'file-selected',
31 ACTIVE
: 'file-active'
38 armadillo
.File
.prototype.disposeInternal
= function() {
41 this.actor_
.dispose();
45 * Returns the name of the file.
48 armadillo
.File
.prototype.getName
= function() {
53 * Returns the path the file without the name. This is equivalent to calling
54 * dirname on the absolute path.
57 armadillo
.File
.prototype.getParentPath
= function() {
62 * Gets the fully qualified path of the file, from the root of the jail to the
66 armadillo
.File
.prototype.getFullPath
= function() {
67 return this.path_
+ this.name_
;
71 * Returns whether or not this is a directory.
74 armadillo
.File
.prototype.isDirectory
= function() {
75 return this.isDirectory_
;
79 * Returns the extension of the file, or an empty string if theh file is a
80 * directory or does not have an extension.
83 armadillo
.File
.prototype.getExtension
= function() {
84 if (this.isDirectory())
86 var index
= this.getName().lastIndexOf('.');
89 return this.getName().substring(index
);
93 * Sets the highlight state.
95 armadillo
.File
.prototype.setHighlight
= function(h
) {
98 goog.dom.classes.addRemove(this.element_, this.highlight_, h);
104 * Constructs the Elements that make up the UI.
105 * @returns {Element} An element ready for insertion into DOM.
107 armadillo
.File
.prototype.draw
= function() {
108 // Create the element if it does not exist. If it does, remove all children.
109 if (!this.element_
) {
110 this.element_
= document
.createElement('li');
111 this.element_
.representedObject
= this;
112 var handler
= (this.isSpecial_() ? this.clickHandler_
: this.actorHandler_
);
114 $(this.element_
).empty();
116 // Set the name of the entry.
117 this.title_
= document
.createElement('div');
118 if (this.isDirectory()) {
119 this.link_
= $(document
.createElement('a'));
120 this.link_
.text(this.name_
);
121 this.link_
.click(this.clickHandler_
.bind(this));
122 $(this.title_
).append(this.link_
);
124 $(this.title_
).text(this.name_
);
126 $(this.element_
).append(this.title_
);
127 $(this.title_
).click(handler
.bind(this));
129 return this.element_
;
133 * Deletes the given file in the backend by sending a request. On return, it
134 * will re-query the directory.
136 armadillo
.File
.prototype.remove
= function() {
138 var callback
= function(data
, status
, xhr
) {
140 app
.showError(data
['message']);
145 app
.list(file
.path_
);
147 app
.sendRequest('remove', {'path':this.path_
+ this.name_
}, callback
);
151 * Moves a file from one absolute path to another. On success, it will navigate
153 * @param {string} dest The destination path.
155 armadillo
.File
.prototype.move
= function(dest
) {
157 var callback
= function(data
, status
, xhr
) {
159 app
.showError(data
['message']);
162 app
.list(app
.stripLastPathComponent(dest
));
165 app
.sendRequest('move', {'source':this.getFullPath(), 'target':dest
}, callback
);
169 * Click handler for the link element; only for directories.
172 armadillo
.File
.prototype.clickHandler_
= function(e
) {
173 if (this.isDirectory_
) {
174 app
.navigate(this.name_
);
180 * Click handler for the row, which brings up the Actor interface.
183 armadillo
.File
.prototype.actorHandler_
= function(e
) {
185 if (!this.actor_
.element
) {
186 var elm
= this.actor_
.createDom();
188 $(this.element_
).append(elm
);
190 this.actor_
.element_
.slideToggle('fast');
194 * Returns TRUE if this File is not a real file, but a special kind.
197 armadillo
.File
.prototype.isSpecial_
= function() {
198 return this.name_
== '../';