Remove a few calls do document.createElement
[armadillo.git] / web_frontend / actor.js
1 //
2 // Armadillo File Manager
3 // Copyright (c) 2010-2011, 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 $.namespace('armadillo.Actor');
11
12 /**
13 * The Actor is a popup that displays the various actions that can be performed
14 * on a given File.
15 * @param {armadillo.File} file The file to act on.
16 * @param {goog.dom.DomHelper} opt_domHelper
17 * @constructor
18 */
19 armadillo.Actor = function(file, opt_domHelper) {
20 /**
21 * The file object on which this acts.
22 * @type {armadillo.File}
23 */
24 this.file_ = file;
25
26 /**
27 * The root DOM element for the actor.
28 * @type {Element}
29 */
30 this.element_ = null;
31
32 /**
33 * The UI element used for a specific action.
34 * @type {goog.Disposable}
35 */
36 this.actionObject_ = null;
37
38 /**
39 * Controls for the current action.
40 * @type {goog.ui.Control}
41 */
42 this.controlContainer_ = null;
43 }
44
45 /**
46 * Gets the root of the Actor.
47 * @return {Element}
48 */
49 armadillo.Actor.prototype.getElement = function() {
50 return this.element_;
51 };
52
53 /**
54 * The different options that the Actor can perform.
55 */
56 armadillo.Actor.options_ = {
57 OPEN : 'open',
58 MOVE : 'move',
59 DELETE : 'delete',
60 TV_RENAME : 'tv-rename',
61 DOWNLOAD : 'download'
62 };
63
64 /**
65 * String values for the options.
66 */
67 armadillo.Actor.optionStrings_ = {
68 'open' : 'Open',
69 'move' : 'Move',
70 'delete' : 'Delete',
71 'tv-rename' : 'Rename TV Episode',
72 'download' : 'Download'
73 };
74
75 armadillo.Actor.prototype.createDom = function() {
76 this.decorateInternal($.createDom('div'));
77 return this.element_;
78 };
79
80 /**
81 * Decorates the given element into a path control.
82 * @param {Element} element
83 */
84 armadillo.Actor.prototype.decorateInternal = function(element) {
85 this.element_ = element;
86 this.element_.addClass('actor');
87 this.element_.empty();
88 for (var option in armadillo.Actor.options_) {
89 var tile = this.createTile_(option);
90 if (tile) {
91 this.element_.append(tile);
92 }
93 }
94 this.controlContainer_ = $.createDom('div');
95 this.element_.append(this.controlContainer_);
96 };
97
98 /**
99 * Creates the DOM Element that is inserted into the popup.
100 * @param {armadillo.Actor.options_} Key of the option to create
101 * @returns {goog.ui.Control}
102 */
103 armadillo.Actor.prototype.createTile_ = function(option) {
104 var value = armadillo.Actor.options_[option];
105
106 // Create the title element.
107 var title = $.createDom('span').addClass('title');
108 title.text(armadillo.Actor.optionStrings_[value]);
109
110 var tile = $.createDom('div').addClass('tile');
111 tile.append(title);
112
113 // Cannot open non-directory files.
114 if (value == armadillo.Actor.options_.OPEN && !this.file_.isDirectory()) {
115 return null;
116 }
117
118 tile.click(this.tileClickHandler_.bind(this, value));
119 return tile;
120 };
121
122 /**
123 * Click handler for individual tiles.
124 * @param {int} option The Actor.option used
125 * @param {Event} e
126 */
127 armadillo.Actor.prototype.tileClickHandler_ = function(option, e) {
128 this.controlContainer_.empty();
129 this.controlContainer_.show();
130 if (option == armadillo.Actor.options_.OPEN) {
131 // TODO: assert that this.file_.isDirectory().
132 app.navigate(this.file_.getName());
133 } else if (option == armadillo.Actor.options_.MOVE) {
134 this.performMove_();
135 } else if (option == armadillo.Actor.options_.DELETE) {
136 this.performDelete_();
137 } else if (option == armadillo.Actor.options_.TV_RENAME) {
138 this.performTVRename_();
139 } else if (option == armadillo.Actor.options_.DOWNLOAD) {
140 this.performDownload_();
141 }
142 };
143
144 /**
145 * Subroutine to handle bringing up the move confirmation UI.
146 * @private
147 */
148 armadillo.Actor.prototype.performMove_ = function() {
149 var editor = new armadillo.PathControl(this.file_.getFullPath(), true);
150 this.controlContainer_.append(editor.createDom());
151
152 var okCallback = function(e) {
153 var newPath = editor.getPath();
154 this.file_.move(newPath);
155 };
156 this.createOkCancel_(okCallback.bind(this), null);
157 };
158
159 /**
160 * Subroutine to handle bringing up the delete confirmation UI.
161 * @private
162 */
163 armadillo.Actor.prototype.performDelete_ = function() {
164 var content = $('<div>Are you sure you want to delete:<br/><strong>' +
165 this.file_.getName() + '</strong></div>');
166 this.controlContainer_.append(content);
167
168 var okCallback = function(e) {
169 this.file_.remove();
170 };
171 this.createOkCancel_(okCallback.bind(this), null);
172 };
173
174 /**
175 * Subroutine that renames a file to it's title based on season and episode.
176 * @private
177 */
178 armadillo.Actor.prototype.performTVRename_ = function() {
179 var renamer = new armadillo.TVRenamer(this.file_);
180 renamer.run();
181 };
182
183 /**
184 * Subroutine that streams a file.
185 * @private
186 */
187 armadillo.Actor.prototype.performDownload_ = function() {
188 window.location = '/download?path=' + this.file_.getFullPath();
189 };
190
191 /**
192 * Creates two buttons: one for OK one for Cancel and attahes them to the
193 * |controlContainer_|.
194 * @param {function(Event)?} okCallback
195 * @param {function(Event)?} cancelCallback
196 */
197 armadillo.Actor.prototype.createOkCancel_ = function(okCallback, cancelCallback) {
198 var ok = $.createDom('button').text('OK');
199 if (okCallback)
200 ok.click(okCallback);
201 var cancel = $.createDom('button').text('Cancel');
202 if (!cancelCallback)
203 cancelCallback = this.defaultCancelCallback_.bind(this);
204 cancel.click(cancelCallback);
205 this.controlContainer_.append(ok);
206 this.controlContainer_.append(cancel);
207 };
208
209 /**
210 * The default cancel callback for the above createOkCancel_().
211 * @param {event} e
212 * @private
213 */
214 armadillo.Actor.prototype.defaultCancelCallback_ = function(e) {
215 this.controlContainer_.empty();
216 };