Move files around to organize along packages
[armadillo.git] / frontend / file.js
1 //
2 // Armadillo File Manager
3 // Copyright (c) 2010-2011, Robert Sesek <http://www.bluestatic.org>
4 //
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.
8 //
9
10 $.namespace('armadillo.File');
11
12 /**
13 * A file in a directory listing.
14 * @param {string} File name.
15 * @param {string} The path the file resides at.
16 * @constructor
17 */
18 armadillo.File = function(name, path) {
19 this.name_ = name;
20 this.path_ = path;
21 this.highlight_ = '';
22 this.isDirectory_ = app.isDirectory(name);
23 this.actor_ = new armadillo.Actor(this);
24 };
25
26 armadillo.File.Highlight = {
27 NONE : '',
28 SELECTED : 'file-selected',
29 ACTIVE : 'file-active'
30 };
31
32 /**
33 * Returns the name of the file.
34 * @returns string
35 */
36 armadillo.File.prototype.getName = function() {
37 return this.name_;
38 };
39
40 /**
41 * Returns the path the file without the name. This is equivalent to calling
42 * dirname on the absolute path.
43 * @returns string
44 */
45 armadillo.File.prototype.getParentPath = function() {
46 return this.path_;
47 };
48
49 /**
50 * Gets the fully qualified path of the file, from the root of the jail to the
51 * name of the file.
52 * @returns string
53 */
54 armadillo.File.prototype.getFullPath = function() {
55 return this.path_ + this.name_;
56 };
57
58 /**
59 * Returns whether or not this is a directory.
60 * @returns boolean
61 */
62 armadillo.File.prototype.isDirectory = function() {
63 return this.isDirectory_;
64 };
65
66 /**
67 * Returns the extension of the file, or an empty string if theh file is a
68 * directory or does not have an extension.
69 * @returns string
70 */
71 armadillo.File.prototype.getExtension = function() {
72 if (this.isDirectory())
73 return '';
74 var index = this.getName().lastIndexOf('.');
75 if (index == -1)
76 return '';
77 return this.getName().substring(index);
78 };
79
80 /**
81 * Sets the highlight state.
82 */
83 armadillo.File.prototype.setHighlight = function(h) {
84 /*
85 // TODO: enable.
86 goog.dom.classes.addRemove(this.element_, this.highlight_, h);
87 this.highlight_ = h;
88 */
89 };
90
91 /**
92 * Constructs the Elements that make up the UI.
93 * @returns {Element} An element ready for insertion into DOM.
94 */
95 armadillo.File.prototype.createDom = function() {
96 // Create the element if it does not exist. If it does, remove all children.
97 if (!this.element_) {
98 this.element_ = $.createDom('li');
99 var handler = (this.isSpecial_() ? this.clickHandler_ : this.actorHandler_);
100 }
101 this.element_.empty();
102
103 // Set the name of the entry.
104 this.title_ = $.createDom('div');
105 if (this.isDirectory()) {
106 this.link_ = $.createDom('a');
107 this.link_.text(this.name_);
108 this.link_.click(this.clickHandler_.bind(this));
109 this.title_.append(this.link_);
110 } else {
111 this.title_.text(this.name_);
112 }
113 this.element_.append(this.title_);
114 this.title_.click(handler.bind(this));
115
116 return this.element_;
117 };
118
119 /**
120 * Deletes the given file in the backend by sending a request. On return, it
121 * will re-query the directory.
122 */
123 armadillo.File.prototype.remove = function() {
124 var file = this;
125 var callback = function(data, status, xhr) {
126 if (data['error']) {
127 app.showError(data['message']);
128 return;
129 } else {
130 app.clearError();
131 }
132 app.list(file.path_);
133 };
134 app.sendRequest('remove', {'path':this.path_ + this.name_}, callback);
135 };
136
137 /**
138 * Moves a file from one absolute path to another. On success, it will navigate
139 * to the new path.
140 * @param {string} dest The destination path.
141 */
142 armadillo.File.prototype.move = function(dest) {
143 var file = this;
144 var callback = function(data, status, xhr) {
145 if (data['error']) {
146 app.showError(data['message']);
147 } else {
148 app.clearError();
149 app.list(app.stripLastPathComponent(dest));
150 }
151 };
152 app.sendRequest('move', {'source':this.getFullPath(), 'target':dest}, callback);
153 };
154
155 /**
156 * Click handler for the link element; only for directories.
157 * @param {Event} e
158 */
159 armadillo.File.prototype.clickHandler_ = function(e) {
160 if (this.isDirectory_) {
161 app.navigate(this.name_);
162 }
163 e.stopPropagation();
164 };
165
166 /**
167 * Click handler for the row, which brings up the Actor interface.
168 * @param {Event} e
169 */
170 armadillo.File.prototype.actorHandler_ = function(e) {
171 e.stopPropagation();
172 if (!this.actor_.getElement()) {
173 var elm = this.actor_.createDom();
174 elm.hide();
175 this.element_.append(elm);
176 }
177 this.actor_.getElement().slideToggle('fast');
178 };
179
180 /**
181 * Returns TRUE if this File is not a real file, but a special kind.
182 * @returns boolean
183 */
184 armadillo.File.prototype.isSpecial_ = function() {
185 return this.name_ == '../';
186 };