Remove the RENAME action as MOVE does the same thing.
[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 DELETE : 'delete',
69 TV_RENAME : 'tv-rename'
70 };
71
72 /**
73 * String values for the options.
74 */
75 armadillo.Actor.optionStrings_ = {
76 'open' : 'Open',
77 'move' : 'Move',
78 'delete' : 'Delete',
79 'tv-rename' : 'Rename TV Episode'
80 };
81
82 /**
83 * Disposer
84 * @protected
85 */
86 armadillo.Actor.prototype.disposeInternal = function() {
87 armadillo.Actor.superClass_.disposeInternal.call(this);
88
89 this.eh_.dispose();
90
91 if (this.controlContainer_)
92 this.controlContainer_.dispose();
93 this.controlContainer_ = null;
94
95 // Remove the actor display element.
96 goog.dom.removeNode(this.element_);
97 this.element_ = null;
98
99 if (this.actionObject_) {
100 this.actionObject_.dispose();
101 this.actionObject_ = null;
102 }
103
104 this.file_ = null;
105 };
106
107 armadillo.Actor.prototype.createDom = function() {
108 this.setElementInternal(this.dom_.createDom('div'));
109 this.decorate(this.getElement());
110 };
111
112 /**
113 * Decorates the given element into a path control.
114 * @param {Element} element
115 */
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);
122 if (tile) {
123 this.addChild(tile, true);
124 }
125 }
126 this.controlContainer_ = new goog.ui.Control();
127 this.controlContainer_.setSupportedState(goog.ui.Component.State.FOCUSED, false);
128 this.addChild(this.controlContainer_, true);
129 };
130
131 /**
132 * Creates the DOM Element that is inserted into the popup.
133 * @param {armadillo.Actor.options_} Key of the option to create
134 * @returns {goog.ui.Control}
135 */
136 armadillo.Actor.prototype.createTile_ = function(option) {
137 var value = armadillo.Actor.options_[option];
138
139 // Create the title element.
140 var title = this.dom_.createDom('span', 'title',
141 armadillo.Actor.optionStrings_[value]);
142
143 var tile = new goog.ui.Control(title, new armadillo.Actor.TileControlRenderer_());
144 tile.actorOption = value;
145
146 // Cannot open non-directory files.
147 if (value == armadillo.Actor.options_.OPEN && !this.file_.isDirectory()) {
148 return null;
149 }
150
151 this.eh_.listen(tile, goog.ui.Component.EventType.ACTION,
152 this.tileClickHandler_, false, this);
153 return tile;
154 };
155
156 /**
157 * Click handler for individual tiles.
158 * @param {Event} e
159 */
160 armadillo.Actor.prototype.tileClickHandler_ = function(e) {
161 var option = e.target.actorOption;
162 this.controlContainer_.removeChildren(true);
163 this.controlContainer_.setVisible(true);
164 if (option == armadillo.Actor.options_.OPEN) {
165 // TODO: assert that this.file_.isDirectory().
166 app.navigate(this.file_.getName());
167 } else if (option == armadillo.Actor.options_.MOVE) {
168 this.performMove_();
169 } else if (option == armadillo.Actor.options_.DELETE) {
170 this.performDelete_();
171 } else if (option == armadillo.Actor.options_.TV_RENAME) {
172 this.performTVRename_();
173 }
174 };
175
176 /**
177 * Subroutine to handle bringing up the move confirmation UI.
178 * @private
179 */
180 armadillo.Actor.prototype.performMove_ = function() {
181 var editor = new armadillo.PathControl(this.file_.getFullPath(), true);
182 this.controlContainer_.addChild(editor, true);
183
184 var okCallback = function(e) {
185 var newPath = editor.getPath();
186 this.file_.move(newPath);
187 };
188 this.createOkCancel_(goog.bind(okCallback, this), null);
189 };
190
191 /**
192 * Subroutine to handle bringing up the delete confirmation UI.
193 * @private
194 */
195 armadillo.Actor.prototype.performDelete_ = function() {
196 var content = goog.dom.createDom('span', null,
197 'Are you sure you want to delete:',
198 goog.dom.createElement('br'),
199 goog.dom.createDom('strong', null, this.file_.getName()));
200 this.controlContainer_.addChild(new goog.ui.Control(content), true);
201
202 var okCallback = function(e) {
203 this.file_.remove();
204 };
205 this.createOkCancel_(goog.bind(okCallback, this), null);
206 };
207
208 /**
209 * Subroutine that renames a file to it's title based on season and episode.
210 * @private
211 */
212 armadillo.Actor.prototype.performTVRename_ = function() {
213 var renamer = new armadillo.TVRenamer(this.file_);
214 renamer.run();
215 };
216
217 /**
218 * Creates two buttons: one for OK one for Cancel and attahes them to the
219 * |controlContainer_|.
220 * @param {function(Event)?} okCallback
221 * @param {function(Event)?} cancelCallback
222 */
223 armadillo.Actor.prototype.createOkCancel_ = function(okCallback, cancelCallback) {
224 var ok = new goog.ui.Button('OK');
225 if (okCallback)
226 this.eh_.listen(ok, goog.ui.Component.EventType.ACTION, okCallback);
227 var cancel = new goog.ui.Button('Cancel');
228 if (!cancelCallback)
229 cancelCallback = goog.bind(this.defaultCancelCallback_, this);
230 this.eh_.listen(cancel, goog.ui.Component.EventType.ACTION, cancelCallback);
231 this.controlContainer_.addChild(ok, true);
232 this.controlContainer_.addChild(cancel, true);
233 };
234
235 /**
236 * The default cancel callback for the above createOkCancel_().
237 * @param {event} e
238 * @private
239 */
240 armadillo.Actor.prototype.defaultCancelCallback_ = function(e) {
241 this.controlContainer_.removeChildren(true);
242 };
243
244 /**
245 * Tile Control Renderer
246 * @constructor
247 */
248 armadillo.Actor.TileControlRenderer_ = function() {
249 goog.ui.ControlRenderer.call(this);
250 };
251 goog.inherits(armadillo.Actor.TileControlRenderer_, goog.ui.ControlRenderer);
252
253 /**
254 * Returns the control's contents wrapped in a DIV, with the renderer's own
255 * CSS class and additional state-specific classes applied to it.
256 * @param {goog.ui.Control} control Control to render.
257 * @return {Element} Root element for the control.
258 */
259 armadillo.Actor.TileControlRenderer_.prototype.createDom = function(control) {
260 // Create and return DIV wrapping contents.
261 return control.getDomHelper().createDom('div', 'tile', control.getContent());
262 };