2 // Armadillo File Manager
3 // Copyright (c) 2010-2011, 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 $.namespace('armadillo.File');
13 * A file in a directory listing.
14 * @param {string} File name.
15 * @param {string} The path the file resides at.
18 armadillo
.File
= function(name
, path
) {
22 this.isDirectory_
= app
.isDirectory(name
);
23 this.actor_
= new armadillo
.Actor(this);
26 armadillo
.File
.Highlight
= {
28 SELECTED
: 'file-selected',
29 ACTIVE
: 'file-active'
33 * Returns the name of the file.
36 armadillo
.File
.prototype.getName
= function() {
41 * Returns the path the file without the name. This is equivalent to calling
42 * dirname on the absolute path.
45 armadillo
.File
.prototype.getParentPath
= function() {
50 * Gets the fully qualified path of the file, from the root of the jail to the
54 armadillo
.File
.prototype.getFullPath
= function() {
55 return this.path_
+ this.name_
;
59 * Returns whether or not this is a directory.
62 armadillo
.File
.prototype.isDirectory
= function() {
63 return this.isDirectory_
;
67 * Returns the extension of the file, or an empty string if theh file is a
68 * directory or does not have an extension.
71 armadillo
.File
.prototype.getExtension
= function() {
72 if (this.isDirectory())
74 var index
= this.getName().lastIndexOf('.');
77 return this.getName().substring(index
);
81 * Sets the highlight state.
83 armadillo
.File
.prototype.setHighlight
= function(h
) {
86 goog.dom.classes.addRemove(this.element_, this.highlight_, h);
92 * Constructs the Elements that make up the UI.
93 * @returns {Element} An element ready for insertion into DOM.
95 armadillo
.File
.prototype.createDom
= function() {
96 // Create the element if it does not exist. If it does, remove all children.
98 this.element_
= document
.createElement('li');
99 this.element_
.representedObject
= this;
100 var handler
= (this.isSpecial_() ? this.clickHandler_
: this.actorHandler_
);
102 $(this.element_
).empty();
104 // Set the name of the entry.
105 this.title_
= $.createDom('div');
106 if (this.isDirectory()) {
107 this.link_
= $.createDom('a');
108 this.link_
.text(this.name_
);
109 this.link_
.click(this.clickHandler_
.bind(this));
110 this.title_
.append(this.link_
);
112 this.title_
.text(this.name_
);
114 $(this.element_
).append(this.title_
);
115 this.title_
.click(handler
.bind(this));
117 return this.element_
;
121 * Deletes the given file in the backend by sending a request. On return, it
122 * will re-query the directory.
124 armadillo
.File
.prototype.remove
= function() {
126 var callback
= function(data
, status
, xhr
) {
128 app
.showError(data
['message']);
133 app
.list(file
.path_
);
135 app
.sendRequest('remove', {'path':this.path_
+ this.name_
}, callback
);
139 * Moves a file from one absolute path to another. On success, it will navigate
141 * @param {string} dest The destination path.
143 armadillo
.File
.prototype.move
= function(dest
) {
145 var callback
= function(data
, status
, xhr
) {
147 app
.showError(data
['message']);
150 app
.list(app
.stripLastPathComponent(dest
));
153 app
.sendRequest('move', {'source':this.getFullPath(), 'target':dest
}, callback
);
157 * Click handler for the link element; only for directories.
160 armadillo
.File
.prototype.clickHandler_
= function(e
) {
161 if (this.isDirectory_
) {
162 app
.navigate(this.name_
);
168 * Click handler for the row, which brings up the Actor interface.
171 armadillo
.File
.prototype.actorHandler_
= function(e
) {
173 if (!this.actor_
.getElement()) {
174 var elm
= this.actor_
.createDom();
176 $(this.element_
).append(elm
);
178 this.actor_
.getElement().slideToggle('fast');
182 * Returns TRUE if this File is not a real file, but a special kind.
185 armadillo
.File
.prototype.isSpecial_
= function() {
186 return this.name_
== '../';