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