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');
12 goog
.require('goog.ui.Component');
13 goog
.require('goog.ui.FilteredMenu');
14 goog
.require('goog.ui.MenuButton');
15 goog
.require('goog.ui.MenuItem');
18 * Creates a new path editing control for a given path.
19 * @param {string} path The path to create an editor for
20 * @param {bool} editLastComponent Whether the last component should be shown as an edit box
21 * @param {DomHelper} opt_domHelper Optional DOM helper
24 armadillo
.PathControl
= function(path
, editLastComponent
, opt_domHelper
) {
25 goog
.ui
.Component
.call(this, opt_domHelper
);
28 * Full path of the control.
34 * Whether or not the last component is editable.
37 this.editableLastComponent_
= editLastComponent
;
40 * List of path components
43 this.components_
= new Array();
45 goog
.inherits(armadillo
.PathControl
, goog
.ui
.Component
);
51 armadillo
.PathControl
.prototype.disposeInternal
= function() {
52 armadillo
.PathControl
.superClass_
.disposeInternal
.call(this);
53 this.components_
= null;
57 * Creates a new path control object.
59 armadillo
.PathControl
.prototype.createDom
= function() {
60 this.decorateInternal(this.dom_
.createElement('div'));
66 armadillo
.PathControl
.prototype.canDecorate
= function() {
71 * Decorates the given element into a path control.
72 * @param {Element} element
74 armadillo
.PathControl
.prototype.decorateInternal
= function(element
) {
75 this.element_
= element
;
76 var components
= this.path_
.split('/');
77 delete components
[0]; // Don't create an empty item.
80 goog
.array
.forEach(components
, function (part
) {
81 this.addChild(this.createComponentNode_('/' + path
, part
), true);
87 * Creates a node for a single path component.
88 * @param {string} path The path up to this point.
89 * @param {string} name The current component after |path|.
91 armadillo
.PathControl
.prototype.createComponentNode_
= function(path
, name
) {
92 var menu
= new goog
.ui
.FilteredMenu();
93 menu
.setFilterLabel(name
);
94 menu
.setAllowMultiple(false);
95 this.fetchMenuContents_(path
, name
, menu
);
97 var button
= new goog
.ui
.MenuButton(name
, menu
, null, this.dom_
);
98 button
.setVisible(true);
103 * Queries the back-end for all the items at a given path and attaches them to
105 * @param {string} path The path to get a list of items in
106 * @param {string} name The name to select
107 * @param {goog.ui.Menu} The menu to attach items to
109 armadillo
.PathControl
.prototype.fetchMenuContents_
= function(path
, name
, menu
) {
110 var callback
= function(e
) {
111 var data
= e
.target
.getResponseJson();
113 app
.showError(data
['message']);
116 goog
.array
.forEach(data
, function (caption
) {
117 var item
= new goog
.ui
.MenuItem(caption
);
119 if (caption
== name
) {
120 menu
.setHighlighted(item
);
124 app
.sendRequest('list', {'path':path
}, callback
);