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('armadillo.TVRenamer');
15 goog
.require('goog.array');
16 goog
.require('goog.dom');
17 goog
.require('goog.events');
18 goog
.require('goog.events.EventHandler');
19 goog
.require('goog.style');
20 goog
.require('goog.ui.Button');
21 goog
.require('goog.ui.Container');
22 goog
.require('goog.ui.Dialog');
25 * The Actor is a popup that displays the various actions that can be performed
27 * @param {armadillo.File} file The file to act on.
28 * @param {goog.dom.DomHelper} opt_domHelper
31 armadillo
.Actor
= function(file
, opt_domHelper
) {
32 goog
.ui
.Container
.call(this, null, null, opt_domHelper
);
35 * The file object on which this acts.
36 * @type {armadillo.File}
41 * Registrar for all the Actor's events.
42 * @type {goog.events.EventHandler}
44 this.eh_
= new goog
.events
.EventHandler();
47 * The UI element used for a specific action.
48 * @type {goog.Disposable}
50 this.actionObject_
= null;
53 * Controls for the current action.
54 * @type {goog.ui.Control}
56 this.controlContainer_
= null;
58 goog
.inherits(armadillo
.Actor
, goog
.ui
.Container
);
61 * The different options that the Actor can perform.
63 armadillo
.Actor
.options_
= {
68 TV_RENAME
: 'tv-rename'
72 * String values for the options.
74 armadillo
.Actor
.optionStrings_
= {
79 'tv-rename' : 'Rename TV Episode'
86 armadillo
.Actor
.prototype.disposeInternal
= function() {
87 armadillo
.Actor
.superClass_
.disposeInternal
.call(this);
91 if (this.controlContainer_
)
92 this.controlContainer_
.dispose();
93 this.controlContainer_
= null;
95 // Remove the actor display element.
96 goog
.dom
.removeNode(this.element_
);
99 if (this.actionObject_
) {
100 this.actionObject_
.dispose();
101 this.actionObject_
= null;
107 armadillo
.Actor
.prototype.createDom
= function() {
108 this.setElementInternal(this.dom_
.createDom('div'));
109 this.decorate(this.getElement());
113 * Decorates the given element into a path control.
114 * @param {Element} element
116 armadillo
.Actor
.prototype.decorateInternal
= function(element
) {
117 this.element_
= element
;
118 goog
.dom
.classes
.add(this.element_
, 'actor');
119 this.dom_
.removeChildren(this.element_
);
120 for (var option
in armadillo
.Actor
.options_
) {
121 var tile
= this.createTile_(option
);
123 this.addChild(tile
, true);
126 this.controlContainer_
= new goog
.ui
.Control();
127 this.addChild(this.controlContainer_
, true);
131 * Creates the DOM Element that is inserted into the popup.
132 * @param {armadillo.Actor.options_} Key of the option to create
133 * @returns {goog.ui.Control}
135 armadillo
.Actor
.prototype.createTile_
= function(option
) {
136 var value
= armadillo
.Actor
.options_
[option];
138 // Create the title element.
139 var title
= this.dom_
.createDom('span', 'title',
140 armadillo
.Actor
.optionStrings_
[value]);
142 var tile
= new goog
.ui
.Control(title
, new armadillo
.Actor
.TileControlRenderer_());
143 tile
.actorOption
= value
;
145 // Cannot open non-directory files.
146 if (value
== armadillo
.Actor
.options_
.OPEN
&& !this.file_
.isDirectory()) {
150 this.eh_
.listen(tile
, goog
.ui
.Component
.EventType
.ACTION
,
151 this.tileClickHandler_
, false, this);
156 * Click handler for individual tiles.
159 armadillo
.Actor
.prototype.tileClickHandler_
= function(e
) {
160 var option
= e
.target
.actorOption
;
161 this.controlContainer_
.removeChildren(true);
162 this.controlContainer_
.setVisible(true);
163 if (option
== armadillo
.Actor
.options_
.OPEN
) {
164 // TODO: assert that this.file_.isDirectory().
165 app
.navigate(this.file_
.getName());
166 } else if (option
== armadillo
.Actor
.options_
.MOVE
||
167 option
== armadillo
.Actor
.options_
.RENAME
) {
169 } else if (option
== armadillo
.Actor
.options_
.DELETE
) {
170 this.performDelete_();
171 } else if (option
== armadillo
.Actor
.options_
.TV_RENAME
) {
172 this.performTVRename_();
177 * Subroutine to handle bringing up the move confirmation UI.
180 armadillo
.Actor
.prototype.performMove_
= function() {
181 var editor
= new armadillo
.PathControl(this.file_
.getFullPath(), true);
182 this.controlContainer_
.addChild(editor
, true);
184 var okCallback
= function(e
) {
185 var newPath
= editor
.getPath();
186 this.file_
.move(newPath
);
188 this.createOkCancel_(goog
.bind(okCallback
, this), null);
192 * Subroutine to handle bringing up the delete confirmation UI.
195 armadillo
.Actor
.prototype.performDelete_
= function() {
196 this.actionObject_
= this.createActionDialog_();
197 this.actionObject_
.setTitle('Confirm Delete');
199 var container
= this.actionObject_
.getContentElement();
200 var content
= goog
.dom
.createDom('span', null,
201 'Are you sure you want to delete:',
202 goog
.dom
.createElement('br'),
203 goog
.dom
.createDom('strong', null, this.file_
.getName()));
204 goog
.dom
.appendChild(container
, content
);
206 var closeCallback
= function(e
) {
207 if (e
.key
!= goog
.ui
.Dialog
.DefaultButtonKeys
.CANCEL
) {
211 // Will be removed when the event source closes.
212 this.eh_
.listen(this.actionObject_
, goog
.ui
.Dialog
.SELECT_EVENT
,
213 closeCallback
, false, this);
215 this.actionObject_
.setVisible(true);
219 * Subroutine that renames a file to it's title based on season and episode.
222 armadillo
.Actor
.prototype.performTVRename_
= function() {
223 var renamer
= new armadillo
.TVRenamer(this.file_
);
228 * Creates a new instance of a Dialog that has some basic properties set that
229 * are common to performing actions.
232 armadillo
.Actor
.prototype.createActionDialog_
= function() {
233 var confirm
= new goog
.ui
.Dialog();
234 confirm
.setDisposeOnHide(true);
235 confirm
.setEscapeToCancel(true);
236 confirm
.setModal(true);
237 confirm
.setDraggable(false);
238 confirm
.setHasTitleCloseButton(false);
243 * Creates two buttons: one for OK one for Cancel and attahes them to the
244 * |controlContainer_|.
245 * @param {function(Event)?} okCallback
246 * @param {function(Event)?} cancelCallback
248 armadillo
.Actor
.prototype.createOkCancel_
= function(okCallback
, cancelCallback
) {
249 var ok
= new goog
.ui
.Button('OK');
251 this.eh_
.listen(ok
, goog
.ui
.Component
.EventType
.ACTION
, okCallback
);
252 var cancel
= new goog
.ui
.Button('Cancel');
254 cancelCallback
= goog
.bind(this.defaultCancelCallback_
, this);
255 this.eh_
.listen(cancel
, goog
.ui
.Component
.EventType
.ACTION
, cancelCallback
);
256 this.controlContainer_
.addChild(ok
, true);
257 this.controlContainer_
.addChild(cancel
, true);
261 * The default cancel callback for the above createOkCancel_().
265 armadillo
.Actor
.prototype.defaultCancelCallback_
= function(e
) {
266 this.controlContainer_
.removeChildren(true);
270 * Tile Control Renderer
273 armadillo
.Actor
.TileControlRenderer_
= function() {
274 goog
.ui
.ControlRenderer
.call(this);
276 goog
.inherits(armadillo
.Actor
.TileControlRenderer_
, goog
.ui
.ControlRenderer
);
279 * Returns the control's contents wrapped in a DIV, with the renderer's own
280 * CSS class and additional state-specific classes applied to it.
281 * @param {goog.ui.Control} control Control to render.
282 * @return {Element} Root element for the control.
284 armadillo
.Actor
.TileControlRenderer_
.prototype.createDom
= function(control
) {
285 // Create and return DIV wrapping contents.
286 return control
.getDomHelper().createDom('div', 'tile', control
.getContent());