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