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.style');
19 goog
.require('goog.ui.Container');
20 goog
.require('goog.ui.Dialog');
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.
26 * @param {goog.dom.DomHelper} opt_domHelper
29 armadillo
.Actor
= function(file
, opt_domHelper
) {
30 goog
.ui
.Container
.call(this, null, null, opt_domHelper
);
33 * The file object on which this acts.
34 * @type {armadillo.File}
39 * Registrar for all the Actor's events.
40 * @type {goog.events.EventHandler}
42 this.eh_
= new goog
.events
.EventHandler();
45 * The UI element used for a specific action.
46 * @type {goog.Disposable}
48 this.actionObject_
= null;
50 goog
.inherits(armadillo
.Actor
, goog
.ui
.Container
);
53 * The different options that the Actor can perform.
55 armadillo
.Actor
.options_
= {
63 * String values for the options.
65 armadillo
.Actor
.optionStrings_
= {
76 armadillo
.Actor
.prototype.disposeInternal
= function() {
77 armadillo
.Actor
.superClass_
.disposeInternal
.call(this);
81 // Remove the actor display element.
82 goog
.dom
.removeNode(this.element_
);
85 if (this.actionObject_
) {
86 this.actionObject_
.dispose();
87 this.actionObject_
= null;
93 armadillo
.Actor
.prototype.createDom
= function() {
94 this.setElementInternal(this.dom_
.createDom('div'));
95 this.decorate(this.getElement());
99 * Decorates the given element into a path control.
100 * @param {Element} element
102 armadillo
.Actor
.prototype.decorateInternal
= function(element
) {
103 this.element_
= element
;
104 goog
.dom
.classes
.add(this.element_
, 'actor');
105 this.dom_
.removeChildren(this.element_
);
106 for (var option
in armadillo
.Actor
.options_
) {
107 var tile
= this.createTile_(option
);
109 this.addChild(tile
, true);
115 * Creates the DOM Element that is inserted into the popup.
116 * @param {armadillo.Actor.options_} Key of the option to create
117 * @returns {goog.ui.Control}
119 armadillo
.Actor
.prototype.createTile_
= function(option
) {
120 var value
= armadillo
.Actor
.options_
[option];
122 // Create the title element.
123 var title
= this.dom_
.createDom('span', 'title',
124 armadillo
.Actor
.optionStrings_
[value]);
126 var tile
= new goog
.ui
.Control(title
, new armadillo
.Actor
.TileControlRenderer_());
127 tile
.actorOption
= value
;
129 // Cannot open non-directory files.
130 if (value
== armadillo
.Actor
.options_
.OPEN
&& !this.file_
.isDirectory()) {
134 this.eh_
.listen(tile
, goog
.ui
.Component
.EventType
.ACTION
,
135 this.tileClickHandler_
, false, this);
140 * Click handler for individual tiles.
143 armadillo
.Actor
.prototype.tileClickHandler_
= function(e
) {
144 var option
= e
.target
.actorOption
;
145 if (option
== armadillo
.Actor
.options_
.OPEN
) {
146 // TODO: assert that this.file_.isDirectory().
147 app
.navigate(this.file_
.getName());
148 } else if (option
== armadillo
.Actor
.options_
.MOVE
||
149 option
== armadillo
.Actor
.options_
.RENAME
) {
151 } else if (option
== armadillo
.Actor
.options_
.DELETE
) {
152 this.performDelete_();
158 * Subroutine to handle bringing up the move confirmation UI.
161 armadillo
.Actor
.prototype.performMove_
= function() {
162 this.actionObject_
= this.createActionDialog_();
163 this.actionObject_
.setTitle('Move File');
165 var editor
= new armadillo
.PathControl(this.file_
.getFullPath(), true);
166 this.actionObject_
.addChild(editor
, true);
168 var closeCallback
= function(e
) {
169 if (e
.key
!= goog
.ui
.Dialog
.DefaultButtonKeys
.CANCEL
) {
170 var newPath
= editor
.getPath();
171 this.file_
.move(newPath
);
175 // Will be removed when the event source closes.
176 this.eh_
.listen(this.actionObject_
, goog
.ui
.Dialog
.SELECT_EVENT
,
177 closeCallback
, false, this);
179 this.actionObject_
.setVisible(true);
180 var position
= goog
.style
.getPosition(this.actionObject_
.getElement());
181 goog
.style
.setPosition(this.actionObject_
.getElement(), position
.x
, '10%');
185 * Subroutine to handle bringing up the delete confirmation UI.
188 armadillo
.Actor
.prototype.performDelete_
= function() {
189 this.actionObject_
= this.createActionDialog_();
190 this.actionObject_
.setTitle('Confirm Delete');
192 var container
= this.actionObject_
.getContentElement();
193 var content
= goog
.dom
.createDom('span', null,
194 'Are you sure you want to delete:',
195 goog
.dom
.createElement('br'),
196 goog
.dom
.createDom('strong', null, this.file_
.getName()));
197 goog
.dom
.appendChild(container
, content
);
199 var closeCallback
= function(e
) {
200 if (e
.key
!= goog
.ui
.Dialog
.DefaultButtonKeys
.CANCEL
) {
205 // Will be removed when the event source closes.
206 this.eh_
.listen(this.actionObject_
, goog
.ui
.Dialog
.SELECT_EVENT
,
207 closeCallback
, false, this);
209 this.actionObject_
.setVisible(true);
213 * Creates a new instance of a Dialog that has some basic properties set that
214 * are common to performing actions.
217 armadillo
.Actor
.prototype.createActionDialog_
= function() {
218 var confirm
= new goog
.ui
.Dialog();
219 confirm
.setDisposeOnHide(true);
220 confirm
.setEscapeToCancel(true);
221 confirm
.setModal(true);
222 confirm
.setDraggable(false);
223 confirm
.setHasTitleCloseButton(false);
228 * Tile Control Renderer
231 armadillo
.Actor
.TileControlRenderer_
= function() {
232 goog
.ui
.ControlRenderer
.call(this);
234 goog
.inherits(armadillo
.Actor
.TileControlRenderer_
, goog
.ui
.ControlRenderer
);
237 * Returns the control's contents wrapped in a DIV, with the renderer's own
238 * CSS class and additional state-specific classes applied to it.
239 * @param {goog.ui.Control} control Control to render.
240 * @return {Element} Root element for the control.
242 armadillo
.Actor
.TileControlRenderer_
.prototype.createDom
= function(control
) {
243 // Create and return DIV wrapping contents.
244 return control
.getDomHelper().createDom('div', 'tile', control
.getContent());