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.array');
13 goog
.require('goog.ui.Component');
14 goog
.require('goog.ui.FilteredMenu');
15 goog
.require('goog.ui.LabelInput');
16 goog
.require('goog.ui.MenuButton');
17 goog
.require('goog.ui.MenuItem');
20 * Creates a new path editing control for a given path.
21 * @param {string} path The path to create an editor for
22 * @param {bool} editLastComponent Whether the last component should be shown as an edit box
23 * @param {DomHelper} opt_domHelper Optional DOM helper
26 armadillo
.PathControl
= function(path
, editLastComponent
, opt_domHelper
) {
27 goog
.ui
.Component
.call(this, opt_domHelper
);
30 * Full path of the control.
36 * The name of the file at the |path_|.
44 * Whether or not the last component is editable.
47 this.editableLastComponent_
= editLastComponent
;
50 * List of path components
53 this.components_
= new Array();
55 goog
.inherits(armadillo
.PathControl
, goog
.ui
.Component
);
61 armadillo
.PathControl
.prototype.disposeInternal
= function() {
62 armadillo
.PathControl
.superClass_
.disposeInternal
.call(this);
63 this.components_
= null;
68 * @param {string} path
70 armadillo
.PathControl
.prototype.setPath
= function(path
) {
71 this.path_
= app
.stripLastPathComponent(path
);
72 this.name_
= path
.substr(this.path_
.length
);
73 console
.log(this.path_
+ ' = ' + this.name_
);
77 * Creates a new path control object.
79 armadillo
.PathControl
.prototype.createDom
= function() {
80 this.decorateInternal(this.dom_
.createElement('div'));
86 armadillo
.PathControl
.prototype.canDecorate
= function() {
91 * Decorates the given element into a path control.
92 * @param {Element} element
94 armadillo
.PathControl
.prototype.decorateInternal
= function(element
) {
95 this.element_
= element
;
96 var components
= this.path_
.split('/');
98 if (components
.length
== 2) {
99 // If this is an item that lives at the root, generate a special node for
100 // moving between items at the top level.
103 // Otherwise, just remove it as the first node will list all items at the
105 goog
.array
.removeAt(components
, 0);
108 // If the last component is emtpy, do not use it because it means a directory
110 if (components
[components
.length
- 1] == '') {
111 goog
.array
.removeAt(components
, components
.length
- 1);
115 goog
.array
.forEach(components
, function (part
, i
) {
116 this.addChild(this.createComponentNode_(path
, part
), true);
120 if (this.editableLastComponent_
) {
121 var input
= new goog
.ui
.Control(this.dom_
.createDom('input'));
122 input
.getElement().value
= this.name_
;
123 this.addChild(input
, true);
125 var label
= new goog
.ui
.Control(this.name_
);
126 this.addChild(label
, true);
127 goog
.dom
.classes
.add(label
.getElement(), 'goog-inline-block');
132 * Creates a node for a single path component.
133 * @param {string} path The path up to this point.
134 * @param {string} name The current component after |path|.
136 armadillo
.PathControl
.prototype.createComponentNode_
= function(path
, name
) {
137 var menu
= new goog
.ui
.FilteredMenu();
138 menu
.setFilterLabel(name
);
139 menu
.setAllowMultiple(false);
140 menu
.setOpenFollowsHighlight(true);
141 goog
.events
.listen(menu
, goog
.ui
.Component
.EventType
.ACTION
,
142 this.componentChanged_
, false, this);
143 this.fetchMenuContents_(path
, name
, menu
);
145 var button
= new goog
.ui
.MenuButton(name
, menu
, null, this.dom_
);
146 button
.setFocusablePopupMenu(true);
147 button
.setScrollOnOverflow(true);
148 button
.setVisible(true);
153 * Queries the back-end for all the items at a given path and attaches them to
155 * @param {string} path The path to get a list of items in
156 * @param {string} name The name to select
157 * @param {goog.ui.Menu} The menu to attach items to
159 armadillo
.PathControl
.prototype.fetchMenuContents_
= function(path
, name
, menu
) {
160 var callback
= function(e
) {
161 var data
= e
.target
.getResponseJson();
163 app
.showError(data
['message']);
167 // If this is the root path element, make sure the root is accessible for
169 goog
.array
.insertAt(data
, '/', 0);
171 goog
.array
.forEach(data
, function (caption
) {
172 // It only makes sense to be able to move into directories.
173 if (!app
.isDirectory(caption
)) {
176 var item
= new goog
.ui
.MenuItem(caption
);
177 item
.setValue(path
+ caption
);
179 if (caption
== name
) {
180 menu
.setHighlighted(item
);
184 app
.sendRequest('list', {'path':path
}, callback
);
188 * Handler for changing a component of the control.
191 armadillo
.PathControl
.prototype.componentChanged_
= function(e
) {
192 console
.log(e
.target
.getValue());
193 this.path_
= e
.target
.getValue();
194 this.removeChildren(true);
195 this.decorateInternal(this.element_
);