2 // Armadillo File Manager
3 // Copyright (c) 2010-2011, 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');
12 goog
.require('goog.array');
15 * Creates a new path editing control for a given path.
16 * @param {string} path The path to create an editor for
17 * @param {bool} editLastComponent Whether the last component should be shown as an edit box
18 * @param {DomHelper} opt_domHelper Optional DOM helper
21 armadillo
.PathControl
= function(path
, editLastComponent
, opt_domHelper
) {
23 * Full path of the control.
29 * The name of the file at the |path_|.
36 * Whether or not the last component is editable.
39 this.editableLastComponent_
= editLastComponent
;
42 * Control UI for the name component of the path.
43 * @type {goog.ui.Control}
45 this.nameControl_
= null;
50 * @param {string} path
52 armadillo
.PathControl
.prototype.setPath
= function(path
) {
53 this.path_
= app
.stripLastPathComponent(path
);
54 this.name_
= path
.substr(this.path_
.length
);
61 armadillo
.PathControl
.prototype.getPath
= function() {
62 return app
.joinPath(this.path_
, this.name_
);
66 * Gets the name control.
67 * @returns {goog.ui.Control}
69 armadillo
.PathControl
.prototype.getNameControl
= function() {
70 return this.nameControl_
;
74 * Creates a new path control object.
76 armadillo
.PathControl
.prototype.createDom
= function() {
77 this.decorateInternal($.createDom('div'));
82 * Decorates the given element into a path control.
83 * @param {Element} element
85 armadillo
.PathControl
.prototype.decorateInternal
= function(element
) {
86 this.element_
= element
;
87 var components
= this.path_
.split('/');
89 // If this is an item that lives at the root, generate a special node for
90 // moving between items at the top level.
93 // If the last component is emtpy, do not use it because it means a directory
95 if (components
[components
.length
- 1] == '') {
96 goog
.array
.removeAt(components
, components
.length
- 1);
100 $.each(components
, function (i
, part
) {
101 this.element_
.append(this.createComponentNode_(path
, part
), true);
102 path
= app
.joinPath(path
, part
);
105 if (this.editableLastComponent_
) {
106 this.nameControl_
= $.createDom('input');
107 this.nameControl_
.attr({
113 this.nameControl_
.bind('change keydown', this.nameChanged_
.bind(this));
115 this.nameControl_
= $.createDom('span').text(this.name_
);
118 this.element_
.append(this.nameControl_
);
124 armadillo
.PathControl
.prototype.enterDocument
= function() {
125 armadillo
.PathControl
.superClass_
.enterDocument
.call(this);
126 this.nameControl_
.getElement().focus();
130 * Creates a node for a single path component.
131 * @param {string} path The path up to this point.
132 * @param {string} name The current component after |path|.
134 armadillo
.PathControl
.prototype.createComponentNode_
= function(path
, name
) {
135 var menu
= $.createDom('select');
136 this.fetchMenuContents_(path
, name
, menu
);
138 var option
= $.createDom('option');
139 option
.text(name
).attr('selected', 'selected');
142 menu
.change(this.componentChanged_
.bind(this));
148 * Queries the back-end for all the items at a given path and attaches them to
150 * @param {string} path The path to get a list of items in
151 * @param {string} name The name to select
152 * @param {goog.ui.Menu} The menu to attach items to
154 armadillo
.PathControl
.prototype.fetchMenuContents_
= function(path
, name
, menu
) {
155 var callback
= function(e
) {
156 var data
= e
.target
.getResponseJson();
158 app
.showError(data
['message']);
162 // If this is the root path element, make sure the root is accessible for
164 goog
.array
.insertAt(data
, '/', 0);
167 $.each(data
, function (i
, caption
) {
168 // It only makes sense to be able to move into directories.
169 if (!app
.isDirectory(caption
)) {
172 var item
= $.createDom('option');
173 item
.val(app
.joinPath(path
, name
, caption
)).text(caption
);
175 if (caption
== name
) {
176 item
.attr('selected', 'selected');
180 app
.sendRequest('list', {'path' : app
.joinPath(path
, name
)}, callback
);
184 * Handler for changing a component of the control.
187 armadillo
.PathControl
.prototype.componentChanged_
= function(e
) {
188 this.path_
= $(e
.target
).val();
189 this.element_
.empty();
190 this.decorateInternal(this.element_
);
194 * Handler for changing the editable name component.
197 armadillo
.PathControl
.prototype.nameChanged_
= function(e
) {
199 // TODO: assert(this.editableLastComponent_)
200 this.name_
= e
.target
.value
;