Pass the File to the Actor on construction.
[armadillo.git] / web_frontend / file.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.File');
11
12 goog.require('armadillo.Actor');
13 goog.require('goog.Disposable');
14 goog.require('goog.dom');
15
16 /**
17 * A file in a directory listing.
18 * @param {string} File name.
19 * @constructor
20 */
21 armadillo.File = function(name) {
22 goog.Disposable.call(this);
23 this.name_ = name;
24 this.isDirectory_ = app.isDirectory(name);
25 };
26 goog.inherits(armadillo.File, goog.Disposable);
27
28 /**
29 * Disposer
30 * @protected
31 */
32 armadillo.File.prototype.disposeInternal = function() {
33 armadillo.File.superClass_.disposeInternal.call(this);
34 this.element_ = null;
35 goog.events.unlistenByKey(this.clickListener_);
36 goog.events.unlistenByKey(this.mouseOverListener_);
37 goog.events.unlistenByKey(this.mouseOutListener_);
38 this.button_ = null;
39 goog.events.unlistenByKey(this.buttonListener_);
40 };
41
42 /**
43 * Returns the name of the file.
44 * @returns string
45 */
46 armadillo.File.prototype.getName = function() {
47 return this.name_;
48 };
49
50 /**
51 * Constructs the Elements that make up the UI.
52 * @returns {Element} An element ready for insertion into DOM.
53 */
54 armadillo.File.prototype.draw = function() {
55 // Create the element if it does not exist. If it does, remove all children.
56 if (!this.element_) {
57 this.element_ = goog.dom.createElement('li');
58 this.element_.representedObject = this;
59 this.clickListener_ = goog.events.listen(this.element_,
60 goog.events.EventType.CLICK, this.clickHandler_, false, this);
61 if (!this.isSpecial_()) {
62 this.mouseOverListener_ = goog.events.listen(this.element_,
63 goog.events.EventType.MOUSEOVER, this.hoverHandler_, false, this);
64 this.mouseOutListener_ = goog.events.listen(this.element_,
65 goog.events.EventType.MOUSEOUT, this.hoverHandler_, false, this);
66 }
67 }
68 goog.dom.removeChildren(this.element_);
69
70 // Set the name of the entry.
71 goog.dom.setTextContent(this.element_, this.name_);
72
73 // Create the edit button.
74 if (!this.isSpecial_()) {
75 this.button_ = goog.dom.createElement('button');
76 goog.dom.setTextContent(this.button_, 'Edit');
77 goog.dom.appendChild(this.element_, this.button_);
78 this.button_.style.display = 'none';
79 this.buttonListener_ = goog.events.listen(this.button_,
80 goog.events.EventType.CLICK, this.buttonClickHandler_, false, this);
81 }
82
83 return this.element_;
84 };
85
86 /**
87 * Click handler for the list element.
88 * @param {Event} e
89 */
90 armadillo.File.prototype.clickHandler_ = function(e) {
91 if (armadillo.Actor.isModal()) {
92 return;
93 }
94 if (this.isDirectory_) {
95 app.navigate(this.name_);
96 }
97 };
98
99 /**
100 * Hover event handler for the list element. This can handle both mouseover
101 * and mouseout events.
102 * @param {Event} e
103 */
104 armadillo.File.prototype.hoverHandler_ = function(e) {
105 if (armadillo.Actor.isModal())
106 return;
107 var display = (e.type == goog.events.EventType.MOUSEOVER);
108 this.button_.style.display = (display ? '' : 'none');
109 };
110
111 /**
112 * Click handler for the button element.
113 * @param {Event} e
114 */
115 armadillo.File.prototype.buttonClickHandler_ = function(e) {
116 if (armadillo.Actor.isModal())
117 return;
118 e.stopPropagation();
119 var actor = new armadillo.Actor(this);
120 actor.show(e.clientX, e.clientY);
121 };
122
123 /**
124 * Returns TRUE if this File is not a real file, but a special kind.
125 * @returns boolean
126 */
127 armadillo.File.prototype.isSpecial_ = function() {
128 return this.name_ == '../';
129 };