Fix 2 compiler errors with training commas.
[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 ');
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');
23
24 /**
25 * The Actor is a popup that displays the various actions that can be performed
26 * on a given File.
27 * @param {armadillo.File} file The file to act on.
28 * @param {goog.dom.DomHelper} opt_domHelper
29 * @constructor
30 */
31 armadillo.Actor = function(file, opt_domHelper) {
32 goog.ui.Container.call(this, null, null, opt_domHelper);
33
34 /**
35 * The file object on which this acts.
36 * @type {armadillo.File}
37 */
38 this.file_ = file;
39
40 /**
41 * Registrar for all the Actor's events.
42 * @type {goog.events.EventHandler}
43 */
44 this.eh_ = new goog.events.EventHandler();
45
46 /**
47 * The UI element used for a specific action.
48 * @type {goog.Disposable}
49 */
50 this.actionObject_ = null;
51 }
52 goog.inherits(armadillo.Actor, goog.ui.Container);
53
54 /**
55 * The different options that the Actor can perform.
56 */
57 armadillo.Actor.options_ = {
58 OPEN : 'open',
59 MOVE : 'move',
60 RENAME : 'rename',
61 DELETE : 'delete',
62 TV_RENAME : 'tv-rename'
63 };
64
65 /**
66 * String values for the options.
67 */
68 armadillo.Actor.optionStrings_ = {
69 'open' : 'Open',
70 'move' : 'Move',
71 'rename' : 'Rename',
72 'delete' : 'Delete',
73 'tv-rename' : 'Rename TV Episode'
74 };
75
76 /**
77 * Disposer
78 * @protected
79 */
80 armadillo.Actor.prototype.disposeInternal = function() {
81 armadillo.Actor.superClass_.disposeInternal.call(this);
82
83 this.eh_.dispose();
84
85 // Remove the actor display element.
86 goog.dom.removeNode(this.element_);
87 this.element_ = null;
88
89 if (this.actionObject_) {
90 this.actionObject_.dispose();
91 this.actionObject_ = null;
92 }
93
94 this.file_ = null;
95 };
96
97 armadillo.Actor.prototype.createDom = function() {
98 this.setElementInternal(this.dom_.createDom('div'));
99 this.decorate(this.getElement());
100 };
101
102 /**
103 * Decorates the given element into a path control.
104 * @param {Element} element
105 */
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);
112 if (tile) {
113 this.addChild(tile, true);
114 }
115 }
116 };
117
118 /**
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}
122 */
123 armadillo.Actor.prototype.createTile_ = function(option) {
124 var value = armadillo.Actor.options_[option];
125
126 // Create the title element.
127 var title = this.dom_.createDom('span', 'title',
128 armadillo.Actor.optionStrings_[value]);
129
130 var tile = new goog.ui.Control(title, new armadillo.Actor.TileControlRenderer_());
131 tile.actorOption = value;
132
133 // Cannot open non-directory files.
134 if (value == armadillo.Actor.options_.OPEN && !this.file_.isDirectory()) {
135 return null;
136 }
137
138 this.eh_.listen(tile, goog.ui.Component.EventType.ACTION,
139 this.tileClickHandler_, false, this);
140 return tile;
141 };
142
143 /**
144 * Click handler for individual tiles.
145 * @param {Event} e
146 */
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) {
154 this.performMove_();
155 } else if (option == armadillo.Actor.options_.DELETE) {
156 this.performDelete_();
157 } else if (option == armadillo.Actor.options_.TV_RENAME) {
158 this.performTVRename_();
159 }
160 };
161
162 /**
163 * Subroutine to handle bringing up the move confirmation UI.
164 * @private
165 */
166 armadillo.Actor.prototype.performMove_ = function() {
167 this.actionObject_ = this.createActionDialog_();
168 this.actionObject_.setTitle('Move File');
169
170 var editor = new armadillo.PathControl(this.file_.getFullPath(), true);
171 this.actionObject_.addChild(editor, true);
172
173 var closeCallback = function(e) {
174 if (e.key != goog.ui.Dialog.DefaultButtonKeys.CANCEL) {
175 var newPath = editor.getPath();
176 this.file_.move(newPath);
177 }
178 };
179 // Will be removed when the event source closes.
180 this.eh_.listen(this.actionObject_, goog.ui.Dialog.SELECT_EVENT,
181 closeCallback, false, this);
182
183 this.actionObject_.setVisible(true);
184 var position = goog.style.getPosition(this.actionObject_.getElement());
185 goog.style.setPosition(this.actionObject_.getElement(), position.x, '10%');
186 };
187
188 /**
189 * Subroutine to handle bringing up the delete confirmation UI.
190 * @private
191 */
192 armadillo.Actor.prototype.performDelete_ = function() {
193 this.actionObject_ = this.createActionDialog_();
194 this.actionObject_.setTitle('Confirm Delete');
195
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);
202
203 var closeCallback = function(e) {
204 if (e.key != goog.ui.Dialog.DefaultButtonKeys.CANCEL) {
205 this.file_.remove();
206 }
207 };
208 // Will be removed when the event source closes.
209 this.eh_.listen(this.actionObject_, goog.ui.Dialog.SELECT_EVENT,
210 closeCallback, false, this);
211
212 this.actionObject_.setVisible(true);
213 };
214
215 /**
216 * Subroutine that renames a file to it's title based on season and episode.
217 * @private
218 */
219 armadillo.Actor.prototype.performTVRename_ = function() {
220 var renamer = new armadillo.TVRenamer(this.file_);
221 renamer.run();
222 };
223
224 /**
225 * Creates a new instance of a Dialog that has some basic properties set that
226 * are common to performing actions.
227 * @private
228 */
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);
236 return confirm;
237 };
238
239 /**
240 * Tile Control Renderer
241 * @constructor
242 */
243 armadillo.Actor.TileControlRenderer_ = function() {
244 goog.ui.ControlRenderer.call(this);
245 };
246 goog.inherits(armadillo.Actor.TileControlRenderer_, goog.ui.ControlRenderer);
247
248 /**
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.
253 */
254 armadillo.Actor.TileControlRenderer_.prototype.createDom = function(control) {
255 // Create and return DIV wrapping contents.
256 return control.getDomHelper().createDom('div', 'tile', control.getContent());
257 };