Make delete confirmation nonmodal. Remove dependencies on goog.ui.Dialog now.
[armadillo.git] / web_frontend / actor.js
1 //
2 // Armadillo File Manager
3 // Copyright (c) 2010, Robert Sesek <http://www.bluestatic.org>
4 //
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.
8 //
9
10 goog.provide('armadillo.Actor');
11 goog.provide('armadillo.Actor.TileControlRenderer_');
12
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
23 /**
24 * The Actor is a popup that displays the various actions that can be performed
25 * on a given File.
26 * @param {armadillo.File} file The file to act on.
27 * @param {goog.dom.DomHelper} opt_domHelper
28 * @constructor
29 */
30 armadillo.Actor = function(file, opt_domHelper) {
31 goog.ui.Container.call(this, null, null, opt_domHelper);
32
33 this.setFocusable(false);
34 this.setFocusableChildrenAllowed(true);
35
36 /**
37 * The file object on which this acts.
38 * @type {armadillo.File}
39 */
40 this.file_ = file;
41
42 /**
43 * Registrar for all the Actor's events.
44 * @type {goog.events.EventHandler}
45 */
46 this.eh_ = new goog.events.EventHandler();
47
48 /**
49 * The UI element used for a specific action.
50 * @type {goog.Disposable}
51 */
52 this.actionObject_ = null;
53
54 /**
55 * Controls for the current action.
56 * @type {goog.ui.Control}
57 */
58 this.controlContainer_ = null;
59 }
60 goog.inherits(armadillo.Actor, goog.ui.Container);
61
62 /**
63 * The different options that the Actor can perform.
64 */
65 armadillo.Actor.options_ = {
66 OPEN : 'open',
67 MOVE : 'move',
68 RENAME : 'rename',
69 DELETE : 'delete',
70 TV_RENAME : 'tv-rename'
71 };
72
73 /**
74 * String values for the options.
75 */
76 armadillo.Actor.optionStrings_ = {
77 'open' : 'Open',
78 'move' : 'Move',
79 'rename' : 'Rename',
80 'delete' : 'Delete',
81 'tv-rename' : 'Rename TV Episode'
82 };
83
84 /**
85 * Disposer
86 * @protected
87 */
88 armadillo.Actor.prototype.disposeInternal = function() {
89 armadillo.Actor.superClass_.disposeInternal.call(this);
90
91 this.eh_.dispose();
92
93 if (this.controlContainer_)
94 this.controlContainer_.dispose();
95 this.controlContainer_ = null;
96
97 // Remove the actor display element.
98 goog.dom.removeNode(this.element_);
99 this.element_ = null;
100
101 if (this.actionObject_) {
102 this.actionObject_.dispose();
103 this.actionObject_ = null;
104 }
105
106 this.file_ = null;
107 };
108
109 armadillo.Actor.prototype.createDom = function() {
110 this.setElementInternal(this.dom_.createDom('div'));
111 this.decorate(this.getElement());
112 };
113
114 /**
115 * Decorates the given element into a path control.
116 * @param {Element} element
117 */
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);
124 if (tile) {
125 this.addChild(tile, true);
126 }
127 }
128 this.controlContainer_ = new goog.ui.Control();
129 this.controlContainer_.setSupportedState(goog.ui.Component.State.FOCUSED, false);
130 this.addChild(this.controlContainer_, true);
131 };
132
133 /**
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}
137 */
138 armadillo.Actor.prototype.createTile_ = function(option) {
139 var value = armadillo.Actor.options_[option];
140
141 // Create the title element.
142 var title = this.dom_.createDom('span', 'title',
143 armadillo.Actor.optionStrings_[value]);
144
145 var tile = new goog.ui.Control(title, new armadillo.Actor.TileControlRenderer_());
146 tile.actorOption = value;
147
148 // Cannot open non-directory files.
149 if (value == armadillo.Actor.options_.OPEN && !this.file_.isDirectory()) {
150 return null;
151 }
152
153 this.eh_.listen(tile, goog.ui.Component.EventType.ACTION,
154 this.tileClickHandler_, false, this);
155 return tile;
156 };
157
158 /**
159 * Click handler for individual tiles.
160 * @param {Event} e
161 */
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) {
171 this.performMove_();
172 } else if (option == armadillo.Actor.options_.DELETE) {
173 this.performDelete_();
174 } else if (option == armadillo.Actor.options_.TV_RENAME) {
175 this.performTVRename_();
176 }
177 };
178
179 /**
180 * Subroutine to handle bringing up the move confirmation UI.
181 * @private
182 */
183 armadillo.Actor.prototype.performMove_ = function() {
184 var editor = new armadillo.PathControl(this.file_.getFullPath(), true);
185 this.controlContainer_.addChild(editor, true);
186
187 var okCallback = function(e) {
188 var newPath = editor.getPath();
189 this.file_.move(newPath);
190 };
191 this.createOkCancel_(goog.bind(okCallback, this), null);
192 };
193
194 /**
195 * Subroutine to handle bringing up the delete confirmation UI.
196 * @private
197 */
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);
204
205 var okCallback = function(e) {
206 this.file_.remove();
207 };
208 this.createOkCancel_(goog.bind(okCallback, this), null);
209 };
210
211 /**
212 * Subroutine that renames a file to it's title based on season and episode.
213 * @private
214 */
215 armadillo.Actor.prototype.performTVRename_ = function() {
216 var renamer = new armadillo.TVRenamer(this.file_);
217 renamer.run();
218 };
219
220 /**
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
225 */
226 armadillo.Actor.prototype.createOkCancel_ = function(okCallback, cancelCallback) {
227 var ok = new goog.ui.Button('OK');
228 if (okCallback)
229 this.eh_.listen(ok, goog.ui.Component.EventType.ACTION, okCallback);
230 var cancel = new goog.ui.Button('Cancel');
231 if (!cancelCallback)
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);
236 };
237
238 /**
239 * The default cancel callback for the above createOkCancel_().
240 * @param {event} e
241 * @private
242 */
243 armadillo.Actor.prototype.defaultCancelCallback_ = function(e) {
244 this.controlContainer_.removeChildren(true);
245 };
246
247 /**
248 * Tile Control Renderer
249 * @constructor
250 */
251 armadillo.Actor.TileControlRenderer_ = function() {
252 goog.ui.ControlRenderer.call(this);
253 };
254 goog.inherits(armadillo.Actor.TileControlRenderer_, goog.ui.ControlRenderer);
255
256 /**
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.
261 */
262 armadillo.Actor.TileControlRenderer_.prototype.createDom = function(control) {
263 // Create and return DIV wrapping contents.
264 return control.getDomHelper().createDom('div', 'tile', control.getContent());
265 };