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