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