Do not add the Open tile for non-directory items.
[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 * @param {string} The path the file resides at.
20 * @constructor
21 */
22 armadillo.File = function(name, path) {
23 goog.Disposable.call(this);
24 this.name_ = name;
25 this.path_ = path;
26 this.isDirectory_ = app.isDirectory(name);
27 };
28 goog.inherits(armadillo.File, goog.Disposable);
29
30 /**
31 * Disposer
32 * @protected
33 */
34 armadillo.File.prototype.disposeInternal = function() {
35 armadillo.File.superClass_.disposeInternal.call(this);
36 this.element_ = null;
37 goog.events.unlistenByKey(this.clickListener_);
38 goog.events.unlistenByKey(this.mouseOverListener_);
39 goog.events.unlistenByKey(this.mouseOutListener_);
40 this.button_ = null;
41 goog.events.unlistenByKey(this.buttonListener_);
42 };
43
44 /**
45 * Returns the name of the file.
46 * @returns string
47 */
48 armadillo.File.prototype.getName = function() {
49 return this.name_;
50 };
51
52 /**
53 * Returns whether or not this is a directory.
54 * @returns boolean
55 */
56 armadillo.File.prototype.isDirectory = function() {
57 return this.isDirectory_;
58 };
59
60 /**
61 * Constructs the Elements that make up the UI.
62 * @returns {Element} An element ready for insertion into DOM.
63 */
64 armadillo.File.prototype.draw = function() {
65 // Create the element if it does not exist. If it does, remove all children.
66 if (!this.element_) {
67 this.element_ = goog.dom.createElement('li');
68 this.element_.representedObject = this;
69 this.clickListener_ = goog.events.listen(this.element_,
70 goog.events.EventType.CLICK, this.clickHandler_, false, this);
71 if (!this.isSpecial_()) {
72 this.mouseOverListener_ = goog.events.listen(this.element_,
73 goog.events.EventType.MOUSEOVER, this.hoverHandler_, false, this);
74 this.mouseOutListener_ = goog.events.listen(this.element_,
75 goog.events.EventType.MOUSEOUT, this.hoverHandler_, false, this);
76 }
77 }
78 goog.dom.removeChildren(this.element_);
79
80 // Set the name of the entry.
81 goog.dom.setTextContent(this.element_, this.name_);
82
83 // Create the edit button.
84 if (!this.isSpecial_()) {
85 this.button_ = goog.dom.createElement('button');
86 goog.dom.setTextContent(this.button_, 'Edit');
87 goog.dom.appendChild(this.element_, this.button_);
88 this.button_.style.display = 'none';
89 this.buttonListener_ = goog.events.listen(this.button_,
90 goog.events.EventType.CLICK, this.buttonClickHandler_, false, this);
91 }
92
93 return this.element_;
94 };
95
96 /**
97 * Deletes the given file in the backend by sending a request. On return, it
98 * will re-query the directory.
99 */
100 armadillo.File.prototype.delete = function() {
101 var file = this;
102 var callback = function(data) {
103 if (data['error']) {
104 app.showError(data['message']);
105 return;
106 } else {
107 app.clearError();
108 }
109 app.list(file.path_);
110 };
111 app.sendRequest('remove', {'path':this.path_ + this.name_}, callback);
112 };
113
114 /**
115 * Click handler for the list element.
116 * @param {Event} e
117 */
118 armadillo.File.prototype.clickHandler_ = function(e) {
119 if (armadillo.Actor.isModal()) {
120 return;
121 }
122 if (this.isDirectory_) {
123 app.navigate(this.name_);
124 }
125 };
126
127 /**
128 * Hover event handler for the list element. This can handle both mouseover
129 * and mouseout events.
130 * @param {Event} e
131 */
132 armadillo.File.prototype.hoverHandler_ = function(e) {
133 if (armadillo.Actor.isModal())
134 return;
135 var display = (e.type == goog.events.EventType.MOUSEOVER);
136 this.button_.style.display = (display ? '' : 'none');
137 };
138
139 /**
140 * Click handler for the button element.
141 * @param {Event} e
142 */
143 armadillo.File.prototype.buttonClickHandler_ = function(e) {
144 if (armadillo.Actor.isModal())
145 return;
146 e.stopPropagation();
147 var actor = new armadillo.Actor(this);
148 actor.show(e.clientX, e.clientY);
149 };
150
151 /**
152 * Returns TRUE if this File is not a real file, but a special kind.
153 * @returns boolean
154 */
155 armadillo.File.prototype.isSpecial_ = function() {
156 return this.name_ == '../';
157 };