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