]>
src.bluestatic.org Git - armadillo.git/blob - web_frontend/actor.js
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);
143 this.file_
.setHighlight(armadillo
.File
.Highlight
.ACTIVE
);
149 armadillo
.Actor
.prototype.hide = function() {
150 this.popup_
.setVisible(false);
155 * Creates a new path control object.
157 armadillo
.Actor
.prototype.createDom = function() {
158 this.decorateInternal(this.dom_
.createDom('div', 'actor'));
159 this.popup_
.setElement(this.element_
);
165 armadillo
.Actor
.prototype.canDecorate = function() {
170 * Decorates the given element into a path control.
171 * @param {Element} element
173 armadillo
.Actor
.prototype.decorateInternal = function(element
) {
174 this.element_
= element
;
175 this.dom_
.removeChildren(this.element_
);
176 for (var option
in armadillo
.Actor
.options_
) {
177 var tile
= this.createTile_(option
);
179 this.addChild(tile
, true);
185 * Creates the DOM Element that is inserted into the popup.
186 * @param {armadillo.Actor.options_} Key of the option to create
187 * @returns {goog.ui.Control}
189 armadillo
.Actor
.prototype.createTile_ = function(option
) {
190 var value
= armadillo
.Actor
.options_
[option
];
192 // Create the title element.
193 var title
= this.dom_
.createDom('span', 'title',
194 armadillo
.Actor
.optionStrings_
[value
]);
196 var tile
= new goog
.ui
.Control(title
, new armadillo
.Actor
.TileControlRenderer_());
197 tile
.actorOption
= value
;
199 // Cannot open non-directory files.
200 if (value
== armadillo
.Actor
.options_
.OPEN
&& !this.file_
.isDirectory()) {
204 this.eh_
.listen(tile
, goog
.ui
.Component
.EventType
.ACTION
,
205 this.tileClickHandler_
, false, this);
210 * Click handler for individual tiles.
213 armadillo
.Actor
.prototype.tileClickHandler_ = function(e
) {
214 var option
= e
.target
.actorOption
;
215 if (option
== armadillo
.Actor
.options_
.OPEN
) {
216 // TODO: assert that this.file_.isDirectory().
217 app
.navigate(this.file_
.getName());
218 } else if (option
== armadillo
.Actor
.options_
.MOVE
||
219 option
== armadillo
.Actor
.options_
.RENAME
) {
221 } else if (option
== armadillo
.Actor
.options_
.DELETE
) {
222 this.performDelete_();
228 * Subroutine to handle bringing up the move confirmation UI.
231 armadillo
.Actor
.prototype.performMove_ = function() {
232 this.actionObject_
= this.createActionDialog_();
233 this.actionObject_
.setTitle('Move File');
235 var editor
= new armadillo
.PathControl(this.file_
.getFullPath(), true);
236 this.actionObject_
.addChild(editor
, true);
238 var closeCallback = function(e
) {
239 if (e
.key
!= goog
.ui
.Dialog
.DefaultButtonKeys
.CANCEL
) {
240 var newPath
= editor
.getPath();
241 this.file_
.move(newPath
);
245 // Will be removed when the event source closes.
246 this.eh_
.listen(this.actionObject_
, goog
.ui
.Dialog
.SELECT_EVENT
,
247 closeCallback
, false, this);
249 this.actionObject_
.setVisible(true);
250 var position
= goog
.style
.getPosition(this.actionObject_
.getElement());
251 goog
.style
.setPosition(this.actionObject_
.getElement(), position
.x
, '10%');
255 * Subroutine to handle bringing up the delete confirmation UI.
258 armadillo
.Actor
.prototype.performDelete_ = function() {
259 this.actionObject_
= this.createActionDialog_();
260 this.actionObject_
.setTitle('Confirm Delete');
262 var container
= this.actionObject_
.getContentElement();
263 var content
= goog
.dom
.createDom('span', null,
264 'Are you sure you want to delete:',
265 goog
.dom
.createElement('br'),
266 goog
.dom
.createDom('strong', null, this.file_
.getName()));
267 goog
.dom
.appendChild(container
, content
);
269 var closeCallback = function(e
) {
270 if (e
.key
!= goog
.ui
.Dialog
.DefaultButtonKeys
.CANCEL
) {
275 // Will be removed when the event source closes.
276 this.eh_
.listen(this.actionObject_
, goog
.ui
.Dialog
.SELECT_EVENT
,
277 closeCallback
, false, this);
279 this.actionObject_
.setVisible(true);
283 * Creates a new instance of a Dialog that has some basic properties set that
284 * are common to performing actions.
287 armadillo
.Actor
.prototype.createActionDialog_ = function() {
288 var confirm
= new goog
.ui
.Dialog();
289 confirm
.setDisposeOnHide(true);
290 confirm
.setEscapeToCancel(true);
291 confirm
.setModal(true);
292 confirm
.setDraggable(false);
293 confirm
.setHasTitleCloseButton(false);
298 * Event handler for when this.popup_ closes.
301 armadillo
.Actor
.prototype.onPopupClosed_ = function(e
) {
302 // If an action is not being performed, then dispose the Actor. Otherwise,
303 // this will get cleaned up after the actionObject_ closes.
304 if (!this.actionObject_
) {
305 this.file_
.setHighlight(armadillo
.File
.Highlight
.SELECTED
);
311 * Tile Control Renderer
314 armadillo
.Actor
.TileControlRenderer_ = function() {
315 goog
.ui
.ControlRenderer
.call(this);
317 goog
.inherits(armadillo
.Actor
.TileControlRenderer_
, goog
.ui
.ControlRenderer
);
320 * Returns the control's contents wrapped in a DIV, with the renderer's own
321 * CSS class and additional state-specific classes applied to it.
322 * @param {goog.ui.Control} control Control to render.
323 * @return {Element} Root element for the control.
325 goog
.ui
.ControlRenderer
.prototype.createDom = function(control
) {
326 // Create and return DIV wrapping contents.
327 return control
.getDomHelper().createDom('div', 'tile', control
.getContent());