Convert the Actor to a goog.ui.Container().
[armadillo.git] / web_frontend / actor.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.Actor');
11 goog.provide('armadillo.Actor.TileControlRenderer_');
12
13 goog.require('armadillo.PathControl');
14 goog.require('goog.array');
15 goog.require('goog.dom');
16 goog.require('goog.events');
17 goog.require('goog.events.EventHandler');
18 goog.require('goog.positioning.ClientPosition');
19 goog.require('goog.positioning.Corner');
20 goog.require('goog.style');
21 goog.require('goog.ui.Container');
22 goog.require('goog.ui.Dialog');
23 goog.require('goog.ui.Popup');
24
25 /**
26 * The Actor is a popup that displays the various actions that can be performed
27 * on a given File.
28 * @param {armadillo.File} file The file to act on.
29 * @param {goog.dom.DomHelper} opt_domHelper
30 * @constructor
31 */
32 armadillo.Actor = function(file, opt_domHelper) {
33 goog.ui.Container.call(this, null, null, opt_domHelper);
34
35 /**
36 * The file object on which this acts.
37 * @type {armadillo.File}
38 */
39 this.file_ = file;
40
41 /**
42 * Registrar for all the Actor's events.
43 * @type {goog.events.EventHandler}
44 */
45 this.eh_ = new goog.events.EventHandler();
46
47 /**
48 * The Container that holds the display element.
49 * @type {goog.ui.Popup}
50 */
51 this.popup_ = new goog.ui.Popup(this.element_);
52 this.eh_.listenOnce(this.popup_, goog.ui.PopupBase.EventType.HIDE,
53 this.onPopupClosed_, false, this);
54
55 /**
56 * The UI element used for a specific action.
57 * @type {goog.Disposable}
58 */
59 this.actionObject_ = null;
60
61 armadillo.Actor.actors_.push(this);
62 }
63 goog.inherits(armadillo.Actor, goog.ui.Container);
64
65 /**
66 * An array of all the Actors that have been created.
67 */
68 armadillo.Actor.actors_ = new Array();
69
70 /**
71 * The different options that the Actor can perform.
72 */
73 armadillo.Actor.options_ = {
74 OPEN : 'open',
75 MOVE : 'move',
76 RENAME : 'rename',
77 DELETE : 'delete'
78 };
79
80 /**
81 * String values for the options.
82 */
83 armadillo.Actor.optionStrings_ = {
84 'open' : 'Open',
85 'move' : 'Move',
86 'rename' : 'Rename',
87 'delete' : 'Delete'
88 };
89
90 /**
91 * A global property that should be checked to see if an actor is present,
92 * creating a modal session.
93 */
94 armadillo.Actor.isModal = function() {
95 var isVisible = false;
96 goog.array.forEach(armadillo.Actor.actors_, function (e) {
97 isVisible |= e.popup_.isVisible();
98 });
99 return isVisible;
100 };
101
102 /**
103 * Disposer
104 * @protected
105 */
106 armadillo.Actor.prototype.disposeInternal = function() {
107 armadillo.Actor.superClass_.disposeInternal.call(this);
108
109 this.eh_.dispose();
110
111 // Remove the actor display element.
112 goog.dom.removeNode(this.element_);
113 this.element_ = null;
114
115 // Kill the popup.
116 this.popup_.dispose();
117 this.popup_ = null;
118
119 if (this.actionObject_) {
120 this.actionObject_.dispose();
121 this.actionObject_ = null;
122 }
123
124 // Remove the actor from the list.
125 goog.array.remove(armadillo.Actor.actors_, this);
126
127 this.file_ = null;
128 };
129
130 /**
131 * Shows the popup.
132 * @param {int} x The X position to show at
133 * @param {int} y The Y position to show at
134 */
135 armadillo.Actor.prototype.show = function(x, y) {
136 if (armadillo.Actor.isModal())
137 return;
138 this.popup_.setPinnedCorner(goog.positioning.Corner.TOP_LEFT);
139 this.popup_.setPosition(new goog.positioning.ClientPosition(x, y));
140 this.popup_.setHideOnEscape(true);
141 this.render();
142 this.popup_.setVisible(true);
143 };
144
145 /**
146 * Hides the popup.
147 */
148 armadillo.Actor.prototype.hide = function() {
149 this.popup_.setVisible(false);
150 };
151
152
153 /**
154 * Creates a new path control object.
155 */
156 armadillo.Actor.prototype.createDom = function() {
157 this.decorateInternal(this.dom_.createDom('div', 'actor'));
158 this.popup_.setElement(this.element_);
159 };
160
161 /**
162 * @inheritDoc
163 */
164 armadillo.Actor.prototype.canDecorate = function() {
165 return false;
166 };
167
168 /**
169 * Decorates the given element into a path control.
170 * @param {Element} element
171 */
172 armadillo.Actor.prototype.decorateInternal = function(element) {
173 this.element_ = element;
174 this.dom_.removeChildren(this.element_);
175 for (var option in armadillo.Actor.options_) {
176 var tile = this.createTile_(option);
177 if (tile) {
178 this.addChild(tile, true);
179 }
180 }
181 };
182
183 /**
184 * Creates the DOM Element that is inserted into the popup.
185 * @param {armadillo.Actor.options_} Key of the option to create
186 * @returns {goog.ui.Control}
187 */
188 armadillo.Actor.prototype.createTile_ = function(option) {
189 var value = armadillo.Actor.options_[option];
190
191 // Create the title element.
192 var title = this.dom_.createDom('span', 'title',
193 armadillo.Actor.optionStrings_[value]);
194
195 var tile = new goog.ui.Control(title, new armadillo.Actor.TileControlRenderer_());
196 tile.actorOption = value;
197
198 // Cannot open non-directory files.
199 if (value == armadillo.Actor.options_.OPEN && !this.file_.isDirectory()) {
200 return null;
201 }
202
203 this.eh_.listen(tile, goog.ui.Component.EventType.ACTION,
204 this.tileClickHandler_, false, this);
205 return tile;
206 };
207
208 /**
209 * Click handler for individual tiles.
210 * @param {Event} e
211 */
212 armadillo.Actor.prototype.tileClickHandler_ = function(e) {
213 var option = e.target.actorOption;
214 if (option == armadillo.Actor.options_.OPEN) {
215 // TODO: assert that this.file_.isDirectory().
216 app.navigate(this.file_.getName());
217 } else if (option == armadillo.Actor.options_.MOVE ||
218 option == armadillo.Actor.options_.RENAME) {
219 this.performMove_();
220 } else if (option == armadillo.Actor.options_.DELETE) {
221 this.performDelete_();
222 }
223 this.hide();
224 };
225
226 /**
227 * Subroutine to handle bringing up the move confirmation UI.
228 * @private
229 */
230 armadillo.Actor.prototype.performMove_ = function() {
231 this.actionObject_ = this.createActionDialog_();
232 this.actionObject_.setTitle('Move File');
233
234 var editor = new armadillo.PathControl(this.file_.getFullPath(), true);
235 this.actionObject_.addChild(editor, true);
236
237 var closeCallback = function(e) {
238 if (e.key != goog.ui.Dialog.DefaultButtonKeys.CANCEL) {
239 var newPath = editor.getPath();
240 this.file_.move(newPath);
241 }
242 this.dispose();
243 };
244 // Will be removed when the event source closes.
245 this.eh_.listen(this.actionObject_, goog.ui.Dialog.SELECT_EVENT,
246 closeCallback, false, this);
247
248 this.actionObject_.setVisible(true);
249 var position = goog.style.getPosition(this.actionObject_.getElement());
250 goog.style.setPosition(this.actionObject_.getElement(), position.x, '10%');
251 };
252
253 /**
254 * Subroutine to handle bringing up the delete confirmation UI.
255 * @private
256 */
257 armadillo.Actor.prototype.performDelete_ = function() {
258 this.actionObject_ = this.createActionDialog_();
259 this.actionObject_.setTitle('Confirm Delete');
260
261 var container = this.actionObject_.getContentElement();
262 var content = goog.dom.createDom('span', null,
263 'Are you sure you want to delete:',
264 goog.dom.createElement('br'),
265 goog.dom.createDom('strong', null, this.file_.getName()));
266 goog.dom.appendChild(container, content);
267
268 var closeCallback = function(e) {
269 if (e.key != goog.ui.Dialog.DefaultButtonKeys.CANCEL) {
270 this.file_.remove();
271 }
272 this.dispose();
273 };
274 // Will be removed when the event source closes.
275 this.eh_.listen(this.actionObject_, goog.ui.Dialog.SELECT_EVENT,
276 closeCallback, false, this);
277
278 this.actionObject_.setVisible(true);
279 };
280
281 /**
282 * Creates a new instance of a Dialog that has some basic properties set that
283 * are common to performing actions.
284 * @private
285 */
286 armadillo.Actor.prototype.createActionDialog_ = function() {
287 var confirm = new goog.ui.Dialog();
288 confirm.setDisposeOnHide(true);
289 confirm.setEscapeToCancel(true);
290 confirm.setModal(true);
291 confirm.setDraggable(false);
292 confirm.setHasTitleCloseButton(false);
293 return confirm;
294 };
295
296 /**
297 * Event handler for when this.popup_ closes.
298 * @param {Event} e
299 */
300 armadillo.Actor.prototype.onPopupClosed_ = function(e) {
301 // If an action is not being performed, then dispose the Actor. Otherwise,
302 // this will get cleaned up after the actionObject_ closes.
303 if (!this.actionObject_) {
304 this.dispose();
305 }
306 };
307
308 /**
309 * Tile Control Renderer
310 * @constructor
311 */
312 armadillo.Actor.TileControlRenderer_ = function() {
313 goog.ui.ControlRenderer.call(this);
314 };
315 goog.inherits(armadillo.Actor.TileControlRenderer_, goog.ui.ControlRenderer);
316
317 /**
318 * Returns the control's contents wrapped in a DIV, with the renderer's own
319 * CSS class and additional state-specific classes applied to it.
320 * @param {goog.ui.Control} control Control to render.
321 * @return {Element} Root element for the control.
322 */
323 goog.ui.ControlRenderer.prototype.createDom = function(control) {
324 // Create and return DIV wrapping contents.
325 return control.getDomHelper().createDom('div', 'tile', control.getContent());
326 };