aboutsummaryrefslogtreecommitdiffstats
path: root/lib/completion.js
diff options
context:
space:
mode:
authorPhil Crosby2012-04-29 22:05:11 -0700
committerPhil Crosby2012-04-29 22:05:11 -0700
commit7b4225050d34dd1a58fe20b782c81c90bcf0d0e9 (patch)
treee98355e8fefa6b3e09e3ada226f30384412522ee /lib/completion.js
parente1caed5b35997302d6b3ac14137f8374216057c6 (diff)
downloadvimium-7b4225050d34dd1a58fe20b782c81c90bcf0d0e9.tar.bz2
Shorten the declaration of the domain completer.
Diffstat (limited to 'lib/completion.js')
-rw-r--r--lib/completion.js119
1 files changed, 58 insertions, 61 deletions
diff --git a/lib/completion.js b/lib/completion.js
index 3209a2f4..c46723c7 100644
--- a/lib/completion.js
+++ b/lib/completion.js
@@ -186,75 +186,72 @@ var completion = (function() {
}
/** A domain completer as it is provided by Chrome's omnibox */
- var DomainCompletionSource = function() {
- this.domains = null;
- }
+ var DomainCompletionSource = Class.extend({
+ domains: null,
- DomainCompletionSource.prototype.withDomains = function(callback) {
- var self = this;
- function buildResult() {
- return Object.keys(self.domains).map(function(dom) {
- return [dom, self.domains[dom]];
- });
- }
- if (self.domains !== null)
- return callback(buildResult());
+ withDomains: function(callback) {
+ var self = this;
+ function buildResult() {
+ return Object.keys(self.domains).map(function(dom) {
+ return [dom, self.domains[dom]];
+ });
+ }
+ if (self.domains !== null)
+ return callback(buildResult());
- self.domains = {};
+ self.domains = {};
- function processDomain(domain, https) {
- // non-www version is preferrable, so check if we have it already
- if (domain.indexOf('www.') == 0 && self.domains.hasOwnProperty(domain.slice(4)))
- domain = domain.slice(4);
+ function processDomain(domain, https) {
+ // non-www version is preferrable, so check if we have it already
+ if (domain.indexOf('www.') == 0 && self.domains.hasOwnProperty(domain.slice(4)))
+ domain = domain.slice(4);
- // HTTPS is preferrable
- https = https || self.domains[domain] || self.domains['www.' + domain];
+ // HTTPS is preferrable
+ https = https || self.domains[domain] || self.domains['www.' + domain];
- self.domains[domain] = !!https;
- delete self.domains['www.' + domain];
- }
- function processUrl(url) {
- parts = url.split('/');
- processDomain(parts[2], parts[0] == 'https:');
- }
+ self.domains[domain] = !!https;
+ delete self.domains['www.' + domain];
+ }
+ function processUrl(url) {
+ parts = url.split('/');
+ processDomain(parts[2], parts[0] == 'https:');
+ }
- historyCache.use(function(history) {
- history.forEach(function(item) {
- processUrl(item.url);
+ historyCache.use(function(history) {
+ history.forEach(function(item) { processUrl(item.url); });
});
- });
- chrome.history.onVisited.addListener(function(item) {
- processUrl(item.url);
- });
+ chrome.history.onVisited.addListener(function(item) { processUrl(item.url); });
- callback(buildResult());
- }
+ callback(buildResult());
+ },
- DomainCompletionSource.prototype.refresh = function() { }
- DomainCompletionSource.prototype.filter = function(query, callback) {
- var best = null;
- this.withDomains(function(domains) {
- var bestOffset = 1000;
- domains.forEach(function(result) {
- var domain = result[0];
- var protocol = result[1] ? 'https' : 'http';
-
- var offset = domain.indexOf(query);
- if (offset < 0 || offset >= bestOffset)
- return;
-
- // found a new optimum
- bestOffset = offset;
- best = new LazyCompletion(-1.5, function() {
- return {
- html: createCompletionHtml('site', domain),
- action: {func: 'completion.createActionOpenUrl', args: [protocol + '://' + domain]},
- }});
+ refresh: function() { },
+
+ filter: function(query, callback) {
+ var best = null;
+ this.withDomains(function(domains) {
+ var bestOffset = 1000;
+ domains.forEach(function(result) {
+ var domain = result[0];
+ var protocol = result[1] ? 'https' : 'http';
+
+ var offset = domain.indexOf(query);
+ if (offset < 0 || offset >= bestOffset)
+ return;
+
+ // found a new optimum
+ bestOffset = offset;
+ best = new LazyCompletion(-1.5, function() {
+ return {
+ html: createCompletionHtml('site', domain),
+ action: {func: 'completion.createActionOpenUrl', args: [protocol + '://' + domain]},
+ }});
+ });
});
- });
- callback(best ? [best] : []);
- }
+ callback(best ? [best] : []);
+ }
+ });
/** Get completion results from the background page */
var BackgroundCompleter = function(name) {
@@ -292,10 +289,9 @@ var completion = (function() {
this.sources = sources;
this.queryThreshold = queryThreshold;
}
+
MultiCompleter.prototype = {
- refresh: function() {
- this.sources.forEach(function(x) { x.refresh(); });
- },
+ refresh: function() { this.sources.forEach(function(x) { x.refresh(); }); },
filter: function(query, maxResults, callback) {
if (query.length < this.queryThreshold) {
@@ -321,6 +317,7 @@ var completion = (function() {
});
}
}
+
/** Singleton object that provides helpers and caching for fuzzy completion. */
var fuzzyMatcher = (function() {
var self = {};