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