]>
src.bluestatic.org Git - armadillo.git/blob - frontend/path_control.js
2 // Armadillo File Manager
3 // Copyright (c) 2010-2012, 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 $.namespace('armadillo.PathControl');
13 * Creates a new path editing control for a given path.
14 * @param {string} path The path to create an editor for
15 * @param {bool} editLastComponent Whether the last component should be shown as an edit box
18 armadillo
.PathControl = function(path
, editLastComponent
) {
20 * Full path of the control.
26 * The name of the file at the |path_|.
33 * Whether or not the last component is editable.
36 this.editableLastComponent_
= editLastComponent
;
39 * Control UI for the name component of the path.
42 this.nameControl_
= null;
47 * @param {string} path
49 armadillo
.PathControl
.prototype.setPath = function(path
) {
50 this.path_
= app
.stripLastPathComponent(path
);
51 this.name_
= path
.substr(this.path_
.length
);
58 armadillo
.PathControl
.prototype.getPath = function() {
59 return app
.joinPath(this.path_
, this.name_
);
63 * Gets the name control.
66 armadillo
.PathControl
.prototype.getNameControl = function() {
67 return this.nameControl_
;
71 * Creates a new path control object.
74 armadillo
.PathControl
.prototype.createDom = function() {
75 this.element_
= $.createDom('div');
76 this.createDom_(this.element_
);
81 * Decorates the given element into a path control.
82 * @param {Element} element
84 armadillo
.PathControl
.prototype.createDom_ = function(element
) {
85 var components
= this.path_
.split('/');
87 // If this is an item that lives at the root, generate a special node for
88 // moving between items at the top level.
91 // If the last component is emtpy, do not use it because it means a directory
93 if (components
[components
.length
- 1] == '') {
94 components
.splice(-1);
98 $.each(components
, function (i
, part
) {
99 element
.append(this.createComponentNode_(path
, part
));
100 path
= app
.joinPath(path
, part
);
103 if (this.editableLastComponent_
) {
104 this.nameControl_
= $.createDom('input');
105 this.nameControl_
.attr({
111 this.nameControl_
.bind('change keydown', this.nameChanged_
.bind(this));
113 this.nameControl_
= $.createDom('span').text(this.name_
);
116 element
.append(this.nameControl_
);
122 armadillo
.PathControl
.prototype.enterDocument = function() {
123 armadillo
.PathControl
.superClass_
.enterDocument
.call(this);
124 this.nameControl_
.getElement().focus();
128 * Creates a node for a single path component.
129 * @param {string} path The path up to this point.
130 * @param {string} name The current component after |path|.
132 armadillo
.PathControl
.prototype.createComponentNode_ = function(path
, name
) {
133 var menu
= $.createDom('select');
134 this.fetchMenuContents_(path
, name
, menu
);
136 var option
= $.createDom('option');
137 option
.text('Loading...').attr('selected', 'selected');
140 menu
.change(this.componentChanged_
.bind(this));
146 * Queries the back-end for all the items at a given path and attaches them to
148 * @param {string} path The path to get a list of items in
149 * @param {string} name The name to select
150 * @param {Element} The menu to attach items to
152 armadillo
.PathControl
.prototype.fetchMenuContents_ = function(path
, name
, menu
) {
153 var fullPath
= this.path_
;
154 var callback = function(data
, status
, xhr
) {
155 // Create an empty node for the current directory.
159 $.each(data
, function (i
, caption
) {
160 // It only makes sense to be able to move into directories.
161 if (!app
.isDirectory(caption
)) {
164 var item
= $.createDom('option');
165 var componentPath
= app
.joinPath(path
, name
, caption
);
166 item
.val(componentPath
).text(caption
);
168 if (fullPath
.substr(0, componentPath
.length
) == componentPath
) {
169 item
.attr('selected', 'selected');
173 app
.sendRequest('list', {'path' : app
.joinPath(path
, name
)}, callback
);
177 * Handler for changing a component of the control.
180 armadillo
.PathControl
.prototype.componentChanged_ = function(e
) {
181 this.path_
= $(e
.target
).val();
182 this.element_
.empty();
183 this.createDom_(this.element_
);
187 * Handler for changing the editable name component.
190 armadillo
.PathControl
.prototype.nameChanged_ = function(e
) {
192 // TODO: assert(this.editableLastComponent_)
193 this.name_
= e
.target
.value
;