From f42cf13daaa7ec39d2fbba905dcec9d4777af306 Mon Sep 17 00:00:00 2001 From: Phil Crosby Date: Sat, 5 May 2012 17:39:40 -0700 Subject: move the async completer next to the classes which use it. --- lib/completion.js | 95 +++++++++++++++++++++++++++---------------------------- 1 file changed, 47 insertions(+), 48 deletions(-) (limited to 'lib') diff --git a/lib/completion.js b/lib/completion.js index 0e6b1b8d..268a33d8 100644 --- a/lib/completion.js +++ b/lib/completion.js @@ -41,6 +41,53 @@ var completion = (function() { this.build = buildFunction; } + /** A simple completer that suggests to open the input string as an URL or to trigger a web search for the + * given term, depending on whether it thinks the input is a URL or not. */ + var SmartCompletionSource = Class.extend({ + init: function(commands) { + this.commands = commands || {}; + this.commandKeys = Object.keys(commands); + }, + + refresh: function() { }, + + /** Returns the suggestions matching the user-defined commands */ + getCommandSuggestions: function(query, suggestions) { + return this.commandKeys.filter(function(cmd) { return query.indexOf(cmd) == 0 }).map(function(cmd) { + var term = query.slice(cmd.length); + var desc = this.commands[cmd][0]; + var pattern = this.commands[cmd][1]; + var url = (typeof pattern == "function") ? pattern(term) : pattern.replace(/%s/g, term); + + // this will appear even before the URL/search suggestion + return new LazyCompletionResult(-2, function() { + return { + html: createCompletionHtml(desc, term), + action: { functionName: "navigateToUrl", args: [utils.createFullUrl(url)] }, + }}) + }.proxy(this)); + }, + + /** Checks if the input is a URL. If yes, returns a suggestion to visit it. If no, returns a suggestion + * to start a web search. */ + getUrlOrSearchSuggestion: function(query, suggestions) { + // trim query + query = query.replace(/^\s+|\s+$/g, ''); + var isUrl = utils.isUrl(query); + return new LazyCompletionResult(-1, function() { + return { + html: createCompletionHtml(isUrl ? "goto" : "search", query), + action: { functionName: "navigateToUrl", + args: isUrl ? [utils.createFullUrl(query)] : [utils.createSearchUrl(query)] } + }}); + }, + + filter: function(query, callback) { + suggestions = this.getCommandSuggestions(query); + suggestions.push(this.getUrlOrSearchSuggestion(query)); + callback(suggestions); + } + }); /** Helper class to construct fuzzy completers for asynchronous data sources like history or bookmark * matchers. */ @@ -107,54 +154,6 @@ var completion = (function() { } }); - /** A simple completer that suggests to open the input string as an URL or to trigger a web search for the - * given term, depending on whether it thinks the input is a URL or not. */ - var SmartCompletionSource = Class.extend({ - init: function(commands) { - this.commands = commands || {}; - this.commandKeys = Object.keys(commands); - }, - - refresh: function() { }, - - /** Returns the suggestions matching the user-defined commands */ - getCommandSuggestions: function(query, suggestions) { - return this.commandKeys.filter(function(cmd) { return query.indexOf(cmd) == 0 }).map(function(cmd) { - var term = query.slice(cmd.length); - var desc = this.commands[cmd][0]; - var pattern = this.commands[cmd][1]; - var url = (typeof pattern == "function") ? pattern(term) : pattern.replace(/%s/g, term); - - // this will appear even before the URL/search suggestion - return new LazyCompletionResult(-2, function() { - return { - html: createCompletionHtml(desc, term), - action: { functionName: "navigateToUrl", args: [utils.createFullUrl(url)] }, - }}) - }.proxy(this)); - }, - - /** Checks if the input is a URL. If yes, returns a suggestion to visit it. If no, returns a suggestion - * to start a web search. */ - getUrlOrSearchSuggestion: function(query, suggestions) { - // trim query - query = query.replace(/^\s+|\s+$/g, ''); - var isUrl = utils.isUrl(query); - return new LazyCompletionResult(-1, function() { - return { - html: createCompletionHtml(isUrl ? "goto" : "search", query), - action: { functionName: "navigateToUrl", - args: isUrl ? [utils.createFullUrl(query)] : [utils.createSearchUrl(query)] } - }}); - }, - - filter: function(query, callback) { - suggestions = this.getCommandSuggestions(query); - suggestions.push(this.getUrlOrSearchSuggestion(query)); - callback(suggestions); - } - }); - /** A fuzzy bookmark completer */ var FuzzyBookmarkCompletionSource = Class.extend({ init: function() { this.asyncCompleter = new AsyncCompleter(); }, -- cgit v1.2.3