Mercurial > my-ubiquity-commands
changeset 8:abe54fcda16c
Suggestions for the 'go' command now show up in the preview area rather than as noun suggestions, since async noun suggestions aren't very mature and suggestions don't have much display room.
author | Atul Varma <varmaa@toolness.com> |
---|---|
date | Fri, 27 Mar 2009 17:31:05 -0700 |
parents | c89423660272 |
children | 6b955e36a9e9 |
files | chromeless-browsing/chromeless.js |
diffstat | 1 files changed, 60 insertions(+), 108 deletions(-) [+] |
line wrap: on
line diff
--- a/chromeless-browsing/chromeless.js Fri Mar 27 16:43:18 2009 -0700 +++ b/chromeless-browsing/chromeless.js Fri Mar 27 17:31:05 2009 -0700 @@ -1,118 +1,70 @@ -var noun_type_url = { - _name: "url", - _getHistoryLinks: function(partialSearch, onSearchComplete) { - function AutoCompleteInput(aSearches) { - this.searches = aSearches; - } - AutoCompleteInput.prototype = { - constructor: AutoCompleteInput, - searches: null, - minResultsForPopup: 0, - timeout: 10, - searchParam: "", - textValue: "", - disableAutoComplete: false, - completeDefaultIndex: false, - get searchCount() { - return this.searches.length; - }, - getSearchAt: function(aIndex) { - return this.searches[aIndex]; - }, - onSearchBegin: function() {}, - onSearchComplete: function() {}, - popupOpen: false, - popup: { - setSelectedIndex: function(aIndex) {}, - invalidate: function() {}, - // nsISupports implementation - QueryInterface: function(iid) { - if (iid.equals(Ci.nsISupports) || - iid.equals(Ci.nsIAutoCompletePopup)) - return this; - throw Components.results.NS_ERROR_NO_INTERFACE; - } - }, - // nsISupports implementation - QueryInterface: function(iid) { - if (iid.equals(Ci.nsISupports) || - iid.equals(Ci.nsIAutoCompleteInput)) - return this; - throw Components.results.NS_ERROR_NO_INTERFACE; - } - }; - - var controller = Cc["@mozilla.org/autocomplete/controller;1"] - .getService(Ci.nsIAutoCompleteController); - var input = new AutoCompleteInput(["history"]); - controller.input = input; - input.onSearchComplete = function() { - onSearchComplete(controller); - }; - - controller.startSearch(partialSearch); - }, - _onSearchComplete: function(controller, callback) { - var max_suggs = 5; - for (var i = 0; i < controller.matchCount && i <= max_suggs; i++) { - var result = { - title: controller.getCommentAt(i), - url: controller.getValueAt(i), - image: controller.getImageAt(i), - style: controller.getStyleAt(i) - }; - if (!result.title) - result.title = result.url; - if (result.image.indexOf("moz-anno:favicon:") == 0) - result.image = result.image.slice("moz-anno:favicon:".length); - else - result.image = ""; - callback(CmdUtils.makeSugg(result.title, undefined, result)); - } - }, - suggest: function(fragment, html, callback) { - var self = this; - this._getHistoryLinks( - fragment, - function(controller) { - self._onSearchComplete(controller, callback); - }); - return [CmdUtils.makeSugg(fragment)]; - } -}; - CmdUtils.CreateCommand( {name: "go", - takes: { - "search terms": noun_type_url - }, - preview: function(pblock, theWords) { - if (theWords.text.length == 0) { + takes: {"search terms": /.*/}, + preview: function(pblock, dobj) { + if (dobj.text.length == 0) { pblock.innerHTML = "<b>Please enter a search term</b>"; return; } - var result = ""; - if (theWords.data) { - if (theWords.data.image) - result += ("<img src=\"" + - Utils.escapeHtml(theWords.data.image) + - "\"/> "); - if (theWords.data.title != theWords.data.url) - result += ("<i>" + - Utils.escapeHtml(theWords.data.title) + - "</i>"); - result += ("<p><tt>" + - Utils.escapeHtml(theWords.data.url) + - "</tt></p>"); - } else - result = "Takes you to " + Utils.escapeHtml(theWords.text) + "."; - pblock.innerHTML = result; + + var history = Cc["@mozilla.org/autocomplete/search;1?name=history"] + .createInstance(Ci.nsIAutoCompleteSearch); + + var observer = { + onSearchResult: function onSearchResult(search, result) { + if (result.searchResult == result.RESULT_SUCCESS || + result.searchResult == result.RESULT_SUCCESS_ONGOING) { + var html = ""; + for (var i = 0; i < result.matchCount; i++) { + var image = result.getImageAt(i); + var title = result.getCommentAt(i); + var url = result.getValueAt(i); + + if (!title) + title = url; + + if (image.indexOf("moz-anno:favicon:") == 0) + image = image.slice("moz-anno:favicon:".length); + else + image = ""; + + html += '<div class="gresult">'; + if (image) + html += ('<img src="' + + Utils.escapeHtml(image) + + '"/> '); + html += ('<a href="' + Utils.escapeHtml(url) + '">' + + Utils.escapeHtml(title) + + "</a>"); + html += ('<div class="gresult-url">' + Utils.escapeHtml(url) + + '</div'); + html += "</div>"; + } + pblock.innerHTML = html; + } + } + }; + + history.startSearch( + dobj.text, + "", + null, + observer + ); + + pblock.addEventListener( + "preview-change", + function onPreviewChange() { + pblock.removeEventListener("preview-change", onPreviewChange, false); + history.stopSearch(); + }, + false); }, - execute: function(theWords) { - if (theWords.data) - Utils.openUrlInBrowser(theWords.data.url); + execute: function(dobj) { + if (dobj.text) + Utils.openUrlInBrowser(dobj.text); else - Utils.openUrlInBrowser(theWords.text); + displayMessage("No URL was provided."); }} );