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 UI element used for a specific action.
28 * @type {goog.Disposable}
30 this.actionObject_
= null;
33 * Controls for the current action.
34 * @type {goog.ui.Control}
36 this.controlContainer_
= null;
40 * The different options that the Actor can perform.
42 armadillo
.Actor
.options_
= {
46 TV_RENAME
: 'tv-rename',
51 * String values for the options.
53 armadillo
.Actor
.optionStrings_
= {
57 'tv-rename' : 'Rename TV Episode',
58 'download' : 'Download'
61 armadillo
.Actor
.prototype.createDom
= function() {
62 this.decorateInternal($.createDom('div'));
67 * Decorates the given element into a path control.
68 * @param {Element} element
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
);
77 this.element_
.append(tile
);
80 this.controlContainer_
= $.createDom('div');
81 this.element_
.append(this.controlContainer_
);
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}
89 armadillo
.Actor
.prototype.createTile_
= function(option
) {
90 var value
= armadillo
.Actor
.options_
[option];
92 // Create the title element.
93 var title
= $.createDom('span').addClass('title');
94 title
.text(armadillo
.Actor
.optionStrings_
[value]);
96 var tile
= $.createDom('div').addClass('tile');
99 // Cannot open non-directory files.
100 if (value
== armadillo
.Actor
.options_
.OPEN
&& !this.file_
.isDirectory()) {
104 tile
.click(this.tileClickHandler_
.bind(this, value
));
109 * Click handler for individual tiles.
110 * @param {int} option The Actor.option used
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
) {
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_();
131 * Subroutine to handle bringing up the move confirmation UI.
134 armadillo
.Actor
.prototype.performMove_
= function() {
135 var editor
= new armadillo
.PathControl(this.file_
.getFullPath(), true);
136 this.controlContainer_
.append(editor
.createDom());
138 var okCallback
= function(e
) {
139 var newPath
= editor
.getPath();
140 this.file_
.move(newPath
);
142 this.createOkCancel_(okCallback
.bind(this), null);
146 * Subroutine to handle bringing up the delete confirmation UI.
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
);
154 var okCallback
= function(e
) {
157 this.createOkCancel_(okCallback
.bind(this), null);
161 * Subroutine that renames a file to it's title based on season and episode.
164 armadillo
.Actor
.prototype.performTVRename_
= function() {
165 var renamer
= new armadillo
.TVRenamer(this.file_
);
170 * Subroutine that streams a file.
173 armadillo
.Actor
.prototype.performDownload_
= function() {
174 window
.location
= '/download?path=' + this.file_
.getFullPath();
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
183 armadillo
.Actor
.prototype.createOkCancel_
= function(okCallback
, cancelCallback
) {
184 var ok
= $.createDom('button').text('OK');
186 ok
.click(okCallback
);
187 var cancel
= $.createDom('button').text('Cancel');
189 cancelCallback
= this.defaultCancelCallback_
.bind(this);
190 cancel
.click(cancelCallback
);
191 this.controlContainer_
.append(ok
);
192 this.controlContainer_
.append(cancel
);
196 * The default cancel callback for the above createOkCancel_().
200 armadillo
.Actor
.prototype.defaultCancelCallback_
= function(e
) {
201 this.controlContainer_
.empty();