Start working with the zippy. It opens correctly, but it does not close correcly.
[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 };
155
156 /**
157 * Subroutine to handle bringing up the move confirmation UI.
158 * @private
159 */
160 armadillo.Actor.prototype.performMove_ = function() {
161 this.actionObject_ = this.createActionDialog_();
162 this.actionObject_.setTitle('Move File');
163
164 var editor = new armadillo.PathControl(this.file_.getFullPath(), true);
165 this.actionObject_.addChild(editor, true);
166
167 var closeCallback = function(e) {
168 if (e.key != goog.ui.Dialog.DefaultButtonKeys.CANCEL) {
169 var newPath = editor.getPath();
170 this.file_.move(newPath);
171 }
172 };
173 // Will be removed when the event source closes.
174 this.eh_.listen(this.actionObject_, goog.ui.Dialog.SELECT_EVENT,
175 closeCallback, false, this);
176
177 this.actionObject_.setVisible(true);
178 var position = goog.style.getPosition(this.actionObject_.getElement());
179 goog.style.setPosition(this.actionObject_.getElement(), position.x, '10%');
180 };
181
182 /**
183 * Subroutine to handle bringing up the delete confirmation UI.
184 * @private
185 */
186 armadillo.Actor.prototype.performDelete_ = function() {
187 this.actionObject_ = this.createActionDialog_();
188 this.actionObject_.setTitle('Confirm Delete');
189
190 var container = this.actionObject_.getContentElement();
191 var content = goog.dom.createDom('span', null,
192 'Are you sure you want to delete:',
193 goog.dom.createElement('br'),
194 goog.dom.createDom('strong', null, this.file_.getName()));
195 goog.dom.appendChild(container, content);
196
197 var closeCallback = function(e) {
198 if (e.key != goog.ui.Dialog.DefaultButtonKeys.CANCEL) {
199 this.file_.remove();
200 }
201 };
202 // Will be removed when the event source closes.
203 this.eh_.listen(this.actionObject_, goog.ui.Dialog.SELECT_EVENT,
204 closeCallback, false, this);
205
206 this.actionObject_.setVisible(true);
207 };
208
209 /**
210 * Creates a new instance of a Dialog that has some basic properties set that
211 * are common to performing actions.
212 * @private
213 */
214 armadillo.Actor.prototype.createActionDialog_ = function() {
215 var confirm = new goog.ui.Dialog();
216 confirm.setDisposeOnHide(true);
217 confirm.setEscapeToCancel(true);
218 confirm.setModal(true);
219 confirm.setDraggable(false);
220 confirm.setHasTitleCloseButton(false);
221 return confirm;
222 };
223
224 /**
225 * Tile Control Renderer
226 * @constructor
227 */
228 armadillo.Actor.TileControlRenderer_ = function() {
229 goog.ui.ControlRenderer.call(this);
230 };
231 goog.inherits(armadillo.Actor.TileControlRenderer_, goog.ui.ControlRenderer);
232
233 /**
234 * Returns the control's contents wrapped in a DIV, with the renderer's own
235 * CSS class and additional state-specific classes applied to it.
236 * @param {goog.ui.Control} control Control to render.
237 * @return {Element} Root element for the control.
238 */
239 armadillo.Actor.TileControlRenderer_.prototype.createDom = function(control) {
240 // Create and return DIV wrapping contents.
241 return control.getDomHelper().createDom('div', 'tile', control.getContent());
242 };