Debian Codegraph @

closure-library/closure/goog/ui/tree/typeahead.js

raw
1 // Copyright 2007 The Closure Library Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS-IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 /**
16 * @fileoverview Provides the typeahead functionality for the tree class.
17 *
18 */
19
20 goog.provide('goog.ui.tree.TypeAhead');
21 goog.provide('goog.ui.tree.TypeAhead.Offset');
22
23 goog.require('goog.array');
24 goog.require('goog.events.KeyCodes');
25 goog.require('goog.string');
26 goog.require('goog.structs.Trie');
27
28
29
30 /**
31 * Constructs a TypeAhead object.
32 * @constructor
33 * @final
34 */
35 goog.ui.tree.TypeAhead = function() {
36 /**
37 * Map of tree nodes to allow for quick access by characters in the label
38 * text.
39 * @private {goog.structs.Trie<Array<goog.ui.tree.BaseNode>>}
40 */
41 this.nodeMap_ = new goog.structs.Trie();
42
43 /**
44 * Buffer for storing typeahead characters.
45 * @private {string}
46 */
47 this.buffer_ = '';
48
49 /**
50 * Matching labels from the latest typeahead search.
51 * @private {?Array<string>}
52 */
53 this.matchingLabels_ = null;
54
55 /**
56 * Matching nodes from the latest typeahead search. Used when more than
57 * one node is present with the same label text.
58 * @private {?Array<goog.ui.tree.BaseNode>}
59 */
60 this.matchingNodes_ = null;
61
62 /**
63 * Specifies the current index of the label from the latest typeahead search.
64 * @private {number}
65 */
66 this.matchingLabelIndex_ = 0;
67
68 /**
69 * Specifies the index into matching nodes when more than one node is found
70 * with the same label.
71 * @private {number}
72 */
73 this.matchingNodeIndex_ = 0;
74 };
75
76
77 /**
78 * Enum for offset values that are used for ctrl-key navigation among the
79 * multiple matches of a given typeahead buffer.
80 *
81 * @enum {number}
82 */
83 goog.ui.tree.TypeAhead.Offset = {
84 DOWN: 1,
85 UP: -1
86 };
87
88
89 /**
90 * Handles navigation keys.
91 * @param {goog.events.BrowserEvent} e The browser event.
92 * @return {boolean} The handled value.
93 */
94 goog.ui.tree.TypeAhead.prototype.handleNavigation = function(e) {
95 var handled = false;
96
97 switch (e.keyCode) {
98 // Handle ctrl+down, ctrl+up to navigate within typeahead results.
99 case goog.events.KeyCodes.DOWN:
100 case goog.events.KeyCodes.UP:
101 if (e.ctrlKey) {
102 this.jumpTo_(
103 e.keyCode == goog.events.KeyCodes.DOWN ?
104 goog.ui.tree.TypeAhead.Offset.DOWN :
105 goog.ui.tree.TypeAhead.Offset.UP);
106 handled = true;
107 }
108 break;
109
110 // Remove the last typeahead char.
111 case goog.events.KeyCodes.BACKSPACE:
112 var length = this.buffer_.length - 1;
113 handled = true;
114 if (length > 0) {
115 this.buffer_ = this.buffer_.substring(0, length);
116 this.jumpToLabel_(this.buffer_);
117 } else if (length == 0) {
118 // Clear the last character in typeahead.
119 this.buffer_ = '';
120 } else {
121 handled = false;
122 }
123 break;
124
125 // Clear typeahead buffer.
126 case goog.events.KeyCodes.ESC:
127 this.buffer_ = '';
128 handled = true;
129 break;
130 }
131
132 return handled;
133 };
134
135
136 /**
137 * Handles the character presses.
138 * @param {goog.events.BrowserEvent} e The browser event.
139 * Expected event type is goog.events.KeyHandler.EventType.KEY.
140 * @return {boolean} The handled value.
141 */
142 goog.ui.tree.TypeAhead.prototype.handleTypeAheadChar = function(e) {
143 var handled = false;
144
145 if (!e.ctrlKey && !e.altKey) {
146 // Since goog.structs.Trie.getKeys compares characters during
147 // lookup, we should use charCode instead of keyCode where possible.
148 // Convert to lowercase, typeahead is case insensitive.
149 var ch = String.fromCharCode(e.charCode || e.keyCode).toLowerCase();
150 if (goog.string.isUnicodeChar(ch) && (ch != ' ' || this.buffer_)) {
151 this.buffer_ += ch;
152 handled = this.jumpToLabel_(this.buffer_);
153 }
154 }
155
156 return handled;
157 };
158
159
160 /**
161 * Adds or updates the given node in the nodemap. The label text is used as a
162 * key and the node id is used as a value. In the case that the key already
163 * exists, such as when more than one node exists with the same label, then this
164 * function creates an array to hold the multiple nodes.
165 * @param {goog.ui.tree.BaseNode} node Node to be added or updated.
166 */
167 goog.ui.tree.TypeAhead.prototype.setNodeInMap = function(node) {
168 var labelText = node.getText();
169 if (labelText &&
170 !goog.string.isEmptyOrWhitespace(goog.string.makeSafe(labelText))) {
171 // Typeahead is case insensitive, convert to lowercase.
172 labelText = labelText.toLowerCase();
173
174 var previousValue = this.nodeMap_.get(labelText);
175 if (previousValue) {
176 // Found a previously created array, add the given node.
177 previousValue.push(node);
178 } else {
179 // Create a new array and set the array as value.
180 var nodeList = [node];
181 this.nodeMap_.set(labelText, nodeList);
182 }
183 }
184 };
185
186
187 /**
188 * Removes the given node from the nodemap.
189 * @param {goog.ui.tree.BaseNode} node Node to be removed.
190 */
191 goog.ui.tree.TypeAhead.prototype.removeNodeFromMap = function(node) {
192 var labelText = node.getText();
193 if (labelText &&
194 !goog.string.isEmptyOrWhitespace(goog.string.makeSafe(labelText))) {
195 labelText = labelText.toLowerCase();
196
197 var nodeList = this.nodeMap_.get(labelText);
198 if (nodeList) {
199 // Remove the node's descendants from the nodemap.
200 var count = node.getChildCount();
201 for (var i = 0; i < count; i++) {
202 this.removeNodeFromMap(node.getChildAt(i));
203 }
204 // Remove the node from the array.
205 goog.array.remove(nodeList, node);
206 if (!nodeList.length) {
207 this.nodeMap_.remove(labelText);
208 }
209 }
210 }
211 };
212
213
214 /**
215 * Select the first matching node for the given typeahead.
216 * @param {string} typeAhead Typeahead characters to match.
217 * @return {boolean} True iff a node is found.
218 * @private
219 */
220 goog.ui.tree.TypeAhead.prototype.jumpToLabel_ = function(typeAhead) {
221 var handled = false;
222 var labels = this.nodeMap_.getKeys(typeAhead);
223
224 // Make sure we have at least one matching label.
225 if (labels && labels.length) {
226 this.matchingNodeIndex_ = 0;
227 this.matchingLabelIndex_ = 0;
228
229 var nodes = this.nodeMap_.get(labels[0]);
230 if ((handled = this.selectMatchingNode_(nodes))) {
231 this.matchingLabels_ = labels;
232 }
233 }
234
235 // TODO(user): beep when no node is found
236 return handled;
237 };
238
239
240 /**
241 * Select the next or previous node based on the offset.
242 * @param {goog.ui.tree.TypeAhead.Offset} offset DOWN or UP.
243 * @return {boolean} Whether a node is found.
244 * @private
245 */
246 goog.ui.tree.TypeAhead.prototype.jumpTo_ = function(offset) {
247 var handled = false;
248 var labels = this.matchingLabels_;
249
250 if (labels) {
251 var nodes = null;
252 var nodeIndexOutOfRange = false;
253
254 // Navigate within the nodes array.
255 if (this.matchingNodes_) {
256 var newNodeIndex = this.matchingNodeIndex_ + offset;
257 if (newNodeIndex >= 0 && newNodeIndex < this.matchingNodes_.length) {
258 this.matchingNodeIndex_ = newNodeIndex;
259 nodes = this.matchingNodes_;
260 } else {
261 nodeIndexOutOfRange = true;
262 }
263 }
264
265 // Navigate to the next or previous label.
266 if (!nodes) {
267 var newLabelIndex = this.matchingLabelIndex_ + offset;
268 if (newLabelIndex >= 0 && newLabelIndex < labels.length) {
269 this.matchingLabelIndex_ = newLabelIndex;
270 }
271
272 if (labels.length > this.matchingLabelIndex_) {
273 nodes = this.nodeMap_.get(labels[this.matchingLabelIndex_]);
274 }
275
276 // Handle the case where we are moving beyond the available nodes,
277 // while going UP select the last item of multiple nodes with same label
278 // and while going DOWN select the first item of next set of nodes
279 if (nodes && nodes.length && nodeIndexOutOfRange) {
280 this.matchingNodeIndex_ =
281 (offset == goog.ui.tree.TypeAhead.Offset.UP) ? nodes.length - 1 : 0;
282 }
283 }
284
285 if ((handled = this.selectMatchingNode_(nodes))) {
286 this.matchingLabels_ = labels;
287 }
288 }
289
290 // TODO(user): beep when no node is found
291 return handled;
292 };
293
294
295 /**
296 * Given a nodes array reveals and selects the node while using node index.
297 * @param {Array<goog.ui.tree.BaseNode>|undefined} nodes Nodes array to select
298 * the node from.
299 * @return {boolean} Whether a matching node was found.
300 * @private
301 */
302 goog.ui.tree.TypeAhead.prototype.selectMatchingNode_ = function(nodes) {
303 var node;
304
305 if (nodes) {
306 // Find the matching node.
307 if (this.matchingNodeIndex_ < nodes.length) {
308 node = nodes[this.matchingNodeIndex_];
309 this.matchingNodes_ = nodes;
310 }
311
312 if (node) {
313 node.reveal();
314 node.select();
315 }
316 }
317
318 return !!node;
319 };
320
321
322 /**
323 * Clears the typeahead buffer.
324 */
325 goog.ui.tree.TypeAhead.prototype.clear = function() {
326 this.buffer_ = '';
327 };