Display the actor panel without using the zippy.
[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('goog.array');
15 goog.require('goog.dom');
16 goog.require('goog.events');
17 goog.require('goog.events.EventHandler');
18 goog.require('goog.style');
19 goog.require('goog.ui.Container');
20 goog.require('goog.ui.Dialog');
21
22 /**
23 * The Actor is a popup that displays the various actions that can be performed
24 * on a given File.
25 * @param {armadillo.File} file The file to act on.
26 * @param {goog.dom.DomHelper} opt_domHelper
27 * @constructor
28 */
29 armadillo.Actor = function(file, opt_domHelper) {
30 goog.ui.Container.call(this, null, null, opt_domHelper);
31
32 /**
33 * The file object on which this acts.
34 * @type {armadillo.File}
35 */
36 this.file_ = file;
37
38 /**
39 * Registrar for all the Actor's events.
40 * @type {goog.events.EventHandler}
41 */
42 this.eh_ = new goog.events.EventHandler();
43
44 /**
45 * The UI element used for a specific action.
46 * @type {goog.Disposable}
47 */
48 this.actionObject_ = null;
49 }
50 goog.inherits(armadillo.Actor, goog.ui.Container);
51
52 /**
53 * The different options that the Actor can perform.
54 */
55 armadillo.Actor.options_ = {
56 OPEN : 'open',
57 MOVE : 'move',
58 RENAME : 'rename',
59 DELETE : 'delete'
60 };
61
62 /**
63 * String values for the options.
64 */
65 armadillo.Actor.optionStrings_ = {
66 'open' : 'Open',
67 'move' : 'Move',
68 'rename' : 'Rename',
69 'delete' : 'Delete'
70 };
71
72 /**
73 * Disposer
74 * @protected
75 */
76 armadillo.Actor.prototype.disposeInternal = function() {
77 armadillo.Actor.superClass_.disposeInternal.call(this);
78
79 this.eh_.dispose();
80
81 // Remove the actor display element.
82 goog.dom.removeNode(this.element_);
83 this.element_ = null;
84
85 if (this.actionObject_) {
86 this.actionObject_.dispose();
87 this.actionObject_ = null;
88 }
89
90 this.file_ = null;
91 };
92
93 armadillo.Actor.prototype.createDom = function() {
94 this.setElementInternal(this.dom_.createDom('div'));
95 this.decorate(this.getElement());
96 };
97
98 /**
99 * Decorates the given element into a path control.
100 * @param {Element} element
101 */
102 armadillo.Actor.prototype.decorateInternal = function(element) {
103 this.element_ = element;
104 goog.dom.classes.add(this.element_, 'actor');
105 this.dom_.removeChildren(this.element_);
106 for (var option in armadillo.Actor.options_) {
107 var tile = this.createTile_(option);
108 if (tile) {
109 this.addChild(tile, true);
110 }
111 }
112 };
113
114 /**
115 * Creates the DOM Element that is inserted into the popup.
116 * @param {armadillo.Actor.options_} Key of the option to create
117 * @returns {goog.ui.Control}
118 */
119 armadillo.Actor.prototype.createTile_ = function(option) {
120 var value = armadillo.Actor.options_[option];
121
122 // Create the title element.
123 var title = this.dom_.createDom('span', 'title',
124 armadillo.Actor.optionStrings_[value]);
125
126 var tile = new goog.ui.Control(title, new armadillo.Actor.TileControlRenderer_());
127 tile.actorOption = value;
128
129 // Cannot open non-directory files.
130 if (value == armadillo.Actor.options_.OPEN && !this.file_.isDirectory()) {
131 return null;
132 }
133
134 this.eh_.listen(tile, goog.ui.Component.EventType.ACTION,
135 this.tileClickHandler_, false, this);
136 return tile;
137 };
138
139 /**
140 * Click handler for individual tiles.
141 * @param {Event} e
142 */
143 armadillo.Actor.prototype.tileClickHandler_ = function(e) {
144 var option = e.target.actorOption;
145 if (option == armadillo.Actor.options_.OPEN) {
146 // TODO: assert that this.file_.isDirectory().
147 app.navigate(this.file_.getName());
148 } else if (option == armadillo.Actor.options_.MOVE ||
149 option == armadillo.Actor.options_.RENAME) {
150 this.performMove_();
151 } else if (option == armadillo.Actor.options_.DELETE) {
152 this.performDelete_();
153 }
154 this.hide();
155 };
156
157 /**
158 * Subroutine to handle bringing up the move confirmation UI.
159 * @private
160 */
161 armadillo.Actor.prototype.performMove_ = function() {
162 this.actionObject_ = this.createActionDialog_();
163 this.actionObject_.setTitle('Move File');
164
165 var editor = new armadillo.PathControl(this.file_.getFullPath(), true);
166 this.actionObject_.addChild(editor, true);
167
168 var closeCallback = function(e) {
169 if (e.key != goog.ui.Dialog.DefaultButtonKeys.CANCEL) {
170 var newPath = editor.getPath();
171 this.file_.move(newPath);
172 }
173 this.dispose();
174 };
175 // Will be removed when the event source closes.
176 this.eh_.listen(this.actionObject_, goog.ui.Dialog.SELECT_EVENT,
177 closeCallback, false, this);
178
179 this.actionObject_.setVisible(true);
180 var position = goog.style.getPosition(this.actionObject_.getElement());
181 goog.style.setPosition(this.actionObject_.getElement(), position.x, '10%');
182 };
183
184 /**
185 * Subroutine to handle bringing up the delete confirmation UI.
186 * @private
187 */
188 armadillo.Actor.prototype.performDelete_ = function() {
189 this.actionObject_ = this.createActionDialog_();
190 this.actionObject_.setTitle('Confirm Delete');
191
192 var container = this.actionObject_.getContentElement();
193 var content = goog.dom.createDom('span', null,
194 'Are you sure you want to delete:',
195 goog.dom.createElement('br'),
196 goog.dom.createDom('strong', null, this.file_.getName()));
197 goog.dom.appendChild(container, content);
198
199 var closeCallback = function(e) {
200 if (e.key != goog.ui.Dialog.DefaultButtonKeys.CANCEL) {
201 this.file_.remove();
202 }
203 this.dispose();
204 };
205 // Will be removed when the event source closes.
206 this.eh_.listen(this.actionObject_, goog.ui.Dialog.SELECT_EVENT,
207 closeCallback, false, this);
208
209 this.actionObject_.setVisible(true);
210 };
211
212 /**
213 * Creates a new instance of a Dialog that has some basic properties set that
214 * are common to performing actions.
215 * @private
216 */
217 armadillo.Actor.prototype.createActionDialog_ = function() {
218 var confirm = new goog.ui.Dialog();
219 confirm.setDisposeOnHide(true);
220 confirm.setEscapeToCancel(true);
221 confirm.setModal(true);
222 confirm.setDraggable(false);
223 confirm.setHasTitleCloseButton(false);
224 return confirm;
225 };
226
227 /**
228 * Tile Control Renderer
229 * @constructor
230 */
231 armadillo.Actor.TileControlRenderer_ = function() {
232 goog.ui.ControlRenderer.call(this);
233 };
234 goog.inherits(armadillo.Actor.TileControlRenderer_, goog.ui.ControlRenderer);
235
236 /**
237 * Returns the control's contents wrapped in a DIV, with the renderer's own
238 * CSS class and additional state-specific classes applied to it.
239 * @param {goog.ui.Control} control Control to render.
240 * @return {Element} Root element for the control.
241 */
242 armadillo.Actor.TileControlRenderer_.prototype.createDom = function(control) {
243 // Create and return DIV wrapping contents.
244 return control.getDomHelper().createDom('div', 'tile', control.getContent());
245 };