Disable the highlight mechanism after fixing the behavior in File.onPopupClosed_().
[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 goog.require('goog.Disposable');
14 goog.require('goog.dom');
15
16 /**
17 * A file in a directory listing.
18 * @param {string} File name.
19 * @param {string} The path the file resides at.
20 * @constructor
21 */
22 armadillo.File = function(name, path) {
23 goog.Disposable.call(this);
24 this.name_ = name;
25 this.path_ = path;
26 this.highlight_ = '';
27 this.isDirectory_ = app.isDirectory(name);
28 };
29 goog.inherits(armadillo.File, goog.Disposable);
30
31 armadillo.File.Highlight = {
32 NONE : '',
33 SELECTED : 'file-selected',
34 ACTIVE : 'file-active'
35 };
36
37 /**
38 * Disposer
39 * @protected
40 */
41 armadillo.File.prototype.disposeInternal = function() {
42 armadillo.File.superClass_.disposeInternal.call(this);
43 this.element_ = null;
44 this.link_ = null;
45 goog.events.unlistenByKey(this.linkListener_);
46 goog.events.unlistenByKey(this.actorListener_);
47 };
48
49 /**
50 * Returns the name of the file.
51 * @returns string
52 */
53 armadillo.File.prototype.getName = function() {
54 return this.name_;
55 };
56
57 /**
58 * Returns the path the file without the name. This is equivalent to calling
59 * dirname on the absolute path.
60 * @returns string
61 */
62 armadillo.File.prototype.getParentPath = function() {
63 return this.path_;
64 };
65
66 /**
67 * Gets the fully qualified path of the file, from the root of the jail to the
68 * name of the file.
69 * @returns string
70 */
71 armadillo.File.prototype.getFullPath = function() {
72 return this.path_ + this.name_;
73 };
74
75 /**
76 * Returns whether or not this is a directory.
77 * @returns boolean
78 */
79 armadillo.File.prototype.isDirectory = function() {
80 return this.isDirectory_;
81 };
82
83 /**
84 * Sets the highlight state.
85 */
86 armadillo.File.prototype.setHighlight = function(h) {
87 return; // TODO: enable.
88 goog.dom.classes.addRemove(this.element_, this.highlight_, h);
89 this.highlight_ = h;
90 };
91
92 /**
93 * Constructs the Elements that make up the UI.
94 * @returns {Element} An element ready for insertion into DOM.
95 */
96 armadillo.File.prototype.draw = function() {
97 // Create the element if it does not exist. If it does, remove all children.
98 if (!this.element_) {
99 this.element_ = goog.dom.createElement('li');
100 this.element_.representedObject = this;
101 var handler = (this.isSpecial_() ? this.clickHandler_ : this.actorHandler_);
102 this.actorListener_ = goog.events.listen(this.element_,
103 goog.events.EventType.CLICK, handler, false, this);
104 }
105 goog.dom.removeChildren(this.element_);
106
107 // Set the name of the entry.
108 if (this.isDirectory()) {
109 this.link_ = goog.dom.createDom('a', null, this.name_);
110 this.linkListener_ = goog.events.listen(this.link_,
111 goog.events.EventType.CLICK, this.clickHandler_, false, this);
112 goog.dom.appendChild(this.element_, this.link_);
113 } else {
114 goog.dom.setTextContent(this.element_, this.name_);
115 }
116
117 return this.element_;
118 };
119
120 /**
121 * Deletes the given file in the backend by sending a request. On return, it
122 * will re-query the directory.
123 */
124 armadillo.File.prototype.remove = function() {
125 var file = this;
126 var callback = function(data) {
127 if (data['error']) {
128 app.showError(data['message']);
129 return;
130 } else {
131 app.clearError();
132 }
133 app.list(file.path_);
134 };
135 app.sendRequest('remove', {'path':this.path_ + this.name_}, callback);
136 };
137
138 /**
139 * Moves a file from one absolute path to another. On success, it will navigate
140 * to the new path.
141 * @param {string} dest The destination path.
142 */
143 armadillo.File.prototype.move = function(dest) {
144 var file = this;
145 var callback = function(data) {
146 if (data['error']) {
147 app.showError(data['message']);
148 } else {
149 app.clearError();
150 app.list(app.stripLastPathComponent(dest));
151 }
152 };
153 app.sendRequest('move', {'source':this.getFullPath(), 'target':dest}, callback);
154 };
155
156 /**
157 * Click handler for the link element; only for directories.
158 * @param {Event} e
159 */
160 armadillo.File.prototype.clickHandler_ = function(e) {
161 if (armadillo.Actor.isModal()) {
162 return;
163 }
164 if (this.isDirectory_) {
165 app.navigate(this.name_);
166 }
167 e.stopPropagation();
168 };
169
170 /**
171 * Click handler for the row, which brings up the Actor interface.
172 * @param {Event} e
173 */
174 armadillo.File.prototype.actorHandler_ = function(e) {
175 if (armadillo.Actor.isModal())
176 return;
177 e.stopPropagation();
178 var actor = new armadillo.Actor(this);
179 // Adjust the mouse position so that if "Open" is the first tile, it is easy
180 // to navigate.
181 var x = e.clientX;
182 var y = e.clientY;
183 if (this.isDirectory()) {
184 x -= 20;
185 y -= 20;
186 }
187 actor.show(x, y);
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 };