var fuzzyMode = (function() { var fuzzyBox = null; // the dialog instance for this window var completers = { }; function getCompleter(name) { if (!(name in completers)) completers[name] = new BackgroundCompleter(name); return completers[name]; } /** Trigger the fuzzy mode dialog */ function start(name, refreshInterval) { var completer = getCompleter(name); if (!fuzzyBox) fuzzyBox = new FuzzyBox(10); completer.refresh(); fuzzyBox.setCompleter(completer); fuzzyBox.setRefreshInterval(refreshInterval); fuzzyBox.show(); } /** User interface for fuzzy completion */ var FuzzyBox = function(maxResults) { this.prompt = '>'; this.maxResults = maxResults; this.refreshInterval = 0; this.initDom(); } FuzzyBox.prototype = { setCompleter: function(completer) { this.completer = completer; this.reset(); }, setRefreshInterval: function(refreshInterval) { this.refreshInterval = refreshInterval; }, show: function() { this.box.style.display = "block"; this.input.focus(); handlerStack.push({ keydown: this.onKeydown.bind(this) }); }, hide: function() { this.box.style.display = "none"; this.completionList.style.display = "none"; this.input.blur(); handlerStack.pop(); }, reset: function() { this.input.value = ""; this.updateTimer = null; this.completions = []; this.selection = 0; this.update(true); }, updateSelection: function() { if (this.completions.length > 0) this.selection = Math.min(this.selection, this.completions.length - 1); for (var i = 0; i < this.completionList.children.length; ++i) this.completionList.children[i].className = (i == this.selection) ? "selected" : ""; }, onKeydown: function(event) { var self = this; var keyChar = getKeyChar(event); if (isEscape(event)) { this.hide(); } // move selection with Up/Down, Tab/Shift-Tab, Ctrl-k/Ctrl-j else if (keyChar === "up" || (event.keyCode == keyCodes.tab && event.shiftKey) || (keyChar === "k" && event.ctrlKey)) { if (this.selection > 0) this.selection -= 1; this.updateSelection(); } else if (keyChar === "down" || (event.keyCode == keyCodes.tab && !event.shiftKey) || (keyChar === "j" && isPrimaryModifierKey(event))) { if (this.selection < this.completions.length - 1) this.selection += 1; this.updateSelection(); } // refresh with F5 else if (keyChar == "f5") { this.completer.refresh(); this.update(true); // force immediate update } else if (event.keyCode == keyCodes.enter) { this.update(true, function() { // Shift+Enter will open the result in a new tab instead of the current tab. var openInNewTab = (event.shiftKey || isPrimaryModifierKey(event)); self.completions[self.selection].performAction(openInNewTab); self.hide(); }); } else { return true; // pass through } // it seems like we have to manually supress the event here and still return true... event.stopPropagation(); event.preventDefault(); return true; }, updateCompletions: function(callback) { var self = this; query = this.input.value.replace(/^\s*/, ""); this.completer.filter(query, this.maxResults, function(completions) { self.completions = completions; // update completion list with the new data self.completionList.innerHTML = completions.map(function(completion) { return "