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');
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.
27 * @param {goog.dom.DomHelper} opt_domHelper
30 armadillo
.Actor
= function(file
, opt_domHelper
) {
31 goog
.ui
.Container
.call(this, null, null, opt_domHelper
);
33 this.setFocusable(false);
34 this.setFocusableChildrenAllowed(true);
37 * The file object on which this acts.
38 * @type {armadillo.File}
43 * Registrar for all the Actor's events.
44 * @type {goog.events.EventHandler}
46 this.eh_
= new goog
.events
.EventHandler();
49 * The UI element used for a specific action.
50 * @type {goog.Disposable}
52 this.actionObject_
= null;
55 * Controls for the current action.
56 * @type {goog.ui.Control}
58 this.controlContainer_
= null;
60 goog
.inherits(armadillo
.Actor
, goog
.ui
.Container
);
63 * The different options that the Actor can perform.
65 armadillo
.Actor
.options_
= {
70 TV_RENAME
: 'tv-rename'
74 * String values for the options.
76 armadillo
.Actor
.optionStrings_
= {
81 'tv-rename' : 'Rename TV Episode'
88 armadillo
.Actor
.prototype.disposeInternal
= function() {
89 armadillo
.Actor
.superClass_
.disposeInternal
.call(this);
93 if (this.controlContainer_
)
94 this.controlContainer_
.dispose();
95 this.controlContainer_
= null;
97 // Remove the actor display element.
98 goog
.dom
.removeNode(this.element_
);
101 if (this.actionObject_
) {
102 this.actionObject_
.dispose();
103 this.actionObject_
= null;
109 armadillo
.Actor
.prototype.createDom
= function() {
110 this.setElementInternal(this.dom_
.createDom('div'));
111 this.decorate(this.getElement());
115 * Decorates the given element into a path control.
116 * @param {Element} element
118 armadillo
.Actor
.prototype.decorateInternal
= function(element
) {
119 this.element_
= element
;
120 goog
.dom
.classes
.add(this.element_
, 'actor');
121 this.dom_
.removeChildren(this.element_
);
122 for (var option
in armadillo
.Actor
.options_
) {
123 var tile
= this.createTile_(option
);
125 this.addChild(tile
, true);
128 this.controlContainer_
= new goog
.ui
.Control();
129 this.controlContainer_
.setSupportedState(goog
.ui
.Component
.State
.FOCUSED
, false);
130 this.addChild(this.controlContainer_
, true);
134 * Creates the DOM Element that is inserted into the popup.
135 * @param {armadillo.Actor.options_} Key of the option to create
136 * @returns {goog.ui.Control}
138 armadillo
.Actor
.prototype.createTile_
= function(option
) {
139 var value
= armadillo
.Actor
.options_
[option];
141 // Create the title element.
142 var title
= this.dom_
.createDom('span', 'title',
143 armadillo
.Actor
.optionStrings_
[value]);
145 var tile
= new goog
.ui
.Control(title
, new armadillo
.Actor
.TileControlRenderer_());
146 tile
.actorOption
= value
;
148 // Cannot open non-directory files.
149 if (value
== armadillo
.Actor
.options_
.OPEN
&& !this.file_
.isDirectory()) {
153 this.eh_
.listen(tile
, goog
.ui
.Component
.EventType
.ACTION
,
154 this.tileClickHandler_
, false, this);
159 * Click handler for individual tiles.
162 armadillo
.Actor
.prototype.tileClickHandler_
= function(e
) {
163 var option
= e
.target
.actorOption
;
164 this.controlContainer_
.removeChildren(true);
165 this.controlContainer_
.setVisible(true);
166 if (option
== armadillo
.Actor
.options_
.OPEN
) {
167 // TODO: assert that this.file_.isDirectory().
168 app
.navigate(this.file_
.getName());
169 } else if (option
== armadillo
.Actor
.options_
.MOVE
||
170 option
== armadillo
.Actor
.options_
.RENAME
) {
172 } else if (option
== armadillo
.Actor
.options_
.DELETE
) {
173 this.performDelete_();
174 } else if (option
== armadillo
.Actor
.options_
.TV_RENAME
) {
175 this.performTVRename_();
180 * Subroutine to handle bringing up the move confirmation UI.
183 armadillo
.Actor
.prototype.performMove_
= function() {
184 var editor
= new armadillo
.PathControl(this.file_
.getFullPath(), true);
185 this.controlContainer_
.addChild(editor
, true);
187 var okCallback
= function(e
) {
188 var newPath
= editor
.getPath();
189 this.file_
.move(newPath
);
191 this.createOkCancel_(goog
.bind(okCallback
, this), null);
195 * Subroutine to handle bringing up the delete confirmation UI.
198 armadillo
.Actor
.prototype.performDelete_
= function() {
199 var content
= goog
.dom
.createDom('span', null,
200 'Are you sure you want to delete:',
201 goog
.dom
.createElement('br'),
202 goog
.dom
.createDom('strong', null, this.file_
.getName()));
203 this.controlContainer_
.addChild(new goog
.ui
.Control(content
), true);
205 var okCallback
= function(e
) {
208 this.createOkCancel_(goog
.bind(okCallback
, this), null);
212 * Subroutine that renames a file to it's title based on season and episode.
215 armadillo
.Actor
.prototype.performTVRename_
= function() {
216 var renamer
= new armadillo
.TVRenamer(this.file_
);
221 * Creates two buttons: one for OK one for Cancel and attahes them to the
222 * |controlContainer_|.
223 * @param {function(Event)?} okCallback
224 * @param {function(Event)?} cancelCallback
226 armadillo
.Actor
.prototype.createOkCancel_
= function(okCallback
, cancelCallback
) {
227 var ok
= new goog
.ui
.Button('OK');
229 this.eh_
.listen(ok
, goog
.ui
.Component
.EventType
.ACTION
, okCallback
);
230 var cancel
= new goog
.ui
.Button('Cancel');
232 cancelCallback
= goog
.bind(this.defaultCancelCallback_
, this);
233 this.eh_
.listen(cancel
, goog
.ui
.Component
.EventType
.ACTION
, cancelCallback
);
234 this.controlContainer_
.addChild(ok
, true);
235 this.controlContainer_
.addChild(cancel
, true);
239 * The default cancel callback for the above createOkCancel_().
243 armadillo
.Actor
.prototype.defaultCancelCallback_
= function(e
) {
244 this.controlContainer_
.removeChildren(true);
248 * Tile Control Renderer
251 armadillo
.Actor
.TileControlRenderer_
= function() {
252 goog
.ui
.ControlRenderer
.call(this);
254 goog
.inherits(armadillo
.Actor
.TileControlRenderer_
, goog
.ui
.ControlRenderer
);
257 * Returns the control's contents wrapped in a DIV, with the renderer's own
258 * CSS class and additional state-specific classes applied to it.
259 * @param {goog.ui.Control} control Control to render.
260 * @return {Element} Root element for the control.
262 armadillo
.Actor
.TileControlRenderer_
.prototype.createDom
= function(control
) {
263 // Create and return DIV wrapping contents.
264 return control
.getDomHelper().createDom('div', 'tile', control
.getContent());