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.Actor');
11 goog
.provide('armadillo.Actor.TileControlRenderer_');
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');
26 * The Actor is a popup that displays the various actions that can be performed
28 * @param {armadillo.File} file The file to act on.
29 * @param {goog.dom.DomHelper} opt_domHelper
32 armadillo
.Actor
= function(file
, opt_domHelper
) {
33 goog
.ui
.Container
.call(this, null, null, opt_domHelper
);
36 * The file object on which this acts.
37 * @type {armadillo.File}
42 * Registrar for all the Actor's events.
43 * @type {goog.events.EventHandler}
45 this.eh_
= new goog
.events
.EventHandler();
48 * The Container that holds the display element.
49 * @type {goog.ui.Popup}
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);
56 * The UI element used for a specific action.
57 * @type {goog.Disposable}
59 this.actionObject_
= null;
61 armadillo
.Actor
.actors_
.push(this);
63 goog
.inherits(armadillo
.Actor
, goog
.ui
.Container
);
66 * An array of all the Actors that have been created.
68 armadillo
.Actor
.actors_
= new Array();
71 * The different options that the Actor can perform.
73 armadillo
.Actor
.options_
= {
81 * String values for the options.
83 armadillo
.Actor
.optionStrings_
= {
91 * A global property that should be checked to see if an actor is present,
92 * creating a modal session.
94 armadillo
.Actor
.isModal
= function() {
95 var isVisible
= false;
96 goog
.array
.forEach(armadillo
.Actor
.actors_
, function (e
) {
97 isVisible
|= e
.popup_
.isVisible();
106 armadillo
.Actor
.prototype.disposeInternal
= function() {
107 armadillo
.Actor
.superClass_
.disposeInternal
.call(this);
111 // Remove the actor display element.
112 goog
.dom
.removeNode(this.element_
);
113 this.element_
= null;
116 this.popup_
.dispose();
119 if (this.actionObject_
) {
120 this.actionObject_
.dispose();
121 this.actionObject_
= null;
124 // Remove the actor from the list.
125 goog
.array
.remove(armadillo
.Actor
.actors_
, this);
132 * @param {int} x The X position to show at
133 * @param {int} y The Y position to show at
135 armadillo
.Actor
.prototype.show
= function(x
, y
) {
136 if (armadillo
.Actor
.isModal())
138 this.popup_
.setPinnedCorner(goog
.positioning
.Corner
.TOP_LEFT
);
139 this.popup_
.setPosition(new goog
.positioning
.ClientPosition(x
, y
));
140 this.popup_
.setHideOnEscape(true);
142 this.popup_
.setVisible(true);
148 armadillo
.Actor
.prototype.hide
= function() {
149 this.popup_
.setVisible(false);
154 * Creates a new path control object.
156 armadillo
.Actor
.prototype.createDom
= function() {
157 this.decorateInternal(this.dom_
.createDom('div', 'actor'));
158 this.popup_
.setElement(this.element_
);
164 armadillo
.Actor
.prototype.canDecorate
= function() {
169 * Decorates the given element into a path control.
170 * @param {Element} element
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
);
178 this.addChild(tile
, true);
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}
188 armadillo
.Actor
.prototype.createTile_
= function(option
) {
189 var value
= armadillo
.Actor
.options_
[option];
191 // Create the title element.
192 var title
= this.dom_
.createDom('span', 'title',
193 armadillo
.Actor
.optionStrings_
[value]);
195 var tile
= new goog
.ui
.Control(title
, new armadillo
.Actor
.TileControlRenderer_());
196 tile
.actorOption
= value
;
198 // Cannot open non-directory files.
199 if (value
== armadillo
.Actor
.options_
.OPEN
&& !this.file_
.isDirectory()) {
203 this.eh_
.listen(tile
, goog
.ui
.Component
.EventType
.ACTION
,
204 this.tileClickHandler_
, false, this);
209 * Click handler for individual tiles.
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
) {
220 } else if (option
== armadillo
.Actor
.options_
.DELETE
) {
221 this.performDelete_();
227 * Subroutine to handle bringing up the move confirmation UI.
230 armadillo
.Actor
.prototype.performMove_
= function() {
231 this.actionObject_
= this.createActionDialog_();
232 this.actionObject_
.setTitle('Move File');
234 var editor
= new armadillo
.PathControl(this.file_
.getFullPath(), true);
235 this.actionObject_
.addChild(editor
, true);
237 var closeCallback
= function(e
) {
238 if (e
.key
!= goog
.ui
.Dialog
.DefaultButtonKeys
.CANCEL
) {
239 var newPath
= editor
.getPath();
240 this.file_
.move(newPath
);
244 // Will be removed when the event source closes.
245 this.eh_
.listen(this.actionObject_
, goog
.ui
.Dialog
.SELECT_EVENT
,
246 closeCallback
, false, this);
248 this.actionObject_
.setVisible(true);
249 var position
= goog
.style
.getPosition(this.actionObject_
.getElement());
250 goog
.style
.setPosition(this.actionObject_
.getElement(), position
.x
, '10%');
254 * Subroutine to handle bringing up the delete confirmation UI.
257 armadillo
.Actor
.prototype.performDelete_
= function() {
258 this.actionObject_
= this.createActionDialog_();
259 this.actionObject_
.setTitle('Confirm Delete');
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
);
268 var closeCallback
= function(e
) {
269 if (e
.key
!= goog
.ui
.Dialog
.DefaultButtonKeys
.CANCEL
) {
274 // Will be removed when the event source closes.
275 this.eh_
.listen(this.actionObject_
, goog
.ui
.Dialog
.SELECT_EVENT
,
276 closeCallback
, false, this);
278 this.actionObject_
.setVisible(true);
282 * Creates a new instance of a Dialog that has some basic properties set that
283 * are common to performing actions.
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);
297 * Event handler for when this.popup_ closes.
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_
) {
309 * Tile Control Renderer
312 armadillo
.Actor
.TileControlRenderer_
= function() {
313 goog
.ui
.ControlRenderer
.call(this);
315 goog
.inherits(armadillo
.Actor
.TileControlRenderer_
, goog
.ui
.ControlRenderer
);
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.
323 goog
.ui
.ControlRenderer
.prototype.createDom
= function(control
) {
324 // Create and return DIV wrapping contents.
325 return control
.getDomHelper().createDom('div', 'tile', control
.getContent());