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_();
157 * Subroutine to handle bringing up the move confirmation UI.
160 armadillo
.Actor
.prototype.performMove_
= function() {
161 this.actionObject_
= this.createActionDialog_();
162 this.actionObject_
.setTitle('Move File');
164 var editor
= new armadillo
.PathControl(this.file_
.getFullPath(), true);
165 this.actionObject_
.addChild(editor
, true);
167 var closeCallback
= function(e
) {
168 if (e
.key
!= goog
.ui
.Dialog
.DefaultButtonKeys
.CANCEL
) {
169 var newPath
= editor
.getPath();
170 this.file_
.move(newPath
);
173 // Will be removed when the event source closes.
174 this.eh_
.listen(this.actionObject_
, goog
.ui
.Dialog
.SELECT_EVENT
,
175 closeCallback
, false, this);
177 this.actionObject_
.setVisible(true);
178 var position
= goog
.style
.getPosition(this.actionObject_
.getElement());
179 goog
.style
.setPosition(this.actionObject_
.getElement(), position
.x
, '10%');
183 * Subroutine to handle bringing up the delete confirmation UI.
186 armadillo
.Actor
.prototype.performDelete_
= function() {
187 this.actionObject_
= this.createActionDialog_();
188 this.actionObject_
.setTitle('Confirm Delete');
190 var container
= this.actionObject_
.getContentElement();
191 var content
= goog
.dom
.createDom('span', null,
192 'Are you sure you want to delete:',
193 goog
.dom
.createElement('br'),
194 goog
.dom
.createDom('strong', null, this.file_
.getName()));
195 goog
.dom
.appendChild(container
, content
);
197 var closeCallback
= function(e
) {
198 if (e
.key
!= goog
.ui
.Dialog
.DefaultButtonKeys
.CANCEL
) {
202 // Will be removed when the event source closes.
203 this.eh_
.listen(this.actionObject_
, goog
.ui
.Dialog
.SELECT_EVENT
,
204 closeCallback
, false, this);
206 this.actionObject_
.setVisible(true);
210 * Creates a new instance of a Dialog that has some basic properties set that
211 * are common to performing actions.
214 armadillo
.Actor
.prototype.createActionDialog_
= function() {
215 var confirm
= new goog
.ui
.Dialog();
216 confirm
.setDisposeOnHide(true);
217 confirm
.setEscapeToCancel(true);
218 confirm
.setModal(true);
219 confirm
.setDraggable(false);
220 confirm
.setHasTitleCloseButton(false);
225 * Tile Control Renderer
228 armadillo
.Actor
.TileControlRenderer_
= function() {
229 goog
.ui
.ControlRenderer
.call(this);
231 goog
.inherits(armadillo
.Actor
.TileControlRenderer_
, goog
.ui
.ControlRenderer
);
234 * Returns the control's contents wrapped in a DIV, with the renderer's own
235 * CSS class and additional state-specific classes applied to it.
236 * @param {goog.ui.Control} control Control to render.
237 * @return {Element} Root element for the control.
239 armadillo
.Actor
.TileControlRenderer_
.prototype.createDom
= function(control
) {
240 // Create and return DIV wrapping contents.
241 return control
.getDomHelper().createDom('div', 'tile', control
.getContent());