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