* Implement the basic UI of the Actor popup
[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 goog.dom.removeNode(this.element_);
76 this.element_ = null;
77 this.popup_.dispose();
78 this.popup_ = null;
79 goog.array.remove(armadillo.Actor.actors_, this);
80 };
81
82 /**
83 * Shows the popup.
84 * @param {int} x The X position to show at
85 * @param {int} y The Y position to show at
86 */
87 armadillo.Actor.prototype.show = function(x, y) {
88 if (armadillo.Actor.isModal())
89 return;
90 var firstBodyElement = goog.dom.getFirstElementChild(document.body);
91 goog.dom.insertSiblingBefore(this.element_, firstBodyElement);
92 this.popup_.setPinnedCorner(goog.positioning.Corner.TOP_LEFT);
93 this.popup_.setPosition(new goog.positioning.ClientPosition(x, y));
94 this.popup_.setHideOnEscape(true);
95 this.popup_.setVisible(true);
96 };
97
98 /**
99 * Hides the popup.
100 */
101 armadillo.Actor.prototype.hide = function() {
102 this.popup_.setVisible(false);
103 };
104
105 /**
106 * Creates the DOM Element that is inserted into the popup.
107 * @returns Element
108 */
109 armadillo.Actor.prototype.createElement_ = function() {
110 var root = goog.dom.createDom('div', 'actor');
111 for (var option in armadillo.Actor.options_) {
112 var tile = goog.dom.createDom('div', 'tile');
113 var value = armadillo.Actor.options_[option];
114 var title = goog.dom.createDom('span', 'title',
115 armadillo.Actor.optionStrings_[value]);
116 goog.dom.appendChild(tile, title);
117 goog.dom.appendChild(root, tile);
118 tile.actorOption = value;
119 tile.actorListener = goog.events.listen(tile, goog.events.EventType.CLICK,
120 this.tileClickHandler_, false, this);
121 }
122 return root;
123 };
124
125 /**
126 * Click handler for individual tiles.
127 * @param {Event} e
128 */
129 armadillo.Actor.prototype.tileClickHandler_ = function(e) {
130 if (e.target.actorOption == armadillo.Actor.options_.DELETE) {
131 console.log("DELETE DELETE DELETE");
132 }
133 console.log('You clicked ' + e.target.actorOption);
134 };