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