aboutsummaryrefslogtreecommitdiffstats
path: root/lib/completion.js
diff options
context:
space:
mode:
authorNiklas Baumstark2012-01-24 00:08:40 +0100
committerNiklas Baumstark2012-04-10 23:54:38 +0200
commita9ea93a8fb706b2d8efc847ede2b937440c66c18 (patch)
tree4502e477a3c544a2c2cca7679a9c53511f1d4955 /lib/completion.js
parent2eae98f3b4b6cccaa49f91b282854c39145f12a4 (diff)
downloadvimium-a9ea93a8fb706b2d8efc847ede2b937440c66c18.tar.bz2
DRY up code
Diffstat (limited to 'lib/completion.js')
-rw-r--r--lib/completion.js134
1 files changed, 58 insertions, 76 deletions
diff --git a/lib/completion.js b/lib/completion.js
index f06c6708..338ccbbb 100644
--- a/lib/completion.js
+++ b/lib/completion.js
@@ -109,7 +109,6 @@ var completion = (function() {
*/
self.filter = function(query, source, getValue, id) {
var filtered = [];
- var source = ary;
if (!(id in self.filterCache))
self.filterCache[id] = {};
@@ -265,6 +264,22 @@ var completion = (function() {
return createActionOpenUrl(match.url);
},
+ /** Convenience function to remove shared code in the completers. Clear the completion cache, sends
+ * a message to an extension port and pipes the returned message through a callback before storing it into
+ * the instance's completion cache.
+ */
+ fetchFromPort: function(name, query, callback) {
+ this.completions = null; // reset completions
+
+ // asynchronously fetch from a port
+ var port = chrome.extension.connect({ name: name }) ;
+ var self = this;
+ port.onMessage.addListener(function(msg) {
+ self.readyCallback(callback(msg));
+ });
+ port.postMessage(query);
+ },
+
filter: function(query, callback) {
var self = this;
@@ -336,6 +351,7 @@ var completion = (function() {
// trim query
query = query.replace(/^\s+|\s+$/g, '');
+ // TODO fix HTML injection
if (utils.isUrl(query)) {
url = utils.createFullUrl(query);
str = '<em>goto</em> ' + query;
@@ -359,6 +375,28 @@ var completion = (function() {
};
}
+ // TODO fix HTML injection
+ function createUrlSuggestion(type, url, title) {
+ title = title.length > 0 ? ' <span class="title">' + title + '</span>' : '';
+ return { str: '<em>' + type + '</em> ' + url + title,
+ url: url };
+ }
+
+ /** Convenience function to remove shared code in the completers. Clear the completion cache, sends
+ * a message to an extension port and pipes the returned message through a callback before storing it into
+ * the instance's completion cache.
+ */
+ function fetchFromPort(self, name, query, callback) {
+ self.completions = null; // reset completions
+
+ // asynchronously fetch from a port
+ var port = chrome.extension.connect({ name: name }) ;
+ port.onMessage.addListener(function(msg) {
+ self.readyCallback(callback(msg));
+ });
+ port.postMessage(query);
+ };
+
/** A fuzzy history completer */
var FuzzyHistoryCompleter = function(maxResults) {
AsyncFuzzyUrlCompleter.call(this);
@@ -366,30 +404,11 @@ var completion = (function() {
}
FuzzyHistoryCompleter.prototype = new AsyncFuzzyUrlCompleter;
FuzzyHistoryCompleter.prototype.refresh = function() {
- this.completions = null; // reset completions
-
- // asynchronously fetch history items
- var port = chrome.extension.connect({ name: "getHistory" }) ;
- var self = this;
- port.onMessage.addListener(function(msg) {
- var results = [];
-
- for (var i = 0; i < msg.history.length; ++i) {
- var historyItem = msg.history[i];
- var title = '';
-
- if (historyItem.title.length > 0)
- title = ' <span class="title">' + historyItem.title + '</span>';
-
- results.push({
- str: '<em>history</em> ' + historyItem.url + title,
- url: historyItem.url,
- });
- }
- port = null;
- self.readyCallback(results);
+ this.fetchFromPort('getHistory', { maxResults: this.maxResults }, function(msg) {
+ return msg.history.map(function(historyItem) {
+ return createUrlSuggestion('history', historyItem.url, historyItem.title);
+ });
});
- port.postMessage({ maxResults: this.maxEntries });
}
/** A fuzzy bookmark completer */
@@ -398,31 +417,12 @@ var completion = (function() {
}
FuzzyBookmarkCompleter.prototype = new AsyncFuzzyUrlCompleter;
FuzzyBookmarkCompleter.prototype.refresh = function() {
- this.completions = null; // reset completions
-
- var port = chrome.extension.connect({ name: "getAllBookmarks" }) ;
- var self = this;
- port.onMessage.addListener(function(msg) {
- var results = [];
-
- for (var i = 0; i < msg.bookmarks.length; ++i) {
- var bookmark = msg.bookmarks[i];
- if (bookmark.url === undefined)
- continue;
-
- var title = '';
- if (bookmark.title.length > 0)
- title = ' <span class="title">' + bookmark.title + '</span>';
-
- results.push({
- str: '<em>bookmark</em> ' + bookmark.url + title,
- url: bookmark.url,
- });
- }
- port = null;
- self.readyCallback(results);
+ this.fetchFromPort('getAllBookmarks', {}, function(msg) {
+ return msg.bookmarks.filter(function(bookmark) { return bookmark.url !== undefined })
+ .map(function(bookmark) {
+ return createUrlSuggestion('bookmark', bookmark.url, bookmark.title);
+ })
});
- port.postMessage();
}
/** A fuzzy tab completer */
@@ -437,30 +437,13 @@ var completion = (function() {
return [ open, open ];
}
FuzzyTabCompleter.prototype.refresh = function() {
- this.completions = null; // reset completions
-
- var port = chrome.extension.connect({ name: 'getTabsInCurrentWindow' }) ;
- var self = this;
- port.onMessage.addListener(function(msg) {
- var results = [];
-
- for (var i = 0; i < msg.tabs.length; ++i) {
- var tab = msg.tabs[i];
-
- var title = '';
- if (tab.title.length > 0)
- title = ' <span class="title">' + tab.title + '</span>';
-
- results.push({
- str: '<em>tab</em> ' + tab.url + title,
- url: tab.url,
- tab: tab,
- });
- }
- port = null;
- self.readyCallback(results);
+ this.fetchFromPort('getTabsInCurrentWindow', {}, function(msg) {
+ return msg.tabs.map(function(tab) {
+ suggestion = createUrlSuggestion('tab', tab.url, tab.title);
+ suggestion.tab = tab;
+ return suggestion;
+ });
});
- port.postMessage();
}
/** A meta-completer that delegates queries and merges and sorts the results of a collection of other
@@ -470,16 +453,15 @@ var completion = (function() {
}
MergingCompleter.prototype = {
refresh: function() {
- for (var i = 0; i < this.sources.length; ++i)
- this.sources[i].refresh();
+ this.sources.forEach(function(x) { x.refresh(); });
},
filter: function(query, callback) {
var all = [];
var counter = this.sources.length;
- for (var i = 0; i < this.sources.length; ++i) {
- this.sources[i].filter(query, function(results) {
+ this.sources.forEach(function(source) {
+ source.filter(query, function(results) {
all = all.concat(results);
if (--counter > 0)
return;
@@ -490,7 +472,7 @@ var completion = (function() {
});
callback(all);
});
- }
+ });
}
}