Set some properties on the menu button to make the popup menu work better.
[armadillo.git] / web_frontend / path_control.js
1 //
2 // Armadillo File Manager
3 // Copyright (c) 2010, Robert Sesek <http://www.bluestatic.org>
4 //
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.
8 //
9
10 goog.provide('armadillo.PathControl');
11
12 goog.require('goog.ui.Component');
13 goog.require('goog.ui.FilteredMenu');
14 goog.require('goog.ui.LabelInput');
15 goog.require('goog.ui.MenuButton');
16 goog.require('goog.ui.MenuItem');
17
18 /**
19 * Creates a new path editing control for a given path.
20 * @param {string} path The path to create an editor for
21 * @param {bool} editLastComponent Whether the last component should be shown as an edit box
22 * @param {DomHelper} opt_domHelper Optional DOM helper
23 * @constructor
24 */
25 armadillo.PathControl = function(path, editLastComponent, opt_domHelper) {
26 goog.ui.Component.call(this, opt_domHelper);
27
28 /**
29 * Full path of the control.
30 * @type {string}
31 */
32 this.path_ = path;
33
34 /**
35 * Whether or not the last component is editable.
36 * @type {bool}
37 */
38 this.editableLastComponent_ = editLastComponent;
39
40 /**
41 * List of path components
42 * @type {Array}
43 */
44 this.components_ = new Array();
45 };
46 goog.inherits(armadillo.PathControl, goog.ui.Component);
47
48 /**
49 * Disposer
50 * @protected
51 */
52 armadillo.PathControl.prototype.disposeInternal = function() {
53 armadillo.PathControl.superClass_.disposeInternal.call(this);
54 this.components_ = null;
55 };
56
57 /**
58 * Creates a new path control object.
59 */
60 armadillo.PathControl.prototype.createDom = function() {
61 this.decorateInternal(this.dom_.createElement('div'));
62 };
63
64 /**
65 * @inheritDoc
66 */
67 armadillo.PathControl.prototype.canDecorate = function() {
68 return true;
69 };
70
71 /**
72 * Decorates the given element into a path control.
73 * @param {Element} element
74 */
75 armadillo.PathControl.prototype.decorateInternal = function(element) {
76 this.element_ = element;
77 var components = this.path_.split('/');
78 delete components[0]; // Don't create an empty item.
79
80 var path = '';
81 goog.array.forEach(components, function (part, i) {
82 if (i != components.length - 1) {
83 this.addChild(this.createComponentNode_('/' + path, part), true);
84 } else {
85 var input = new goog.ui.LabelInput(part, this.dom_);
86 this.addChild(input, true);
87 input.setEnabled(this.editableLastComponent_);
88 input.setValue(part);
89 }
90 path += '/' + part;
91 }, this);
92 };
93
94 /**
95 * Creates a node for a single path component.
96 * @param {string} path The path up to this point.
97 * @param {string} name The current component after |path|.
98 */
99 armadillo.PathControl.prototype.createComponentNode_ = function(path, name) {
100 var menu = new goog.ui.FilteredMenu();
101 menu.setFilterLabel(name);
102 menu.setAllowMultiple(false);
103 this.fetchMenuContents_(path, name, menu);
104
105 var button = new goog.ui.MenuButton(name, menu, null, this.dom_);
106 button.setFocusablePopupMenu(true);
107 button.setScrollOnOverflow(true);
108 button.setVisible(true);
109 return button;
110 };
111
112 /**
113 * Queries the back-end for all the items at a given path and attaches them to
114 * the given menu.
115 * @param {string} path The path to get a list of items in
116 * @param {string} name The name to select
117 * @param {goog.ui.Menu} The menu to attach items to
118 */
119 armadillo.PathControl.prototype.fetchMenuContents_ = function(path, name, menu) {
120 var callback = function(e) {
121 var data = e.target.getResponseJson();
122 if (data['error']) {
123 app.showError(data['message']);
124 return;
125 }
126 goog.array.forEach(data, function (caption) {
127 var item = new goog.ui.MenuItem(caption);
128 menu.addItem(item);
129 if (caption == name) {
130 menu.setHighlighted(item);
131 }
132 });
133 };
134 app.sendRequest('list', {'path':path}, callback);
135 };
136