comparison chromeless-browsing/chromeless.js @ 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
comparison
equal deleted inserted replaced
7:c89423660272 8:abe54fcda16c
1 var noun_type_url = {
2 _name: "url",
3 _getHistoryLinks: function(partialSearch, onSearchComplete) {
4 function AutoCompleteInput(aSearches) {
5 this.searches = aSearches;
6 }
7 AutoCompleteInput.prototype = {
8 constructor: AutoCompleteInput,
9 searches: null,
10 minResultsForPopup: 0,
11 timeout: 10,
12 searchParam: "",
13 textValue: "",
14 disableAutoComplete: false,
15 completeDefaultIndex: false,
16 get searchCount() {
17 return this.searches.length;
18 },
19 getSearchAt: function(aIndex) {
20 return this.searches[aIndex];
21 },
22 onSearchBegin: function() {},
23 onSearchComplete: function() {},
24 popupOpen: false,
25 popup: {
26 setSelectedIndex: function(aIndex) {},
27 invalidate: function() {},
28 // nsISupports implementation
29 QueryInterface: function(iid) {
30 if (iid.equals(Ci.nsISupports) ||
31 iid.equals(Ci.nsIAutoCompletePopup))
32 return this;
33 throw Components.results.NS_ERROR_NO_INTERFACE;
34 }
35 },
36 // nsISupports implementation
37 QueryInterface: function(iid) {
38 if (iid.equals(Ci.nsISupports) ||
39 iid.equals(Ci.nsIAutoCompleteInput))
40 return this;
41 throw Components.results.NS_ERROR_NO_INTERFACE;
42 }
43 };
44
45 var controller = Cc["@mozilla.org/autocomplete/controller;1"]
46 .getService(Ci.nsIAutoCompleteController);
47 var input = new AutoCompleteInput(["history"]);
48 controller.input = input;
49 input.onSearchComplete = function() {
50 onSearchComplete(controller);
51 };
52
53 controller.startSearch(partialSearch);
54 },
55 _onSearchComplete: function(controller, callback) {
56 var max_suggs = 5;
57 for (var i = 0; i < controller.matchCount && i <= max_suggs; i++) {
58 var result = {
59 title: controller.getCommentAt(i),
60 url: controller.getValueAt(i),
61 image: controller.getImageAt(i),
62 style: controller.getStyleAt(i)
63 };
64 if (!result.title)
65 result.title = result.url;
66 if (result.image.indexOf("moz-anno:favicon:") == 0)
67 result.image = result.image.slice("moz-anno:favicon:".length);
68 else
69 result.image = "";
70 callback(CmdUtils.makeSugg(result.title, undefined, result));
71 }
72 },
73 suggest: function(fragment, html, callback) {
74 var self = this;
75 this._getHistoryLinks(
76 fragment,
77 function(controller) {
78 self._onSearchComplete(controller, callback);
79 });
80 return [CmdUtils.makeSugg(fragment)];
81 }
82 };
83
84 CmdUtils.CreateCommand( 1 CmdUtils.CreateCommand(
85 {name: "go", 2 {name: "go",
86 takes: { 3 takes: {"search terms": /.*/},
87 "search terms": noun_type_url 4 preview: function(pblock, dobj) {
88 }, 5 if (dobj.text.length == 0) {
89 preview: function(pblock, theWords) {
90 if (theWords.text.length == 0) {
91 pblock.innerHTML = "<b>Please enter a search term</b>"; 6 pblock.innerHTML = "<b>Please enter a search term</b>";
92 return; 7 return;
93 } 8 }
94 var result = ""; 9
95 if (theWords.data) { 10 var history = Cc["@mozilla.org/autocomplete/search;1?name=history"]
96 if (theWords.data.image) 11 .createInstance(Ci.nsIAutoCompleteSearch);
97 result += ("<img src=\"" + 12
98 Utils.escapeHtml(theWords.data.image) + 13 var observer = {
99 "\"/> "); 14 onSearchResult: function onSearchResult(search, result) {
100 if (theWords.data.title != theWords.data.url) 15 if (result.searchResult == result.RESULT_SUCCESS ||
101 result += ("<i>" + 16 result.searchResult == result.RESULT_SUCCESS_ONGOING) {
102 Utils.escapeHtml(theWords.data.title) + 17 var html = "";
103 "</i>"); 18 for (var i = 0; i < result.matchCount; i++) {
104 result += ("<p><tt>" + 19 var image = result.getImageAt(i);
105 Utils.escapeHtml(theWords.data.url) + 20 var title = result.getCommentAt(i);
106 "</tt></p>"); 21 var url = result.getValueAt(i);
107 } else 22
108 result = "Takes you to " + Utils.escapeHtml(theWords.text) + "."; 23 if (!title)
109 pblock.innerHTML = result; 24 title = url;
25
26 if (image.indexOf("moz-anno:favicon:") == 0)
27 image = image.slice("moz-anno:favicon:".length);
28 else
29 image = "";
30
31 html += '<div class="gresult">';
32 if (image)
33 html += ('<img src="' +
34 Utils.escapeHtml(image) +
35 '"/> ');
36 html += ('<a href="' + Utils.escapeHtml(url) + '">' +
37 Utils.escapeHtml(title) +
38 "</a>");
39 html += ('<div class="gresult-url">' + Utils.escapeHtml(url) +
40 '</div');
41 html += "</div>";
42 }
43 pblock.innerHTML = html;
44 }
45 }
46 };
47
48 history.startSearch(
49 dobj.text,
50 "",
51 null,
52 observer
53 );
54
55 pblock.addEventListener(
56 "preview-change",
57 function onPreviewChange() {
58 pblock.removeEventListener("preview-change", onPreviewChange, false);
59 history.stopSearch();
60 },
61 false);
110 }, 62 },
111 execute: function(theWords) { 63 execute: function(dobj) {
112 if (theWords.data) 64 if (dobj.text)
113 Utils.openUrlInBrowser(theWords.data.url); 65 Utils.openUrlInBrowser(dobj.text);
114 else 66 else
115 Utils.openUrlInBrowser(theWords.text); 67 displayMessage("No URL was provided.");
116 }} 68 }}
117 ); 69 );
118 70
119 function cmd_show_tabs() { 71 function cmd_show_tabs() {
120 var content = context.chromeWindow.document.getElementById("appcontent"); 72 var content = context.chromeWindow.document.getElementById("appcontent");