2 // Armadillo File Manager
3 // Copyright (c) 2010, 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 goog
.provide('armadillo.Actor');
12 goog
.require('goog.array');
13 goog
.require('goog.dom');
14 goog
.require('goog.events');
15 goog
.require('goog.positioning.ClientPosition');
16 goog
.require('goog.positioning.Corner');
17 goog
.require('goog.ui.Dialog');
18 goog
.require('goog.ui.Popup');
21 * The Actor is a popup that displays the various actions that can be performed
23 * @param {armadillo.File} file The file to act on.
26 armadillo
.Actor
= function(file
) {
27 goog
.Disposable
.call(this);
29 this.element_
= this.createElement_();
30 this.popup_
= new goog
.ui
.Popup(this.element_
);
31 armadillo
.Actor
.actors_
.push(this);
33 goog
.inherits(armadillo
.Actor
, goog
.Disposable
);
36 * An array of all the Actors that have been created.
38 armadillo
.Actor
.actors_
= new Array();
41 * The different options that the Actor can perform.
43 armadillo
.Actor
.options_
= {
51 * String values for the options.
53 armadillo
.Actor
.optionStrings_
= {
61 * A global property that should be checked to see if an actor is present,
62 * creating a modal session.
64 armadillo
.Actor
.isModal
= function() {
65 var isVisible
= false;
66 goog
.array
.forEach(armadillo
.Actor
.actors_
, function (e
) {
67 isVisible
|= e
.popup_
.isVisible();
76 armadillo
.Actor
.prototype.disposeInternal
= function() {
77 armadillo
.Actor
.superClass_
.disposeInternal
.call(this);
79 // Unlisten the tiles.
80 var tiles
= goog
.dom
.getElementsByClassName('tile', this.element_
);
81 goog
.array
.forEach(tiles
, function (tile
) {
82 goog
.events
.unlistenByKey(tile
.actorListener
);
85 // Remove the actor display element.
86 goog
.dom
.removeNode(this.element_
);
90 this.popup_
.dispose();
93 // Remove the actor from the list.
94 goog
.array
.remove(armadillo
.Actor
.actors_
, this);
101 * @param {int} x The X position to show at
102 * @param {int} y The Y position to show at
104 armadillo
.Actor
.prototype.show
= function(x
, y
) {
105 if (armadillo
.Actor
.isModal())
107 var firstBodyElement
= goog
.dom
.getFirstElementChild(document
.body
);
108 goog
.dom
.insertSiblingBefore(this.element_
, firstBodyElement
);
109 this.popup_
.setPinnedCorner(goog
.positioning
.Corner
.TOP_LEFT
);
110 this.popup_
.setPosition(new goog
.positioning
.ClientPosition(x
, y
));
111 this.popup_
.setHideOnEscape(true);
112 this.popup_
.setVisible(true);
118 armadillo
.Actor
.prototype.hide
= function() {
119 this.popup_
.setVisible(false);
123 * Creates the DOM Element that is inserted into the popup.
126 armadillo
.Actor
.prototype.createElement_
= function() {
127 var root
= goog
.dom
.createDom('div', 'actor');
128 for (var option
in armadillo
.Actor
.options_
) {
129 var tile
= goog
.dom
.createDom('div', 'tile');
130 var value
= armadillo
.Actor
.options_
[option];
131 // Cannot open non-directory files.
132 if (value
== armadillo
.Actor
.options_
.OPEN
&& !this.file_
.isDirectory()) {
135 var title
= goog
.dom
.createDom('span', 'title',
136 armadillo
.Actor
.optionStrings_
[value]);
137 goog
.dom
.appendChild(tile
, title
);
138 goog
.dom
.appendChild(root
, tile
);
139 tile
.actorOption
= value
;
140 tile
.actorListener
= goog
.events
.listen(tile
, goog
.events
.EventType
.CLICK
,
141 this.tileClickHandler_
, false, this);
147 * Click handler for individual tiles.
150 armadillo
.Actor
.prototype.tileClickHandler_
= function(e
) {
151 if (e
.target
.actorOption
== armadillo
.Actor
.options_
.OPEN
) {
152 // TODO: assert that this.file_.isDirectory().
153 app
.navigate(this.file_
.getName());
155 } else if (e
.target
.actorOption
== armadillo
.Actor
.options_
.DELETE
) {
156 this.performDelete_();
161 * Subroutine to handle bringing up the delete confirmation UI.
164 armadillo
.Actor
.prototype.performDelete_
= function() {
165 var confirm
= new goog
.ui
.Dialog();
166 confirm
.setDisposeOnHide(true);
167 confirm
.setEscapeToCancel(true);
168 confirm
.setModal(true);
169 confirm
.setDraggable(false);
170 confirm
.setHasTitleCloseButton(false);
171 confirm
.setTitle('Confirm Delete');
173 var container
= confirm
.getContentElement();
174 var content
= goog
.dom
.createDom('span', null,
175 'Are you sure you want to delete:',
176 goog
.dom
.createElement('br'),
177 goog
.dom
.createDom('strong', null, this.file_
.getName()));
178 goog
.dom
.appendChild(container
, content
);
180 var closeCallback
= function(e
) {
181 if (e
.key
!= goog
.ui
.Dialog
.DefaultButtonKeys
.CANCEL
) {
185 // Will be removed when the event source closes.
186 goog
.events
.listen(confirm
, goog
.ui
.Dialog
.SELECT_EVENT
,
187 closeCallback
, false, this);
189 confirm
.setVisible(true);