Remove references to goog in web_frontend/
[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 armadillo.Actor.prototype.createDom = function() {
69 this.decorateInternal($.createDom('div'));
70 return this.element_;
71 };
72
73 /**
74 * Decorates the given element into a path control.
75 * @param {Element} element
76 */
77 armadillo.Actor.prototype.decorateInternal = function(element) {
78 this.element_ = element;
79 this.element_.addClass('actor');
80 this.element_.empty();
81 for (var option in armadillo.Actor.options_) {
82 var tile = this.createTile_(option);
83 if (tile) {
84 this.element_.append(tile);
85 }
86 }
87 this.controlContainer_ = $.createDom('div');
88 this.element_.append(this.controlContainer_);
89 };
90
91 /**
92 * Creates the DOM Element that is inserted into the popup.
93 * @param {armadillo.Actor.options_} Key of the option to create
94 * @returns {Element}
95 */
96 armadillo.Actor.prototype.createTile_ = function(option) {
97 var value = armadillo.Actor.options_[option];
98
99 // Create the title element.
100 var title = $.createDom('span').addClass('title');
101 title.text(armadillo.Actor.optionStrings_[value]);
102
103 var tile = $.createDom('div').addClass('tile');
104 tile.append(title);
105
106 // Cannot open non-directory files.
107 if (value == armadillo.Actor.options_.OPEN && !this.file_.isDirectory()) {
108 return null;
109 }
110
111 tile.click(this.tileClickHandler_.bind(this, value));
112 return tile;
113 };
114
115 /**
116 * Click handler for individual tiles.
117 * @param {int} option The Actor.option used
118 * @param {Event} e
119 */
120 armadillo.Actor.prototype.tileClickHandler_ = function(option, e) {
121 this.controlContainer_.empty();
122 this.controlContainer_.show();
123 if (option == armadillo.Actor.options_.OPEN) {
124 // TODO: assert that this.file_.isDirectory().
125 app.navigate(this.file_.getName());
126 } else if (option == armadillo.Actor.options_.MOVE) {
127 this.performMove_();
128 } else if (option == armadillo.Actor.options_.DELETE) {
129 this.performDelete_();
130 } else if (option == armadillo.Actor.options_.TV_RENAME) {
131 this.performTVRename_();
132 } else if (option == armadillo.Actor.options_.DOWNLOAD) {
133 this.performDownload_();
134 }
135 };
136
137 /**
138 * Subroutine to handle bringing up the move confirmation UI.
139 * @private
140 */
141 armadillo.Actor.prototype.performMove_ = function() {
142 var editor = new armadillo.PathControl(this.file_.getFullPath(), true);
143 this.controlContainer_.append(editor.createDom());
144
145 var okCallback = function(e) {
146 var newPath = editor.getPath();
147 this.file_.move(newPath);
148 };
149 this.createOkCancel_(okCallback.bind(this), null);
150 };
151
152 /**
153 * Subroutine to handle bringing up the delete confirmation UI.
154 * @private
155 */
156 armadillo.Actor.prototype.performDelete_ = function() {
157 var content = $('<div>Are you sure you want to delete:<br/><strong>' +
158 this.file_.getName() + '</strong></div>');
159 this.controlContainer_.append(content);
160
161 var okCallback = function(e) {
162 this.file_.remove();
163 };
164 this.createOkCancel_(okCallback.bind(this), null);
165 };
166
167 /**
168 * Subroutine that renames a file to it's title based on season and episode.
169 * @private
170 */
171 armadillo.Actor.prototype.performTVRename_ = function() {
172 var renamer = new armadillo.TVRenamer(this.file_);
173 renamer.run();
174 };
175
176 /**
177 * Subroutine that streams a file.
178 * @private
179 */
180 armadillo.Actor.prototype.performDownload_ = function() {
181 window.location = '/download?path=' + this.file_.getFullPath();
182 };
183
184 /**
185 * Creates two buttons: one for OK one for Cancel and attahes them to the
186 * |controlContainer_|.
187 * @param {function(Event)?} okCallback
188 * @param {function(Event)?} cancelCallback
189 */
190 armadillo.Actor.prototype.createOkCancel_ = function(okCallback, cancelCallback) {
191 var ok = $.createDom('button').text('OK');
192 if (okCallback)
193 ok.click(okCallback);
194 var cancel = $.createDom('button').text('Cancel');
195 if (!cancelCallback)
196 cancelCallback = this.defaultCancelCallback_.bind(this);
197 cancel.click(cancelCallback);
198 this.controlContainer_.append(ok);
199 this.controlContainer_.append(cancel);
200 };
201
202 /**
203 * The default cancel callback for the above createOkCancel_().
204 * @param {event} e
205 * @private
206 */
207 armadillo.Actor.prototype.defaultCancelCallback_ = function(e) {
208 this.controlContainer_.empty();
209 };