Pass the File to the Actor on construction.
[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('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.Popup');
18
19 /**
20 * The Actor is a popup that displays the various actions that can be performed
21 * on a given File.
22 * @param {armadillo.File} file The file to act on.
23 * @constructor
24 */
25 armadillo.Actor = function(file) {
26 goog.Disposable.call(this);
27 this.file_ = file;
28 this.element_ = this.createElement_();
29 this.popup_ = new goog.ui.Popup(this.element_);
30 armadillo.Actor.actors_.push(this);
31 }
32 goog.inherits(armadillo.Actor, goog.Disposable);
33
34 /**
35 * An array of all the Actors that have been created.
36 */
37 armadillo.Actor.actors_ = new Array();
38
39 /**
40 * The different options that the Actor can perform.
41 */
42 armadillo.Actor.options_ = {
43 OPEN : 'open',
44 MOVE : 'move',
45 RENAME : 'rename',
46 DELETE : 'delete'
47 };
48
49 /**
50 * String values for the options.
51 */
52 armadillo.Actor.optionStrings_ = {
53 'open' : 'Open',
54 'move' : 'Move',
55 'rename' : 'Rename',
56 'delete' : 'Delete'
57 };
58
59 /**
60 * A global property that should be checked to see if an actor is present,
61 * creating a modal session.
62 */
63 armadillo.Actor.isModal = function() {
64 var isVisible = false;
65 goog.array.forEach(armadillo.Actor.actors_, function (e) {
66 isVisible |= e.popup_.isVisible();
67 });
68 return isVisible;
69 };
70
71 /**
72 * Disposer
73 * @protected
74 */
75 armadillo.Actor.prototype.disposeInternal = function() {
76 armadillo.Actor.superClass_.disposeInternal.call(this);
77
78 // Unlisten the tiles.
79 var tiles = goog.dom.getElementsByClassName('tile', this.element_);
80 goog.array.forEach(tiles, function (tile) {
81 goog.events.unlistenByKey(tile.actorListener);
82 });
83
84 // Remove the actor display element.
85 goog.dom.removeNode(this.element_);
86 this.element_ = null;
87
88 // Kill the popup.
89 this.popup_.dispose();
90 this.popup_ = null;
91
92 // Remove the actor from the list.
93 goog.array.remove(armadillo.Actor.actors_, this);
94
95 this.file_ = null;
96 };
97
98 /**
99 * Shows the popup.
100 * @param {int} x The X position to show at
101 * @param {int} y The Y position to show at
102 */
103 armadillo.Actor.prototype.show = function(x, y) {
104 if (armadillo.Actor.isModal())
105 return;
106 var firstBodyElement = goog.dom.getFirstElementChild(document.body);
107 goog.dom.insertSiblingBefore(this.element_, firstBodyElement);
108 this.popup_.setPinnedCorner(goog.positioning.Corner.TOP_LEFT);
109 this.popup_.setPosition(new goog.positioning.ClientPosition(x, y));
110 this.popup_.setHideOnEscape(true);
111 this.popup_.setVisible(true);
112 };
113
114 /**
115 * Hides the popup.
116 */
117 armadillo.Actor.prototype.hide = function() {
118 this.popup_.setVisible(false);
119 };
120
121 /**
122 * Creates the DOM Element that is inserted into the popup.
123 * @returns Element
124 */
125 armadillo.Actor.prototype.createElement_ = function() {
126 var root = goog.dom.createDom('div', 'actor');
127 for (var option in armadillo.Actor.options_) {
128 var tile = goog.dom.createDom('div', 'tile');
129 var value = armadillo.Actor.options_[option];
130 var title = goog.dom.createDom('span', 'title',
131 armadillo.Actor.optionStrings_[value]);
132 goog.dom.appendChild(tile, title);
133 goog.dom.appendChild(root, tile);
134 tile.actorOption = value;
135 tile.actorListener = goog.events.listen(tile, goog.events.EventType.CLICK,
136 this.tileClickHandler_, false, this);
137 }
138 return root;
139 };
140
141 /**
142 * Click handler for individual tiles.
143 * @param {Event} e
144 */
145 armadillo.Actor.prototype.tileClickHandler_ = function(e) {
146 if (e.target.actorOption == armadillo.Actor.options_.DELETE) {
147 console.log("DELETE DELETE DELETE " + this.file_.getName());
148 }
149 console.log('You clicked ' + e.target.actorOption);
150 };