Implement the Open action.
[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.isDirectory_ = app.isDirectory(name);
27 };
28 goog.inherits(armadillo.File, goog.Disposable);
29
30 /**
31 * Disposer
32 * @protected
33 */
34 armadillo.File.prototype.disposeInternal = function() {
35 armadillo.File.superClass_.disposeInternal.call(this);
36 this.element_ = null;
37 goog.events.unlistenByKey(this.clickListener_);
38 goog.events.unlistenByKey(this.mouseOverListener_);
39 goog.events.unlistenByKey(this.mouseOutListener_);
40 this.button_ = null;
41 goog.events.unlistenByKey(this.buttonListener_);
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 * Gets the fully qualified path of the file, from the root of the jail to the
54 * name of the file.
55 * @returns string
56 */
57 armadillo.File.prototype.getFullPath = function() {
58 return this.path_ + this.name_;
59 };
60
61 /**
62 * Returns whether or not this is a directory.
63 * @returns boolean
64 */
65 armadillo.File.prototype.isDirectory = function() {
66 return this.isDirectory_;
67 };
68
69 /**
70 * Constructs the Elements that make up the UI.
71 * @returns {Element} An element ready for insertion into DOM.
72 */
73 armadillo.File.prototype.draw = function() {
74 // Create the element if it does not exist. If it does, remove all children.
75 if (!this.element_) {
76 this.element_ = goog.dom.createElement('li');
77 this.element_.representedObject = this;
78 this.clickListener_ = goog.events.listen(this.element_,
79 goog.events.EventType.CLICK, this.clickHandler_, false, this);
80 if (!this.isSpecial_()) {
81 this.mouseOverListener_ = goog.events.listen(this.element_,
82 goog.events.EventType.MOUSEOVER, this.hoverHandler_, false, this);
83 this.mouseOutListener_ = goog.events.listen(this.element_,
84 goog.events.EventType.MOUSEOUT, this.hoverHandler_, false, this);
85 }
86 }
87 goog.dom.removeChildren(this.element_);
88
89 // Set the name of the entry.
90 goog.dom.setTextContent(this.element_, this.name_);
91
92 // Create the edit button.
93 if (!this.isSpecial_()) {
94 this.button_ = goog.dom.createElement('button');
95 goog.dom.setTextContent(this.button_, 'Edit');
96 goog.dom.appendChild(this.element_, this.button_);
97 this.button_.style.display = 'none';
98 this.buttonListener_ = goog.events.listen(this.button_,
99 goog.events.EventType.CLICK, this.buttonClickHandler_, false, this);
100 }
101
102 return this.element_;
103 };
104
105 /**
106 * Deletes the given file in the backend by sending a request. On return, it
107 * will re-query the directory.
108 */
109 armadillo.File.prototype.delete = function() {
110 var file = this;
111 var callback = function(data) {
112 if (data['error']) {
113 app.showError(data['message']);
114 return;
115 } else {
116 app.clearError();
117 }
118 app.list(file.path_);
119 };
120 app.sendRequest('remove', {'path':this.path_ + this.name_}, callback);
121 };
122
123 /**
124 * Click handler for the list element.
125 * @param {Event} e
126 */
127 armadillo.File.prototype.clickHandler_ = function(e) {
128 if (armadillo.Actor.isModal()) {
129 return;
130 }
131 if (this.isDirectory_) {
132 app.navigate(this.name_);
133 }
134 };
135
136 /**
137 * Hover event handler for the list element. This can handle both mouseover
138 * and mouseout events.
139 * @param {Event} e
140 */
141 armadillo.File.prototype.hoverHandler_ = function(e) {
142 if (armadillo.Actor.isModal())
143 return;
144 var display = (e.type == goog.events.EventType.MOUSEOVER);
145 this.button_.style.display = (display ? '' : 'none');
146 };
147
148 /**
149 * Click handler for the button element.
150 * @param {Event} e
151 */
152 armadillo.File.prototype.buttonClickHandler_ = function(e) {
153 if (armadillo.Actor.isModal())
154 return;
155 e.stopPropagation();
156 var actor = new armadillo.Actor(this);
157 actor.show(e.clientX, e.clientY);
158 };
159
160 /**
161 * Returns TRUE if this File is not a real file, but a special kind.
162 * @returns boolean
163 */
164 armadillo.File.prototype.isSpecial_ = function() {
165 return this.name_ == '../';
166 };