2 // Armadillo File Manager
3 // Copyright (c) 2010, Robert Sesek <http://www.bluestatic.org>
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.
10 goog
.provide('armadillo.PathControl');
11 goog
.provide('armadillo.PathControl.NameControlRenderer_');
13 goog
.require('goog.array');
14 goog
.require('goog.ui.Control');
15 goog
.require('goog.ui.FilteredMenu');
16 goog
.require('goog.ui.LabelInput');
17 goog
.require('goog.ui.MenuButton');
18 goog
.require('goog.ui.MenuItem');
21 * Creates a new path editing control for a given path.
22 * @param {string} path The path to create an editor for
23 * @param {bool} editLastComponent Whether the last component should be shown as an edit box
24 * @param {DomHelper} opt_domHelper Optional DOM helper
27 armadillo
.PathControl
= function(path
, editLastComponent
, opt_domHelper
) {
28 goog
.ui
.Control
.call(this, opt_domHelper
);
30 this.setSupportedState(goog
.ui
.Component
.State
.FOCUSED
, false);
33 * Full path of the control.
39 * The name of the file at the |path_|.
46 * Whether or not the last component is editable.
49 this.editableLastComponent_
= editLastComponent
;
52 * Control UI for the name component of the path.
53 * @type {goog.ui.Control}
55 this.nameControl_
= null;
59 * @type {goog.events.EventHandler}
61 this.eh_
= new goog
.events
.EventHandler();
63 goog
.inherits(armadillo
.PathControl
, goog
.ui
.Control
);
69 armadillo
.PathControl
.prototype.disposeInternal
= function() {
70 armadillo
.PathControl
.superClass_
.disposeInternal
.call(this);
71 this.nameControl_
= null;
79 * @param {string} path
81 armadillo
.PathControl
.prototype.setPath
= function(path
) {
82 this.path_
= app
.stripLastPathComponent(path
);
83 this.name_
= path
.substr(this.path_
.length
);
90 armadillo
.PathControl
.prototype.getPath
= function() {
91 return app
.joinPath(this.path_
, this.name_
);
95 * Gets the name control.
96 * @returns {goog.ui.Control}
98 armadillo
.PathControl
.prototype.getNameControl
= function() {
99 return this.nameControl_
;
103 * Creates a new path control object.
105 armadillo
.PathControl
.prototype.createDom
= function() {
106 this.decorateInternal(this.dom_
.createElement('div'));
112 armadillo
.PathControl
.prototype.canDecorate
= function() {
117 * Decorates the given element into a path control.
118 * @param {Element} element
120 armadillo
.PathControl
.prototype.decorateInternal
= function(element
) {
121 this.element_
= element
;
122 var components
= this.path_
.split('/');
124 // If this is an item that lives at the root, generate a special node for
125 // moving between items at the top level.
128 // If the last component is emtpy, do not use it because it means a directory
130 if (components
[components
.length
- 1] == '') {
131 goog
.array
.removeAt(components
, components
.length
- 1);
135 goog
.array
.forEach(components
, function (part
, i
) {
136 this.addChild(this.createComponentNode_(path
, part
), true);
137 path
= app
.joinPath(path
, part
);
140 if (this.editableLastComponent_
) {
146 this.nameControl_
= new goog
.ui
.Control(this.dom_
.createDom('input', attrs
),
147 new armadillo
.PathControl
.NameControlRenderer_());
148 this.nameControl_
.setAllowTextSelection(true);
149 this.nameControl_
.setHandleMouseEvents(false);
150 this.addChild(this.nameControl_
, true);
152 this.eh_
.listen(this.nameControl_
.getElement(), goog
.events
.EventType
.CHANGE
,
153 this.nameChanged_
, false, this);
154 this.eh_
.listen(this.nameControl_
.getElement(), goog
.events
.EventType
.KEYDOWN
,
155 this.nameChanged_
, false, this);
157 this.nameControl_
= new goog
.ui
.Control(this.name_
);
158 this.addChild(this.nameControl_
, true);
160 goog
.dom
.classes
.add(this.nameControl_
.getElement(), 'goog-inline-block');
166 armadillo
.PathControl
.prototype.enterDocument
= function() {
167 armadillo
.PathControl
.superClass_
.enterDocument
.call(this);
168 this.nameControl_
.getElement().focus();
172 * Creates a node for a single path component.
173 * @param {string} path The path up to this point.
174 * @param {string} name The current component after |path|.
176 armadillo
.PathControl
.prototype.createComponentNode_
= function(path
, name
) {
177 var menu
= new goog
.ui
.FilteredMenu();
178 menu
.setFilterLabel(name
);
179 menu
.setAllowMultiple(false);
180 menu
.setOpenFollowsHighlight(true);
181 goog
.events
.listen(menu
, goog
.ui
.Component
.EventType
.ACTION
,
182 this.componentChanged_
, false, this);
183 this.fetchMenuContents_(path
, name
, menu
);
185 var button
= new goog
.ui
.MenuButton(name
, menu
, null, this.dom_
);
186 button
.setFocusablePopupMenu(true);
187 button
.setScrollOnOverflow(true);
188 button
.setVisible(true);
193 * Queries the back-end for all the items at a given path and attaches them to
195 * @param {string} path The path to get a list of items in
196 * @param {string} name The name to select
197 * @param {goog.ui.Menu} The menu to attach items to
199 armadillo
.PathControl
.prototype.fetchMenuContents_
= function(path
, name
, menu
) {
200 var callback
= function(e
) {
201 var data
= e
.target
.getResponseJson();
203 app
.showError(data
['message']);
207 // If this is the root path element, make sure the root is accessible for
209 goog
.array
.insertAt(data
, '/', 0);
211 goog
.array
.forEach(data
, function (caption
) {
212 // It only makes sense to be able to move into directories.
213 if (!app
.isDirectory(caption
)) {
216 var item
= new goog
.ui
.MenuItem(caption
);
217 item
.setValue(app
.joinPath(path
, name
, caption
));
219 if (caption
== name
) {
220 menu
.setHighlighted(item
);
224 app
.sendRequest('list', {'path' : app
.joinPath(path
, name
)}, callback
);
228 * Handler for changing a component of the control.
231 armadillo
.PathControl
.prototype.componentChanged_
= function(e
) {
232 this.path_
= e
.target
.getValue();
233 this.removeChildren(true);
234 this.decorateInternal(this.element_
);
238 * Handler for changing the editable name component.
241 armadillo
.PathControl
.prototype.nameChanged_
= function(e
) {
242 // TODO: assert(this.editableLastComponent_)
243 this.name_
= e
.target
.value
;
249 * Renderer for the Name Control of the Path Control
252 armadillo
.PathControl
.NameControlRenderer_
= function() {
253 goog
.ui
.ControlRenderer
.call(this);
255 goog
.inherits(armadillo
.PathControl
.NameControlRenderer_
, goog
.ui
.ControlRenderer
);
257 armadillo
.PathControl
.NameControlRenderer_
.prototype.createDom
= function(control
) {
258 var content
= control
.getContent();
259 if (content
instanceof HTMLElement
) {
262 return armadillo
.PathControl
.NameControlRenderer_
.superClass_
.createDom
.call(this, control
);