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');
12 goog
.require('armadillo.PathControl');
13 goog
.require('goog.array');
14 goog
.require('goog.dom');
15 goog
.require('goog.events');
16 goog
.require('goog.positioning.ClientPosition');
17 goog
.require('goog.positioning.Corner');
18 goog
.require('goog.style');
19 goog
.require('goog.ui.Dialog');
20 goog
.require('goog.ui.Popup');
23 * The Actor is a popup that displays the various actions that can be performed
25 * @param {armadillo.File} file The file to act on.
28 armadillo
.Actor
= function(file
) {
29 goog
.Disposable
.call(this);
32 * The file object on which this acts.
33 * @type {armadillo.File}
38 * The HTML element that draws the actor buttons
41 this.element_
= this.createElement_();
44 * The Container that holds the display element.
45 * @type {goog.ui.Popup}
47 this.popup_
= new goog
.ui
.Popup(this.element_
);
50 * The UI element used for a specific action.
51 * @type {goog.Disposable}
53 this.actionObject_
= null;
55 armadillo
.Actor
.actors_
.push(this);
57 goog
.inherits(armadillo
.Actor
, goog
.Disposable
);
60 * An array of all the Actors that have been created.
62 armadillo
.Actor
.actors_
= new Array();
65 * The different options that the Actor can perform.
67 armadillo
.Actor
.options_
= {
75 * String values for the options.
77 armadillo
.Actor
.optionStrings_
= {
85 * A global property that should be checked to see if an actor is present,
86 * creating a modal session.
88 armadillo
.Actor
.isModal
= function() {
89 var isVisible
= false;
90 goog
.array
.forEach(armadillo
.Actor
.actors_
, function (e
) {
91 isVisible
|= e
.popup_
.isVisible();
100 armadillo
.Actor
.prototype.disposeInternal
= function() {
101 armadillo
.Actor
.superClass_
.disposeInternal
.call(this);
103 // Unlisten the tiles.
104 var tiles
= goog
.dom
.getElementsByClassName('tile', this.element_
);
105 goog
.array
.forEach(tiles
, function (tile
) {
106 goog
.events
.unlistenByKey(tile
.actorListener
);
109 // Remove the actor display element.
110 goog
.dom
.removeNode(this.element_
);
111 this.element_
= null;
114 this.popup_
.dispose();
117 if (this.actionObject_
) {
118 this.actionObject_
.dispose();
119 this.actionObject_
= null;
122 // Remove the actor from the list.
123 goog
.array
.remove(armadillo
.Actor
.actors_
, this);
130 * @param {int} x The X position to show at
131 * @param {int} y The Y position to show at
133 armadillo
.Actor
.prototype.show
= function(x
, y
) {
134 if (armadillo
.Actor
.isModal())
136 var firstBodyElement
= goog
.dom
.getFirstElementChild(document
.body
);
137 goog
.dom
.insertSiblingBefore(this.element_
, firstBodyElement
);
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.popup_
.setVisible(true);
147 armadillo
.Actor
.prototype.hide
= function() {
148 this.popup_
.setVisible(false);
152 * Creates the DOM Element that is inserted into the popup.
155 armadillo
.Actor
.prototype.createElement_
= function() {
156 var root
= goog
.dom
.createDom('div', 'actor');
157 for (var option
in armadillo
.Actor
.options_
) {
158 var tile
= goog
.dom
.createDom('div', 'tile');
159 var value
= armadillo
.Actor
.options_
[option];
160 // Cannot open non-directory files.
161 if (value
== armadillo
.Actor
.options_
.OPEN
&& !this.file_
.isDirectory()) {
164 var title
= goog
.dom
.createDom('span', 'title',
165 armadillo
.Actor
.optionStrings_
[value]);
166 goog
.dom
.appendChild(tile
, title
);
167 goog
.dom
.appendChild(root
, tile
);
168 tile
.actorOption
= value
;
169 tile
.actorListener
= goog
.events
.listen(tile
, goog
.events
.EventType
.CLICK
,
170 this.tileClickHandler_
, false, this);
176 * Click handler for individual tiles.
179 armadillo
.Actor
.prototype.tileClickHandler_
= function(e
) {
180 var option
= e
.target
.actorOption
;
181 if (option
== armadillo
.Actor
.options_
.OPEN
) {
182 // TODO: assert that this.file_.isDirectory().
183 app
.navigate(this.file_
.getName());
184 } else if (option
== armadillo
.Actor
.options_
.MOVE
||
185 option
== armadillo
.Actor
.options_
.RENAME
) {
187 } else if (option
== armadillo
.Actor
.options_
.DELETE
) {
188 this.performDelete_();
194 * Subroutine to handle bringing up the move confirmation UI.
197 armadillo
.Actor
.prototype.performMove_
= function() {
198 this.actionObject_
= this.createActionDialog_();
199 this.actionObject_
.setTitle('Move File');
201 var editor
= new armadillo
.PathControl(this.file_
.getFullPath(), true);
202 this.actionObject_
.addChild(editor
, true);
204 var closeCallback
= function(e
) {
205 if (e
.key
!= goog
.ui
.Dialog
.DefaultButtonKeys
.CANCEL
) {
206 var newPath
= editor
.getPath();
207 this.file_
.move(newPath
);
211 // Will be removed when the event source closes.
212 goog
.events
.listen(this.actionObject_
, goog
.ui
.Dialog
.SELECT_EVENT
,
213 closeCallback
, false, this);
215 this.actionObject_
.setVisible(true);
216 var position
= goog
.style
.getPosition(this.actionObject_
.getElement());
217 goog
.style
.setPosition(this.actionObject_
.getElement(), position
.x
, '10%');
221 * Subroutine to handle bringing up the delete confirmation UI.
224 armadillo
.Actor
.prototype.performDelete_
= function() {
225 this.actionObject_
= this.createActionDialog_();
226 confirm
.setTitle('Confirm Delete');
228 var container
= this.actionObject_
.getContentElement();
229 var content
= goog
.dom
.createDom('span', null,
230 'Are you sure you want to delete:',
231 goog
.dom
.createElement('br'),
232 goog
.dom
.createDom('strong', null, this.file_
.getName()));
233 goog
.dom
.appendChild(container
, content
);
235 var closeCallback
= function(e
) {
236 if (e
.key
!= goog
.ui
.Dialog
.DefaultButtonKeys
.CANCEL
) {
241 // Will be removed when the event source closes.
242 goog
.events
.listen(this.actionObject_
, goog
.ui
.Dialog
.SELECT_EVENT
,
243 closeCallback
, false, this);
245 this.actionObject_
.setVisible(true);
249 * Creates a new instance of a Dialog that has some basic properties set that
250 * are common to performing actions.
253 armadillo
.Actor
.prototype.createActionDialog_
= function() {
254 var confirm
= new goog
.ui
.Dialog();
255 confirm
.setDisposeOnHide(true);
256 confirm
.setEscapeToCancel(true);
257 confirm
.setModal(true);
258 confirm
.setDraggable(false);
259 confirm
.setHasTitleCloseButton(false);