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.ModalDialog');
13 goog
.require('armadillo.PathControl');
14 goog
.require('goog.array');
15 goog
.require('goog.dom');
16 goog
.require('goog.events');
17 goog
.require('goog.positioning.ClientPosition');
18 goog
.require('goog.positioning.Corner');
19 goog
.require('goog.style');
20 goog
.require('goog.ui.Dialog');
21 goog
.require('goog.ui.Popup');
24 * The Actor is a popup that displays the various actions that can be performed
26 * @param {armadillo.File} file The file to act on.
29 armadillo
.Actor
= function(file
) {
30 goog
.Disposable
.call(this);
32 this.element_
= this.createElement_();
33 this.popup_
= new goog
.ui
.Popup(this.element_
);
34 armadillo
.Actor
.actors_
.push(this);
36 goog
.inherits(armadillo
.Actor
, goog
.Disposable
);
39 * An array of all the Actors that have been created.
41 armadillo
.Actor
.actors_
= new Array();
44 * The different options that the Actor can perform.
46 armadillo
.Actor
.options_
= {
54 * String values for the options.
56 armadillo
.Actor
.optionStrings_
= {
64 * A global property that should be checked to see if an actor is present,
65 * creating a modal session.
67 armadillo
.Actor
.isModal
= function() {
68 var isVisible
= false;
69 goog
.array
.forEach(armadillo
.Actor
.actors_
, function (e
) {
70 isVisible
|= e
.popup_
.isVisible();
79 armadillo
.Actor
.prototype.disposeInternal
= function() {
80 armadillo
.Actor
.superClass_
.disposeInternal
.call(this);
82 // Unlisten the tiles.
83 var tiles
= goog
.dom
.getElementsByClassName('tile', this.element_
);
84 goog
.array
.forEach(tiles
, function (tile
) {
85 goog
.events
.unlistenByKey(tile
.actorListener
);
88 // Remove the actor display element.
89 goog
.dom
.removeNode(this.element_
);
93 this.popup_
.dispose();
96 // Remove the actor from the list.
97 goog
.array
.remove(armadillo
.Actor
.actors_
, this);
104 * @param {int} x The X position to show at
105 * @param {int} y The Y position to show at
107 armadillo
.Actor
.prototype.show
= function(x
, y
) {
108 if (armadillo
.Actor
.isModal())
110 var firstBodyElement
= goog
.dom
.getFirstElementChild(document
.body
);
111 goog
.dom
.insertSiblingBefore(this.element_
, firstBodyElement
);
112 this.popup_
.setPinnedCorner(goog
.positioning
.Corner
.TOP_LEFT
);
113 this.popup_
.setPosition(new goog
.positioning
.ClientPosition(x
, y
));
114 this.popup_
.setHideOnEscape(true);
115 this.popup_
.setVisible(true);
121 armadillo
.Actor
.prototype.hide
= function() {
122 this.popup_
.setVisible(false);
126 * Creates the DOM Element that is inserted into the popup.
129 armadillo
.Actor
.prototype.createElement_
= function() {
130 var root
= goog
.dom
.createDom('div', 'actor');
131 for (var option
in armadillo
.Actor
.options_
) {
132 var tile
= goog
.dom
.createDom('div', 'tile');
133 var value
= armadillo
.Actor
.options_
[option];
134 // Cannot open non-directory files.
135 if (value
== armadillo
.Actor
.options_
.OPEN
&& !this.file_
.isDirectory()) {
138 var title
= goog
.dom
.createDom('span', 'title',
139 armadillo
.Actor
.optionStrings_
[value]);
140 goog
.dom
.appendChild(tile
, title
);
141 goog
.dom
.appendChild(root
, tile
);
142 tile
.actorOption
= value
;
143 tile
.actorListener
= goog
.events
.listen(tile
, goog
.events
.EventType
.CLICK
,
144 this.tileClickHandler_
, false, this);
150 * Click handler for individual tiles.
153 armadillo
.Actor
.prototype.tileClickHandler_
= function(e
) {
154 var option
= e
.target
.actorOption
;
155 if (option
== armadillo
.Actor
.options_
.OPEN
) {
156 // TODO: assert that this.file_.isDirectory().
157 app
.navigate(this.file_
.getName());
159 } else if (option
== armadillo
.Actor
.options_
.MOVE
) {
161 } else if (option
== armadillo
.Actor
.options_
.DELETE
) {
162 this.performDelete_();
163 } else if (option
== armadillo
.Actor
.options_
.RENAME
) {
164 var dialog
= new armadillo
.ModalDialog();
166 dialog
.setVisible(true);
171 * Subroutine to handle bringing up the move confirmation UI.
174 armadillo
.Actor
.prototype.performMove_
= function() {
175 var dialog
= this.createActionDialog_();
176 dialog
.setTitle('Move File');
178 var editor
= new armadillo
.PathControl(this.file_
.getFullPath(), false);
179 dialog
.addChild(editor
, true);
181 dialog
.setVisible(true);
182 var position
= goog
.style
.getPosition(dialog
.getElement());
183 goog
.style
.setPosition(dialog
.getElement(), position
.x
, '10%');
187 * Subroutine to handle bringing up the delete confirmation UI.
190 armadillo
.Actor
.prototype.performDelete_
= function() {
191 var confirm
= this.createActionDialog_();
192 confirm
.setTitle('Confirm Delete');
194 var container
= confirm
.getContentElement();
195 var content
= goog
.dom
.createDom('span', null,
196 'Are you sure you want to delete:',
197 goog
.dom
.createElement('br'),
198 goog
.dom
.createDom('strong', null, this.file_
.getName()));
199 goog
.dom
.appendChild(container
, content
);
201 var closeCallback
= function(e
) {
202 if (e
.key
!= goog
.ui
.Dialog
.DefaultButtonKeys
.CANCEL
) {
206 // Will be removed when the event source closes.
207 goog
.events
.listen(confirm
, goog
.ui
.Dialog
.SELECT_EVENT
,
208 closeCallback
, false, this);
210 confirm
.setVisible(true);
214 * Creates a new instance of a Dialog that has some basic properties set that
215 * are common to performing actions.
218 armadillo
.Actor
.prototype.createActionDialog_
= function() {
219 var confirm
= new goog
.ui
.Dialog();
220 confirm
.setDisposeOnHide(true);
221 confirm
.setEscapeToCancel(true);
222 confirm
.setModal(true);
223 confirm
.setDraggable(false);
224 confirm
.setHasTitleCloseButton(false);