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
);
31 * Full path of the control.
37 * The name of the file at the |path_|.
44 * Whether or not the last component is editable.
47 this.editableLastComponent_
= editLastComponent
;
50 * Control UI for the name component of the path.
51 * @type {goog.ui.Control}
53 this.nameControl_
= null;
57 * @type {goog.events.EventHandler}
59 this.eh_
= new goog
.events
.EventHandler();
61 goog
.inherits(armadillo
.PathControl
, goog
.ui
.Control
);
67 armadillo
.PathControl
.prototype.disposeInternal
= function() {
68 armadillo
.PathControl
.superClass_
.disposeInternal
.call(this);
69 this.nameControl_
= null;
77 * @param {string} path
79 armadillo
.PathControl
.prototype.setPath
= function(path
) {
80 this.path_
= app
.stripLastPathComponent(path
);
81 this.name_
= path
.substr(this.path_
.length
);
88 armadillo
.PathControl
.prototype.getPath
= function() {
89 return app
.joinPath(this.path_
, this.name_
);
93 * Gets the name control.
94 * @returns {goog.ui.Control}
96 armadillo
.PathControl
.prototype.getNameControl
= function() {
97 return this.nameControl_
;
101 * Creates a new path control object.
103 armadillo
.PathControl
.prototype.createDom
= function() {
104 this.decorateInternal(this.dom_
.createElement('div'));
110 armadillo
.PathControl
.prototype.canDecorate
= function() {
115 * Decorates the given element into a path control.
116 * @param {Element} element
118 armadillo
.PathControl
.prototype.decorateInternal
= function(element
) {
119 this.element_
= element
;
120 var components
= this.path_
.split('/');
122 // If this is an item that lives at the root, generate a special node for
123 // moving between items at the top level.
126 // If the last component is emtpy, do not use it because it means a directory
128 if (components
[components
.length
- 1] == '') {
129 goog
.array
.removeAt(components
, components
.length
- 1);
133 goog
.array
.forEach(components
, function (part
, i
) {
134 this.addChild(this.createComponentNode_(path
, part
), true);
135 path
= app
.joinPath(path
, part
);
138 if (this.editableLastComponent_
) {
144 this.nameControl_
= new goog
.ui
.Control(this.dom_
.createDom('input', attrs
),
145 new armadillo
.PathControl
.NameControlRenderer_());
146 this.nameControl_
.setAllowTextSelection(true);
147 this.nameControl_
.setHandleMouseEvents(true);
148 this.addChild(this.nameControl_
, true);
150 this.eh_
.listen(this.nameControl_
.getElement(), goog
.events
.EventType
.CHANGE
,
151 this.nameChanged_
, false, this);
152 this.eh_
.listen(this.nameControl_
.getElement(), goog
.events
.EventType
.KEYDOWN
,
153 this.nameChanged_
, false, this);
155 this.nameControl_
= new goog
.ui
.Control(this.name_
);
156 this.addChild(this.nameControl_
, true);
158 goog
.dom
.classes
.add(this.nameControl_
.getElement(), 'goog-inline-block');
164 armadillo
.PathControl
.prototype.enterDocument
= function() {
165 armadillo
.PathControl
.superClass_
.enterDocument
.call(this);
166 this.nameControl_
.getElement().focus();
170 * Creates a node for a single path component.
171 * @param {string} path The path up to this point.
172 * @param {string} name The current component after |path|.
174 armadillo
.PathControl
.prototype.createComponentNode_
= function(path
, name
) {
175 var menu
= new goog
.ui
.FilteredMenu();
176 menu
.setFilterLabel(name
);
177 menu
.setAllowMultiple(false);
178 menu
.setOpenFollowsHighlight(true);
179 goog
.events
.listen(menu
, goog
.ui
.Component
.EventType
.ACTION
,
180 this.componentChanged_
, false, this);
181 this.fetchMenuContents_(path
, name
, menu
);
183 var button
= new goog
.ui
.MenuButton(name
, menu
, null, this.dom_
);
184 button
.setFocusablePopupMenu(true);
185 button
.setScrollOnOverflow(true);
186 button
.setVisible(true);
191 * Queries the back-end for all the items at a given path and attaches them to
193 * @param {string} path The path to get a list of items in
194 * @param {string} name The name to select
195 * @param {goog.ui.Menu} The menu to attach items to
197 armadillo
.PathControl
.prototype.fetchMenuContents_
= function(path
, name
, menu
) {
198 var callback
= function(e
) {
199 var data
= e
.target
.getResponseJson();
201 app
.showError(data
['message']);
205 // If this is the root path element, make sure the root is accessible for
207 goog
.array
.insertAt(data
, '/', 0);
209 goog
.array
.forEach(data
, function (caption
) {
210 // It only makes sense to be able to move into directories.
211 if (!app
.isDirectory(caption
)) {
214 var item
= new goog
.ui
.MenuItem(caption
);
215 item
.setValue(app
.joinPath(path
, name
, caption
));
217 if (caption
== name
) {
218 menu
.setHighlighted(item
);
222 app
.sendRequest('list', {'path' : app
.joinPath(path
, name
)}, callback
);
226 * Handler for changing a component of the control.
229 armadillo
.PathControl
.prototype.componentChanged_
= function(e
) {
230 this.path_
= e
.target
.getValue();
231 this.removeChildren(true);
232 this.decorateInternal(this.element_
);
236 * Handler for changing the editable name component.
239 armadillo
.PathControl
.prototype.nameChanged_
= function(e
) {
240 // TODO: assert(this.editableLastComponent_)
241 this.name_
= e
.target
.value
;
247 * Renderer for the Name Control of the Path Control
250 armadillo
.PathControl
.NameControlRenderer_
= function() {
251 goog
.ui
.ControlRenderer
.call(this);
253 goog
.inherits(armadillo
.PathControl
.NameControlRenderer_
, goog
.ui
.ControlRenderer
);
255 armadillo
.PathControl
.NameControlRenderer_
.prototype.createDom
= function(control
) {
256 var content
= control
.getContent();
257 if (content
instanceof HTMLElement
) {
260 return armadillo
.PathControl
.NameControlRenderer_
.superClass_
.createDom
.call(this, control
);