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
16 goog
.require('goog.array');
17 goog
.require('goog.dom');
18 goog
.require('goog.events');
19 goog
.require('goog.events.EventHandler');
20 goog
.require('goog.style');
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;
52 goog
.inherits(armadillo
.Actor
, goog
.ui
.Container
);
55 * The different options that the Actor can perform.
57 armadillo
.Actor
.options_
= {
62 TV_RENAME
: 'tv-rename'
66 * String values for the options.
68 armadillo
.Actor
.optionStrings_
= {
73 'tv-rename' : 'Rename TV Episode'
80 armadillo
.Actor
.prototype.disposeInternal
= function() {
81 armadillo
.Actor
.superClass_
.disposeInternal
.call(this);
85 // Remove the actor display element.
86 goog
.dom
.removeNode(this.element_
);
89 if (this.actionObject_
) {
90 this.actionObject_
.dispose();
91 this.actionObject_
= null;
97 armadillo
.Actor
.prototype.createDom
= function() {
98 this.setElementInternal(this.dom_
.createDom('div'));
99 this.decorate(this.getElement());
103 * Decorates the given element into a path control.
104 * @param {Element} element
106 armadillo
.Actor
.prototype.decorateInternal
= function(element
) {
107 this.element_
= element
;
108 goog
.dom
.classes
.add(this.element_
, 'actor');
109 this.dom_
.removeChildren(this.element_
);
110 for (var option
in armadillo
.Actor
.options_
) {
111 var tile
= this.createTile_(option
);
113 this.addChild(tile
, true);
119 * Creates the DOM Element that is inserted into the popup.
120 * @param {armadillo.Actor.options_} Key of the option to create
121 * @returns {goog.ui.Control}
123 armadillo
.Actor
.prototype.createTile_
= function(option
) {
124 var value
= armadillo
.Actor
.options_
[option];
126 // Create the title element.
127 var title
= this.dom_
.createDom('span', 'title',
128 armadillo
.Actor
.optionStrings_
[value]);
130 var tile
= new goog
.ui
.Control(title
, new armadillo
.Actor
.TileControlRenderer_());
131 tile
.actorOption
= value
;
133 // Cannot open non-directory files.
134 if (value
== armadillo
.Actor
.options_
.OPEN
&& !this.file_
.isDirectory()) {
138 this.eh_
.listen(tile
, goog
.ui
.Component
.EventType
.ACTION
,
139 this.tileClickHandler_
, false, this);
144 * Click handler for individual tiles.
147 armadillo
.Actor
.prototype.tileClickHandler_
= function(e
) {
148 var option
= e
.target
.actorOption
;
149 if (option
== armadillo
.Actor
.options_
.OPEN
) {
150 // TODO: assert that this.file_.isDirectory().
151 app
.navigate(this.file_
.getName());
152 } else if (option
== armadillo
.Actor
.options_
.MOVE
||
153 option
== armadillo
.Actor
.options_
.RENAME
) {
155 } else if (option
== armadillo
.Actor
.options_
.DELETE
) {
156 this.performDelete_();
157 } else if (option
== armadillo
.Actor
.options_
.TV_RENAME
) {
158 this.performTVRename_();
163 * Subroutine to handle bringing up the move confirmation UI.
166 armadillo
.Actor
.prototype.performMove_
= function() {
167 this.actionObject_
= this.createActionDialog_();
168 this.actionObject_
.setTitle('Move File');
170 var editor
= new armadillo
.PathControl(this.file_
.getFullPath(), true);
171 this.actionObject_
.addChild(editor
, true);
173 var closeCallback
= function(e
) {
174 if (e
.key
!= goog
.ui
.Dialog
.DefaultButtonKeys
.CANCEL
) {
175 var newPath
= editor
.getPath();
176 this.file_
.move(newPath
);
179 // Will be removed when the event source closes.
180 this.eh_
.listen(this.actionObject_
, goog
.ui
.Dialog
.SELECT_EVENT
,
181 closeCallback
, false, this);
183 this.actionObject_
.setVisible(true);
184 var position
= goog
.style
.getPosition(this.actionObject_
.getElement());
185 goog
.style
.setPosition(this.actionObject_
.getElement(), position
.x
, '10%');
189 * Subroutine to handle bringing up the delete confirmation UI.
192 armadillo
.Actor
.prototype.performDelete_
= function() {
193 this.actionObject_
= this.createActionDialog_();
194 this.actionObject_
.setTitle('Confirm Delete');
196 var container
= this.actionObject_
.getContentElement();
197 var content
= goog
.dom
.createDom('span', null,
198 'Are you sure you want to delete:',
199 goog
.dom
.createElement('br'),
200 goog
.dom
.createDom('strong', null, this.file_
.getName()));
201 goog
.dom
.appendChild(container
, content
);
203 var closeCallback
= function(e
) {
204 if (e
.key
!= goog
.ui
.Dialog
.DefaultButtonKeys
.CANCEL
) {
208 // Will be removed when the event source closes.
209 this.eh_
.listen(this.actionObject_
, goog
.ui
.Dialog
.SELECT_EVENT
,
210 closeCallback
, false, this);
212 this.actionObject_
.setVisible(true);
216 * Subroutine that renames a file to it's title based on season and episode.
219 armadillo
.Actor
.prototype.performTVRename_
= function() {
220 var renamer
= new armadillo
.TVRenamer(this.file_
);
225 * Creates a new instance of a Dialog that has some basic properties set that
226 * are common to performing actions.
229 armadillo
.Actor
.prototype.createActionDialog_
= function() {
230 var confirm
= new goog
.ui
.Dialog();
231 confirm
.setDisposeOnHide(true);
232 confirm
.setEscapeToCancel(true);
233 confirm
.setModal(true);
234 confirm
.setDraggable(false);
235 confirm
.setHasTitleCloseButton(false);
240 * Tile Control Renderer
243 armadillo
.Actor
.TileControlRenderer_
= function() {
244 goog
.ui
.ControlRenderer
.call(this);
246 goog
.inherits(armadillo
.Actor
.TileControlRenderer_
, goog
.ui
.ControlRenderer
);
249 * Returns the control's contents wrapped in a DIV, with the renderer's own
250 * CSS class and additional state-specific classes applied to it.
251 * @param {goog.ui.Control} control Control to render.
252 * @return {Element} Root element for the control.
254 armadillo
.Actor
.TileControlRenderer_
.prototype.createDom
= function(control
) {
255 // Create and return DIV wrapping contents.
256 return control
.getDomHelper().createDom('div', 'tile', control
.getContent());