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.
18 armadillo
.Actor
= function(file
) {
20 * The file object on which this acts.
21 * @type {armadillo.File}
26 * The root DOM element for the actor.
32 * Controls for the current action.
35 this.controlContainer_
= null;
39 * Gets the root of the Actor.
42 armadillo
.Actor
.prototype.getElement
= function() {
47 * The different options that the Actor can perform.
49 armadillo
.Actor
.options_
= {
53 TV_RENAME
: 'tv-rename',
58 * String values for the options.
60 armadillo
.Actor
.optionStrings_
= {
64 'tv-rename' : 'Rename TV Episode',
65 'download' : 'Download'
68 armadillo
.Actor
.prototype.createDom
= function() {
69 this.decorateInternal($.createDom('div'));
74 * Decorates the given element into a path control.
75 * @param {Element} element
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
);
84 this.element_
.append(tile
);
87 this.controlContainer_
= $.createDom('div');
88 this.element_
.append(this.controlContainer_
);
92 * Creates the DOM Element that is inserted into the popup.
93 * @param {armadillo.Actor.options_} Key of the option to create
96 armadillo
.Actor
.prototype.createTile_
= function(option
) {
97 var value
= armadillo
.Actor
.options_
[option];
99 // Create the title element.
100 var title
= $.createDom('span').addClass('title');
101 title
.text(armadillo
.Actor
.optionStrings_
[value]);
103 var tile
= $.createDom('div').addClass('tile');
106 // Cannot open non-directory files.
107 if (value
== armadillo
.Actor
.options_
.OPEN
&& !this.file_
.isDirectory()) {
111 tile
.click(this.tileClickHandler_
.bind(this, value
));
116 * Click handler for individual tiles.
117 * @param {int} option The Actor.option used
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
) {
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_();
138 * Subroutine to handle bringing up the move confirmation UI.
141 armadillo
.Actor
.prototype.performMove_
= function() {
142 var editor
= new armadillo
.PathControl(this.file_
.getFullPath(), true);
143 this.controlContainer_
.append(editor
.createDom());
145 var okCallback
= function(e
) {
146 var newPath
= editor
.getPath();
147 this.file_
.move(newPath
);
149 this.createOkCancel_(okCallback
.bind(this), null);
153 * Subroutine to handle bringing up the delete confirmation UI.
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
);
161 var okCallback
= function(e
) {
164 this.createOkCancel_(okCallback
.bind(this), null);
168 * Subroutine that renames a file to it's title based on season and episode.
171 armadillo
.Actor
.prototype.performTVRename_
= function() {
172 var renamer
= new armadillo
.TVRenamer(this.file_
);
177 * Subroutine that streams a file.
180 armadillo
.Actor
.prototype.performDownload_
= function() {
181 window
.location
= '/download?path=' + this.file_
.getFullPath();
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
190 armadillo
.Actor
.prototype.createOkCancel_
= function(okCallback
, cancelCallback
) {
191 var ok
= $.createDom('button').text('OK');
193 ok
.click(okCallback
);
194 var cancel
= $.createDom('button').text('Cancel');
196 cancelCallback
= this.defaultCancelCallback_
.bind(this);
197 cancel
.click(cancelCallback
);
198 this.controlContainer_
.append(ok
);
199 this.controlContainer_
.append(cancel
);
203 * The default cancel callback for the above createOkCancel_().
207 armadillo
.Actor
.prototype.defaultCancelCallback_
= function(e
) {
208 this.controlContainer_
.empty();