2 // Armadillo File Manager
3 // Copyright (c) 2010-2011, Robert Sesek <http://www.bluestatic.org>
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.
10 $.namespace('armadillo.Actor');
13 * The Actor is a popup that displays the various actions that can be performed
15 * @param {armadillo.File} file The file to act on.
16 * @param {goog.dom.DomHelper} opt_domHelper
19 armadillo
.Actor
= function(file
, opt_domHelper
) {
21 * The file object on which this acts.
22 * @type {armadillo.File}
27 * The root DOM element for the actor.
33 * The UI element used for a specific action.
34 * @type {goog.Disposable}
36 this.actionObject_
= null;
39 * Controls for the current action.
40 * @type {goog.ui.Control}
42 this.controlContainer_
= null;
46 * Gets the root of the Actor.
49 armadillo
.Actor
.prototype.getElement
= function() {
54 * The different options that the Actor can perform.
56 armadillo
.Actor
.options_
= {
60 TV_RENAME
: 'tv-rename',
65 * String values for the options.
67 armadillo
.Actor
.optionStrings_
= {
71 'tv-rename' : 'Rename TV Episode',
72 'download' : 'Download'
75 armadillo
.Actor
.prototype.createDom
= function() {
76 this.decorateInternal($.createDom('div'));
81 * Decorates the given element into a path control.
82 * @param {Element} element
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
);
91 this.element_
.append(tile
);
94 this.controlContainer_
= $.createDom('div');
95 this.element_
.append(this.controlContainer_
);
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}
103 armadillo
.Actor
.prototype.createTile_
= function(option
) {
104 var value
= armadillo
.Actor
.options_
[option];
106 // Create the title element.
107 var title
= $.createDom('span').addClass('title');
108 title
.text(armadillo
.Actor
.optionStrings_
[value]);
110 var tile
= $.createDom('div').addClass('tile');
113 // Cannot open non-directory files.
114 if (value
== armadillo
.Actor
.options_
.OPEN
&& !this.file_
.isDirectory()) {
118 tile
.click(this.tileClickHandler_
.bind(this, value
));
123 * Click handler for individual tiles.
124 * @param {int} option The Actor.option used
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
) {
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_();
145 * Subroutine to handle bringing up the move confirmation UI.
148 armadillo
.Actor
.prototype.performMove_
= function() {
149 var editor
= new armadillo
.PathControl(this.file_
.getFullPath(), true);
150 this.controlContainer_
.append(editor
.createDom());
152 var okCallback
= function(e
) {
153 var newPath
= editor
.getPath();
154 this.file_
.move(newPath
);
156 this.createOkCancel_(okCallback
.bind(this), null);
160 * Subroutine to handle bringing up the delete confirmation UI.
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
);
168 var okCallback
= function(e
) {
171 this.createOkCancel_(okCallback
.bind(this), null);
175 * Subroutine that renames a file to it's title based on season and episode.
178 armadillo
.Actor
.prototype.performTVRename_
= function() {
179 var renamer
= new armadillo
.TVRenamer(this.file_
);
184 * Subroutine that streams a file.
187 armadillo
.Actor
.prototype.performDownload_
= function() {
188 window
.location
= '/download?path=' + this.file_
.getFullPath();
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
197 armadillo
.Actor
.prototype.createOkCancel_
= function(okCallback
, cancelCallback
) {
198 var ok
= $.createDom('button').text('OK');
200 ok
.click(okCallback
);
201 var cancel
= $.createDom('button').text('Cancel');
203 cancelCallback
= this.defaultCancelCallback_
.bind(this);
204 cancel
.click(cancelCallback
);
205 this.controlContainer_
.append(ok
);
206 this.controlContainer_
.append(cancel
);
210 * The default cancel callback for the above createOkCancel_().
214 armadillo
.Actor
.prototype.defaultCancelCallback_
= function(e
) {
215 this.controlContainer_
.empty();