Fix all the path joining nastiness by writing a smart helper armadillo.App.joinPath().
[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.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');
18
19 /**
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
24 * @constructor
25 */
26 armadillo.PathControl = function(path, editLastComponent, opt_domHelper) {
27 goog.ui.Component.call(this, opt_domHelper);
28
29 /**
30 * Full path of the control.
31 * @type {string}
32 */
33 this.path_ = null;
34
35 /**
36 * The name of the file at the |path_|.
37 * @type {string}
38 */
39 this.name_ = null;
40
41 this.setPath(path);
42
43 /**
44 * Whether or not the last component is editable.
45 * @type {bool}
46 */
47 this.editableLastComponent_ = editLastComponent;
48
49 /**
50 * List of path components
51 * @type {Array}
52 */
53 this.components_ = new Array();
54 };
55 goog.inherits(armadillo.PathControl, goog.ui.Component);
56
57 /**
58 * Disposer
59 * @protected
60 */
61 armadillo.PathControl.prototype.disposeInternal = function() {
62 armadillo.PathControl.superClass_.disposeInternal.call(this);
63 this.components_ = null;
64 };
65
66 /**
67 * Sets the path.
68 * @param {string} path
69 */
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_);
74 };
75
76 /**
77 * Creates a new path control object.
78 */
79 armadillo.PathControl.prototype.createDom = function() {
80 this.decorateInternal(this.dom_.createElement('div'));
81 };
82
83 /**
84 * @inheritDoc
85 */
86 armadillo.PathControl.prototype.canDecorate = function() {
87 return true;
88 };
89
90 /**
91 * Decorates the given element into a path control.
92 * @param {Element} element
93 */
94 armadillo.PathControl.prototype.decorateInternal = function(element) {
95 this.element_ = element;
96 var components = this.path_.split('/');
97
98 // If this is an item that lives at the root, generate a special node for
99 // moving between items at the top level.
100 components[0] = '/';
101
102 // If the last component is emtpy, do not use it because it means a directory
103 // is being moved.
104 if (components[components.length - 1] == '') {
105 goog.array.removeAt(components, components.length - 1);
106 }
107
108 var path = '';
109 goog.array.forEach(components, function (part, i) {
110 this.addChild(this.createComponentNode_(path, part), true);
111 path = app.joinPath(path, part);
112 }, this);
113
114 if (this.editableLastComponent_) {
115 var input = new goog.ui.Control(this.dom_.createDom('input'));
116 input.getElement().value = this.name_;
117 this.addChild(input, true);
118 } else {
119 var label = new goog.ui.Control(this.name_);
120 this.addChild(label, true);
121 goog.dom.classes.add(label.getElement(), 'goog-inline-block');
122 }
123 };
124
125 /**
126 * Creates a node for a single path component.
127 * @param {string} path The path up to this point.
128 * @param {string} name The current component after |path|.
129 */
130 armadillo.PathControl.prototype.createComponentNode_ = function(path, name) {
131 var menu = new goog.ui.FilteredMenu();
132 menu.setFilterLabel(name);
133 menu.setAllowMultiple(false);
134 menu.setOpenFollowsHighlight(true);
135 goog.events.listen(menu, goog.ui.Component.EventType.ACTION,
136 this.componentChanged_, false, this);
137 this.fetchMenuContents_(path, name, menu);
138
139 var button = new goog.ui.MenuButton(name, menu, null, this.dom_);
140 button.setFocusablePopupMenu(true);
141 button.setScrollOnOverflow(true);
142 button.setVisible(true);
143 return button;
144 };
145
146 /**
147 * Queries the back-end for all the items at a given path and attaches them to
148 * the given menu.
149 * @param {string} path The path to get a list of items in
150 * @param {string} name The name to select
151 * @param {goog.ui.Menu} The menu to attach items to
152 */
153 armadillo.PathControl.prototype.fetchMenuContents_ = function(path, name, menu) {
154 var callback = function(e) {
155 var data = e.target.getResponseJson();
156 if (data['error']) {
157 app.showError(data['message']);
158 return;
159 }
160 if (path == '') {
161 // If this is the root path element, make sure the root is accessible for
162 // moving items.
163 goog.array.insertAt(data, '/', 0);
164 }
165 goog.array.forEach(data, function (caption) {
166 // It only makes sense to be able to move into directories.
167 if (!app.isDirectory(caption)) {
168 return;
169 }
170 var item = new goog.ui.MenuItem(caption);
171 item.setValue(app.joinPath(path, name, caption));
172 menu.addItem(item);
173 if (caption == name) {
174 menu.setHighlighted(item);
175 }
176 });
177 };
178 app.sendRequest('list', {'path' : app.joinPath(path, name)}, callback);
179 };
180
181 /**
182 * Handler for changing a component of the control.
183 * @param {Event} e
184 */
185 armadillo.PathControl.prototype.componentChanged_ = function(e) {
186 console.log(e.target.getValue());
187 this.path_ = e.target.getValue();
188 this.removeChildren(true);
189 this.decorateInternal(this.element_);
190 };