2 // Armadillo File Manager
3 // Copyright (c) 2010, 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 goog
.provide('armadillo.File');
12 goog
.require('armadillo.Actor');
13 goog
.require('goog.Disposable');
14 goog
.require('goog.dom');
17 * A file in a directory listing.
18 * @param {string} File name.
19 * @param {string} The path the file resides at.
22 armadillo
.File
= function(name
, path
) {
23 goog
.Disposable
.call(this);
26 this.isDirectory_
= app
.isDirectory(name
);
28 goog
.inherits(armadillo
.File
, goog
.Disposable
);
34 armadillo
.File
.prototype.disposeInternal
= function() {
35 armadillo
.File
.superClass_
.disposeInternal
.call(this);
37 goog
.events
.unlistenByKey(this.clickListener_
);
38 goog
.events
.unlistenByKey(this.mouseOverListener_
);
39 goog
.events
.unlistenByKey(this.mouseOutListener_
);
41 goog
.events
.unlistenByKey(this.buttonListener_
);
45 * Returns the name of the file.
48 armadillo
.File
.prototype.getName
= function() {
53 * Returns the path the file without the name. This is equivalent to calling
54 * dirname on the absolute path.
57 armadillo
.File
.prototype.getParentPath
= function() {
62 * Gets the fully qualified path of the file, from the root of the jail to the
66 armadillo
.File
.prototype.getFullPath
= function() {
67 return this.path_
+ this.name_
;
71 * Returns whether or not this is a directory.
74 armadillo
.File
.prototype.isDirectory
= function() {
75 return this.isDirectory_
;
79 * Constructs the Elements that make up the UI.
80 * @returns {Element} An element ready for insertion into DOM.
82 armadillo
.File
.prototype.draw
= function() {
83 // Create the element if it does not exist. If it does, remove all children.
85 this.element_
= goog
.dom
.createElement('li');
86 this.element_
.representedObject
= this;
87 this.clickListener_
= goog
.events
.listen(this.element_
,
88 goog
.events
.EventType
.CLICK
, this.clickHandler_
, false, this);
89 if (!this.isSpecial_()) {
90 this.mouseOverListener_
= goog
.events
.listen(this.element_
,
91 goog
.events
.EventType
.MOUSEOVER
, this.hoverHandler_
, false, this);
92 this.mouseOutListener_
= goog
.events
.listen(this.element_
,
93 goog
.events
.EventType
.MOUSEOUT
, this.hoverHandler_
, false, this);
96 goog
.dom
.removeChildren(this.element_
);
98 // Set the name of the entry.
99 goog
.dom
.setTextContent(this.element_
, this.name_
);
101 // Create the edit button.
102 if (!this.isSpecial_()) {
103 this.button_
= goog
.dom
.createElement('button');
104 goog
.dom
.setTextContent(this.button_
, 'Edit');
105 goog
.dom
.appendChild(this.element_
, this.button_
);
106 this.button_
.style
.display
= 'none';
107 this.buttonListener_
= goog
.events
.listen(this.button_
,
108 goog
.events
.EventType
.CLICK
, this.buttonClickHandler_
, false, this);
111 return this.element_
;
115 * Deletes the given file in the backend by sending a request. On return, it
116 * will re-query the directory.
118 armadillo
.File
.prototype.remove
= function() {
120 var callback
= function(data
) {
122 app
.showError(data
['message']);
127 app
.list(file
.path_
);
129 app
.sendRequest('remove', {'path':this.path_
+ this.name_
}, callback
);
133 * Click handler for the list element.
136 armadillo
.File
.prototype.clickHandler_
= function(e
) {
137 if (armadillo
.Actor
.isModal()) {
140 if (this.isDirectory_
) {
141 app
.navigate(this.name_
);
146 * Hover event handler for the list element. This can handle both mouseover
147 * and mouseout events.
150 armadillo
.File
.prototype.hoverHandler_
= function(e
) {
151 if (armadillo
.Actor
.isModal())
153 var display
= (e
.type
== goog
.events
.EventType
.MOUSEOVER
);
154 this.button_
.style
.display
= (display
? '' : 'none');
158 * Click handler for the button element.
161 armadillo
.File
.prototype.buttonClickHandler_
= function(e
) {
162 if (armadillo
.Actor
.isModal())
165 var actor
= new armadillo
.Actor(this);
166 actor
.show(e
.clientX
, e
.clientY
);
170 * Returns TRUE if this File is not a real file, but a special kind.
173 armadillo
.File
.prototype.isSpecial_
= function() {
174 return this.name_
== '../';