Hook up the PathControl and Actor up to the move service request.
[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 this.link_ = null;
38 goog.events.unlistenByKey(this.linkListener_);
39 goog.events.unlistenByKey(this.actorListener_);
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 * Constructs the Elements that make up the UI.
78 * @returns {Element} An element ready for insertion into DOM.
79 */
80 armadillo.File.prototype.draw = function() {
81 // Create the element if it does not exist. If it does, remove all children.
82 if (!this.element_) {
83 this.element_ = goog.dom.createElement('li');
84 this.element_.representedObject = this;
85 var handler = (this.isSpecial_() ? this.clickHandler_ : this.actorHandler_);
86 this.actorListener_ = goog.events.listen(this.element_,
87 goog.events.EventType.CLICK, handler, false, this);
88 }
89 goog.dom.removeChildren(this.element_);
90
91 // Set the name of the entry.
92 if (this.isDirectory()) {
93 this.link_ = goog.dom.createDom('a', null, this.name_);
94 this.linkListener_ = goog.events.listen(this.link_,
95 goog.events.EventType.CLICK, this.clickHandler_, false, this);
96 goog.dom.appendChild(this.element_, this.link_);
97 } else {
98 goog.dom.setTextContent(this.element_, this.name_);
99 }
100
101 return this.element_;
102 };
103
104 /**
105 * Deletes the given file in the backend by sending a request. On return, it
106 * will re-query the directory.
107 */
108 armadillo.File.prototype.remove = function() {
109 var file = this;
110 var callback = function(data) {
111 if (data['error']) {
112 app.showError(data['message']);
113 return;
114 } else {
115 app.clearError();
116 }
117 app.list(file.path_);
118 };
119 app.sendRequest('remove', {'path':this.path_ + this.name_}, callback);
120 };
121
122 /**
123 * Moves a file from one absolute path to another. On success, it will navigate
124 * to the new path.
125 * @param {string} dest The destination path.
126 */
127 armadillo.File.prototype.move = function(dest) {
128 var file = this;
129 var callback = function(data) {
130 if (data['error']) {
131 app.showError(data['message']);
132 } else {
133 app.clearError();
134 app.list(app.stripLastPathComponent(dest));
135 }
136 };
137 app.sendRequest('move', {'source':this.getFullPath(), 'target':dest}, callback);
138 };
139
140 /**
141 * Click handler for the list element.
142 * @param {Event} e
143 */
144 armadillo.File.prototype.clickHandler_ = function(e) {
145 if (armadillo.Actor.isModal()) {
146 return;
147 }
148 if (this.isDirectory_) {
149 app.navigate(this.name_);
150 }
151 e.stopPropagation();
152 };
153
154 /**
155 * Click handler for the button element.
156 * @param {Event} e
157 */
158 armadillo.File.prototype.actorHandler_ = function(e) {
159 if (armadillo.Actor.isModal())
160 return;
161 e.stopPropagation();
162 var actor = new armadillo.Actor(this);
163 actor.show(e.clientX, e.clientY);
164 };
165
166 /**
167 * Returns TRUE if this File is not a real file, but a special kind.
168 * @returns boolean
169 */
170 armadillo.File.prototype.isSpecial_ = function() {
171 return this.name_ == '../';
172 };