First pass at the PathControl.
[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.MenuButton');
15 goog.require('goog.ui.MenuItem');
16
17 /**
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
22 * @constructor
23 */
24 armadillo.PathControl = function(path, editLastComponent, opt_domHelper) {
25 goog.ui.Component.call(this, opt_domHelper);
26
27 /**
28 * Full path of the control.
29 * @type {string}
30 */
31 this.path_ = path;
32
33 /**
34 * Whether or not the last component is editable.
35 * @type {bool}
36 */
37 this.editableLastComponent_ = editLastComponent;
38
39 /**
40 * List of path components
41 * @type {Array}
42 */
43 this.components_ = new Array();
44 };
45 goog.inherits(armadillo.PathControl, goog.ui.Component);
46
47 /**
48 * Disposer
49 * @protected
50 */
51 armadillo.PathControl.prototype.disposeInternal = function() {
52 armadillo.PathControl.superClass_.disposeInternal.call(this);
53 this.components_ = null;
54 };
55
56 /**
57 * Creates a new path control object.
58 */
59 armadillo.PathControl.prototype.createDom = function() {
60 this.decorateInternal(this.dom_.createElement('div'));
61 };
62
63 /**
64 * @inheritDoc
65 */
66 armadillo.PathControl.prototype.canDecorate = function() {
67 return true;
68 };
69
70 /**
71 * Decorates the given element into a path control.
72 * @param {Element} element
73 */
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.
78
79 var path = '';
80 goog.array.forEach(components, function (part) {
81 this.addChild(this.createComponentNode_('/' + path, part), true);
82 path += '/' + part;
83 }, this);
84 };
85
86 /**
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|.
90 */
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);
96
97 var button = new goog.ui.MenuButton(name, menu, null, this.dom_);
98 button.setVisible(true);
99 return button;
100 };
101
102 /**
103 * Queries the back-end for all the items at a given path and attaches them to
104 * the given menu.
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
108 */
109 armadillo.PathControl.prototype.fetchMenuContents_ = function(path, name, menu) {
110 var callback = function(e) {
111 var data = e.target.getResponseJson();
112 if (data['error']) {
113 app.showError(data['message']);
114 return;
115 }
116 goog.array.forEach(data, function (caption) {
117 var item = new goog.ui.MenuItem(caption);
118 menu.addItem(item);
119 if (caption == name) {
120 menu.setHighlighted(item);
121 }
122 });
123 };
124 app.sendRequest('list', {'path':path}, callback);
125 };
126